Skip to content

Commit

Permalink
[Metrics SDK] Add support for Pull Metric Reader (open-telemetry#1701)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored and yxue committed Dec 5, 2022
1 parent 8e6ad8c commit c6552dc
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 64 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Increment the:
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
* [TRACE SDK] Add trace sdk builders (#1393)
[#1471](https://github.com/open-telemetry/opentelemetry-cpp/pull/1471)
* [EXAMPLE] Fix memory ownership of InMemorySpanExporter (#1473)
Expand All @@ -38,6 +39,10 @@ Increment the:
=======
=======
=======
=======
* [Metrics SDK] Add support for Pull Metric Exporter [#1701](https://github.com/open-telemetry/opentelemetry-cpp/pull/1701)
which includes breaking change in the Metrics api.
>>>>>>> 70777ef9 ([Metrics SDK] Add support for Pull Metric Reader (#1701))
* [BUILD] Add CMake OTELCPP_MAINTAINER_MODE [#1650](https://github.com/open-telemetry/opentelemetry-cpp/pull/1650)
* [SEMANTIC CONVENTIONS] Upgrade to version 1.14.0 [#1697](https://github.com/open-telemetry/opentelemetry-cpp/pull/1697)

Expand Down
2 changes: 1 addition & 1 deletion examples/metrics_simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Exporter which will print to stdout by default.
The reader periodically collects metrics from the Aggregation Store and exports them.
```cpp
std::unique_ptr<metric_sdk::MetricExporter> exporter{new exportermetrics::OStreamMetricExporter};
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{new exportermetrics::OStreamMetricExporter};
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
```
Expand Down
3 changes: 2 additions & 1 deletion examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace

void initMetrics(const std::string &name)
{
std::unique_ptr<metric_sdk::MetricExporter> exporter{new exportermetrics::OStreamMetricExporter};
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{
new exportermetrics::OStreamMetricExporter};

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};
Expand Down
4 changes: 2 additions & 2 deletions examples/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ The main difference between the [ostream-metrics](../metrics_simple/README.md)
example with this one is that the line below is replaced:

```cpp
std::unique_ptr<metric_sdk::MetricExporter> exporter{
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{
new exportermetrics::OStreamMetricExporter};

```
with
```cpp
std::unique_ptr<metrics_sdk::MetricExporter> exporter{
std::unique_ptr<metrics_sdk::PushMetricExporter> exporter{
new metrics_exporter::PrometheusExporter(opts)};
```

Expand Down
2 changes: 1 addition & 1 deletion examples/prometheus/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void initMetrics(const std::string &name, const std::string &addr)
}
std::puts("PrometheusExporter example program running ...");

std::unique_ptr<metrics_sdk::MetricExporter> exporter{
std::unique_ptr<metrics_sdk::PushMetricExporter> exporter{
new metrics_exporter::PrometheusExporter(opts)};

std::string version{"1.2.0"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/sdk/metrics/data/metric_data.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -21,7 +21,7 @@ namespace metrics
/**
* The OStreamMetricExporter exports record data through an ostream
*/
class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricExporter
class OStreamMetricExporter final : public opentelemetry::sdk::metrics::PushMetricExporter
{
public:
/**
Expand Down
10 changes: 5 additions & 5 deletions exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace exportermetrics = opentelemetry::exporter::metrics;
TEST(OStreamMetricsExporter, Shutdown)
{
auto exporter =
std::unique_ptr<metric_sdk::MetricExporter>(new exportermetrics::OStreamMetricExporter);
std::unique_ptr<metric_sdk::PushMetricExporter>(new exportermetrics::OStreamMetricExporter);
ASSERT_TRUE(exporter->Shutdown());
auto result = exporter->Export(metric_sdk::ResourceMetrics{});
EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure);
Expand All @@ -32,7 +32,7 @@ TEST(OStreamMetricsExporter, Shutdown)
TEST(OStreamMetricsExporter, ExportSumPointData)
{
auto exporter =
std::unique_ptr<metric_sdk::MetricExporter>(new exportermetrics::OStreamMetricExporter);
std::unique_ptr<metric_sdk::PushMetricExporter>(new exportermetrics::OStreamMetricExporter);

metric_sdk::SumPointData sum_point_data{};
sum_point_data.value_ = 10.0;
Expand Down Expand Up @@ -95,7 +95,7 @@ TEST(OStreamMetricsExporter, ExportSumPointData)
TEST(OStreamMetricsExporter, ExportHistogramPointData)
{
auto exporter =
std::unique_ptr<metric_sdk::MetricExporter>(new exportermetrics::OStreamMetricExporter);
std::unique_ptr<metric_sdk::PushMetricExporter>(new exportermetrics::OStreamMetricExporter);

metric_sdk::HistogramPointData histogram_point_data{};
histogram_point_data.boundaries_ = std::list<double>{10.1, 20.2, 30.2};
Expand Down Expand Up @@ -177,7 +177,7 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
TEST(OStreamMetricsExporter, ExportLastValuePointData)
{
auto exporter =
std::unique_ptr<metric_sdk::MetricExporter>(new exportermetrics::OStreamMetricExporter);
std::unique_ptr<metric_sdk::PushMetricExporter>(new exportermetrics::OStreamMetricExporter);

metric_sdk::ResourceMetrics data;
auto resource = opentelemetry::sdk::resource::Resource::Create(
Expand Down Expand Up @@ -246,7 +246,7 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
TEST(OStreamMetricsExporter, ExportDropPointData)
{
auto exporter =
std::unique_ptr<metric_sdk::MetricExporter>(new exportermetrics::OStreamMetricExporter);
std::unique_ptr<metric_sdk::PushMetricExporter>(new exportermetrics::OStreamMetricExporter);

metric_sdk::ResourceMetrics data;
auto resource = opentelemetry::sdk::resource::Resource::Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# include "opentelemetry/exporters/otlp/otlp_environment.h"
# include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
Expand All @@ -26,7 +26,7 @@ namespace otlp
/**
* The OTLP exporter exports metrics data in OpenTelemetry Protocol (OTLP) format in gRPC.
*/
class OtlpGrpcMetricExporter : public opentelemetry::sdk::metrics::MetricExporter
class OtlpGrpcMetricExporter : public opentelemetry::sdk::metrics::PushMetricExporter
{
public:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
Expand All @@ -23,12 +23,12 @@ class OtlpGrpcMetricExporterFactory
/**
* Create a OtlpGrpcMetricExporter.
*/
static std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> Create();
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create();

/**
* Create a OtlpGrpcMetricExporter.
*/
static std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> Create(
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
const OtlpGrpcMetricExporterOptions &options);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"

# include "opentelemetry/exporters/otlp/otlp_environment.h"
# include "opentelemetry/exporters/otlp/otlp_http_client.h"
Expand All @@ -24,7 +24,7 @@ namespace otlp
/**
* The OTLP exporter exports metrics data in OpenTelemetry Protocol (OTLP) format in HTTP.
*/
class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::MetricExporter
class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMetricExporter
{
public:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"

# include <memory>

Expand All @@ -24,12 +24,12 @@ class OtlpHttpMetricExporterFactory
/**
* Create a OtlpHttpMetricExporter.
*/
static std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> Create();
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create();

/**
* Create a OtlpHttpMetricExporter.
*/
static std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> Create(
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
const OtlpHttpMetricExporterOptions &options);
};

Expand Down
9 changes: 5 additions & 4 deletions exporters/otlp/src/otlp_grpc_metric_exporter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ namespace exporter
namespace otlp
{

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> OtlpGrpcMetricExporterFactory::Create()
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>
OtlpGrpcMetricExporterFactory::Create()
{
OtlpGrpcMetricExporterOptions options;
return Create(options);
}

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> OtlpGrpcMetricExporterFactory::Create(
const OtlpGrpcMetricExporterOptions &options)
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>
OtlpGrpcMetricExporterFactory::Create(const OtlpGrpcMetricExporterOptions &options)
{
std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> exporter(
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> exporter(
new OtlpGrpcMetricExporter(options));
return exporter;
}
Expand Down
9 changes: 5 additions & 4 deletions exporters/otlp/src/otlp_http_metric_exporter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ namespace exporter
namespace otlp
{

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> OtlpHttpMetricExporterFactory::Create()
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>
OtlpHttpMetricExporterFactory::Create()
{
OtlpHttpMetricExporterOptions options;
return Create(options);
}

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> OtlpHttpMetricExporterFactory::Create(
const OtlpHttpMetricExporterOptions &options)
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>
OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &options)
{
std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> exporter(
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> exporter(
new OtlpHttpMetricExporter(options));
return exporter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST(OtlpGrpcMetricExporterFactory, BuildTest)
OtlpGrpcMetricExporterOptions opts;
opts.endpoint = "localhost:45454";

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> exporter =
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> exporter =
OtlpGrpcMetricExporterFactory::Create(opts);

EXPECT_TRUE(exporter != nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TEST(OtlpHttpMetricExporterFactory, BuildTest)
OtlpHttpMetricExporterOptions opts;
opts.url = "localhost:45454";

std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> exporter =
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> exporter =
OtlpHttpMetricExporterFactory::Create(opts);

EXPECT_TRUE(exporter != nullptr);
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/test/otlp_http_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ namespace http_client = opentelemetry::ext::http::client;
class OtlpHttpMetricExporterTestPeer : public ::testing::Test
{
public:
std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> GetExporter(
std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> GetExporter(
std::unique_ptr<OtlpHttpClient> http_client)
{
return std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter>(
return std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>(
new OtlpHttpMetricExporter(std::move(http_client)));
}

Expand Down Expand Up @@ -734,8 +734,8 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test

TEST(OtlpHttpMetricExporterTest, Shutdown)
{
auto exporter =
std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter>(new OtlpHttpMetricExporter());
auto exporter = std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter>(
new OtlpHttpMetricExporter());
ASSERT_TRUE(exporter->Shutdown());
auto result = exporter->Export(opentelemetry::sdk::metrics::ResourceMetrics{});
EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# include "opentelemetry/exporters/prometheus/collector.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/sdk/common/env_variables.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/push_metric_exporter.h"
# include "opentelemetry/version.h"

/**
Expand Down Expand Up @@ -46,7 +46,7 @@ struct PrometheusExporterOptions
std::string url = GetPrometheusDefaultHttpEndpoint();
};

class PrometheusExporter : public sdk::metrics::MetricExporter
class PrometheusExporter : public sdk::metrics::PushMetricExporter
{
public:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace sdk
namespace metrics
{

class MetricExporter;
class PushMetricExporter;
/**
* Struct to hold PeriodicExortingMetricReader options.
*/
Expand All @@ -40,7 +40,7 @@ class PeriodicExportingMetricReader : public MetricReader
{

public:
PeriodicExportingMetricReader(std::unique_ptr<MetricExporter> exporter,
PeriodicExportingMetricReader(std::unique_ptr<PushMetricExporter> exporter,
const PeriodicExportingMetricReaderOptions &option);

AggregationTemporality GetAggregationTemporality(
Expand All @@ -53,7 +53,7 @@ class PeriodicExportingMetricReader : public MetricReader

void OnInitialized() noexcept override;

std::unique_ptr<MetricExporter> exporter_;
std::unique_ptr<PushMetricExporter> exporter_;
std::chrono::milliseconds export_interval_millis_;
std::chrono::milliseconds export_timeout_millis_;

Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
* data. Note: This method is not thread safe, and should ideally be called from main thread.
*/
void AddMetricReader(std::unique_ptr<MetricReader> reader) noexcept;
void AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept;

/**
* Attaches a View to list of configured Views for this Meter context.
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
* data. Note: This method is not thread safe, and should ideally be called from main thread.
*/
void AddMetricReader(std::unique_ptr<MetricReader> reader) noexcept;
void AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept;

/**
* Attaches a View to list of configured Views for this Meter provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ namespace metrics

class MetricData;
/**
* MetricExporter defines the interface to be used by metrics libraries to
* PushMetricExporter defines the interface to be used by metrics libraries to
* push metrics data to the OpenTelemetry exporters.
*/
class MetricExporter
class PushMetricExporter
{
public:
virtual ~MetricExporter() = default;
virtual ~PushMetricExporter() = default;

/**
* Exports a batch of metrics data. This method must not be called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CollectorHandle
class MetricCollector : public MetricProducer, public CollectorHandle
{
public:
MetricCollector(MeterContext *context, std::unique_ptr<MetricReader> metric_reader);
MetricCollector(MeterContext *context, std::shared_ptr<MetricReader> metric_reader);

~MetricCollector() override = default;

Expand Down
Loading

0 comments on commit c6552dc

Please sign in to comment.