From df44c963a313cc310b61157887ceebbd1f443232 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 16 Apr 2022 17:27:27 +0200 Subject: [PATCH 1/4] metrics histogram example --- .../common/metrics_foo_library/foo_library.cc | 35 +++++++++++++----- .../common/metrics_foo_library/foo_library.h | 7 +++- examples/metrics_simple/metrics_ostream.cc | 36 ++++++++++++++++--- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 344f51c082..93494599d2 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -2,24 +2,43 @@ // SPDX-License-Identifier: Apache-2.0 #ifndef ENABLE_METRICS_PREVIEW +# include "foo_library.h" # include +# include # include # include "opentelemetry/metrics/provider.h" namespace nostd = opentelemetry::nostd; namespace metrics_api = opentelemetry::metrics; -void foo_library(const std::string &name) +void foo_library::counter_example(const std::string &name) { - // Get the Meter from the MeterProvider auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); auto double_counter = meter->CreateDoubleCounter(name); - double_counter->Add(28.5); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - double_counter->Add(3.14); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - double_counter->Add(23.5); - std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + + while (true) + { + double val = (rand() % 700) + 1.1; + double_counter->Add(val); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } } + +void foo_library::histogram_example(const std::string &name) +{ + auto provider = metrics_api::Provider::GetMeterProvider(); + nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); + auto histogram_counter = meter->CreateDoubleHistogram(name); + auto context = opentelemetry::context::Context{}; + std::map labels = {{"key", "value"}}; + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; + while (true) + { + double val = (rand() % 700) + 1.1; + histogram_counter->Record(val, labelkv, context); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } +} + #endif diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 662646a6a4..73cd30d2d5 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -5,5 +5,10 @@ #ifndef ENABLE_METRICS_PREVIEW # include -void foo_library(const std::string &name); +class foo_library +{ +public: + static void counter_example(const std::string &name); + static void histogram_example(const std::string &name); +}; #endif diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 9fae5f07b7..ad2adba50c 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -44,21 +44,49 @@ void initMetrics(const std::string &name) new metric_sdk::MeterProvider(std::move(exporters))); auto p = std::static_pointer_cast(provider); p->AddMetricReader(std::move(reader)); + + // counter view std::unique_ptr instrument_selector{ new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, name)}; std::unique_ptr meter_selector{ new metric_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr view{ + std::unique_ptr sum_view{ new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}}; - p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view)); + p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); + + // histogram view + std::unique_ptr histogram_instrument_selector{ + new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, name)}; + std::unique_ptr histogram_meter_selector{ + new metric_sdk::MeterSelector(name, version, schema)}; + std::unique_ptr histogram_view{ + new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}}; + p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), + std::move(histogram_view)); metrics_api::Provider::SetMeterProvider(provider); } } // namespace -int main() + +int main(int argc, char **argv) { + if (argc != 2) + { + std::puts("invalid number of command line arguments"); + return 0; + } + + std::string example_type(argv[1]); std::string name{"ostream_metric_example"}; initMetrics(name); - foo_library(name); + + if (example_type == "counter") + { + foo_library::counter_example(name); + } + else + { + foo_library::histogram_example(name); + } } #else int main() {} From 4885df08a9fd3af79006e285479cdca02b37fedc Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 17 Apr 2022 09:45:17 +0000 Subject: [PATCH 2/4] fix CI --- examples/common/metrics_foo_library/foo_library.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 93494599d2..72b9f3761e 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -4,7 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "foo_library.h" # include -# include +# include # include # include "opentelemetry/metrics/provider.h" From 7e377cc65edff500755ec77a2f995ddeaebe35f3 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 18 Apr 2022 09:13:03 +0000 Subject: [PATCH 3/4] comments --- .../common/metrics_foo_library/foo_library.cc | 22 +++++++++++++++--- examples/metrics_simple/metrics_ostream.cc | 23 +++++++++++++------ exporters/ostream/src/metric_exporter.cc | 2 ++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 72b9f3761e..6bda315cc9 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -11,11 +11,26 @@ namespace nostd = opentelemetry::nostd; namespace metrics_api = opentelemetry::metrics; +namespace +{ +std::map get_random_attr() +{ + static const std::vector> labels = {{"key1", "value1"}, + {"key2", "value2"}, + {"key3", "value3"}, + {"key4", "value4"}, + {"key5", "value5"}}; + return std::map{labels[rand() % (labels.size() - 1)], + labels[rand() % (labels.size() - 1)]}; +} +} // namespace + void foo_library::counter_example(const std::string &name) { + std::string counter_name = name + "_counter"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto double_counter = meter->CreateDoubleCounter(name); + auto double_counter = meter->CreateDoubleCounter(counter_name); while (true) { @@ -27,11 +42,12 @@ void foo_library::counter_example(const std::string &name) void foo_library::histogram_example(const std::string &name) { + std::string histogram_name = name + "_histogram"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto histogram_counter = meter->CreateDoubleHistogram(name); + auto histogram_counter = meter->CreateDoubleHistogram(histogram_name); auto context = opentelemetry::context::Context{}; - std::map labels = {{"key", "value"}}; + std::map labels = get_random_attr(); auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; while (true) { diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index ad2adba50c..d0e4dccfeb 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -3,6 +3,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include +# include # include "opentelemetry/exporters/ostream/metric_exporter.h" # include "opentelemetry/metrics/provider.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" @@ -46,8 +47,9 @@ void initMetrics(const std::string &name) 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, name)}; + 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{ @@ -55,8 +57,9 @@ void initMetrics(const std::string &name) 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 metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, name)}; + new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)}; std::unique_ptr histogram_meter_selector{ new metric_sdk::MeterSelector(name, version, schema)}; std::unique_ptr histogram_view{ @@ -69,13 +72,12 @@ void initMetrics(const std::string &name) int main(int argc, char **argv) { - if (argc != 2) + std::string example_type; + if (argc >= 2) { - std::puts("invalid number of command line arguments"); - return 0; + example_type = argv[1]; } - std::string example_type(argv[1]); std::string name{"ostream_metric_example"}; initMetrics(name); @@ -83,10 +85,17 @@ int main(int argc, char **argv) { foo_library::counter_example(name); } - else + else if (example_type == "histogram") { foo_library::histogram_example(name); } + else + { + std::thread counter_example{&foo_library::counter_example, name}; + std::thread histogram_example{&foo_library::histogram_example, name}; + counter_example.join(); + histogram_example.join(); + } } #else int main() {} diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index fad9e03d49..5cdd3fe78d 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -59,6 +59,8 @@ sdk::common::ExportResult OStreamMetricExporter::Export( void OStreamMetricExporter::printInstrumentationInfoMetricData( const sdk::metrics::InstrumentationInfoMetrics &info_metric) { + // sout_ is shared + const std::lock_guard locked(lock_); sout_ << "{"; sout_ << "\n name\t\t: " << info_metric.instrumentation_library_->GetName() << "\n schema url\t: " << info_metric.instrumentation_library_->GetSchemaURL() From ea43cc6a3091a2ec8be4605fc6b6c5a2f65f9543 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 18 Apr 2022 09:28:44 +0000 Subject: [PATCH 4/4] random attributes --- examples/common/metrics_foo_library/foo_library.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 6bda315cc9..cd15adad5b 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -47,11 +47,11 @@ void foo_library::histogram_example(const std::string &name) nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); auto histogram_counter = meter->CreateDoubleHistogram(histogram_name); auto context = opentelemetry::context::Context{}; - std::map labels = get_random_attr(); - auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; while (true) { - double val = (rand() % 700) + 1.1; + double val = (rand() % 700) + 1.1; + std::map labels = get_random_attr(); + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; histogram_counter->Record(val, labelkv, context); std::this_thread::sleep_for(std::chrono::milliseconds(250)); }