From 55f5572dc189a5657a97dc4fea19abf11e257bd7 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 10 May 2022 16:28:52 -0700 Subject: [PATCH 1/5] intgrate async storage with async instrument --- api/include/opentelemetry/metrics/meter.h | 56 +++++------ api/include/opentelemetry/metrics/noop.h | 84 ++++++---------- sdk/include/opentelemetry/sdk/metrics/meter.h | 55 +++++++---- sdk/src/metrics/meter.cc | 96 +++++++++++-------- 4 files changed, 145 insertions(+), 146 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index 9f7f2d2b0a..6a19911390 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -56,17 +56,15 @@ class Meter * @param callback the function to be observed by the instrument. * @return a shared pointer to the created Observable Counter. */ - virtual nostd::shared_ptr> CreateLongObservableCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + virtual void CreateLongObservableCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; - virtual nostd::shared_ptr> CreateDoubleObservableCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + virtual void CreateDoubleObservableCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; /** * Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram. @@ -96,17 +94,15 @@ class Meter * @param callback the function to be observed by the instrument. * @return a shared pointer to the created Observable Gauge. */ - virtual nostd::shared_ptr> CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + virtual void CreateLongObservableGauge(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; - virtual nostd::shared_ptr> CreateDoubleObservableGauge( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + virtual void CreateDoubleObservableGauge(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; /** * Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that @@ -137,17 +133,15 @@ class Meter * @param callback the function to be observed by the instrument. * @return a shared pointer to the created Observable UpDownCounter. */ - virtual nostd::shared_ptr> CreateLongObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; - - virtual nostd::shared_ptr> CreateDoubleObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + virtual void CreateLongObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; + + virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 3332384ee2..0fbda060b9 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -137,25 +137,17 @@ class NoopMeter final : public Meter return nostd::shared_ptr>{new NoopCounter(name, description, unit)}; } - nostd::shared_ptr> CreateLongObservableCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableCounter(name, callback, description, unit)}; - } + void CreateLongObservableCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} - nostd::shared_ptr> CreateDoubleObservableCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableCounter(name, callback, description, unit)}; - } + void CreateDoubleObservableCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, @@ -173,25 +165,17 @@ class NoopMeter final : public Meter return nostd::shared_ptr>{new NoopHistogram(name, description, unit)}; } - nostd::shared_ptr> CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableGauge(name, callback, description, unit)}; - } + void CreateLongObservableGauge(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} - nostd::shared_ptr> CreateDoubleObservableGauge( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableGauge(name, callback, description, unit)}; - } + void CreateDoubleObservableGauge(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, @@ -211,25 +195,17 @@ class NoopMeter final : public Meter new NoopUpDownCounter(name, description, unit)}; } - nostd::shared_ptr> CreateLongObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableUpDownCounter(name, callback, description, unit)}; - } + void CreateLongObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} - nostd::shared_ptr> CreateDoubleObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override - { - return nostd::shared_ptr>{ - new NoopObservableUpDownCounter(name, callback, description, unit)}; - } + void CreateDoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + {} }; /** diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 4a6ea26aeb..8e3295e1d3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -8,6 +8,8 @@ # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/meter_context.h" +# include "opentelemetry/sdk/metrics/state/async_metric_storage.h" + # include "opentelemetry/sdk/resource/resource.h" # include "opentelemetry/version.h" @@ -40,17 +42,16 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept override; - nostd::shared_ptr> CreateLongObservableCounter( + void CreateLongObservableCounter(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override; + + void CreateDoubleObservableCounter( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; - - nostd::shared_ptr> - CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "1") noexcept override; + nostd::string_view unit = "1") noexcept override; nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, @@ -62,13 +63,12 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept override; - nostd::shared_ptr> CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + void CreateLongObservableGauge(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override; - nostd::shared_ptr> CreateDoubleObservableGauge( + void CreateDoubleObservableGauge( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", @@ -84,15 +84,13 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept override; - nostd::shared_ptr> - CreateLongObservableUpDownCounter( + void CreateLongObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; - nostd::shared_ptr> - CreateDoubleObservableUpDownCounter( + void CreateDoubleObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", @@ -116,6 +114,25 @@ class Meter final : public opentelemetry::metrics::Meter std::unique_ptr RegisterMetricStorage( InstrumentDescriptor &instrument_descriptor); + + template + void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, + void (*callback)(opentelemetry::metrics::ObserverResult &)) + { + auto view_registry = meter_context_->GetViewRegistry(); + auto success = view_registry->FindViews( + instrument_descriptor, *instrumentation_library_, + [this, &instrument_descriptor, callback](const View &view) { + auto view_instr_desc = instrument_descriptor; + view_instr_desc.name_ = view.GetName(); + view_instr_desc.description_ = view.GetDescription(); + auto storage = std::shared_ptr>( + new AsyncMetricStorage(view_instr_desc, view.GetAggregationType(), callback, + &view.GetAttributesProcessor())); + storage_registry_[instrument_descriptor.name_] = storage; + return true; + }); + } }; } // namespace metrics } // namespace sdk diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index c0b0ce7a57..17250cc4a1 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -57,24 +57,28 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( new DoubleCounter(instrument_descriptor, std::move(storage))}; } -nostd::shared_ptr> Meter::CreateLongObservableCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateLongObservableCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new LongObservableCounter(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + InstrumentValueType::kLong}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } -nostd::shared_ptr> Meter::CreateDoubleObservableCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateDoubleObservableCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new DoubleObservableCounter(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + InstrumentValueType::kDouble}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } nostd::shared_ptr> Meter::CreateLongHistogram( @@ -105,24 +109,28 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( new DoubleHistogram(instrument_descriptor, std::move(storage))}; } -nostd::shared_ptr> Meter::CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateLongObservableGauge(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new LongObservableGauge(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + InstrumentValueType::kLong}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } -nostd::shared_ptr> Meter::CreateDoubleObservableGauge( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateDoubleObservableGauge(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new DoubleObservableGauge(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + InstrumentValueType::kDouble}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } nostd::shared_ptr> Meter::CreateLongUpDownCounter( @@ -153,24 +161,28 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount new DoubleUpDownCounter(instrument_descriptor, std::move(storage))}; } -nostd::shared_ptr> Meter::CreateLongObservableUpDownCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new LongObservableUpDownCounter(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, + InstrumentValueType::kLong}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } -nostd::shared_ptr> -Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), - nostd::string_view description, - nostd::string_view unit) noexcept +void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &), + nostd::string_view description, + nostd::string_view unit) noexcept { - return nostd::shared_ptr>{ - new DoubleObservableUpDownCounter(name, callback, description, unit)}; + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, + InstrumentValueType::kDouble}; + RegisterAsyncMetricStorage(instrument_descriptor, callback); } const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() From af087dbd9ca9b93f7a75a0fd05879d221861e677 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 10 May 2022 18:08:03 -0700 Subject: [PATCH 2/5] add example --- .../common/metrics_foo_library/foo_library.cc | 22 +++++++++++++++++++ .../common/metrics_foo_library/foo_library.h | 1 + 2 files changed, 23 insertions(+) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index cd15adad5b..6ee75c9417 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -13,6 +13,19 @@ namespace metrics_api = opentelemetry::metrics; namespace { + +class MeasurementFetcher +{ +public: + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result) + { + double val = (rand() % 700) + 1.1; + std::map labels = get_random_attr(); + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; + observer_result.Observe(val, labelkv); + } +}; + std::map get_random_attr() { static const std::vector> labels = {{"key1", "value1"}, @@ -23,6 +36,7 @@ std::map get_random_attr() return std::map{labels[rand() % (labels.size() - 1)], labels[rand() % (labels.size() - 1)]}; } + } // namespace void foo_library::counter_example(const std::string &name) @@ -40,6 +54,14 @@ void foo_library::counter_example(const std::string &name) } } +void foo_library::observable_counter_example(const std::string &name) +{ + std::string counter_name = name + "_observable_counter"; + auto provider = metrics_api::Provider::GetMeterProvider(); + nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); + auto double_observable_counter = meter->CreateDoubleObservableCounter(counter_name); +} + void foo_library::histogram_example(const std::string &name) { std::string histogram_name = name + "_histogram"; diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 73cd30d2d5..19fd69a4c5 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -10,5 +10,6 @@ class foo_library public: static void counter_example(const std::string &name); static void histogram_example(const std::string &name); + static void guage_example(const std::string &name); }; #endif From 5db3275d0c98ee7ee66f6231de21a02bffdee248 Mon Sep 17 00:00:00 2001 From: Lalit Date: Tue, 10 May 2022 20:53:54 -0700 Subject: [PATCH 3/5] fix example --- .../common/metrics_foo_library/foo_library.cc | 27 ++++++++++--------- .../common/metrics_foo_library/foo_library.h | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 6ee75c9417..1c0cd59ba3 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -6,6 +6,8 @@ # include # include # include +# include +# include "opentelemetry/context/context.h" # include "opentelemetry/metrics/provider.h" namespace nostd = opentelemetry::nostd; @@ -14,18 +16,6 @@ namespace metrics_api = opentelemetry::metrics; namespace { -class MeasurementFetcher -{ -public: - static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result) - { - double val = (rand() % 700) + 1.1; - std::map labels = get_random_attr(); - auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; - observer_result.Observe(val, labelkv); - } -}; - std::map get_random_attr() { static const std::vector> labels = {{"key1", "value1"}, @@ -37,6 +27,17 @@ std::map get_random_attr() labels[rand() % (labels.size() - 1)]}; } +class MeasurementFetcher +{ +public: + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result) + { + double val = (rand() % 700) + 1.1; + std::map labels = get_random_attr(); + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; + observer_result.Observe(val, labelkv); + } +}; } // namespace void foo_library::counter_example(const std::string &name) @@ -59,7 +60,7 @@ void foo_library::observable_counter_example(const std::string &name) std::string counter_name = name + "_observable_counter"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto double_observable_counter = meter->CreateDoubleObservableCounter(counter_name); + meter->CreateDoubleObservableCounter(counter_name, MeasurementFetcher::Fetcher); } void foo_library::histogram_example(const std::string &name) diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 19fd69a4c5..2f84bcc34f 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -10,6 +10,6 @@ class foo_library public: static void counter_example(const std::string &name); static void histogram_example(const std::string &name); - static void guage_example(const std::string &name); + static void observable_counter_example(const std::string &name); }; #endif From 0f9172f7bd69c8d1679fe9082155465222f1e3d4 Mon Sep 17 00:00:00 2001 From: Lalit Date: Tue, 10 May 2022 21:50:04 -0700 Subject: [PATCH 4/5] more changes --- .../common/metrics_foo_library/foo_library.cc | 6 +++++- examples/metrics_simple/metrics_ostream.cc | 19 +++++++++++++++++++ sdk/include/opentelemetry/sdk/metrics/meter.h | 2 ++ sdk/src/metrics/meter.cc | 4 ++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 1c0cd59ba3..79e0cbc5ab 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -35,7 +35,7 @@ class MeasurementFetcher double val = (rand() % 700) + 1.1; std::map labels = get_random_attr(); auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; - observer_result.Observe(val, labelkv); + observer_result.Observe(val /*, labelkv*/); } }; } // namespace @@ -61,6 +61,10 @@ void foo_library::observable_counter_example(const std::string &name) auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); meter->CreateDoubleObservableCounter(counter_name, MeasurementFetcher::Fetcher); + while (true) + { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } } void foo_library::histogram_example(const std::string &name) diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index ebb8b34b8a..585856f91f 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -54,6 +54,18 @@ void initMetrics(const std::string &name) new metric_sdk::View{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, "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{ @@ -83,6 +95,10 @@ int main(int argc, char **argv) { 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); @@ -90,8 +106,11 @@ int main(int argc, char **argv) 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(); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 8e3295e1d3..d9b2825d13 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -119,10 +119,12 @@ class Meter final : public opentelemetry::metrics::Meter void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, void (*callback)(opentelemetry::metrics::ObserverResult &)) { + std::cout << "---LALIT--Register async\n"; auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, [this, &instrument_descriptor, callback](const View &view) { + std::cout << "--LALIT Found view\n"; auto view_instr_desc = instrument_descriptor; view_instr_desc.name_ = view.GetName(); view_instr_desc.description_ = view.GetDescription(); diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 9fe4429253..30725bdccf 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -64,7 +64,7 @@ void Meter::CreateLongObservableCounter(nostd::string_view name, { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, - std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kLong}; RegisterAsyncMetricStorage(instrument_descriptor, callback); } @@ -76,7 +76,7 @@ void Meter::CreateDoubleObservableCounter(nostd::string_view name, { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, - std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, + std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kDouble}; RegisterAsyncMetricStorage(instrument_descriptor, callback); } From e365432644bc1a31b10a8ce11f38a86aacc9c96d Mon Sep 17 00:00:00 2001 From: Lalit Date: Tue, 10 May 2022 21:53:59 -0700 Subject: [PATCH 5/5] fix format --- sdk/include/opentelemetry/sdk/metrics/meter.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index d9b2825d13..9d9595f660 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -51,7 +51,7 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", - nostd::string_view unit = "1") noexcept override; + nostd::string_view unit = "") noexcept override; nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, @@ -119,12 +119,10 @@ class Meter final : public opentelemetry::metrics::Meter void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, void (*callback)(opentelemetry::metrics::ObserverResult &)) { - std::cout << "---LALIT--Register async\n"; auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, [this, &instrument_descriptor, callback](const View &view) { - std::cout << "--LALIT Found view\n"; auto view_instr_desc = instrument_descriptor; view_instr_desc.name_ = view.GetName(); view_instr_desc.description_ = view.GetDescription();