From c678c29f4b3cc72ae9ed9289543070264fbf3c02 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 14 Feb 2023 23:20:37 +0100 Subject: [PATCH 01/12] WIP - DRAFT --- examples/otlp/grpc_metric_main.cc | 10 +++- .../export/periodic_exporting_metric_reader.h | 17 +----- ...periodic_exporting_metric_reader_factory.h | 26 +++++++++ ...periodic_exporting_metric_reader_options.h | 35 +++++++++++ .../sdk/metrics/meter_provider_factory.h | 41 +++++++++++++ .../sdk/metrics/view/view_registry.h | 1 + .../sdk/metrics/view/view_registry_factory.h | 22 +++++++ sdk/src/metrics/CMakeLists.txt | 1 + sdk/src/metrics/meter_provider_factory.cc | 58 +++++++++++++++++++ 9 files changed, 194 insertions(+), 17 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h create mode 100644 sdk/src/metrics/meter_provider_factory.cc diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 1c34ebed75..975695b6ff 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -38,10 +38,18 @@ void InitMetrics() metric_sdk::PeriodicExportingMetricReaderOptions options; options.export_interval_millis = std::chrono::milliseconds(1000); options.export_timeout_millis = std::chrono::milliseconds(500); + std::unique_ptr reader{ new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; + + auto reader2 = + metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); + auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); - auto p = std::static_pointer_cast(provider); + + auto provider2 = metric_sdk::MeterProviderFactory::Create(); + + auto p = std::static_pointer_cast(provider); p->AddMetricReader(std::move(reader)); metrics_api::Provider::SetMeterProvider(provider); diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index 83773ffb61..fd743fd0fb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -4,6 +4,7 @@ #pragma once #include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" #include "opentelemetry/version.h" #include @@ -18,22 +19,6 @@ namespace metrics { class PushMetricExporter; -/** - * Struct to hold PeriodicExortingMetricReader options. - */ - -constexpr std::chrono::milliseconds kExportIntervalMillis = std::chrono::milliseconds(60000); -constexpr std::chrono::milliseconds kExportTimeOutMillis = std::chrono::milliseconds(30000); -struct PeriodicExportingMetricReaderOptions -{ - - /* The time interval between two consecutive exports. */ - std::chrono::milliseconds export_interval_millis = - std::chrono::milliseconds(kExportIntervalMillis); - - /* how long the export can run before it is cancelled. */ - std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); -}; class PeriodicExportingMetricReader : public MetricReader { diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h new file mode 100644 index 0000000000..38704bdbb2 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -0,0 +1,26 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class PeriodicExportingMetricReaderFactory +{ +public: + static std::unique_ptr Create( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &option); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h new file mode 100644 index 0000000000..80514f96d8 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +constexpr std::chrono::milliseconds kExportIntervalMillis = std::chrono::milliseconds(60000); +constexpr std::chrono::milliseconds kExportTimeOutMillis = std::chrono::milliseconds(30000); + +/** + * Struct to hold PeriodicExortingMetricReader options. + */ + +struct PeriodicExportingMetricReaderOptions +{ + /* The time interval between two consecutive exports. */ + std::chrono::milliseconds export_interval_millis = + std::chrono::milliseconds(kExportIntervalMillis); + + /* how long the export can run before it is cancelled. */ + std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h new file mode 100644 index 0000000000..73fc66cae2 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include +#include +#include "opentelemetry/metrics/meter.h" +#include "opentelemetry/metrics/meter_provider.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/sdk/metrics/meter.h" +#include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class MeterProviderFactory +{ +public: + static std::unique_ptr Create(); + + static std::unique_ptr Create( + std::unique_ptr views); + + static std::unique_ptr Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource); + + static std::unique_ptr Create( + std::shared_ptr context); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h index 87773b2091..d5436f8d20 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h @@ -73,6 +73,7 @@ class ViewRegistry return true; } + ViewRegistry() = default; ~ViewRegistry() = default; private: diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h new file mode 100644 index 0000000000..90a9165e3f --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/metrics/view/view_registry.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class ViewRegistryFactory +{ +public: + static std::unique_ptr Create(); + +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 46d6d67212..035d01cee1 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -5,6 +5,7 @@ add_library( opentelemetry_metrics async_instruments.cc meter_provider.cc + meter_provider_factory.cc meter.cc meter_context.cc metric_reader.cc diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc new file mode 100644 index 0000000000..be4f9f1eb7 --- /dev/null +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -0,0 +1,58 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/metrics/meter.h" +#include "opentelemetry/sdk/metrics/meter_provider.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/sdk/metrics/view/view_registry_factory.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" + +#include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk_config.h" +#include "opentelemetry/version.h" + +#include + +namespace resource = opentelemetry::sdk::resource; +namespace metrics_api = opentelemetry::metrics; +namespace metrics_sdk = opentelemetry::sdk::metrics; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr MeterProviderFactory::Create() +{ + auto views = ViewRegistryFactory::Create(); + return Create(std::move(views)); +} + +std::unique_ptr MeterProviderFactory::Create( + std::unique_ptr views) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(views), resource); +} + +std::unique_ptr MeterProviderFactory::Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource) +{ + std::unique_ptr provider( + new metrics_sdk::MeterProvider(std::move(views), resource)); + return provider; +} + +std::unique_ptr MeterProviderFactory::Create( + std::shared_ptr context) +{ + std::unique_ptr provider( + new metrics_sdk::MeterProvider(std::move(context))); + return provider; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From 05bd48d6434df323e29ef9236e5d48459bc77d9a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Jun 2023 15:11:39 +0200 Subject: [PATCH 02/12] WIP --- examples/otlp/grpc_metric_main.cc | 2 ++ .../metrics/export/periodic_exporting_metric_reader_factory.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 975695b6ff..2d350ea9d3 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -5,8 +5,10 @@ #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_provider.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" #include #include diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h index 38704bdbb2..564d402d20 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -4,7 +4,7 @@ #pragma once #include "opentelemetry/sdk/metrics/metric_reader.h" -#include "opentelemetry/sdk/metrics/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE From a1ffbfa8dd62e46e36bb38f25eaf85440b37c3f9 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 9 Jun 2023 11:45:50 +0200 Subject: [PATCH 03/12] WIP --- ...periodic_exporting_metric_reader_factory.h | 10 ++++--- .../sdk/metrics/view/view_registry_factory.h | 5 ++-- sdk/src/metrics/CMakeLists.txt | 2 ++ ...eriodic_exporting_metric_reader_factory.cc | 26 +++++++++++++++++++ sdk/src/metrics/view/view_registry_factory.cc | 22 ++++++++++++++++ 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc create mode 100644 sdk/src/metrics/view/view_registry_factory.cc diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h index 564d402d20..ca4f6012eb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -3,8 +3,8 @@ #pragma once -#include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -13,12 +13,14 @@ namespace sdk namespace metrics { +class MetricReader; +class PushMetricExporter; + class PeriodicExportingMetricReaderFactory { public: - static std::unique_ptr Create( - std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option); + static std::unique_ptr Create(std::unique_ptr &&exporter, + const PeriodicExportingMetricReaderOptions &option); }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h index 90a9165e3f..4c550867a7 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h @@ -3,7 +3,7 @@ #pragma once -#include "opentelemetry/sdk/metrics/view/view_registry.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -11,11 +11,12 @@ namespace sdk namespace metrics { +class ViewRegistry; + class ViewRegistryFactory { public: static std::unique_ptr Create(); - }; } // namespace metrics } // namespace sdk diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 0762097e53..4b68b012d4 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -11,6 +11,7 @@ add_library( metric_reader.cc instrument_metadata_validator.cc export/periodic_exporting_metric_reader.cc + export/periodic_exporting_metric_reader_factory.cc state/metric_collector.cc state/observable_registry.cc state/sync_metric_storage.cc @@ -21,6 +22,7 @@ add_library( data/circular_buffer.cc exemplar/filter.cc exemplar/reservoir.cc + view/view_registry_factory.cc sync_instruments.cc) set_target_properties(opentelemetry_metrics PROPERTIES EXPORT_NAME metrics) diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc new file mode 100644 index 0000000000..721d7881f9 --- /dev/null +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc @@ -0,0 +1,26 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" +#include "opentelemetry/sdk/metrics/push_metric_exporter.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr PeriodicExportingMetricReaderFactory::Create( + std::unique_ptr &&exporter, + const PeriodicExportingMetricReaderOptions &option) +{ + std::unique_ptr reader( + new PeriodicExportingMetricReader(std::move(exporter), option)); + return reader; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/view/view_registry_factory.cc b/sdk/src/metrics/view/view_registry_factory.cc new file mode 100644 index 0000000000..ba8a4c5e21 --- /dev/null +++ b/sdk/src/metrics/view/view_registry_factory.cc @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/view/view_registry_factory.h" +#include "opentelemetry/sdk/metrics/view/view_registry.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr ViewRegistryFactory::Create() +{ + std::unique_ptr view_registry(new ViewRegistry()); + return view_registry; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From 7a1b3b81cd92119d14f97978c418a6413731bb0e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 12 Jun 2023 11:16:50 +0200 Subject: [PATCH 04/12] WIP --- examples/otlp/grpc_metric_main.cc | 44 +++++++++++++------ .../sdk/metrics/meter_context_factory.h | 41 +++++++++++++++++ .../sdk/metrics/view/view_registry_factory.h | 2 + sdk/src/metrics/CMakeLists.txt | 1 + sdk/src/metrics/meter_context_factory.cc | 38 ++++++++++++++++ 5 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h create mode 100644 sdk/src/metrics/meter_context_factory.cc diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 2d350ea9d3..d824a09ac0 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -7,6 +7,7 @@ #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" @@ -27,32 +28,47 @@ 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); + metric_sdk::PeriodicExportingMetricReaderOptions reader_options; + reader_options.export_interval_millis = std::chrono::milliseconds(1000); + reader_options.export_timeout_millis = std::chrono::milliseconds(500); - std::unique_ptr reader{ - new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; + // #define OLD - auto reader2 = - metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); +#ifdef OLD - auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); + // TODO: To cleanup for final review. - auto provider2 = metric_sdk::MeterProviderFactory::Create(); + std::unique_ptr reader{ + new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), reader_options)}; + auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); auto p = std::static_pointer_cast(provider); p->AddMetricReader(std::move(reader)); +#else + + 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 + std::shared_ptr s_context(std::move(context)); + + auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); + std::shared_ptr provider(std::move(u_provider)); + +#endif metrics_api::Provider::SetMeterProvider(provider); } @@ -69,14 +85,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]; } } } diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h new file mode 100644 index 0000000000..d5ca16cc27 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" + +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +// forward declaration +class MeterContext; +class ViewRegistry; + +/** + * Factory class for MeterContext. + */ +class MeterContextFactory +{ +public: + /** + * Create a MeterContext. + */ + static std::unique_ptr Create(); + + static std::unique_ptr Create(std::unique_ptr views); + + static std::unique_ptr Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h index 4c550867a7..f8f83c21ad 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 4b68b012d4..333b00c7f6 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -8,6 +8,7 @@ add_library( meter_provider_factory.cc meter.cc meter_context.cc + meter_context_factory.cc metric_reader.cc instrument_metadata_validator.cc export/periodic_exporting_metric_reader.cc diff --git a/sdk/src/metrics/meter_context_factory.cc b/sdk/src/metrics/meter_context_factory.cc new file mode 100644 index 0000000000..d41bb6a193 --- /dev/null +++ b/sdk/src/metrics/meter_context_factory.cc @@ -0,0 +1,38 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/meter_context_factory.h" +#include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/view/view_registry_factory.h" + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr MeterContextFactory::Create() +{ + auto views = ViewRegistryFactory::Create(); + return Create(std::move(views)); +} + +std::unique_ptr MeterContextFactory::Create(std::unique_ptr views) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(views), resource); +} + +std::unique_ptr MeterContextFactory::Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource) +{ + std::unique_ptr context(new MeterContext(std::move(views), resource)); + return context; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From 620989d46971a6e4de10279fcf7cc7ab516ed116 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 12 Jun 2023 11:58:02 +0200 Subject: [PATCH 05/12] Format --- .../sdk/metrics/export/periodic_exporting_metric_reader.h | 2 +- sdk/include/opentelemetry/sdk/metrics/view/view_registry.h | 2 +- sdk/src/metrics/meter_provider_factory.cc | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index fd743fd0fb..cd1823610e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -3,8 +3,8 @@ #pragma once -#include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/version.h" #include diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h index d5436f8d20..23f6a9f625 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h @@ -73,7 +73,7 @@ class ViewRegistry return true; } - ViewRegistry() = default; + ViewRegistry() = default; ~ViewRegistry() = default; private: diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc index be4f9f1eb7..38527c90d8 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -1,11 +1,11 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" #include "opentelemetry/metrics/meter.h" #include "opentelemetry/sdk/metrics/meter_provider.h" -#include "opentelemetry/sdk/metrics/meter_provider_factory.h" -#include "opentelemetry/sdk/metrics/view/view_registry_factory.h" #include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/view/view_registry_factory.h" #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk_config.h" From 95c043c93748e921c8e9d5057b106357c0901a60 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 12 Jun 2023 15:30:27 +0200 Subject: [PATCH 06/12] Add otlp http metric example. --- examples/otlp/BUILD | 21 ++++- examples/otlp/CMakeLists.txt | 34 +++++-- examples/otlp/http_metric_main.cc | 143 ++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 examples/otlp/http_metric_main.cc diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 25c5e84fe7..fcaa535438 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -76,7 +76,7 @@ cc_binary( ) cc_binary( - name = "example_otlp_grpc_metrics", + name = "example_otlp_grpc_metric", srcs = [ "grpc_metric_main.cc", ], @@ -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", + ], +) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index a9263a38b5..dd8cc054c2 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -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} @@ -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) @@ -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} @@ -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} @@ -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() diff --git a/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc new file mode 100644 index 0000000000..48ce4cf0e6 --- /dev/null +++ b/examples/otlp/http_metric_main.cc @@ -0,0 +1,143 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/metrics/provider.h" +#include "opentelemetry/sdk/common/global_log_handler.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 +#include + +#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 common = opentelemetry::common; +namespace metrics_api = opentelemetry::metrics; +namespace otlp_exporter = opentelemetry::exporter::otlp; + +namespace internal_log = opentelemetry::sdk::common::internal_log; + +namespace +{ + +otlp_exporter::OtlpHttpMetricExporterOptions exporter_options; + +void InitMetrics() +{ + auto exporter = otlp_exporter::OtlpHttpMetricExporterFactory::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 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 + std::shared_ptr s_context(std::move(context)); + + auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); + std::shared_ptr provider(std::move(u_provider)); + + metrics_api::Provider::SetMeterProvider(provider); +} + +void CleanupMetrics() +{ + std::shared_ptr none; + metrics_api::Provider::SetMeterProvider(none); +} +} // namespace + +/* + Usage: + - example_otlp_http_metric + - example_otlp_http_metric + - example_otlp_http_metric + - example_otlp_http_metric + - example_otlp_http_metric + = counter|observable_counter|histogram|all + = yes|no, to turn console debug on or off + = bin, to export in binary format +*/ +int main(int argc, char *argv[]) +{ + std::string example_type; + if (argc > 1) + { + exporter_options.url = argv[1]; + } + + if (argc > 2) + { + example_type = argv[2]; + } + + if (argc > 3) + { + std::string debug = argv[3]; + exporter_options.console_debug = debug != "" && debug != "0" && debug != "no"; + } + + if (argc > 4) + { + std::string binary_mode = argv[4]; + if (binary_mode.size() >= 3 && binary_mode.substr(0, 3) == "bin") + { + exporter_options.content_type = otlp_exporter::HttpRequestContentType::kBinary; + } + } + + if (exporter_options.console_debug) + { + internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug); + } + + // Removing this line will leave the default noop MetricProvider in place. + InitMetrics(); + std::string name{"otlp_http_metric_example"}; + + if (example_type == "counter") + { + foo_library::counter_example(name); + } + else if (example_type == "observable_counter") + { + foo_library::observable_counter_example(name); + } + else if (example_type == "histogram") + { + foo_library::histogram_example(name); + } + else + { + std::thread counter_example{&foo_library::counter_example, name}; + std::thread observable_counter_example{&foo_library::observable_counter_example, name}; + std::thread histogram_example{&foo_library::histogram_example, name}; + + counter_example.join(); + observable_counter_example.join(); + histogram_example.join(); + } + + CleanupMetrics(); +} From cb97d58820a922430410386e85fda3611ae1dc47 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 15 Jun 2023 17:31:01 +0200 Subject: [PATCH 07/12] Added OStreamMetricExporterFactory --- examples/metrics_simple/metrics_ostream.cc | 10 +++- examples/otlp/grpc_metric_main.cc | 16 ------ exporters/ostream/BUILD | 2 + exporters/ostream/CMakeLists.txt | 3 +- .../ostream/metric_exporter_factory.h | 49 +++++++++++++++++++ .../ostream/src/metric_exporter_factory.cc | 36 ++++++++++++++ ...periodic_exporting_metric_reader_factory.h | 2 +- .../sdk/metrics/meter_provider_factory.h | 36 ++++++++++++++ ...eriodic_exporting_metric_reader_factory.cc | 2 +- 9 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h create mode 100644 exporters/ostream/src/metric_exporter_factory.cc diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 30a82a02b5..1e638e1565 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -3,6 +3,7 @@ #include #include + #include "opentelemetry/exporters/ostream/metric_exporter.h" #include "opentelemetry/metrics/provider.h" #include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" @@ -10,6 +11,7 @@ #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/meter.h" #include "opentelemetry/sdk/metrics/meter_provider.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" #ifdef BAZEL_BUILD # include "examples/common/metrics_foo_library/foo_library.h" @@ -39,8 +41,10 @@ void InitMetrics(const std::string &name) options.export_timeout_millis = std::chrono::milliseconds(500); std::unique_ptr reader{ new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; - auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); - auto p = std::static_pointer_cast(provider); + + auto u_provider = metric_sdk::MeterProviderFactory::Create(); + auto *p = static_cast(u_provider.get()); + p->AddMetricReader(std::move(reader)); // counter view @@ -80,6 +84,8 @@ void InitMetrics(const std::string &name) name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}}; p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); + + std::shared_ptr provider(std::move(u_provider)); metrics_api::Provider::SetMeterProvider(provider); } diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index d824a09ac0..3e36ecb6d1 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -42,20 +42,6 @@ void InitMetrics() reader_options.export_interval_millis = std::chrono::milliseconds(1000); reader_options.export_timeout_millis = std::chrono::milliseconds(500); - // #define OLD - -#ifdef OLD - - // TODO: To cleanup for final review. - - std::unique_ptr reader{ - new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), reader_options)}; - auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); - - auto p = std::static_pointer_cast(provider); - p->AddMetricReader(std::move(reader)); -#else - auto reader = metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), reader_options); @@ -68,8 +54,6 @@ void InitMetrics() auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); std::shared_ptr provider(std::move(u_provider)); -#endif - metrics_api::Provider::SetMeterProvider(provider); } diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index a80e84a024..a8af2e6c28 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -36,10 +36,12 @@ cc_library( name = "ostream_metric_exporter", srcs = [ "src/metric_exporter.cc", + "src/metric_exporter_factory.cc", ], hdrs = [ "include/opentelemetry/exporters/ostream/common_utils.h", "include/opentelemetry/exporters/ostream/metric_exporter.h", + "include/opentelemetry/exporters/ostream/metric_exporter_factory.h", ], strip_include_prefix = "include", tags = [ diff --git a/exporters/ostream/CMakeLists.txt b/exporters/ostream/CMakeLists.txt index af24e9f1ad..9fdb581fe3 100644 --- a/exporters/ostream/CMakeLists.txt +++ b/exporters/ostream/CMakeLists.txt @@ -40,7 +40,8 @@ if(BUILD_TESTING) TEST_LIST ostream_span_test) endif() # BUILD_TESTING -add_library(opentelemetry_exporter_ostream_metrics src/metric_exporter.cc) +add_library(opentelemetry_exporter_ostream_metrics src/metric_exporter.cc +src/metric_exporter_factory.cc) set_target_properties(opentelemetry_exporter_ostream_metrics PROPERTIES EXPORT_NAME ostream_metrics_exporter) set_target_version(opentelemetry_exporter_ostream_metrics) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h new file mode 100644 index 0000000000..a012e51e8b --- /dev/null +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h @@ -0,0 +1,49 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include + +#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 metrics +{ + +/** + * Factory class for OStreamMetricExporter. + */ +class OPENTELEMETRY_EXPORT OStreamMetricExporterFactory +{ +public: + /** + * Creates an OStreamMetricExporter writing to the default location. + */ + static std::unique_ptr Create(); + + /** + * Creates an OStreamMetricExporter writing to the given location. + */ + static std::unique_ptr Create(std::ostream &sout); + + static std::unique_ptr Create( + std::ostream &sout, + sdk::metrics::AggregationTemporality aggregation_temporality); +}; + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/ostream/src/metric_exporter_factory.cc b/exporters/ostream/src/metric_exporter_factory.cc new file mode 100644 index 0000000000..c5750b6f29 --- /dev/null +++ b/exporters/ostream/src/metric_exporter_factory.cc @@ -0,0 +1,36 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/ostream/metric_exporter_factory.h" +#include "opentelemetry/exporters/ostream/metric_exporter.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ + +std::unique_ptr +OStreamMetricExporterFactory::Create() +{ + return Create(std::cout); +} + +std::unique_ptr +OStreamMetricExporterFactory::Create(std::ostream &sout) +{ + return Create(sout, sdk::metrics::AggregationTemporality::kCumulative); +} + +std::unique_ptr +OStreamMetricExporterFactory::Create(std::ostream &sout, + sdk::metrics::AggregationTemporality aggregation_temporality) +{ + std::unique_ptr exporter( + new OStreamMetricExporter(sout, aggregation_temporality)); + return exporter; +} + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h index ca4f6012eb..c4254793f3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -19,7 +19,7 @@ class PushMetricExporter; class PeriodicExportingMetricReaderFactory { public: - static std::unique_ptr Create(std::unique_ptr &&exporter, + static std::unique_ptr Create(std::unique_ptr exporter, const PeriodicExportingMetricReaderOptions &option); }; diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 73fc66cae2..7a1343980c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -20,6 +20,42 @@ namespace sdk namespace metrics { +/* + MAINTAINER: + + The best design is to return an API object: + std::unique_ptr + to shield the calling application from SDK implementation details. + + This however assumes that the SDK object can be created in one call, + instead of making multiple calls to the SDK to setup a meter provider. + + Because existing code, already advertised in examples: + - creates an instance of sdk::MeterProvider + - calls SDK methods on it to continue the setup, such as + MeterProvider::AddMetricReader() + MeterProvider::AddView() + existing applications will need to access the underlying + class sdk::MeterProvider. + + We need to decide whether to return: + - (1) std::unique_ptr + - (2) std::unique_ptr + from a Create() method. + + In the long term, (1) is better, but forces users to use a down cast, + to make additional calls to the SDK class, until such a time when + the builders can take all the necessary input at once, + for example using a std::vector to add all readers. + + Implementing (2) is forcing technical debt, and prevents the + calling application configuring the SDK to be decoupled from it, + making deployment of shared libraries much more difficult. + + The design choice here is to return (1) an API MeterProvider, + even if this forces, temporarily, existing applications to use a downcast. +*/ + class MeterProviderFactory { public: diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc index 721d7881f9..c4f0b8348a 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc @@ -13,7 +13,7 @@ namespace metrics { std::unique_ptr PeriodicExportingMetricReaderFactory::Create( - std::unique_ptr &&exporter, + std::unique_ptr exporter, const PeriodicExportingMetricReaderOptions &option) { std::unique_ptr reader( From f564a101f86378a676db09e2e6da0230cec29782 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 19 Jun 2023 15:28:26 +0200 Subject: [PATCH 08/12] More metric builders. --- examples/metrics_simple/metrics_ostream.cc | 78 +++++++++++-------- exporters/ostream/CMakeLists.txt | 4 +- .../ostream/metric_exporter_factory.h | 3 +- .../view/instrument_selector_factory.h | 27 +++++++ .../sdk/metrics/view/meter_selector_factory.h | 29 +++++++ .../sdk/metrics/view/view_factory.h | 49 ++++++++++++ sdk/src/metrics/CMakeLists.txt | 3 + .../view/instrument_selector_factory.cc | 25 ++++++ .../metrics/view/meter_selector_factory.cc | 25 ++++++ sdk/src/metrics/view/view_factory.cc | 57 ++++++++++++++ 10 files changed, 266 insertions(+), 34 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/instrument_selector_factory.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/meter_selector_factory.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/view_factory.h create mode 100644 sdk/src/metrics/view/instrument_selector_factory.cc create mode 100644 sdk/src/metrics/view/meter_selector_factory.cc create mode 100644 sdk/src/metrics/view/view_factory.cc diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 1e638e1565..9311f73817 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -4,14 +4,18 @@ #include #include -#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" @@ -29,8 +33,7 @@ namespace void InitMetrics(const std::string &name) { - std::unique_ptr 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"}; @@ -39,49 +42,62 @@ void InitMetrics(const std::string &name) metric_sdk::PeriodicExportingMetricReaderOptions options; options.export_interval_millis = std::chrono::milliseconds(1000); options.export_timeout_millis = std::chrono::milliseconds(500); - std::unique_ptr reader{ - new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; + + auto reader = + metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); auto u_provider = metric_sdk::MeterProviderFactory::Create(); - auto *p = static_cast(u_provider.get()); + auto *p = static_cast(u_provider.get()); p->AddMetricReader(std::move(reader)); // counter view std::string counter_name = name + "_counter"; - std::unique_ptr instrument_selector{ - new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name)}; - std::unique_ptr meter_selector{ - new metric_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr sum_view{ - new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}}; + + auto instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( + metric_sdk::InstrumentType::kCounter, counter_name); + + auto meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + + auto sum_view = + metric_sdk::ViewFactory::Create(name, "description", metric_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 observable_instrument_selector{ - new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kObservableCounter, - observable_counter_name)}; - std::unique_ptr observable_meter_selector{ - new metric_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr observable_sum_view{ - new metric_sdk::View{name, "test_description", metric_sdk::AggregationType::kSum}}; + + auto observable_instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( + metric_sdk::InstrumentType::kObservableCounter, observable_counter_name); + + auto observable_meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + + auto observable_sum_view = + metric_sdk::ViewFactory::Create(name, "test_description", metric_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 histogram_instrument_selector{ - new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)}; - std::unique_ptr histogram_meter_selector{ - new metric_sdk::MeterSelector(name, version, schema)}; - std::shared_ptr aggregation_config{ - new opentelemetry::sdk::metrics::HistogramAggregationConfig}; - static_cast(aggregation_config.get()) - ->boundaries_ = std::vector{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 histogram_view{new metric_sdk::View{ - name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}}; + + auto histogram_instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( + metric_sdk::InstrumentType::kHistogram, histogram_name); + + auto histogram_meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + + auto histogram_aggregation_config = std::unique_ptr( + new metric_sdk::HistogramAggregationConfig); + + histogram_aggregation_config->boundaries_ = std::vector{ + 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 aggregation_config( + std::move(histogram_aggregation_config)); + + auto histogram_view = metric_sdk::ViewFactory::Create( + name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config); + p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); diff --git a/exporters/ostream/CMakeLists.txt b/exporters/ostream/CMakeLists.txt index 9fdb581fe3..10e0792de8 100644 --- a/exporters/ostream/CMakeLists.txt +++ b/exporters/ostream/CMakeLists.txt @@ -40,8 +40,8 @@ if(BUILD_TESTING) TEST_LIST ostream_span_test) endif() # BUILD_TESTING -add_library(opentelemetry_exporter_ostream_metrics src/metric_exporter.cc -src/metric_exporter_factory.cc) +add_library(opentelemetry_exporter_ostream_metrics + src/metric_exporter.cc src/metric_exporter_factory.cc) set_target_properties(opentelemetry_exporter_ostream_metrics PROPERTIES EXPORT_NAME ostream_metrics_exporter) set_target_version(opentelemetry_exporter_ostream_metrics) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h index a012e51e8b..0526165026 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h @@ -37,7 +37,8 @@ class OPENTELEMETRY_EXPORT OStreamMetricExporterFactory /** * Creates an OStreamMetricExporter writing to the given location. */ - static std::unique_ptr Create(std::ostream &sout); + static std::unique_ptr Create( + std::ostream &sout); static std::unique_ptr Create( std::ostream &sout, diff --git a/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector_factory.h new file mode 100644 index 0000000000..e5b8b83704 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector_factory.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/metrics/instruments.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class InstrumentSelector; + +class InstrumentSelectorFactory +{ +public: + static std::unique_ptr Create( + opentelemetry::sdk::metrics::InstrumentType instrument_type, + opentelemetry::nostd::string_view name); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/view/meter_selector_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/meter_selector_factory.h new file mode 100644 index 0000000000..f335f725f7 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/meter_selector_factory.h @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class MeterSelector; + +class MeterSelectorFactory +{ +public: + static std::unique_ptr Create(opentelemetry::nostd::string_view name, + opentelemetry::nostd::string_view version, + opentelemetry::nostd::string_view schema); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h new file mode 100644 index 0000000000..5e7da2ea62 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h @@ -0,0 +1,49 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" +#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +#include "opentelemetry/sdk/metrics/instruments.h" +#include "opentelemetry/sdk/metrics/view/attributes_processor.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class View; + +/** + * Factory class for View. + */ +class OPENTELEMETRY_EXPORT ViewFactory +{ +public: + static std::unique_ptr Create(const std::string &name); + + static std::unique_ptr Create(const std::string &name, const std::string &description); + + static std::unique_ptr Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type); + + static std::unique_ptr Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type, + std::shared_ptr aggregation_config); + + static std::unique_ptr Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type, + std::shared_ptr aggregation_config, + std::unique_ptr attributes_processor); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 333b00c7f6..c1ef0184fc 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -23,6 +23,9 @@ add_library( data/circular_buffer.cc exemplar/filter.cc exemplar/reservoir.cc + view/instrument_selector_factory.cc + view/meter_selector_factory.cc + view/view_factory.cc view/view_registry_factory.cc sync_instruments.cc) diff --git a/sdk/src/metrics/view/instrument_selector_factory.cc b/sdk/src/metrics/view/instrument_selector_factory.cc new file mode 100644 index 0000000000..24542a57cd --- /dev/null +++ b/sdk/src/metrics/view/instrument_selector_factory.cc @@ -0,0 +1,25 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h" +#include "opentelemetry/sdk/metrics/view/instrument_selector.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr InstrumentSelectorFactory::Create( + opentelemetry::sdk::metrics::InstrumentType instrument_type, + opentelemetry::nostd::string_view name) +{ + std::unique_ptr instrument_selector( + new InstrumentSelector(instrument_type, name)); + return instrument_selector; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/view/meter_selector_factory.cc b/sdk/src/metrics/view/meter_selector_factory.cc new file mode 100644 index 0000000000..f8f906d9ad --- /dev/null +++ b/sdk/src/metrics/view/meter_selector_factory.cc @@ -0,0 +1,25 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/view/meter_selector_factory.h" +#include "opentelemetry/sdk/metrics/view/meter_selector.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr MeterSelectorFactory::Create( + opentelemetry::nostd::string_view name, + opentelemetry::nostd::string_view version, + opentelemetry::nostd::string_view schema) +{ + std::unique_ptr meter_selector(new MeterSelector(name, version, schema)); + return meter_selector; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/view/view_factory.cc b/sdk/src/metrics/view/view_factory.cc new file mode 100644 index 0000000000..2e410accc6 --- /dev/null +++ b/sdk/src/metrics/view/view_factory.cc @@ -0,0 +1,57 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/view/view_factory.h" +#include "opentelemetry/sdk/metrics/view/view.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr ViewFactory::Create(const std::string &name) +{ + return Create(name, ""); +} + +std::unique_ptr ViewFactory::Create(const std::string &name, const std::string &description) +{ + return Create(name, description, AggregationType::kDefault); +} + +std::unique_ptr ViewFactory::Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type) +{ + std::shared_ptr aggregation_config(nullptr); + return Create(name, description, aggregation_type, aggregation_config); +} + +std::unique_ptr ViewFactory::Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type, + std::shared_ptr aggregation_config) +{ + auto attributes_processor = + std::unique_ptr(new DefaultAttributesProcessor()); + + return Create(name, description, aggregation_type, aggregation_config, + std::move(attributes_processor)); +} + +std::unique_ptr ViewFactory::Create(const std::string &name, + const std::string &description, + AggregationType aggregation_type, + std::shared_ptr aggregation_config, + std::unique_ptr attributes_processor) +{ + std::unique_ptr view(new View(name, description, aggregation_type, aggregation_config, + std::move(attributes_processor))); + return view; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From db35025d635ddf2f001992b62687619014946dec Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 20 Jun 2023 16:54:22 +0200 Subject: [PATCH 09/12] Implemented builders for prometheus. --- examples/metrics_simple/metrics_ostream.cc | 44 ++++++++-------- examples/prometheus/main.cc | 51 ++++++++++++------- exporters/prometheus/CMakeLists.txt | 5 +- .../exporters/prometheus/exporter.h | 22 +------- .../exporters/prometheus/exporter_factory.h | 34 +++++++++++++ .../exporters/prometheus/exporter_options.h | 29 +++++++++++ exporters/prometheus/src/exporter_factory.cc | 27 ++++++++++ exporters/prometheus/src/exporter_options.cc | 32 ++++++++++++ 8 files changed, 180 insertions(+), 64 deletions(-) create mode 100644 exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_factory.h create mode 100644 exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h create mode 100644 exporters/prometheus/src/exporter_factory.cc create mode 100644 exporters/prometheus/src/exporter_options.cc diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 9311f73817..a81937099d 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -23,7 +23,7 @@ # 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; @@ -39,41 +39,41 @@ void InitMetrics(const std::string &name) 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); auto reader = - metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); + metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); - auto u_provider = metric_sdk::MeterProviderFactory::Create(); - auto *p = static_cast(u_provider.get()); + auto u_provider = metrics_sdk::MeterProviderFactory::Create(); + auto *p = static_cast(u_provider.get()); p->AddMetricReader(std::move(reader)); // counter view std::string counter_name = name + "_counter"; - auto instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( - metric_sdk::InstrumentType::kCounter, counter_name); + auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create( + metrics_sdk::InstrumentType::kCounter, counter_name); - auto meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); auto sum_view = - metric_sdk::ViewFactory::Create(name, "description", metric_sdk::AggregationType::kSum); + 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"; - auto observable_instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( - metric_sdk::InstrumentType::kObservableCounter, observable_counter_name); + auto observable_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create( + metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name); - auto observable_meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + auto observable_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto observable_sum_view = - metric_sdk::ViewFactory::Create(name, "test_description", metric_sdk::AggregationType::kSum); + 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)); @@ -81,22 +81,22 @@ void InitMetrics(const std::string &name) // histogram view std::string histogram_name = name + "_histogram"; - auto histogram_instrument_selector = metric_sdk::InstrumentSelectorFactory::Create( - metric_sdk::InstrumentType::kHistogram, histogram_name); + auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create( + metrics_sdk::InstrumentType::kHistogram, histogram_name); - auto histogram_meter_selector = metric_sdk::MeterSelectorFactory::Create(name, version, schema); + auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto histogram_aggregation_config = std::unique_ptr( - new metric_sdk::HistogramAggregationConfig); + auto histogram_aggregation_config = std::unique_ptr( + new metrics_sdk::HistogramAggregationConfig); histogram_aggregation_config->boundaries_ = std::vector{ 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 aggregation_config( + std::shared_ptr aggregation_config( std::move(histogram_aggregation_config)); - auto histogram_view = metric_sdk::ViewFactory::Create( - name, "description", metric_sdk::AggregationType::kHistogram, 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)); diff --git a/examples/prometheus/main.cc b/examples/prometheus/main.cc index d620c761dd..6902a6b3e0 100644 --- a/examples/prometheus/main.cc +++ b/examples/prometheus/main.cc @@ -3,13 +3,18 @@ #include #include -#include "opentelemetry/exporters/prometheus/exporter.h" + +#include "opentelemetry/exporters/prometheus/exporter_factory.h" +#include "opentelemetry/exporters/prometheus/exporter_options.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/meter.h" #include "opentelemetry/sdk/metrics/meter_provider.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.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" @@ -37,34 +42,42 @@ void InitMetrics(const std::string &name, const std::string &addr) std::string version{"1.2.0"}; std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; - std::shared_ptr prometheus_exporter( - new metrics_exporter::PrometheusExporter(opts)); + auto prometheus_exporter = metrics_exporter::PrometheusExporterFactory::Create(opts); // Initialize and set the global MeterProvider - auto provider = std::shared_ptr(new metrics_sdk::MeterProvider()); - auto p = std::static_pointer_cast(provider); - p->AddMetricReader(prometheus_exporter); + auto u_provider = metrics_sdk::MeterProviderFactory::Create(); + auto *p = static_cast(u_provider.get()); + + p->AddMetricReader(std::move(prometheus_exporter)); // counter view std::string counter_name = name + "_counter"; - std::unique_ptr instrument_selector{ - new metrics_sdk::InstrumentSelector(metrics_sdk::InstrumentType::kCounter, counter_name)}; - std::unique_ptr meter_selector{ - new metrics_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr sum_view{ - new metrics_sdk::View{counter_name, "description", metrics_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(counter_name, "description", + metrics_sdk::AggregationType::kSum); + p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); // histogram view std::string histogram_name = name + "_histogram"; - std::unique_ptr histogram_instrument_selector{ - new metrics_sdk::InstrumentSelector(metrics_sdk::InstrumentType::kHistogram, histogram_name)}; - std::unique_ptr histogram_meter_selector{ - new metrics_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr histogram_view{new metrics_sdk::View{ - histogram_name, "description", metrics_sdk::AggregationType::kHistogram}}; + + 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_view = metrics_sdk::ViewFactory::Create(histogram_name, "description", + metrics_sdk::AggregationType::kHistogram); + p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); + + std::shared_ptr provider(std::move(u_provider)); metrics_api::Provider::SetMeterProvider(provider); } diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 806ca01f70..c3ebd5805c 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -6,8 +6,9 @@ if(NOT TARGET prometheus-cpp::core) find_package(prometheus-cpp CONFIG REQUIRED) endif() -add_library(opentelemetry_exporter_prometheus src/exporter.cc src/collector.cc - src/exporter_utils.cc) +add_library(opentelemetry_exporter_prometheus src/exporter.cc + src/exporter_options.cc src/exporter_factory.cc + src/collector.cc src/exporter_utils.cc) set_target_properties(opentelemetry_exporter_prometheus PROPERTIES EXPORT_NAME prometheus_exporter) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h index fe3c603e8a..4bd624d1e2 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h @@ -10,6 +10,7 @@ #include #include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/exporters/prometheus/collector.h" +#include "opentelemetry/exporters/prometheus/exporter_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/env_variables.h" #include "opentelemetry/sdk/metrics/metric_reader.h" @@ -28,27 +29,6 @@ namespace exporter namespace metrics { -inline const std::string GetPrometheusDefaultHttpEndpoint() -{ - constexpr char kPrometheusEndpointEnv[] = "PROMETHEUS_EXPORTER_ENDPOINT"; - constexpr char kPrometheusEndpointDefault[] = "localhost:9464"; - - std::string endpoint; - - auto exists = - opentelemetry::sdk::common::GetStringEnvironmentVariable(kPrometheusEndpointEnv, endpoint); - return exists ? endpoint : kPrometheusEndpointDefault; -} - -/** - * Struct to hold Prometheus exporter options. - */ -struct PrometheusExporterOptions -{ - // The endpoint the Prometheus backend can collect metrics from - std::string url = GetPrometheusDefaultHttpEndpoint(); -}; - class PrometheusExporter : public sdk::metrics::MetricReader { public: diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_factory.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_factory.h new file mode 100644 index 0000000000..67a30a5fef --- /dev/null +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_factory.h @@ -0,0 +1,34 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ + +struct PrometheusExporterOptions; + +/** + * Factory class for PrometheusExporter. + */ +class PrometheusExporterFactory +{ +public: + /** + * Create a PrometheusExporter using the given options. + */ + static std::unique_ptr Create( + const PrometheusExporterOptions &options); +}; + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h new file mode 100644 index 0000000000..76a08d2a4a --- /dev/null +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ + +/** + * Struct to hold Prometheus exporter options. + */ +struct PrometheusExporterOptions +{ + PrometheusExporterOptions(); + + // The endpoint the Prometheus backend can collect metrics from + std::string url; +}; + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/src/exporter_factory.cc b/exporters/prometheus/src/exporter_factory.cc new file mode 100644 index 0000000000..93fca80977 --- /dev/null +++ b/exporters/prometheus/src/exporter_factory.cc @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include "opentelemetry/exporters/prometheus/exporter.h" +#include "opentelemetry/exporters/prometheus/exporter_factory.h" +#include "opentelemetry/exporters/prometheus/exporter_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ + +std::unique_ptr PrometheusExporterFactory::Create( + const PrometheusExporterOptions &options) +{ + std::unique_ptr exporter( + new PrometheusExporter(options)); + return exporter; +} + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/src/exporter_options.cc b/exporters/prometheus/src/exporter_options.cc new file mode 100644 index 0000000000..507f119eb7 --- /dev/null +++ b/exporters/prometheus/src/exporter_options.cc @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include "opentelemetry/exporters/prometheus/exporter_options.h" +#include "opentelemetry/sdk/common/env_variables.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ + +inline const std::string GetPrometheusDefaultHttpEndpoint() +{ + constexpr char kPrometheusEndpointEnv[] = "PROMETHEUS_EXPORTER_ENDPOINT"; + constexpr char kPrometheusEndpointDefault[] = "localhost:9464"; + + std::string endpoint; + + auto exists = + opentelemetry::sdk::common::GetStringEnvironmentVariable(kPrometheusEndpointEnv, endpoint); + return exists ? endpoint : kPrometheusEndpointDefault; +} + +PrometheusExporterOptions::PrometheusExporterOptions() : url(GetPrometheusDefaultHttpEndpoint()) {} + +} // namespace metrics +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE From e4af50b5201cbc6710617bc6e6b6f300eda05c9a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 20 Jun 2023 17:08:04 +0200 Subject: [PATCH 10/12] build cleanup --- exporters/prometheus/BUILD | 4 ++++ exporters/prometheus/CMakeLists.txt | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index a7fb7e5e8c..fc981921d9 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -7,9 +7,13 @@ cc_library( name = "prometheus_exporter", srcs = [ "src/exporter.cc", + "src/exporter_factory.cc", + "src/exporter_options.cc", ], hdrs = [ "include/opentelemetry/exporters/prometheus/exporter.h", + "include/opentelemetry/exporters/prometheus/exporter_factory.h", + "include/opentelemetry/exporters/prometheus/exporter_options.h", ], strip_include_prefix = "include", tags = ["prometheus"], diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index c3ebd5805c..3f9048357b 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -6,9 +6,9 @@ if(NOT TARGET prometheus-cpp::core) find_package(prometheus-cpp CONFIG REQUIRED) endif() -add_library(opentelemetry_exporter_prometheus src/exporter.cc - src/exporter_options.cc src/exporter_factory.cc - src/collector.cc src/exporter_utils.cc) +add_library(opentelemetry_exporter_prometheus + src/exporter.cc src/exporter_options.cc src/exporter_factory.cc + src/collector.cc src/exporter_utils.cc) set_target_properties(opentelemetry_exporter_prometheus PROPERTIES EXPORT_NAME prometheus_exporter) From 9bc757915936ffcc43185db3b8819174094b1a7f Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 20 Jun 2023 18:04:59 +0200 Subject: [PATCH 11/12] Format --- exporters/prometheus/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 3f9048357b..c6387a66c8 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -6,7 +6,8 @@ if(NOT TARGET prometheus-cpp::core) find_package(prometheus-cpp CONFIG REQUIRED) endif() -add_library(opentelemetry_exporter_prometheus +add_library( + opentelemetry_exporter_prometheus src/exporter.cc src/exporter_options.cc src/exporter_factory.cc src/collector.cc src/exporter_utils.cc) From c6d1e91f2e651e37207f0a57d9bc02218bb5a1f1 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 27 Jun 2023 23:08:22 +0200 Subject: [PATCH 12/12] Code cleanup --- examples/otlp/grpc_metric_main.cc | 1 - examples/otlp/http_metric_main.cc | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 3e36ecb6d1..40d9777776 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -48,7 +48,6 @@ void InitMetrics() auto context = metric_sdk::MeterContextFactory::Create(); context->AddMetricReader(std::move(reader)); - // FIXME: MeterProvider / MeterProviderFactory should take a unique_ptr std::shared_ptr s_context(std::move(context)); auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); diff --git a/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc index 48ce4cf0e6..a361285fed 100644 --- a/examples/otlp/http_metric_main.cc +++ b/examples/otlp/http_metric_main.cc @@ -52,7 +52,6 @@ void InitMetrics() auto context = metric_sdk::MeterContextFactory::Create(); context->AddMetricReader(std::move(reader)); - // FIXME: MeterProvider / MeterProviderFactory should take a unique_ptr std::shared_ptr s_context(std::move(context)); auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context);