Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Provide builders to avoid exposing Metrics SDK internals #2189

Merged
merged 16 commits into from
Jun 28, 2023
90 changes: 56 additions & 34 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@

#include <memory>
#include <thread>
#include "opentelemetry/exporters/ostream/metric_exporter.h"

#include "opentelemetry/exporters/ostream/metric_exporter_factory.h"
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
#include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/meter_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/view_factory.h"

#ifdef BAZEL_BUILD
# include "examples/common/metrics_foo_library/foo_library.h"
#else
# include "metrics_foo_library/foo_library.h"
#endif

namespace metric_sdk = opentelemetry::sdk::metrics;
namespace metrics_sdk = opentelemetry::sdk::metrics;
namespace common = opentelemetry::common;
namespace exportermetrics = opentelemetry::exporter::metrics;
namespace metrics_api = opentelemetry::metrics;
Expand All @@ -27,59 +33,75 @@ namespace

void InitMetrics(const std::string &name)
{
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{
new exportermetrics::OStreamMetricExporter};
auto exporter = exportermetrics::OStreamMetricExporterFactory::Create();

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

// Initialize and set the global MeterProvider
metric_sdk::PeriodicExportingMetricReaderOptions options;
metrics_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);

auto reader =
metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options);

auto u_provider = metrics_sdk::MeterProviderFactory::Create();
auto *p = static_cast<metrics_sdk::MeterProvider *>(u_provider.get());

p->AddMetricReader(std::move(reader));

// counter view
std::string counter_name = name + "_counter";
std::unique_ptr<metric_sdk::InstrumentSelector> instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> sum_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};

auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kCounter, counter_name);

auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto sum_view =
metrics_sdk::ViewFactory::Create(name, "description", metrics_sdk::AggregationType::kSum);

p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// observable counter view
std::string observable_counter_name = name + "_observable_counter";
std::unique_ptr<metric_sdk::InstrumentSelector> observable_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kObservableCounter,
observable_counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> observable_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> observable_sum_view{
new metric_sdk::View{name, "test_description", metric_sdk::AggregationType::kSum}};

auto observable_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name);

auto observable_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description",
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
std::move(observable_sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
std::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)};
std::unique_ptr<metric_sdk::MeterSelector> histogram_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig> aggregation_config{
new opentelemetry::sdk::metrics::HistogramAggregationConfig};
static_cast<opentelemetry::sdk::metrics::HistogramAggregationConfig *>(aggregation_config.get())
->boundaries_ = std::vector<double>{0.0, 50.0, 100.0, 250.0, 500.0, 750.0,
1000.0, 2500.0, 5000.0, 10000.0, 20000.0};
std::unique_ptr<metric_sdk::View> histogram_view{new metric_sdk::View{
name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}};

auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name);

auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto histogram_aggregation_config = std::unique_ptr<metrics_sdk::HistogramAggregationConfig>(
new metrics_sdk::HistogramAggregationConfig);

histogram_aggregation_config->boundaries_ = std::vector<double>{
0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0};

std::shared_ptr<metrics_sdk::AggregationConfig> aggregation_config(
std::move(histogram_aggregation_config));

auto histogram_view = metrics_sdk::ViewFactory::Create(
name, "description", metrics_sdk::AggregationType::kHistogram, aggregation_config);

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));

std::shared_ptr<opentelemetry::metrics::MeterProvider> provider(std::move(u_provider));
Copy link
Member

@lalitb lalitb Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These multiple casts from api:::MeterProvider -> sdk::MeterProvider -> api::MeterProvider are bit confusing, but seems we don't have better choice for now.

metrics_api::Provider::SetMeterProvider(provider);
}

Expand Down
21 changes: 20 additions & 1 deletion examples/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ cc_binary(
)

cc_binary(
name = "example_otlp_grpc_metrics",
name = "example_otlp_grpc_metric",
srcs = [
"grpc_metric_main.cc",
],
Expand All @@ -93,3 +93,22 @@ cc_binary(
"//sdk/src/metrics",
],
)

