diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 30a82a02b5..a81937099d 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -3,13 +3,19 @@ #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" @@ -17,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; @@ -27,59 +33,75 @@ 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"}; // 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 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 reader = + metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); + + 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"; - 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 = 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 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 = 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 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 = 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( + 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::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 provider(std::move(u_provider)); metrics_api::Provider::SetMeterProvider(provider); } 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/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 1c34ebed75..40d9777776 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -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 #include @@ -25,24 +28,30 @@ 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 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); - 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)); + + 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); } @@ -59,14 +68,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/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc new file mode 100644 index 0000000000..a361285fed --- /dev/null +++ b/examples/otlp/http_metric_main.cc @@ -0,0 +1,142 @@ +// 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)); + + 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(); +} 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/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..10e0792de8 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..0526165026 --- /dev/null +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter_factory.h @@ -0,0 +1,50 @@ +// 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/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 806ca01f70..c6387a66c8 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -6,8 +6,10 @@ 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 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 29c2e351e5..323727c873 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 @@ -10,6 +10,7 @@ #include #include +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/version.h" @@ -20,22 +21,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..c4254793f3 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -0,0 +1,28 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#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 +namespace sdk +{ +namespace metrics +{ + +class MetricReader; +class PushMetricExporter; + +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_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/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h new file mode 100644 index 0000000000..7a1343980c --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -0,0 +1,77 @@ +// 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 +{ + +/* + 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: + 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/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/include/opentelemetry/sdk/metrics/view/view_registry.h b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h index 9f12259665..d017bd7107 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry.h @@ -78,6 +78,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..f8f83c21ad --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_registry_factory.h @@ -0,0 +1,25 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class ViewRegistry; + +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 cd831eaac8..f79d17dda2 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -5,11 +5,14 @@ add_library( opentelemetry_metrics async_instruments.cc meter_provider.cc + 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 + export/periodic_exporting_metric_reader_factory.cc state/metric_collector.cc state/observable_registry.cc state/sync_metric_storage.cc @@ -21,6 +24,10 @@ 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) 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..c4f0b8348a --- /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/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 diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc new file mode 100644 index 0000000000..38527c90d8 --- /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/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/metrics/meter.h" +#include "opentelemetry/sdk/metrics/meter_provider.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" +#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 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 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