From b33dd0920ad71f3c04f40e753d3167beabca9dcb Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:37:01 +0200 Subject: [PATCH] metrics histogram example (#1330) --- .../common/metrics_foo_library/foo_library.cc | 53 +++++++++++++++---- .../common/metrics_foo_library/foo_library.h | 7 ++- examples/metrics_simple/metrics_ostream.cc | 47 ++++++++++++++-- exporters/ostream/src/metric_exporter.cc | 2 + 4 files changed, 94 insertions(+), 15 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 344f51c082..cd15adad5b 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -2,24 +2,59 @@ // 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) +namespace { - // Get the Meter from the MeterProvider +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); - 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)); + auto double_counter = meter->CreateDoubleCounter(counter_name); + + 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) +{ + 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(histogram_name); + auto context = opentelemetry::context::Context{}; + while (true) + { + 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)); + } +} + #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..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" @@ -44,21 +45,57 @@ 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::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 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::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::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) { + std::string example_type; + if (argc >= 2) + { + 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 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()