cc_binary(
name = "example_otlp_http_metric",
srcs = [
"http_metric_main.cc",
],
tags = [
"examples",
"metrics",
"otlp",
],
deps = [
"//api",
"//examples/common/metrics_foo_library:common_metrics_foo_library",
"//exporters/otlp:otlp_http_exporter",
"//exporters/otlp:otlp_http_metric_exporter",
"//sdk/src/metrics",
],
)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 26 additions & 8 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ include_directories(
${CMAKE_SOURCE_DIR}/exporters/otlp/include)

if(WITH_OTLP_GRPC)
# TRACE

add_executable(example_otlp_grpc grpc_main.cc)

target_link_libraries(example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -21,6 +23,18 @@ if(WITH_OTLP_GRPC)
target_link_libraries(example_otlp_grpc opentelemetry_trace
opentelemetry_exporter_otlp_grpc)
endif()

# METRIC

add_executable(example_otlp_grpc_metric grpc_metric_main.cc)

target_link_libraries(
example_otlp_grpc_metric ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_grpc_metrics)

# LOG

if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_grpc_log grpc_log_main.cc)

Expand All @@ -37,6 +51,8 @@ if(WITH_OTLP_GRPC)
endif()

if(WITH_OTLP_HTTP)
# TRACE

add_executable(example_otlp_http http_main.cc)

target_link_libraries(example_otlp_http ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -49,6 +65,16 @@ if(WITH_OTLP_HTTP)
opentelemetry_exporter_otlp_http)
endif()

# METRIC

add_executable(example_otlp_http_metric http_metric_main.cc)
target_link_libraries(
example_otlp_http_metric ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_http_metric)

# LOG

if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_http_log http_log_main.cc)
target_link_libraries(example_otlp_http_log ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -66,11 +92,3 @@ if(WITH_OTLP_HTTP)

endif()
endif()

if(WITH_OTLP_GRPC)
add_executable(example_otlp_metric_grpc grpc_metric_main.cc)
target_link_libraries(
example_otlp_metric_grpc ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_grpc_metrics)
endif()
36 changes: 23 additions & 13 deletions examples/otlp/grpc_metric_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_context_factory.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"

#include <memory>
#include <thread>
Expand All @@ -25,24 +28,31 @@ namespace otlp_exporter = opentelemetry::exporter::otlp;
namespace
{

otlp_exporter::OtlpGrpcMetricExporterOptions options;
otlp_exporter::OtlpGrpcMetricExporterOptions exporter_options;

void InitMetrics()
{
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(options);
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(exporter_options);

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

// Initialize and set the global MeterProvider
metric_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
p->AddMetricReader(std::move(reader));
metric_sdk::PeriodicExportingMetricReaderOptions reader_options;
reader_options.export_interval_millis = std::chrono::milliseconds(1000);
reader_options.export_timeout_millis = std::chrono::milliseconds(500);

auto reader =
metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), reader_options);

auto context = metric_sdk::MeterContextFactory::Create();
context->AddMetricReader(std::move(reader));

// FIXME: MeterProvider / MeterProviderFactory should take a unique_ptr
marcalff marked this conversation as resolved.
Show resolved Hide resolved
std::shared_ptr<metric_sdk::MeterContext> s_context(std::move(context));

auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context);
std::shared_ptr<opentelemetry::metrics::MeterProvider> provider(std::move(u_provider));

metrics_api::Provider::SetMeterProvider(provider);
}
Expand All @@ -59,14 +69,14 @@ int main(int argc, char *argv[])
std::string example_type;
if (argc > 1)
{
options.endpoint = argv[1];
exporter_options.endpoint = argv[1];
if (argc > 2)
{
example_type = argv[2];
if (argc > 3)
{
options.use_ssl_credentials = true;
options.ssl_credentials_cacert_path = argv[3];
exporter_options.use_ssl_credentials = true;
exporter_options.ssl_credentials_cacert_path = argv[3];
}
}
}
Expand Down
Loading