-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement in-memory metric exporter (#3043)
Fixes #1405
- Loading branch information
Showing
9 changed files
with
496 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
exporters/memory/include/opentelemetry/exporters/memory/in_memory_metric_data.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
#include <tuple> | ||
|
||
#include "opentelemetry/exporters/memory/in_memory_data.h" | ||
#include "opentelemetry/sdk/metrics/data/metric_data.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
struct ResourceMetrics; | ||
} | ||
} // namespace sdk | ||
namespace exporter | ||
{ | ||
namespace memory | ||
{ | ||
|
||
/// The abstract base class for types used to store in-memory data backing an | ||
/// InMemoryMetricExporter. | ||
class InMemoryMetricData | ||
{ | ||
public: | ||
InMemoryMetricData() = default; | ||
virtual ~InMemoryMetricData() = default; | ||
|
||
InMemoryMetricData(const InMemoryMetricData &) = delete; | ||
InMemoryMetricData(InMemoryMetricData &&) = delete; | ||
InMemoryMetricData &operator=(const InMemoryMetricData &) = delete; | ||
InMemoryMetricData &operator=(InMemoryMetricData &&) = delete; | ||
|
||
virtual void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) = 0; | ||
}; | ||
|
||
/// An implementation of InMemoryMetricData that stores full-fidelity data points in a circular | ||
/// buffer. This allows tests to inspect every aspect of exported data, in exchange for a somewhat | ||
/// cumbersome API. | ||
class CircularBufferInMemoryMetricData final : public InMemoryMetricData, | ||
public InMemoryData<sdk::metrics::ResourceMetrics> | ||
{ | ||
public: | ||
explicit CircularBufferInMemoryMetricData(size_t buffer_size); | ||
void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override; | ||
}; | ||
|
||
/// An implementation of InMemoryMetricData that stores only the most recent data point in each time | ||
/// series, and allows convenient lookups of time series. This makes simple tests easier to write. | ||
class SimpleAggregateInMemoryMetricData final : public InMemoryMetricData | ||
{ | ||
public: | ||
using AttributeToPoint = std::map<opentelemetry::sdk::metrics::PointAttributes, | ||
opentelemetry::sdk::metrics::PointType>; | ||
|
||
void Add(std::unique_ptr<sdk::metrics::ResourceMetrics> resource_metrics) override; | ||
const AttributeToPoint &Get(const std::string &scope, const std::string &metric); | ||
void Clear(); | ||
|
||
private: | ||
std::map<std::tuple<std::string, std::string>, AttributeToPoint> data_; | ||
}; | ||
|
||
} // namespace memory | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
44 changes: 44 additions & 0 deletions
44
exporters/memory/include/opentelemetry/exporters/memory/in_memory_metric_exporter_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "opentelemetry/sdk/metrics/instruments.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
class PushMetricExporter; | ||
} // namespace metrics | ||
} // namespace sdk | ||
namespace exporter | ||
{ | ||
namespace memory | ||
{ | ||
class InMemoryMetricData; | ||
|
||
/// A factory for InMemoryMetricExporter | ||
class InMemoryMetricExporterFactory | ||
{ | ||
public: | ||
/// Create a InMemoryMetricExporter with a default buffer size and aggregation | ||
/// temporality selector. | ||
/// @param [out] data the InMemoryMetricData the exporter will write to, | ||
/// for the caller to inspect | ||
/// @param [in] buffer_size number of entries to save in the circular buffer | ||
/// @param [in] temporality output temporality as a function of instrument kind | ||
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create( | ||
const std::shared_ptr<InMemoryMetricData> &data, | ||
const sdk::metrics::AggregationTemporalitySelector &temporality); | ||
|
||
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create( | ||
const std::shared_ptr<InMemoryMetricData> &data); | ||
}; | ||
} // namespace memory | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/exporters/memory/in_memory_metric_data.h" | ||
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" | ||
#include "opentelemetry/sdk/metrics/export/metric_producer.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace memory | ||
{ | ||
using sdk::metrics::ResourceMetrics; | ||
|
||
CircularBufferInMemoryMetricData::CircularBufferInMemoryMetricData(size_t buffer_size) | ||
: InMemoryData(buffer_size) | ||
{} | ||
|
||
void CircularBufferInMemoryMetricData::Add(std::unique_ptr<ResourceMetrics> resource_metrics) | ||
{ | ||
InMemoryData::Add(std::move(resource_metrics)); | ||
} | ||
|
||
void SimpleAggregateInMemoryMetricData::Add(std::unique_ptr<ResourceMetrics> resource_metrics) | ||
{ | ||
for (const auto &sm : resource_metrics->scope_metric_data_) | ||
{ | ||
const auto &scope = sm.scope_->GetName(); | ||
for (const auto &m : sm.metric_data_) | ||
{ | ||
const auto &metric = m.instrument_descriptor.name_; | ||
for (const auto &pda : m.point_data_attr_) | ||
{ | ||
data_[{scope, metric}].insert({pda.attributes, pda.point_data}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const SimpleAggregateInMemoryMetricData::AttributeToPoint &SimpleAggregateInMemoryMetricData::Get( | ||
const std::string &scope, | ||
const std::string &metric) | ||
{ | ||
return data_[{scope, metric}]; | ||
} | ||
|
||
void SimpleAggregateInMemoryMetricData::Clear() | ||
{ | ||
data_.clear(); | ||
} | ||
|
||
} // namespace memory | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
Oops, something went wrong.