From e072daa229f72431b1c50986645d51180a586d09 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Mon, 21 Mar 2022 15:02:15 +0100 Subject: [PATCH 01/19] install sdk config (#1273) --- sdk/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index 9aa45883e5..6421e3ad15 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -13,6 +13,12 @@ install( LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +install( + DIRECTORY include/opentelemetry/ + DESTINATION include/opentelemetry/ + FILES_MATCHING + PATTERN "*config.h") + set(LOGS_EXCLUDE_PATTERN "") if(NOT WITH_LOGS_PREVIEW) set(LOGS_EXCLUDE_PATTERN "logs") From 6ec1b596fde0a2d443f5d730bcd14384b68c68e9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 22 Mar 2022 18:27:20 +0100 Subject: [PATCH 02/19] Bump actions/cache from 2 to 3 (#1277) --- .github/workflows/benchmark.yml | 2 +- .github/workflows/ci.yml | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index c11e0c564a..cc685faffd 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -17,7 +17,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0fd8f80838..a851728a2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -136,7 +136,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -158,7 +158,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -180,7 +180,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -202,7 +202,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -224,7 +224,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -246,7 +246,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -268,7 +268,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: @@ -285,7 +285,7 @@ jobs: with: submodules: 'recursive' - name: Mount Bazel Cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: bazel_cache with: From b5155a5dea3a8816d1d0aa3f513d6a5c13245d9b Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 23 Mar 2022 08:06:46 -0700 Subject: [PATCH 03/19] Add owent as an Approver (#1276) * add owent as reviewer * fix order --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 18b9aafd14..7dc77fc940 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ For edit access, get in touch on * [Josh Suereth](https://github.com/jsuereth), Google * [Reiley Yang](https://github.com/reyang), Microsoft +* [WenTao Ou](https://github.com/owent), Tencent [Emeritus Maintainer/Approver/Triager](https://github.com/open-telemetry/community/blob/main/community-membership.md#emeritus-maintainerapprovertriager): From 0c9aecea7af2f3f835f79397feb1436f8d51cc25 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Thu, 24 Mar 2022 16:45:38 +0100 Subject: [PATCH 04/19] Disable benchmark action failure (#1284) --- .github/workflows/benchmark.yml | 2 +- docs/{ => public}/performance/benchmarks.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename docs/{ => public}/performance/benchmarks.rst (69%) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index cc685faffd..d6b12b82d0 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -67,6 +67,6 @@ jobs: # Show alert with commit comment on detecting possible performance regression alert-threshold: '200%' comment-on-alert: true - fail-on-alert: true + fail-on-alert: false gh-pages-branch: gh-pages benchmark-data-dir-path: benchmarks diff --git a/docs/performance/benchmarks.rst b/docs/public/performance/benchmarks.rst similarity index 69% rename from docs/performance/benchmarks.rst rename to docs/public/performance/benchmarks.rst index 0f1044be42..39b13571dc 100644 --- a/docs/performance/benchmarks.rst +++ b/docs/public/performance/benchmarks.rst @@ -3,4 +3,4 @@ Performance Tests - Benchmarks Click `here `_ to view the latest performance benchmarks for packages in this repo. -Please note that the flutation in the results are mainly because [machines with different CPUs](https://github.com/benchmark-action/github-action-benchmark/issues/79) are used for tests. +Please note that the flutation in the results are mainly because `machines with different CPUs `_ are used for tests. From 3c7b44bf4f7a615364b78d4449eaa5f7ac121249 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Thu, 24 Mar 2022 22:31:41 +0100 Subject: [PATCH 05/19] metrics exemplar round 1 (#1264) --- api/include/opentelemetry/metrics/noop.h | 17 +++- .../opentelemetry/metrics/sync_instruments.h | 90 +++++++++++++++---- api/test/metrics/BUILD | 31 +++++++ api/test/metrics/noop_sync_instrument_test.cc | 13 ++- .../metrics/exemplar/always_sample_filter.h | 43 +++++++++ .../opentelemetry/sdk/metrics/exemplar/data.h | 45 ++++++++++ .../sdk/metrics/exemplar/filter.h | 39 ++++++++ .../metrics/exemplar/never_sample_filter.h | 43 +++++++++ .../metrics/exemplar/no_exemplar_reservoir.h | 55 ++++++++++++ .../sdk/metrics/exemplar/reservoir.h | 55 ++++++++++++ .../sdk/metrics/measurement_processor.h | 42 +++++---- .../sdk/metrics/state/metric_storage.h | 24 +++-- .../sdk/metrics/state/multi_metric_storage.h | 26 +++--- .../sdk/metrics/state/sync_metric_storage.h | 26 ++++-- .../sdk/metrics/sync_instruments.h | 26 +++++- .../sdk/metrics/view/attributes_processor.h | 2 - sdk/src/metrics/meter.cc | 4 +- sdk/src/metrics/sync_instruments.cc | 90 +++++++++++++++---- sdk/test/metrics/BUILD | 16 ++++ sdk/test/metrics/CMakeLists.txt | 2 + sdk/test/metrics/exemplar/BUILD | 47 ++++++++++ sdk/test/metrics/exemplar/CMakeLists.txt | 10 +++ .../exemplar/always_sample_filter_test.cc | 19 ++++ .../exemplar/never_sample_filter_test.cc | 20 +++++ .../exemplar/no_exemplar_reservoir_test.cc | 22 +++++ sdk/test/metrics/multi_metric_storage_test.cc | 27 ++++-- sdk/test/metrics/sync_instruments_test.cc | 50 ++++++++--- sdk/test/metrics/sync_metric_storage_test.cc | 16 ++-- 28 files changed, 787 insertions(+), 113 deletions(-) create mode 100644 api/test/metrics/BUILD create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/data.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h create mode 100644 sdk/test/metrics/exemplar/BUILD create mode 100644 sdk/test/metrics/exemplar/CMakeLists.txt create mode 100644 sdk/test/metrics/exemplar/always_sample_filter_test.cc create mode 100644 sdk/test/metrics/exemplar/never_sample_filter_test.cc create mode 100644 sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 19b9443a7d..3332384ee2 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -24,7 +24,12 @@ class NoopCounter : public Counter nostd::string_view unit) noexcept {} void Add(T value) noexcept override {} + void Add(T value, const opentelemetry::context::Context &context) noexcept override {} void Add(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template @@ -35,8 +40,11 @@ class NoopHistogram : public Histogram nostd::string_view description, nostd::string_view unit) noexcept {} - void Record(T value) noexcept override {} - void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Record(T value, const opentelemetry::context::Context &context) noexcept override {} + void Record(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template @@ -48,7 +56,12 @@ class NoopUpDownCounter : public UpDownCounter nostd::string_view unit) noexcept {} void Add(T value) noexcept override {} + void Add(T value, const opentelemetry::context::Context &context) noexcept override {} void Add(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 35cb8621ab..e8239743a1 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -6,6 +6,7 @@ # include "opentelemetry/common/attribute_value.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/nostd/string_view.h" # include "opentelemetry/nostd/type_traits.h" @@ -29,6 +30,8 @@ class Counter : public SynchronousInstrument */ virtual void Add(T value) noexcept = 0; + virtual void Add(T value, const opentelemetry::context::Context &context) noexcept = 0; + /** * Add adds the value to the counter's sum. The attributes should contain * the keys and values to be associated with this value. Counters only @@ -40,19 +43,44 @@ class Counter : public SynchronousInstrument virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + template ::value> * = nullptr> void Add(T value, const U &attributes) noexcept { - this->Add(value, common::KeyValueIterableView{attributes}); + auto context = opentelemetry::context::Context{}; + this->Add(value, common::KeyValueIterableView{attributes}, context); + } + + template ::value> * = nullptr> + void Add(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept + { + this->Add(value, common::KeyValueIterableView{attributes}, context); } void Add(T value, std::initializer_list> attributes) noexcept { - this->Add(value, nostd::span>{ - attributes.begin(), attributes.end()}); + auto context = opentelemetry::context::Context{}; + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); + } + + void Add(T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept + { + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; @@ -67,7 +95,7 @@ class Histogram : public SynchronousInstrument * * @param value The increment amount. May be positive, negative or zero. */ - virtual void Record(T value) noexcept = 0; + virtual void Record(T value, const opentelemetry::context::Context &context) noexcept = 0; /** * Records a value with a set of attributes. @@ -75,21 +103,26 @@ class Histogram : public SynchronousInstrument * @param value The increment amount. May be positive, negative or zero. * @param attributes A set of attributes to associate with the count. */ - virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Record(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; template ::value> * = nullptr> - void Record(T value, const U &attributes) noexcept + void Record(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept { - this->Record(value, common::KeyValueIterableView{attributes}); + this->Record(value, common::KeyValueIterableView{attributes}, context); } - void Record(T value, - std::initializer_list> - attributes) noexcept + void Record( + T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept { - this->Record(value, nostd::span>{ - attributes.begin(), attributes.end()}); + this->Record(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; @@ -106,6 +139,8 @@ class UpDownCounter : public SynchronousInstrument */ virtual void Add(T value) noexcept = 0; + virtual void Add(T value, const opentelemetry::context::Context &context) noexcept = 0; + /** * Add a value with a set of attributes. * @@ -114,19 +149,44 @@ class UpDownCounter : public SynchronousInstrument */ virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + template ::value> * = nullptr> void Add(T value, const U &attributes) noexcept { - this->Add(value, common::KeyValueIterableView{attributes}); + auto context = opentelemetry::context::Context{}; + this->Add(value, common::KeyValueIterableView{attributes}, context); + } + + template ::value> * = nullptr> + void Add(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept + { + this->Add(value, common::KeyValueIterableView{attributes}, context); } void Add(T value, std::initializer_list> attributes) noexcept { - this->Add(value, nostd::span>{ - attributes.begin(), attributes.end()}); + auto context = opentelemetry::context::Context{}; + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); + } + + void Add(T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept + { + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD new file mode 100644 index 0000000000..d15d7511f4 --- /dev/null +++ b/api/test/metrics/BUILD @@ -0,0 +1,31 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + +cc_test( + name = "noop_sync_instrument_test", + srcs = [ + "noop_sync_instrument_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "meter_provider_test", + srcs = [ + "meter_provider_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/api/test/metrics/noop_sync_instrument_test.cc b/api/test/metrics/noop_sync_instrument_test.cc index 4597e79e8b..62be4f34ab 100644 --- a/api/test/metrics/noop_sync_instrument_test.cc +++ b/api/test/metrics/noop_sync_instrument_test.cc @@ -14,8 +14,11 @@ TEST(Counter, Add) std::map labels = {{"k1", "v1"}}; EXPECT_NO_THROW(counter->Add(10l, labels)); + EXPECT_NO_THROW(counter->Add(10l, labels, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(2l)); + EXPECT_NO_THROW(counter->Add(2l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } TEST(histogram, Record) @@ -24,9 +27,10 @@ TEST(histogram, Record) new opentelemetry::metrics::NoopHistogram("test", "none", "unitless")}; std::map labels = {{"k1", "v1"}}; - EXPECT_NO_THROW(counter->Record(10l, labels)); - EXPECT_NO_THROW(counter->Record(2l)); - EXPECT_NO_THROW(counter->Record(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Record(10l, labels, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter->Record(2l, opentelemetry::context::Context{})); + EXPECT_NO_THROW( + counter->Record(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } TEST(UpDownCountr, Record) @@ -36,8 +40,11 @@ TEST(UpDownCountr, Record) std::map labels = {{"k1", "v1"}}; EXPECT_NO_THROW(counter->Add(10l, labels)); + EXPECT_NO_THROW(counter->Add(10l, labels, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(2l)); + EXPECT_NO_THROW(counter->Add(2l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } #endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h new file mode 100644 index 0000000000..5e7f0436eb --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/filter.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class AlwaysSampleFilter final : public ExemplarFilter +{ +public: + static nostd::shared_ptr GetAlwaysSampleFilter() + { + static nostd::shared_ptr alwaysSampleFilter{new AlwaysSampleFilter{}}; + return alwaysSampleFilter; + } + + bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return true; + } + + bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return true; + } + +private: + explicit AlwaysSampleFilter() = default; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h new file mode 100644 index 0000000000..14eac62499 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/context/context.h" +# include "opentelemetry/sdk/common/attribute_utils.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap; +/** + * A sample input measurement. + * + * Exemplars also hold information about the environment when the measurement was recorded, for + * example the span and trace ID of the active span when the exemplar was recorded. + */ +class ExemplarData +{ +public: + /** + * The set of key/value pairs that were filtered out by the aggregator, but recorded alongside the + * original measurement. Only key/value pairs that were filtered out by the aggregator should be + * included + */ + MetricAttributes GetFilteredAttributes(); + + /** Returns the timestamp in nanos when measurement was collected. */ + opentelemetry::common::SystemTimestamp GetEpochNanos(); + + /** + * Returns the SpanContext associated with this exemplar. If the exemplar was not recorded + * inside a sampled trace, the Context will be invalid. + */ + opentelemetry::context::Context GetSpanContext(); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h new file mode 100644 index 0000000000..4b512e1317 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/context/context.h" +# include "opentelemetry/sdk/common/attribute_utils.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap; + +/** + * Exemplar filters are used to pre-filter measurements before attempting to store them in a + * reservoir. + */ +class ExemplarFilter +{ +public: + // Returns whether or not a reservoir should attempt to filter a measurement. + virtual bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + + // Returns whether or not a reservoir should attempt to filter a measurement. + virtual bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + + virtual ~ExemplarFilter() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h new file mode 100644 index 0000000000..38f51778ce --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/filter.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class NeverSampleFilter final : public ExemplarFilter +{ +public: + static nostd::shared_ptr GetNeverSampleFilter() + { + nostd::shared_ptr neverSampleFilter{new NeverSampleFilter{}}; + return neverSampleFilter; + } + + bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return false; + } + + bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return false; + } + +private: + explicit NeverSampleFilter() = default; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h new file mode 100644 index 0000000000..1fe0586d28 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h @@ -0,0 +1,55 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/context/context.h" +# include "opentelemetry/nostd/shared_ptr.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/metrics/exemplar/reservoir.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class NoExemplarReservoir final : public ExemplarReservoir +{ + +public: + static nostd::shared_ptr GetNoExemplarReservoir() + { + return nostd::shared_ptr{new NoExemplarReservoir{}}; + } + + void OfferMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept override + { + // Stores nothing + } + + void OfferMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept override + { + // Stores nothing. + } + + std::vector CollectAndReset( + const MetricAttributes &pointAttributes) noexcept override + { + return std::vector{}; + } + +private: + explicit NoExemplarReservoir() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h new file mode 100644 index 0000000000..25e8421d6b --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h @@ -0,0 +1,55 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/sdk/metrics/exemplar/data.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * An interface for an exemplar reservoir of samples. + * + *

This represents a reservoir for a specific "point" of metric data. + */ +class ExemplarReservoir +{ +public: + virtual ~ExemplarReservoir() = default; + + /** Offers a long measurement to be sampled. */ + virtual void OfferMeasurement( + long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept = 0; + + /** Offers a double measurement to be sampled. */ + virtual void OfferMeasurement( + double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept = 0; + + /** + * Builds vector of Exemplars for exporting from the current reservoir. + * + *

Additionally, clears the reservoir for the next sampling period. + * + * @param pointAttributes the Attributes associated with the metric point. + * ExemplarDatas should filter these out of their final data state. + * @return A vector of sampled exemplars for this point. Implementers are expected to + * filter out pointAttributes from the original recorded attributes. + */ + virtual std::vector CollectAndReset( + const MetricAttributes &pointAttributes) noexcept = 0; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h index ffd47c5c92..f3b998451b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h @@ -2,10 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include #ifndef ENABLE_METRICS_PREVIEW # include # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/metric_reader.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" @@ -25,15 +27,18 @@ static std::size_t MakeKey(const MetricReader &metric_reader) class MeasurementProcessor { public: - virtual void RecordLong(long value) noexcept = 0; + virtual void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; - virtual void RecordDouble(double value) noexcept = 0; + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; virtual bool Collect(MetricReader &reader, AggregationTemporarily aggregation_temporarily, @@ -50,43 +55,46 @@ class DefaultMeasurementProcessor : public MeasurementProcessor InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; metric_storages_[MakeKey(reader)] = std::unique_ptr( - new SyncMetricStorage(instr_desc, AggregationType::kSum, new DefaultAttributesProcessor())); + new SyncMetricStorage(instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir())); return true; } - virtual void RecordLong(long value) noexcept override + virtual void RecordLong(long value, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordLong(value); + kv.second->RecordLong(value, context); } } - virtual void RecordLong( - long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordLong(value, attributes); + kv.second->RecordLong(value, attributes, context); } } - virtual void RecordDouble(double value) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordDouble(value); + kv.second->RecordDouble(value, context); } } - virtual void RecordDouble( - double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordDouble(value, attributes); + kv.second->RecordDouble(value, attributes, context); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index 8ae50554f1..20f7d56140 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -5,6 +5,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/common/timestamp.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/sdk/metrics/data/metric_data.h" # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -30,15 +31,19 @@ class MetricStorage class WritableMetricStorage { public: - virtual void RecordLong(long value) noexcept = 0; + virtual void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; - virtual void RecordDouble(double value) noexcept = 0; + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + virtual ~WritableMetricStorage() = default; }; @@ -63,16 +68,19 @@ class NoopMetricStorage : public MetricStorage class NoopWritableMetricStorage : public WritableMetricStorage { public: - void RecordLong(long value) noexcept = 0; + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override {} - void RecordDouble(double value) noexcept override {} + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override + {} void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override {} }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h index d9a66f732a..ceeafa0406 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h @@ -20,39 +20,41 @@ class MultiMetricStorage : public WritableMetricStorage public: void AddStorage(std::shared_ptr storage) { storages_.push_back(storage); } - virtual void RecordLong(long value) noexcept override + virtual void RecordLong(long value, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordLong(value); + s->RecordLong(value, context); } } - virtual void RecordLong( - long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordLong(value, attributes); + s->RecordLong(value, attributes, context); } } - virtual void RecordDouble(double value) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordDouble(value); + s->RecordDouble(value, context); } } - virtual void RecordDouble( - double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordDouble(value, attributes); + s->RecordDouble(value, attributes, context); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h index f8c2a86aa4..bfe50b152d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -7,6 +7,7 @@ # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/exemplar/reservoir.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include "opentelemetry/sdk/metrics/state/metric_storage.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -27,11 +28,13 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage public: SyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, - const AttributesProcessor *attributes_processor) + const AttributesProcessor *attributes_processor, + nostd::shared_ptr &&exemplar_reservoir) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), - attributes_processor_{attributes_processor} + attributes_processor_{attributes_processor}, + exemplar_reservoir_(exemplar_reservoir) { create_default_aggregation_ = [&]() -> std::unique_ptr { return std::move( @@ -39,45 +42,55 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage }; } - void RecordLong(long value) noexcept override + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong) { return; } + exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong) { return; } + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); auto attr = attributes_processor_->process(attributes); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); } - void RecordDouble(double value) noexcept override + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble) { return; } + exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble) { return; } + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); auto attr = attributes_processor_->process(attributes); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); } @@ -102,6 +115,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage std::unique_ptr attributes_hashmap_; const AttributesProcessor *attributes_processor_; std::function()> create_default_aggregation_; + nostd::shared_ptr exemplar_reservoir_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index 3e8c5c8623..f4c9bae323 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -39,8 +39,12 @@ class LongCounter : public Synchronous, public opentelemetry::metrics::Counter storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(long value) noexcept override; + void Add(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter @@ -52,8 +56,12 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(double value) noexcept override; + void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter @@ -63,8 +71,12 @@ class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpD std::unique_ptr storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(long value) noexcept override; + void Add(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter @@ -75,8 +87,12 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(double value) noexcept override; + void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram @@ -86,9 +102,10 @@ class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogr std::unique_ptr storage); void Record(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; - void Record(long value) noexcept override; + void Record(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram @@ -98,9 +115,10 @@ class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histo std::unique_ptr storage); void Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; - void Record(double value) noexcept override; + void Record(double value, const opentelemetry::context::Context &context) noexcept override; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index d82607357f..fdc4e35c53 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -23,8 +23,6 @@ class AttributesProcessor // @returns The processed attributes virtual MetricAttributes process( const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0; - - virtual ~AttributesProcessor() = default; }; /** diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 360630e786..20e3b02768 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -6,6 +6,7 @@ # include "opentelemetry/metrics/noop.h" # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/async_instruments.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/sdk/metrics/sync_instruments.h" @@ -192,7 +193,8 @@ std::unique_ptr Meter::RegisterMetricStorage( view_instr_desc.name_ = view.GetName(); view_instr_desc.description_ = view.GetDescription(); auto storage = std::shared_ptr(new SyncMetricStorage( - view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor())); + view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir())); storage_registry_[instrument_descriptor.name_] = storage; auto multi_storage = static_cast(storages.get()); multi_storage->AddStorage(storage); diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 863e5b6323..5c94ae6f24 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -18,12 +18,26 @@ LongCounter::LongCounter(InstrumentDescriptor instrument_descriptor, void LongCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordLong(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, attributes, context); +} + +void LongCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, attributes, context); } void LongCounter::Add(long value) noexcept { - return storage_->RecordLong(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, context); +} + +void LongCounter::Add(long value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, context); } DoubleCounter::DoubleCounter(InstrumentDescriptor instrument_descriptor, @@ -34,12 +48,26 @@ DoubleCounter::DoubleCounter(InstrumentDescriptor instrument_descriptor, void DoubleCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordDouble(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, attributes, context); } void DoubleCounter::Add(double value) noexcept { - return storage_->RecordDouble(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, context); +} + +void DoubleCounter::Add(double value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, context); } LongUpDownCounter::LongUpDownCounter(InstrumentDescriptor instrument_descriptor, @@ -50,12 +78,26 @@ LongUpDownCounter::LongUpDownCounter(InstrumentDescriptor instrument_descriptor, void LongUpDownCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordLong(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, attributes, context); +} + +void LongUpDownCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, attributes, context); } void LongUpDownCounter::Add(long value) noexcept { - return storage_->RecordLong(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, context); +} + +void LongUpDownCounter::Add(long value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, context); } DoubleUpDownCounter::DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor, @@ -66,12 +108,26 @@ DoubleUpDownCounter::DoubleUpDownCounter(InstrumentDescriptor instrument_descrip void DoubleUpDownCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordDouble(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleUpDownCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, attributes, context); } void DoubleUpDownCounter::Add(double value) noexcept { - return storage_->RecordDouble(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, context); +} + +void DoubleUpDownCounter::Add(double value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, context); } LongHistogram::LongHistogram(InstrumentDescriptor instrument_descriptor, @@ -80,14 +136,15 @@ LongHistogram::LongHistogram(InstrumentDescriptor instrument_descriptor, {} void LongHistogram::Record(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { - return storage_->RecordLong(value, attributes); + return storage_->RecordLong(value, attributes, context); } -void LongHistogram::Record(long value) noexcept +void LongHistogram::Record(long value, const opentelemetry::context::Context &context) noexcept { - return storage_->RecordLong(value); + return storage_->RecordLong(value, context); } DoubleHistogram::DoubleHistogram(InstrumentDescriptor instrument_descriptor, @@ -96,14 +153,15 @@ DoubleHistogram::DoubleHistogram(InstrumentDescriptor instrument_descriptor, {} void DoubleHistogram::Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { - return storage_->RecordDouble(value, attributes); + return storage_->RecordDouble(value, attributes, context); } -void DoubleHistogram::Record(double value) noexcept +void DoubleHistogram::Record(double value, const opentelemetry::context::Context &context) noexcept { - return storage_->RecordDouble(value); + return storage_->RecordDouble(value, context); } } // namespace metrics diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD index 9ca191574f..819a8d225f 100644 --- a/sdk/test/metrics/BUILD +++ b/sdk/test/metrics/BUILD @@ -80,6 +80,22 @@ cc_test( ], ) +cc_test( + name = "sync_instruments_test", + srcs = [ + "sync_instruments_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//sdk/src/metrics", + "//sdk/src/resource", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "async_metric_storage_test", srcs = [ diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 3ee2be6552..fa1f22c73a 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -29,3 +29,5 @@ target_link_libraries(attributes_processor_benchmark benchmark::benchmark add_executable(attributes_hashmap_benchmark attributes_hashmap_benchmark.cc) target_link_libraries(attributes_hashmap_benchmark benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) + +add_subdirectory(exemplar) diff --git a/sdk/test/metrics/exemplar/BUILD b/sdk/test/metrics/exemplar/BUILD new file mode 100644 index 0000000000..6481f679d2 --- /dev/null +++ b/sdk/test/metrics/exemplar/BUILD @@ -0,0 +1,47 @@ +cc_test( + name = "no_exemplar_reservoir_test", + srcs = [ + "no_exemplar_reservoir_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "never_sample_filter_test", + srcs = [ + "never_sample_filter_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "always_sample_filter_test", + srcs = [ + "always_sample_filter_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/sdk/test/metrics/exemplar/CMakeLists.txt b/sdk/test/metrics/exemplar/CMakeLists.txt new file mode 100644 index 0000000000..303294761a --- /dev/null +++ b/sdk/test/metrics/exemplar/CMakeLists.txt @@ -0,0 +1,10 @@ +foreach(testname no_exemplar_reservoir_test never_sample_filter_test + always_sample_filter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) + gtest_add_tests( + TARGET ${testname} + TEST_PREFIX metrics. + TEST_LIST ${testname}) +endforeach() diff --git a/sdk/test/metrics/exemplar/always_sample_filter_test.cc b/sdk/test/metrics/exemplar/always_sample_filter_test.cc new file mode 100644 index 0000000000..cf4e449957 --- /dev/null +++ b/sdk/test/metrics/exemplar/always_sample_filter_test.cc @@ -0,0 +1,19 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/always_sample_filter.h" +# include + +using namespace opentelemetry::sdk::metrics; + +TEST(AlwaysSampleFilter, SampleMeasurement) +{ + auto filter = opentelemetry::sdk::metrics::AlwaysSampleFilter::GetAlwaysSampleFilter(); + ASSERT_TRUE( + filter->ShouldSampleMeasurement(1.0, MetricAttributes{}, opentelemetry::context::Context{})); + ASSERT_TRUE( + filter->ShouldSampleMeasurement(1l, MetricAttributes{}, opentelemetry::context::Context{})); +} + +#endif diff --git a/sdk/test/metrics/exemplar/never_sample_filter_test.cc b/sdk/test/metrics/exemplar/never_sample_filter_test.cc new file mode 100644 index 0000000000..930c572205 --- /dev/null +++ b/sdk/test/metrics/exemplar/never_sample_filter_test.cc @@ -0,0 +1,20 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/context/context.h" +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/sdk/metrics/exemplar/never_sample_filter.h" + +using namespace opentelemetry::sdk::metrics; + +TEST(NeverSampleFilter, SampleMeasurement) +{ + auto filter = opentelemetry::sdk::metrics::NeverSampleFilter::GetNeverSampleFilter(); + ASSERT_FALSE( + filter->ShouldSampleMeasurement(1.0, MetricAttributes{}, opentelemetry::context::Context{})); + ASSERT_FALSE( + filter->ShouldSampleMeasurement(1l, MetricAttributes{}, opentelemetry::context::Context{})); +} + +#endif diff --git a/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc b/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc new file mode 100644 index 0000000000..3e16940ff4 --- /dev/null +++ b/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" +# include + +using namespace opentelemetry::sdk::metrics; + +TEST(NoExemplarReservoir, OfferMeasurement) +{ + auto reservoir = opentelemetry::sdk::metrics::NoExemplarReservoir::GetNoExemplarReservoir(); + EXPECT_NO_THROW(reservoir->OfferMeasurement(1.0, MetricAttributes{}, + opentelemetry::context::Context{}, + std::chrono::system_clock::now())); + EXPECT_NO_THROW(reservoir->OfferMeasurement( + 1l, MetricAttributes{}, opentelemetry::context::Context{}, std::chrono::system_clock::now())); + auto exemplar_data = reservoir->CollectAndReset(MetricAttributes{}); + ASSERT_TRUE(exemplar_data.empty()); +} + +#endif diff --git a/sdk/test/metrics/multi_metric_storage_test.cc b/sdk/test/metrics/multi_metric_storage_test.cc index 6696c7bb0a..d88946485c 100644 --- a/sdk/test/metrics/multi_metric_storage_test.cc +++ b/sdk/test/metrics/multi_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include @@ -15,18 +16,26 @@ using namespace opentelemetry::sdk::metrics; class TestMetricStorage : public WritableMetricStorage { public: - void RecordLong(long value) noexcept override { num_calls_long++; } + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept override + { + num_calls_long++; + } void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { num_calls_long++; } - void RecordDouble(double value) noexcept override { num_calls_double++; } + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override + { + num_calls_double++; + } void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { num_calls_double++; } @@ -39,13 +48,13 @@ TEST(MultiMetricStorageTest, BasicTests) { std::shared_ptr storage( new TestMetricStorage()); - MultiMetricStorage storages; + MultiMetricStorage storages{}; storages.AddStorage(storage); - EXPECT_NO_THROW(storages.RecordLong(10l)); - EXPECT_NO_THROW(storages.RecordLong(20l)); + EXPECT_NO_THROW(storages.RecordLong(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storages.RecordLong(20l, opentelemetry::context::Context{})); - EXPECT_NO_THROW(storages.RecordDouble(10.0)); - EXPECT_NO_THROW(storages.RecordLong(30l)); + EXPECT_NO_THROW(storages.RecordDouble(10.0, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storages.RecordLong(30l, opentelemetry::context::Context{})); EXPECT_EQ(static_cast(storage.get())->num_calls_long, 3); EXPECT_EQ(static_cast(storage.get())->num_calls_double, 1); diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 27af8711d1..e029821d41 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -3,7 +3,9 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/sync_instruments.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include @@ -23,11 +25,16 @@ TEST(SyncInstruments, LongCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); LongCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); - EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleCounter) @@ -37,11 +44,16 @@ TEST(SyncInstruments, DoubleCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); - EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, LongUpDownCounter) @@ -52,11 +64,16 @@ TEST(SyncInstruments, LongUpDownCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); LongUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); - EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleUpDownCounter) @@ -67,10 +84,15 @@ TEST(SyncInstruments, DoubleUpDownCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); - EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); } @@ -80,12 +102,14 @@ TEST(SyncInstruments, LongHistogram) "long_histogram", "description", "1", InstrumentType::kHistogram, InstrumentValueType::kLong}; std::unique_ptr metric_storage(new MultiMetricStorage()); LongHistogram counter(instrument_descriptor, std::move(metric_storage)); - EXPECT_NO_THROW(counter.Record(10l)); - EXPECT_NO_THROW(counter.Record(10l)); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Record( - 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); - EXPECT_NO_THROW(counter.Record(10l, opentelemetry::common::KeyValueIterableView({}))); + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleHistogram) @@ -95,12 +119,14 @@ TEST(SyncInstruments, DoubleHistogram) InstrumentValueType::kDouble}; std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleHistogram counter(instrument_descriptor, std::move(metric_storage)); - EXPECT_NO_THROW(counter.Record(10.10)); - EXPECT_NO_THROW(counter.Record(10.10)); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Record( - 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); - EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::common::KeyValueIterableView({}))); + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } #endif \ No newline at end of file diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 23ef20b11d..c4bd154460 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -18,13 +19,16 @@ TEST(WritableMetricStorageTest, BasicTests) InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; - opentelemetry::sdk::metrics::SyncMetricStorage storage(instr_desc, AggregationType::kSum, - new DefaultAttributesProcessor()); - EXPECT_NO_THROW(storage.RecordLong(10l)); - EXPECT_NO_THROW(storage.RecordDouble(10.10)); + opentelemetry::sdk::metrics::SyncMetricStorage storage( + instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir()); + EXPECT_NO_THROW(storage.RecordLong(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(storage.RecordLong( - 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); - EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } #endif From 2c9ce393e0582a4e0559a73bd9a572cf324b7086 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 24 Mar 2022 16:47:49 -0700 Subject: [PATCH 06/19] [Metrics SDK] - fix spelling (AggregationTemporarily to AggregationTemporality) (#1288) --- exporters/ostream/test/ostream_metric_test.cc | 4 +- .../sdk/metrics/aggregation/sum_aggregation.h | 2 +- .../sdk/metrics/data/point_data.h | 2 +- .../opentelemetry/sdk/metrics/instruments.h | 2 +- .../sdk/metrics/measurement_processor.h | 124 ------------------ .../sdk/metrics/metric_exporter.h | 2 +- .../opentelemetry/sdk/metrics/metric_reader.h | 6 +- .../sdk/metrics/state/metric_collector.h | 4 +- sdk/src/metrics/metric_reader.cc | 8 +- sdk/src/metrics/state/metric_collector.cc | 4 +- sdk/test/metrics/async_metric_storage_test.cc | 4 +- sdk/test/metrics/metric_reader_test.cc | 10 +- 12 files changed, 24 insertions(+), 148 deletions(-) delete mode 100644 sdk/include/opentelemetry/sdk/metrics/measurement_processor.h diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index 51b9882c47..9e98dd4da4 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -35,11 +35,11 @@ TEST(OStreamMetricsExporter, ExportSumPointData) record->instrumentation_library_ = instrumentation_library.get(); record->point_data_ = metric_sdk::SumPointData{ opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, 10.0, - metric_sdk::AggregationTemporarily::kUnspecified, false}; + metric_sdk::AggregationTemporality::kUnspecified, false}; auto record2 = std::unique_ptr(new metric_sdk::MetricData(*record)); record2->point_data_ = metric_sdk::SumPointData{ opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, 20l, - metric_sdk::AggregationTemporarily::kUnspecified, false}; + metric_sdk::AggregationTemporality::kUnspecified, false}; std::vector> records; records.push_back(std::move(record)); records.push_back(std::move(record2)); diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h index c8b2eff33c..ff99cec733 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h @@ -25,7 +25,7 @@ static inline void PopulateSumPointData(SumPointData &sum, sum.end_epoch_nanos_ = end_ts; sum.value_ = value; sum.is_monotonic_ = is_monotonic; - sum.aggregation_temporarily_ = AggregationTemporarily::kDelta; + sum.aggregation_temporality_ = AggregationTemporality::kDelta; } class LongSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation diff --git a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h index 603bcc30bc..5bbcbd0b1e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -25,7 +25,7 @@ class SumPointData opentelemetry::common::SystemTimestamp start_epoch_nanos_; opentelemetry::common::SystemTimestamp end_epoch_nanos_; ValueType value_; - AggregationTemporarily aggregation_temporarily_; + AggregationTemporality aggregation_temporality_; bool is_monotonic_; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index ad64ce718b..15e6a25705 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -36,7 +36,7 @@ enum class AggregationType kDefault }; -enum class AggregationTemporarily +enum class AggregationTemporality { kUnspecified, kDelta, diff --git a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h deleted file mode 100644 index f3b998451b..0000000000 --- a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once -#include -#ifndef ENABLE_METRICS_PREVIEW - -# include -# include "opentelemetry/common/key_value_iterable_view.h" -# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" -# include "opentelemetry/sdk/metrics/instruments.h" -# include "opentelemetry/sdk/metrics/metric_reader.h" -# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" -# include "opentelemetry/sdk/metrics/view/attributes_processor.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk -{ -namespace metrics -{ - -static std::size_t MakeKey(const MetricReader &metric_reader) -{ - return reinterpret_cast(&metric_reader); -} - -class MeasurementProcessor -{ -public: - virtual void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; - - virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept = 0; - - virtual void RecordDouble(double value, - const opentelemetry::context::Context &context) noexcept = 0; - - virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept = 0; - - virtual bool Collect(MetricReader &reader, - AggregationTemporarily aggregation_temporarily, - nostd::function_ref callback) noexcept = 0; -}; - -class DefaultMeasurementProcessor : public MeasurementProcessor -{ -public: - bool AddMetricStorage(const MetricReader &reader) - { - // TBD Check if already present - // pass intrumentation type, and aggregation type instead of hardcodig below. - InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, - InstrumentValueType::kLong}; - metric_storages_[MakeKey(reader)] = std::unique_ptr( - new SyncMetricStorage(instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), - NoExemplarReservoir::GetNoExemplarReservoir())); - return true; - } - - virtual void RecordLong(long value, - const opentelemetry::context::Context &context) noexcept override - { - for (const auto &kv : metric_storages_) - { - kv.second->RecordLong(value, context); - } - } - - virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept override - { - for (const auto &kv : metric_storages_) - { - kv.second->RecordLong(value, attributes, context); - } - } - - virtual void RecordDouble(double value, - const opentelemetry::context::Context &context) noexcept override - { - for (const auto &kv : metric_storages_) - { - kv.second->RecordDouble(value, context); - } - } - - virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept override - { - for (const auto &kv : metric_storages_) - { - kv.second->RecordDouble(value, attributes, context); - } - } - - bool Collect(MetricReader &reader, - AggregationTemporarily aggregation_temporarily, - nostd::function_ref callback) noexcept override - { - auto i = metric_storages_.find(MakeKey(reader)); - - // TBD - Remove hardcodings below - std::vector collectors; - if (i != metric_storages_.end()) - { - - return i->second->Collect(nullptr, collectors, nullptr, nullptr, callback); - } - return false; - } - -private: - std::map> metric_storages_; -}; - -} // namespace metrics -} // namespace sdk -OPENTELEMETRY_END_NAMESPACE -#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h index 96898c9ad1..3769259472 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h @@ -51,7 +51,7 @@ class MetricExporter std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept = 0; private: - AggregationTemporarily aggregation_temporarily; + AggregationTemporality aggregation_temporality_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index fdd5d41291..1f7cee8d30 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -27,7 +27,7 @@ class MetricReader { public: MetricReader( - AggregationTemporarily aggregation_temporarily = AggregationTemporarily::kCummulative); + AggregationTemporality aggregation_temporality = AggregationTemporality::kCummulative); void SetMetricProducer(MetricProducer *metric_producer); @@ -37,7 +37,7 @@ class MetricReader */ bool Collect(nostd::function_ref callback) noexcept; - AggregationTemporarily GetAggregationTemporarily() const noexcept; + AggregationTemporality GetAggregationTemporality() const noexcept; /** * Shutdown the meter reader. @@ -62,7 +62,7 @@ class MetricReader private: MetricProducer *metric_producer_; - AggregationTemporarily aggregation_temporarily_; + AggregationTemporality aggregation_temporality_; mutable opentelemetry::common::SpinLockMutex lock_; bool shutdown_; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h index 3049440d9d..51c9cf6eff 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h @@ -18,7 +18,7 @@ class MeterContext; class CollectorHandle { public: - virtual AggregationTemporarily GetAggregationTemporarily() noexcept = 0; + virtual AggregationTemporality GetAggregationTemporality() noexcept = 0; }; /** @@ -33,7 +33,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle MetricCollector(std::shared_ptr &&context, std::unique_ptr metric_reader); - AggregationTemporarily GetAggregationTemporarily() noexcept override; + AggregationTemporality GetAggregationTemporality() noexcept override; /** * The callback to be called for each metric exporter. This will only be those diff --git a/sdk/src/metrics/metric_reader.cc b/sdk/src/metrics/metric_reader.cc index 71aedc227f..8238ad2a55 100644 --- a/sdk/src/metrics/metric_reader.cc +++ b/sdk/src/metrics/metric_reader.cc @@ -13,8 +13,8 @@ namespace sdk namespace metrics { -MetricReader::MetricReader(AggregationTemporarily aggregation_temporarily) - : aggregation_temporarily_(aggregation_temporarily) +MetricReader::MetricReader(AggregationTemporality aggregation_temporality) + : aggregation_temporality_(aggregation_temporality) {} void MetricReader::SetMetricProducer(MetricProducer *metric_producer) @@ -22,9 +22,9 @@ void MetricReader::SetMetricProducer(MetricProducer *metric_producer) metric_producer_ = metric_producer; } -AggregationTemporarily MetricReader::GetAggregationTemporarily() const noexcept +AggregationTemporality MetricReader::GetAggregationTemporality() const noexcept { - return aggregation_temporarily_; + return aggregation_temporality_; } bool MetricReader::Collect(nostd::function_ref callback) noexcept diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index a52c6e9b1c..5b9fc4ab70 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -24,9 +24,9 @@ MetricCollector::MetricCollector( metric_reader_->SetMetricProducer(this); } -AggregationTemporarily MetricCollector::GetAggregationTemporarily() noexcept +AggregationTemporality MetricCollector::GetAggregationTemporality() noexcept { - return metric_reader_->GetAggregationTemporarily(); + return metric_reader_->GetAggregationTemporality(); } bool MetricCollector::Collect(nostd::function_ref callback) noexcept diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index fd7d24c6f3..0527c7f0ca 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -21,7 +21,7 @@ using namespace opentelemetry::sdk::resource; class MockMetricReader : public MetricReader { public: - MockMetricReader(AggregationTemporarily aggr_temporarily) : MetricReader(aggr_temporarily) {} + MockMetricReader(AggregationTemporality aggr_temporality) : MetricReader(aggr_temporality) {} virtual bool OnForceFlush(std::chrono::microseconds timeout) noexcept override { return true; } @@ -44,7 +44,7 @@ TEST(AsyncMetricStorageTest, BasicTests) std::vector> exporters; std::shared_ptr meter_context(new MeterContext(std::move(exporters))); - std::unique_ptr metric_reader(new MockMetricReader(AggregationTemporarily::kDelta)); + std::unique_ptr metric_reader(new MockMetricReader(AggregationTemporality::kDelta)); std::shared_ptr collector = std::shared_ptr( new MetricCollector(std::move(meter_context), std::move(metric_reader))); diff --git a/sdk/test/metrics/metric_reader_test.cc b/sdk/test/metrics/metric_reader_test.cc index d0f7c14981..214f522118 100644 --- a/sdk/test/metrics/metric_reader_test.cc +++ b/sdk/test/metrics/metric_reader_test.cc @@ -14,7 +14,7 @@ using namespace opentelemetry::sdk::metrics; class MockMetricReader : public MetricReader { public: - MockMetricReader(AggregationTemporarily aggr_temporarily) : MetricReader(aggr_temporarily) {} + MockMetricReader(AggregationTemporality aggr_temporality) : MetricReader(aggr_temporality) {} virtual bool OnForceFlush(std::chrono::microseconds timeout) noexcept override { return true; } @@ -25,15 +25,15 @@ class MockMetricReader : public MetricReader TEST(MetricReaderTest, BasicTests) { - AggregationTemporarily aggr_temporarily = AggregationTemporarily::kDelta; - std::unique_ptr metric_reader1(new MockMetricReader(aggr_temporarily)); - EXPECT_EQ(metric_reader1->GetAggregationTemporarily(), aggr_temporarily); + AggregationTemporality aggr_temporality = AggregationTemporality::kDelta; + std::unique_ptr metric_reader1(new MockMetricReader(aggr_temporality)); + EXPECT_EQ(metric_reader1->GetAggregationTemporality(), aggr_temporality); std::vector> exporters; std::shared_ptr meter_context1(new MeterContext(std::move(exporters))); EXPECT_NO_THROW(meter_context1->AddMetricReader(std::move(metric_reader1))); - std::unique_ptr metric_reader2(new MockMetricReader(aggr_temporarily)); + std::unique_ptr metric_reader2(new MockMetricReader(aggr_temporality)); std::shared_ptr meter_context2(new MeterContext(std::move(exporters))); MetricProducer *metric_producer = new MetricCollector(std::move(meter_context2), std::move(metric_reader2)); From 91b05720ef38bcd065e1ebb8c9c857764d8d0e3b Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Fri, 25 Mar 2022 17:40:21 +0100 Subject: [PATCH 07/19] fix compilation error with protobuf 3.5 (#1289) --- docker/Dockerfile.centos | 14 ++++++++------ .../exporters/otlp/otlp_environment.h | 2 +- exporters/otlp/src/otlp_http_client.cc | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/docker/Dockerfile.centos b/docker/Dockerfile.centos index 66ca8f24b3..66138ba60a 100644 --- a/docker/Dockerfile.centos +++ b/docker/Dockerfile.centos @@ -1,13 +1,15 @@ FROM centos:7 +ARG TOOLSET_VER=11 + RUN yum update -y && yum install -y centos-release-scl epel-release -RUN yum install -y devtoolset-11 \ +RUN yum install -y devtoolset-${TOOLSET_VER} \ cmake3 git \ openssl-devel \ libcurl-devel \ - && source /opt/rh/devtoolset-11/enable + && source /opt/rh/devtoolset-${TOOLSET_VER}/enable -RUN echo "source /opt/rh/devtoolset-11/enable" >> /etc/bashrc +RUN echo "source /opt/rh/devtoolset-${TOOLSET_VER}/enable" >> /etc/bashrc RUN echo "BOOST_LIBRARYDIR=/usr/lib64/boost169" >> /etc/bashrc RUN echo "BOOST_INCLUDEDIR=/usr/include/boost169" >> /etc/bashrc @@ -17,7 +19,7 @@ ARG GRPC_VERSION=v1.43.2 RUN git clone --depth=1 -b $GRPC_VERSION https://github.com/grpc/grpc.git \ && cd grpc && git submodule update --init \ && mkdir -p "third_party/abseil-cpp/build" && cd "third_party/abseil-cpp/build" \ - && source /opt/rh/devtoolset-11/enable \ + && source /opt/rh/devtoolset-${TOOLSET_VER}/enable \ && cmake3 -DCMAKE_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE .. \ && make -j${nproc} install && cd ../../.. \ && mkdir build && cd build \ @@ -40,7 +42,7 @@ RUN yum install -y \ && wget https://github.com/apache/thrift/archive/refs/tags/v$THRIFT_VERSION.tar.gz \ && tar -xvf v$THRIFT_VERSION.tar.gz \ && mkdir -p thrift-$THRIFT_VERSION/build && cd thrift-$THRIFT_VERSION/build \ - && source /opt/rh/devtoolset-11/enable \ + && source /opt/rh/devtoolset-${TOOLSET_VER}/enable \ && export BOOST_INCLUDEDIR=/usr/include/boost169 \ && export BOOST_LIBRARYDIR=/usr/lib64/boost169 \ && cmake3 \ @@ -66,7 +68,7 @@ RUN yum install -y \ RUN git clone --depth=1 https://github.com/open-telemetry/opentelemetry-cpp.git \ && cd opentelemetry-cpp && git submodule update --init \ && mkdir -p build && cd build \ - && source /opt/rh/devtoolset-11/enable \ + && source /opt/rh/devtoolset-${TOOLSET_VER}/enable \ && export BOOST_INCLUDEDIR=/usr/include/boost169 \ && export BOOST_LIBRARYDIR=/usr/lib64/boost169 \ && cmake3 \ diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h index c60de87eb9..bf7ea6a61c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h @@ -53,7 +53,7 @@ inline const std::string GetOtlpDefaultHttpEndpoint() return endpoint.size() ? endpoint : kOtlpEndpointDefault; } -inline const bool GetOtlpDefaultIsSslEnable() +inline bool GetOtlpDefaultIsSslEnable() { constexpr char kOtlpTracesIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE"; constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE"; diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 544f74ca7c..e3e1a8c467 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -18,6 +18,7 @@ #include "google/protobuf/message.h" #include "google/protobuf/reflection.h" #include "google/protobuf/stubs/common.h" +#include "google/protobuf/stubs/stringpiece.h" #include "nlohmann/json.hpp" #if defined(GOOGLE_PROTOBUF_VERSION) && GOOGLE_PROTOBUF_VERSION >= 3007000 From c1b959079bd6ae03183f54f246017aa1be5c706b Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 26 Mar 2022 09:25:42 +0100 Subject: [PATCH 08/19] Fix span SetAttribute crash (#1283) --- CHANGELOG.md | 1 + sdk/src/trace/span.cc | 4 ++++ sdk/test/trace/tracer_test.cc | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f66ec617a..bd41c75626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Increment the: ## [Unreleased] +* [SDK] Bugfix: span SetAttribute crash ([#1283](https://github.com/open-telemetry/opentelemetry-cpp/pull/1283)) * [EXPORTER] Jaeger Exporter - Populate Span Links ([#1251](https://github.com/open-telemetry/opentelemetry-cpp/pull/1251)) ## [1.2.0] 2022-01-31 diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index dff84a22b7..13ea99126c 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -94,6 +94,10 @@ Span::~Span() void Span::SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept { std::lock_guard lock_guard{mu_}; + if (recordable_ == nullptr) + { + return; + } recordable_->SetAttribute(key, value); } diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 9d1029cd02..dc806b079f 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -383,6 +383,34 @@ TEST(Tracer, SpanSetAttribute) ASSERT_EQ(3.1, nostd::get(cur_span_data->GetAttributes().at("abc"))); } +TEST(Tracer, TestAfterEnd) +{ + std::unique_ptr exporter(new InMemorySpanExporter()); + std::shared_ptr span_data = exporter->GetData(); + auto tracer = initTracer(std::move(exporter)); + auto span = tracer->StartSpan("span 1"); + span->SetAttribute("abc", 3.1); + + span->End(); + + // test after end + span->SetAttribute("testing null recordable", 3.1); + span->AddEvent("event 1"); + span->AddEvent("event 2", std::chrono::system_clock::now()); + span->AddEvent("event 3", std::chrono::system_clock::now(), {{"attr1", 1}}); + std::string new_name{"new name"}; + span->UpdateName(new_name); + span->SetAttribute("attr1", 3.1); + std::string description{"description"}; + span->SetStatus(opentelemetry::trace::StatusCode::kError, description); + span->End(); + + auto spans = span_data->GetSpans(); + ASSERT_EQ(1, spans.size()); + auto &cur_span_data = spans.at(0); + ASSERT_EQ(3.1, nostd::get(cur_span_data->GetAttributes().at("abc"))); +} + TEST(Tracer, SpanSetEvents) { std::unique_ptr exporter(new InMemorySpanExporter()); From a7e814a632fdaee0c76246358ffad811ac06a7e9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 29 Mar 2022 18:01:37 -0700 Subject: [PATCH 09/19] Synchronous Metric collection (Delta , Cumulative) (#1265) --- exporters/ostream/BUILD | 57 +++-- .../sdk/metrics/aggregation/aggregation.h | 40 +++- .../metrics/aggregation/default_aggregation.h | 8 +- .../metrics/aggregation/drop_aggregation.h | 20 +- .../aggregation/histogram_aggregation.h | 40 ++-- .../aggregation/lastvalue_aggregation.h | 22 +- .../sdk/metrics/aggregation/sum_aggregation.h | 44 ++-- .../sdk/metrics/data/metric_data.h | 15 +- .../sdk/metrics/data/point_data.h | 56 +++-- .../opentelemetry/sdk/metrics/instruments.h | 2 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 2 +- .../opentelemetry/sdk/metrics/meter_context.h | 1 - .../opentelemetry/sdk/metrics/metric_reader.h | 3 +- .../sdk/metrics/state/async_metric_storage.h | 3 +- .../sdk/metrics/state/attributes_hashmap.h | 19 +- .../sdk/metrics/state/sync_metric_storage.h | 28 ++- sdk/src/metrics/CMakeLists.txt | 1 + .../aggregation/histogram_aggregation.cc | 95 +++++--- .../aggregation/lastvalue_aggregation.cc | 103 ++++++-- .../metrics/aggregation/sum_aggregation.cc | 102 +++++--- sdk/src/metrics/meter.cc | 2 +- sdk/src/metrics/state/metric_collector.cc | 2 +- sdk/src/metrics/state/sync_metric_storage.cc | 131 ++++++++++ sdk/test/metrics/aggregation_test.cc | 38 ++- sdk/test/metrics/async_metric_storage_test.cc | 8 +- sdk/test/metrics/meter_provider_sdk_test.cc | 2 - sdk/test/metrics/metric_reader_test.cc | 2 - sdk/test/metrics/sync_metric_storage_test.cc | 226 +++++++++++++++++- 28 files changed, 804 insertions(+), 268 deletions(-) create mode 100644 sdk/src/metrics/state/sync_metric_storage.cc diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index cca74d6693..19c0f7914d 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -43,36 +43,35 @@ cc_library( ], ) -cc_library( - name = "ostream_metric_exporter", - srcs = [ - "src/metric_exporter.cc", - ], - hdrs = [ - "include/opentelemetry/exporters/ostream/metric_exporter.h", - ], - strip_include_prefix = "include", - tags = [ - "metrics", - "ostream", - ], - deps = [ - "//sdk/src/metrics", - ], -) +#TODO MetricData is still changing, uncomment once it is final +# srcs = [ +# "src/metric_exporter.cc", +# ], +# hdrs = [ +# "include/opentelemetry/exporters/ostream/metric_exporter.h", +# ], +# strip_include_prefix = "include", +# tags = [ +# "metrics", +# "ostream", +# ], +# deps = [ +# "//sdk/src/metrics", +# ], +#) -cc_test( - name = "ostream_metric_test", - srcs = ["test/ostream_metric_test.cc"], - tags = [ - "ostream", - "test", - ], - deps = [ - ":ostream_metric_exporter", - "@com_google_googletest//:gtest_main", - ], -) +#cc_test( +# name = "ostream_metric_test", +# srcs = ["test/ostream_metric_test.cc"], +# tags = [ +# "ostream", +# "test", +# ], +# deps = [ +# ":ostream_metric_exporter", +# "@com_google_googletest//:gtest_main", +#], +#) cc_test( name = "ostream_metrics_test_deprecated", diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h index 3bb133ac15..7ec9a6ea2b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h @@ -11,16 +11,6 @@ namespace sdk { namespace metrics { -class InstrumentMonotonicityAwareAggregation -{ -public: - InstrumentMonotonicityAwareAggregation(bool is_monotonic) : is_monotonic_(is_monotonic) {} - bool IsMonotonic() { return is_monotonic_; } - -private: - bool is_monotonic_; -}; - class Aggregation { public: @@ -28,7 +18,35 @@ class Aggregation virtual void Aggregate(double value, const PointAttributes &attributes = {}) noexcept = 0; - virtual PointType Collect() noexcept = 0; + /** + * Returns the result of the merge of the two aggregations. + * + * This should always assume that the aggregations do not overlap and merge together for a new + * cumulative report. + * + * @param delta the newly captured (delta) aggregation + * @return the result of the merge of the given aggregation. + */ + + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept = 0; + + /** + * Returns a new delta aggregation by comparing two cumulative measurements. + * + * @param next the newly captured (cumulative) aggregation. + * @return The resulting delta aggregation. + */ + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept = 0; + + /** + * Returns the point data that the aggregation will produce. + * + * @return PointType + */ + + virtual PointType ToPoint() const noexcept = 0; + + virtual ~Aggregation() = default; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index f193864926..b5a1283d26 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -30,8 +30,8 @@ class DefaultAggregation case InstrumentType::kUpDownCounter: case InstrumentType::kObservableUpDownCounter: return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) - ? std::move(std::unique_ptr(new LongSumAggregation(true))) - : std::move(std::unique_ptr(new DoubleSumAggregation(true))); + ? std::move(std::unique_ptr(new LongSumAggregation())) + : std::move(std::unique_ptr(new DoubleSumAggregation())); break; case InstrumentType::kHistogram: return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) @@ -79,11 +79,11 @@ class DefaultAggregation case AggregationType::kSum: if (instrument_descriptor.value_type_ == InstrumentValueType::kLong) { - return std::unique_ptr(new LongSumAggregation(true)); + return std::unique_ptr(new LongSumAggregation()); } else { - return std::unique_ptr(new DoubleSumAggregation(true)); + return std::unique_ptr(new DoubleSumAggregation()); } break; default: diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/drop_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/drop_aggregation.h index 0af5dd43a5..4e29fa2e46 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/drop_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/drop_aggregation.h @@ -14,6 +14,10 @@ namespace sdk namespace metrics { +/** + * A null Aggregation which denotes no aggregation should occur. + */ + class DropAggregation : public Aggregation { public: @@ -23,7 +27,21 @@ class DropAggregation : public Aggregation void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} - PointType Collect() noexcept override { return DropPointData(); } + std::unique_ptr Merge(const Aggregation &delta) const noexcept override + { + return std::unique_ptr(new DropAggregation()); + } + + std::unique_ptr Diff(const Aggregation &next) const noexcept override + { + return std::unique_ptr(new DropAggregation()); + } + + PointType ToPoint() const noexcept override + { + static DropPointData point_data; + return point_data; + } }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h index 5a66ecb243..8f33fa27b4 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h @@ -13,57 +13,47 @@ namespace sdk { namespace metrics { -template -static inline void PopulateHistogramDataPoint(HistogramPointData &histogram, - opentelemetry::common::SystemTimestamp epoch_nanos, - T sum, - uint64_t count, - std::vector &counts, - std::vector boundaries) -{ - histogram.epoch_nanos_ = epoch_nanos; - histogram.boundaries_ = boundaries; - histogram.sum_ = sum; - histogram.counts_ = counts; - histogram.count_ = count; -} class LongHistogramAggregation : public Aggregation { public: LongHistogramAggregation(); + LongHistogramAggregation(HistogramPointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: opentelemetry::common::SpinLockMutex lock_; - std::vector boundaries_; - long sum_; - std::vector counts_; - uint64_t count_; + HistogramPointData point_data_; }; class DoubleHistogramAggregation : public Aggregation { public: DoubleHistogramAggregation(); + DoubleHistogramAggregation(HistogramPointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: - opentelemetry::common::SpinLockMutex lock_; - std::vector boundaries_; - double sum_; - std::vector counts_; - uint64_t count_; + mutable opentelemetry::common::SpinLockMutex lock_; + mutable HistogramPointData point_data_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h index 092a0df30e..7f185d51a1 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h @@ -17,34 +17,42 @@ class LongLastValueAggregation : public Aggregation { public: LongLastValueAggregation(); + LongLastValueAggregation(LastValuePointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: opentelemetry::common::SpinLockMutex lock_; - long value_; - bool is_lastvalue_valid_; + LastValuePointData point_data_; }; class DoubleLastValueAggregation : public Aggregation { public: DoubleLastValueAggregation(); + DoubleLastValueAggregation(LastValuePointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: - opentelemetry::common::SpinLockMutex lock_; - double value_; - bool is_lastvalue_valid_; + mutable opentelemetry::common::SpinLockMutex lock_; + mutable LastValuePointData point_data_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h index ff99cec733..b0f0169b24 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h @@ -14,52 +14,46 @@ namespace sdk namespace metrics { -template -static inline void PopulateSumPointData(SumPointData &sum, - opentelemetry::common::SystemTimestamp start_ts, - opentelemetry::common::SystemTimestamp end_ts, - T value, - bool is_monotonic) -{ - sum.start_epoch_nanos_ = start_ts; - sum.end_epoch_nanos_ = end_ts; - sum.value_ = value; - sum.is_monotonic_ = is_monotonic; - sum.aggregation_temporality_ = AggregationTemporality::kDelta; -} - -class LongSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation +class LongSumAggregation : public Aggregation { public: - LongSumAggregation(bool is_monotonic); + LongSumAggregation(); + LongSumAggregation(SumPointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: opentelemetry::common::SpinLockMutex lock_; - opentelemetry::common::SystemTimestamp start_epoch_nanos_; - long sum_; + SumPointData point_data_; }; -class DoubleSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation +class DoubleSumAggregation : public Aggregation { public: - DoubleSumAggregation(bool is_monotonic); + DoubleSumAggregation(); + DoubleSumAggregation(SumPointData &&); void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; - PointType Collect() noexcept override; + virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + + virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; + + PointType ToPoint() const noexcept override; private: - opentelemetry::common::SpinLockMutex lock_; - opentelemetry::common::SystemTimestamp start_epoch_nanos_; - double sum_; + mutable opentelemetry::common::SpinLockMutex lock_; + SumPointData point_data_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h index cb7bc4cf80..738d4540f7 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -17,18 +17,23 @@ namespace sdk namespace metrics { -using PointAttributes = opentelemetry::sdk::common::AttributeMap; +using PointAttributes = opentelemetry::sdk::common::OrderedAttributeMap; using PointType = opentelemetry::nostd:: variant; +struct PointDataAttributes +{ + PointAttributes attributes; + PointType point_data; +}; + class MetricData { public: - opentelemetry::sdk::resource::Resource *resource_; - opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; - PointAttributes attributes_; InstrumentDescriptor instrument_descriptor; - PointType point_data_; + opentelemetry::common::SystemTimestamp start_ts; + opentelemetry::common::SystemTimestamp end_ts; + std::vector point_data_attr_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h index 5bbcbd0b1e..714f96c9af 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -8,7 +8,7 @@ # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" -# include +# include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -17,40 +17,62 @@ namespace metrics { using ValueType = nostd::variant; -using ListType = nostd::variant, std::vector>; +using ListType = nostd::variant, std::list>; + +// TODO: remove ctors and initializers from below classes when GCC<5 stops shipping on Ubuntu class SumPointData { public: - opentelemetry::common::SystemTimestamp start_epoch_nanos_; - opentelemetry::common::SystemTimestamp end_epoch_nanos_; - ValueType value_; - AggregationTemporality aggregation_temporality_; - bool is_monotonic_; + // TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu + SumPointData(SumPointData &&) = default; + SumPointData(const SumPointData &) = default; + SumPointData &operator=(SumPointData &&) = default; + SumPointData() = default; + + ValueType value_ = {}; }; class LastValuePointData { public: - opentelemetry::common::SystemTimestamp epoch_nanos_; - bool is_lastvalue_valid_; - ValueType value_; + // TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu + LastValuePointData(LastValuePointData &&) = default; + LastValuePointData(const LastValuePointData &) = default; + LastValuePointData &operator=(LastValuePointData &&) = default; + LastValuePointData() = default; + + ValueType value_ = {}; + bool is_lastvalue_valid_ = {}; + opentelemetry::common::SystemTimestamp sample_ts_ = {}; }; class HistogramPointData { public: - opentelemetry::common::SystemTimestamp epoch_nanos_; - ListType boundaries_; - ValueType sum_; - std::vector counts_; - uint64_t count_; + // TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu + HistogramPointData(HistogramPointData &&) = default; + HistogramPointData &operator=(HistogramPointData &&) = default; + HistogramPointData(const HistogramPointData &) = default; + HistogramPointData() = default; + + ListType boundaries_ = {}; + ValueType sum_ = {}; + std::vector counts_ = {}; + uint64_t count_ = {}; }; class DropPointData -{}; +{ +public: + // TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu + DropPointData(DropPointData &&) = default; + DropPointData(const DropPointData &) = default; + DropPointData() = default; + DropPointData &operator=(DropPointData &&) = default; +}; } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE -#endif \ No newline at end of file +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index 15e6a25705..5d85357143 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -40,7 +40,7 @@ enum class AggregationTemporality { kUnspecified, kDelta, - kCummulative + kCumulative }; struct InstrumentDescriptor diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index fc9fb36503..c9e02918a0 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -103,7 +103,7 @@ class Meter final : public opentelemetry::metrics::Meter const noexcept; /** collect metrics across all the meters **/ - bool collect(CollectorHandle *collector, + bool Collect(CollectorHandle *collector, opentelemetry::common::SystemTimestamp collect_ts, nostd::function_ref callback) noexcept; diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context.h b/sdk/include/opentelemetry/sdk/metrics/meter_context.h index 8a31821353..74f67a1c46 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -111,7 +111,6 @@ class MeterContext : public std::enable_shared_from_this /** * Adds a meter to the list of configured meters. - * * Note: This method is INTERNAL to sdk not thread safe. * * @param meter diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index 1f7cee8d30..57690ac87c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -7,7 +7,6 @@ # include "opentelemetry/sdk/common/global_log_handler.h" # include "opentelemetry/sdk/metrics/data/metric_data.h" # include "opentelemetry/sdk/metrics/instruments.h" - # include "opentelemetry/version.h" # include @@ -27,7 +26,7 @@ class MetricReader { public: MetricReader( - AggregationTemporality aggregation_temporality = AggregationTemporality::kCummulative); + AggregationTemporality aggregation_temporality = AggregationTemporality::kCumulative); void SetMetricProducer(MetricProducer *metric_producer); diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 019d533006..31ab70c5b8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -6,12 +6,11 @@ # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/metrics/instruments.h" -# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" +# include "opentelemetry/sdk/metrics/state/metric_collector.h" # include "opentelemetry/sdk/metrics/state/metric_storage.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" -# include "opentelemetry/sdk/resource/resource.h" # include # include "opentelemetry/sdk/metrics/observer_result.h" diff --git a/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h b/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h index 32301f8038..50d40e0ae6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h @@ -37,7 +37,7 @@ class AttributesHashMap public: Aggregation *Get(const MetricAttributes &attributes) const { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); auto it = hash_map_.find(attributes); if (it != hash_map_.end()) { @@ -52,7 +52,7 @@ class AttributesHashMap */ bool Has(const MetricAttributes &attributes) const { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); return (hash_map_.find(attributes) == hash_map_.end()) ? false : true; } @@ -64,7 +64,8 @@ class AttributesHashMap Aggregation *GetOrSetDefault(const MetricAttributes &attributes, std::function()> aggregation_callback) { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); + auto it = hash_map_.find(attributes); if (it != hash_map_.end()) { @@ -80,7 +81,7 @@ class AttributesHashMap */ void Set(const MetricAttributes &attributes, std::unique_ptr value) { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); hash_map_[attributes] = std::move(value); } @@ -90,7 +91,7 @@ class AttributesHashMap bool GetAllEnteries( nostd::function_ref callback) const { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); for (auto &kv : hash_map_) { if (!callback(kv.first, *(kv.second.get()))) @@ -106,7 +107,7 @@ class AttributesHashMap */ size_t Size() { - std::lock_guard guard(GetLock()); + std::lock_guard guard(lock_); return hash_map_.size(); } @@ -114,11 +115,7 @@ class AttributesHashMap std::unordered_map, AttributeHashGenerator> hash_map_; - static opentelemetry::common::SpinLockMutex &GetLock() noexcept - { - static opentelemetry::common::SpinLockMutex lock; - return lock; - } + mutable opentelemetry::common::SpinLockMutex lock_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h index bfe50b152d..8269d2fd3c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -9,11 +9,14 @@ # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/exemplar/reservoir.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" +# include "opentelemetry/sdk/metrics/state/metric_collector.h" # include "opentelemetry/sdk/metrics/state/metric_storage.h" + # include "opentelemetry/sdk/metrics/view/attributes_processor.h" # include "opentelemetry/sdk/metrics/view/view.h" # include "opentelemetry/sdk/resource/resource.h" +# include # include OPENTELEMETRY_BEGIN_NAMESPACE @@ -22,6 +25,12 @@ namespace sdk namespace metrics { +struct LastReportedMetrics +{ + std::unique_ptr attributes_map; + opentelemetry::common::SystemTimestamp collection_ts; +}; + class SyncMetricStorage : public MetricStorage, public WritableMetricStorage { @@ -73,7 +82,6 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage { return; } - exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } @@ -88,7 +96,6 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage { return; } - exemplar_reservoir_->OfferMeasurement(value, attributes, context, std::chrono::system_clock::now()); auto attr = attributes_processor_->process(attributes); @@ -99,20 +106,19 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref callback) noexcept override - { - MetricData data; - if (callback(data)) - { - return true; - } - return false; - } + nostd::function_ref callback) noexcept override; private: InstrumentDescriptor instrument_descriptor_; AggregationType aggregation_type_; + + // hashmap to maintain the metrics for delta collection (i.e, collection since last Collect call) std::unique_ptr attributes_hashmap_; + // unreported metrics stash for all the collectors + std::unordered_map>> + unreported_metrics_; + // last reported metrics stash for all the collectors. + std::unordered_map last_reported_metrics_; const AttributesProcessor *attributes_processor_; std::function()> create_default_aggregation_; nostd::shared_ptr exemplar_reservoir_; diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 119477eb06..d8eff48cd5 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -5,6 +5,7 @@ add_library( meter_context.cc metric_reader.cc state/metric_collector.cc + state/sync_metric_storage.cc aggregation/histogram_aggregation.cc aggregation/lastvalue_aggregation.cc aggregation/sum_aggregation.cc diff --git a/sdk/src/metrics/aggregation/histogram_aggregation.cc b/sdk/src/metrics/aggregation/histogram_aggregation.cc index 2ebe3715bb..60f65c51b1 100644 --- a/sdk/src/metrics/aggregation/histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/histogram_aggregation.cc @@ -13,65 +13,104 @@ namespace metrics { LongHistogramAggregation::LongHistogramAggregation() - : boundaries_{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l}, - counts_(boundaries_.size() + 1, 0), - sum_(0l), - count_(0) +{ + + point_data_.boundaries_ = std::list{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l}; + point_data_.counts_ = + std::vector(nostd::get>(point_data_.boundaries_).size() + 1, 0); + point_data_.sum_ = 0l; + point_data_.count_ = 0; +} + +LongHistogramAggregation::LongHistogramAggregation(HistogramPointData &&data) + : point_data_{std::move(data)} {} void LongHistogramAggregation::Aggregate(long value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - count_ += 1; - sum_ += value; - for (auto it = boundaries_.begin(); it != boundaries_.end(); ++it) + point_data_.count_ += 1; + point_data_.sum_ = nostd::get(point_data_.sum_) + value; + size_t index = 0; + for (auto it = nostd::get>(point_data_.boundaries_).begin(); + it != nostd::get>(point_data_.boundaries_).end(); ++it) { if (value < *it) { - counts_[std::distance(boundaries_.begin(), it)] += 1; + point_data_.counts_[index] += 1; return; } + index++; } } -PointType LongHistogramAggregation::Collect() noexcept +std::unique_ptr LongHistogramAggregation::Merge( + const Aggregation &delta) const noexcept { - HistogramPointData point_data; - auto epoch_nanos = std::chrono::system_clock::now(); - const std::lock_guard locked(lock_); - PopulateHistogramDataPoint(point_data, epoch_nanos, sum_, count_, counts_, boundaries_); - return point_data; + return nullptr; +} + +std::unique_ptr LongHistogramAggregation::Diff(const Aggregation &next) const noexcept +{ + return nullptr; +} + +PointType LongHistogramAggregation::ToPoint() const noexcept +{ + return point_data_; } DoubleHistogramAggregation::DoubleHistogramAggregation() - : boundaries_{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0}, - counts_(boundaries_.size() + 1, 0), - sum_(0.0), - count_(0) +{ + + point_data_.boundaries_ = + std::list{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0}; + point_data_.counts_ = + std::vector(nostd::get>(point_data_.boundaries_).size() + 1, 0); + point_data_.sum_ = 0.0; + point_data_.count_ = 0; +} + +DoubleHistogramAggregation::DoubleHistogramAggregation(HistogramPointData &&data) + : point_data_{std::move(data)} {} void DoubleHistogramAggregation::Aggregate(double value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - count_ += 1; - sum_ += value; - for (auto it = boundaries_.begin(); it != boundaries_.end(); ++it) + point_data_.count_ += 1; + point_data_.sum_ = nostd::get(point_data_.sum_) + value; + size_t index = 0; + for (auto it = nostd::get>(point_data_.boundaries_).begin(); + it != nostd::get>(point_data_.boundaries_).end(); ++it) { if (value < *it) { - counts_[std::distance(boundaries_.begin(), it)] += 1; + point_data_.counts_[index] += 1; return; } + index++; } } -PointType DoubleHistogramAggregation::Collect() noexcept +std::unique_ptr DoubleHistogramAggregation::Merge( + const Aggregation &delta) const noexcept { - HistogramPointData point_data; - auto epoch_nanos = std::chrono::system_clock::now(); - const std::lock_guard locked(lock_); - PopulateHistogramDataPoint(point_data, epoch_nanos, sum_, count_, counts_, boundaries_); - return point_data; + // TODO - Implement me + return nullptr; +} + +std::unique_ptr DoubleHistogramAggregation::Diff( + const Aggregation &next) const noexcept +{ + // TODO - Implement me + return nullptr; +} + +PointType DoubleHistogramAggregation::ToPoint() const noexcept +{ + // TODO Implement me + return point_data_; } } // namespace metrics diff --git a/sdk/src/metrics/aggregation/lastvalue_aggregation.cc b/sdk/src/metrics/aggregation/lastvalue_aggregation.cc index 290b526344..9c0252be31 100644 --- a/sdk/src/metrics/aggregation/lastvalue_aggregation.cc +++ b/sdk/src/metrics/aggregation/lastvalue_aggregation.cc @@ -14,46 +14,109 @@ namespace sdk namespace metrics { -LongLastValueAggregation::LongLastValueAggregation() : is_lastvalue_valid_(false) {} +LongLastValueAggregation::LongLastValueAggregation() +{ + point_data_.is_lastvalue_valid_ = false; + point_data_.value_ = 0l; +} +LongLastValueAggregation::LongLastValueAggregation(LastValuePointData &&data) + : point_data_{std::move(data)} +{} void LongLastValueAggregation::Aggregate(long value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - is_lastvalue_valid_ = true; - value_ = value; + point_data_.is_lastvalue_valid_ = true; + point_data_.value_ = value; } -PointType LongLastValueAggregation::Collect() noexcept +std::unique_ptr LongLastValueAggregation::Merge( + const Aggregation &delta) const noexcept { - const std::lock_guard locked(lock_); - if (!is_lastvalue_valid_) + if (nostd::get(ToPoint()).sample_ts_.time_since_epoch() > + nostd::get(delta.ToPoint()).sample_ts_.time_since_epoch()) + { + LastValuePointData merge_data = std::move(nostd::get(ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(merge_data))); + } + else { - return LastValuePointData{ - opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()), false, 0l}; + LastValuePointData merge_data = std::move(nostd::get(delta.ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(merge_data))); } - return LastValuePointData{ - opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()), true, value_}; } -DoubleLastValueAggregation::DoubleLastValueAggregation() : is_lastvalue_valid_(false) {} +std::unique_ptr LongLastValueAggregation::Diff(const Aggregation &next) const noexcept +{ + if (nostd::get(ToPoint()).sample_ts_.time_since_epoch() > + nostd::get(next.ToPoint()).sample_ts_.time_since_epoch()) + { + LastValuePointData diff_data = std::move(nostd::get(ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(diff_data))); + } + else + { + LastValuePointData diff_data = std::move(nostd::get(next.ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(diff_data))); + } +} + +PointType LongLastValueAggregation::ToPoint() const noexcept +{ + return point_data_; +} + +DoubleLastValueAggregation::DoubleLastValueAggregation() +{ + point_data_.is_lastvalue_valid_ = false; + point_data_.value_ = 0.0; +} +DoubleLastValueAggregation::DoubleLastValueAggregation(LastValuePointData &&data) + : point_data_{std::move(data)} +{} void DoubleLastValueAggregation::Aggregate(double value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - is_lastvalue_valid_ = true; - value_ = value; + point_data_.is_lastvalue_valid_ = true; + point_data_.value_ = value; } -PointType DoubleLastValueAggregation::Collect() noexcept +std::unique_ptr DoubleLastValueAggregation::Merge( + const Aggregation &delta) const noexcept { - const std::lock_guard locked(lock_); - if (!is_lastvalue_valid_) + if (nostd::get(ToPoint()).sample_ts_.time_since_epoch() > + nostd::get(delta.ToPoint()).sample_ts_.time_since_epoch()) + { + LastValuePointData merge_data = std::move(nostd::get(ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(merge_data))); + } + else { - return LastValuePointData{ - opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()), false, 0.0}; + LastValuePointData merge_data = std::move(nostd::get(delta.ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(merge_data))); } - return LastValuePointData{ - opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()), true, value_}; +} + +std::unique_ptr DoubleLastValueAggregation::Diff( + const Aggregation &next) const noexcept +{ + if (nostd::get(ToPoint()).sample_ts_.time_since_epoch() > + nostd::get(next.ToPoint()).sample_ts_.time_since_epoch()) + { + LastValuePointData diff_data = std::move(nostd::get(ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(diff_data))); + } + else + { + LastValuePointData diff_data = std::move(nostd::get(next.ToPoint())); + return std::unique_ptr(new LongLastValueAggregation(std::move(diff_data))); + } +} + +PointType DoubleLastValueAggregation::ToPoint() const noexcept +{ + return point_data_; } } // namespace metrics } // namespace sdk diff --git a/sdk/src/metrics/aggregation/sum_aggregation.cc b/sdk/src/metrics/aggregation/sum_aggregation.cc index e0d1330f68..94b871cd34 100644 --- a/sdk/src/metrics/aggregation/sum_aggregation.cc +++ b/sdk/src/metrics/aggregation/sum_aggregation.cc @@ -3,8 +3,10 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h" +# include "opentelemetry/sdk/metrics/data/point_data.h" # include "opentelemetry/version.h" +# include # include OPENTELEMETRY_BEGIN_NAMESPACE @@ -13,54 +15,90 @@ namespace sdk namespace metrics { -LongSumAggregation::LongSumAggregation(bool is_monotonic) - : InstrumentMonotonicityAwareAggregation(is_monotonic), - start_epoch_nanos_(opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now())), - sum_(0l) -{} +LongSumAggregation::LongSumAggregation() +{ + point_data_.value_ = 0l; +} + +LongSumAggregation::LongSumAggregation(SumPointData &&data) : point_data_{std::move(data)} {} void LongSumAggregation::Aggregate(long value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - sum_ += value; + point_data_.value_ = nostd::get(point_data_.value_) + value; } -PointType LongSumAggregation::Collect() noexcept +std::unique_ptr LongSumAggregation::Merge(const Aggregation &delta) const noexcept { - opentelemetry::common::SystemTimestamp current_ts(std::chrono::system_clock::now()); - SumPointData sum; - { - const std::lock_guard locked(lock_); - PopulateSumPointData(sum, start_epoch_nanos_, current_ts, sum_, IsMonotonic()); - start_epoch_nanos_ = current_ts; - sum_ = 0; - } - return sum; + long merge_value = + nostd::get( + nostd::get((static_cast(delta).ToPoint())) + .value_) + + nostd::get(nostd::get(ToPoint()).value_); + std::unique_ptr aggr(new LongSumAggregation()); + static_cast(aggr.get())->point_data_.value_ = merge_value; + return aggr; } -DoubleSumAggregation::DoubleSumAggregation(bool is_monotonic) - : InstrumentMonotonicityAwareAggregation(is_monotonic), - start_epoch_nanos_(opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now())), - sum_(0L) -{} +std::unique_ptr LongSumAggregation::Diff(const Aggregation &next) const noexcept +{ + + long diff_value = nostd::get(nostd::get( + (static_cast(next).ToPoint())) + .value_) - + nostd::get(nostd::get(ToPoint()).value_); + std::unique_ptr aggr(new LongSumAggregation()); + static_cast(aggr.get())->point_data_.value_ = diff_value; + return aggr; +} + +PointType LongSumAggregation::ToPoint() const noexcept +{ + return point_data_; +} + +DoubleSumAggregation::DoubleSumAggregation() +{ + point_data_.value_ = 0.0; +} + +DoubleSumAggregation::DoubleSumAggregation(SumPointData &&data) : point_data_(std::move(data)) {} void DoubleSumAggregation::Aggregate(double value, const PointAttributes &attributes) noexcept { const std::lock_guard locked(lock_); - sum_ += value; + point_data_.value_ = nostd::get(point_data_.value_) + value; +} + +std::unique_ptr DoubleSumAggregation::Merge(const Aggregation &delta) const noexcept +{ + double merge_value = + nostd::get( + nostd::get((static_cast(delta).ToPoint())) + .value_) + + nostd::get(nostd::get(ToPoint()).value_); + std::unique_ptr aggr(new DoubleSumAggregation()); + static_cast(aggr.get())->point_data_.value_ = merge_value; + return aggr; } -PointType DoubleSumAggregation::Collect() noexcept +std::unique_ptr DoubleSumAggregation::Diff(const Aggregation &next) const noexcept { - opentelemetry::common::SystemTimestamp current_ts(std::chrono::system_clock::now()); - SumPointData sum; - { - const std::lock_guard locked(lock_); - PopulateSumPointData(sum, start_epoch_nanos_, current_ts, sum_, IsMonotonic()); - start_epoch_nanos_ = current_ts; - sum_ = 0; - } - return sum; + + double diff_value = + nostd::get( + nostd::get((static_cast(next).ToPoint())) + .value_) - + nostd::get(nostd::get(ToPoint()).value_); + std::unique_ptr aggr(new DoubleSumAggregation()); + static_cast(aggr.get())->point_data_.value_ = diff_value; + return aggr; +} + +PointType DoubleSumAggregation::ToPoint() const noexcept +{ + const std::lock_guard locked(lock_); + return point_data_; } } // namespace metrics diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 20e3b02768..fb74d55eb4 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -211,7 +211,7 @@ std::unique_ptr Meter::RegisterMetricStorage( } /** collect metrics across all the meters **/ -bool Meter::collect(CollectorHandle *collector, +bool Meter::Collect(CollectorHandle *collector, opentelemetry::common::SystemTimestamp collect_ts, nostd::function_ref callback) noexcept { diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index 5b9fc4ab70..cd3b8eb6ca 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -34,7 +34,7 @@ bool MetricCollector::Collect(nostd::function_ref callback) no for (auto &meter : meter_context_->GetMeters()) { auto collection_ts = std::chrono::system_clock::now(); - meter->collect(this, collection_ts, callback); + meter->Collect(this, collection_ts, callback); } return true; } diff --git a/sdk/src/metrics/state/sync_metric_storage.cc b/sdk/src/metrics/state/sync_metric_storage.cc new file mode 100644 index 0000000000..5c14f8bbc3 --- /dev/null +++ b/sdk/src/metrics/state/sync_metric_storage.cc @@ -0,0 +1,131 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +bool SyncMetricStorage::Collect(CollectorHandle *collector, + nostd::span> collectors, + opentelemetry::common::SystemTimestamp sdk_start_ts, + opentelemetry::common::SystemTimestamp collection_ts, + nostd::function_ref callback) noexcept +{ + opentelemetry::common::SystemTimestamp last_collection_ts = sdk_start_ts; + auto aggregation_temporarily = collector->GetAggregationTemporality(); + + // Add the current delta metrics to `unreported metrics stash` for all the collectors, + // this will also empty the delta metrics hashmap, and make it available for + // recordings + std::shared_ptr delta_metrics = std::move(attributes_hashmap_); + attributes_hashmap_.reset(new AttributesHashMap); + for (auto &col : collectors) + { + unreported_metrics_[col.get()].push_back(delta_metrics); + } + + // Get the unreported metrics for the `collector` from `unreported metrics stash` + // since last collection, this will also cleanup the unreported metrics for `collector` + // from the stash. + auto present = unreported_metrics_.find(collector); + if (present == unreported_metrics_.end()) + { + // no unreported metrics for the collector, return. + return true; + } + auto unreported_list = std::move(present->second); + + // Iterate over the unreporter metrics for `collector` and store result in `merged_metrics` + std::unique_ptr merged_metrics(new AttributesHashMap); + for (auto &agg_hashmap : unreported_list) + { + agg_hashmap->GetAllEnteries([&merged_metrics, this](const MetricAttributes &attributes, + Aggregation &aggregation) { + auto agg = merged_metrics->Get(attributes); + if (agg) + { + merged_metrics->Set(attributes, std::move(agg->Merge(aggregation))); + } + else + { + merged_metrics->Set( + attributes, + std::move( + DefaultAggregation::CreateAggregation(instrument_descriptor_)->Merge(aggregation))); + merged_metrics->GetAllEnteries( + [](const MetricAttributes &attr, Aggregation &aggr) { return true; }); + } + return true; + }); + } + // Get the last reported metrics for the `collector` from `last reported metrics` stash + // - If the aggregation_temporarily for the collector is cumulative + // - Merge the last reported metrics with unreported metrics (which is in merged_metrics), + // Final result of merge would be in merged_metrics. + // - Move the final merge to the `last reported metrics` stash. + // - If the aggregation_temporarily is delta + // - Store the unreported metrics for `collector` (which is in merged_mtrics) to + // `last reported metrics` stash. + + auto reported = last_reported_metrics_.find(collector); + if (reported != last_reported_metrics_.end()) + { + last_collection_ts = last_reported_metrics_[collector].collection_ts; + auto last_aggr_hashmap = std::move(last_reported_metrics_[collector].attributes_map); + if (aggregation_temporarily == AggregationTemporality::kCumulative) + { + // merge current delta to previous cumulative + last_aggr_hashmap->GetAllEnteries( + [&merged_metrics, this](const MetricAttributes &attributes, Aggregation &aggregation) { + auto agg = merged_metrics->Get(attributes); + if (agg) + { + merged_metrics->Set(attributes, agg->Merge(aggregation)); + } + else + { + merged_metrics->Set(attributes, + DefaultAggregation::CreateAggregation(instrument_descriptor_)); + } + return true; + }); + } + last_reported_metrics_[collector] = + LastReportedMetrics{std::move(merged_metrics), collection_ts}; + } + else + { + merged_metrics->GetAllEnteries( + [](const MetricAttributes &attr, Aggregation &aggr) { return true; }); + last_reported_metrics_.insert( + std::make_pair(collector, LastReportedMetrics{std::move(merged_metrics), collection_ts})); + } + + // Generate the MetricData from the final merged_metrics, and invoke callback over it. + + AttributesHashMap *result_to_export = (last_reported_metrics_[collector]).attributes_map.get(); + MetricData metric_data; + metric_data.instrument_descriptor = instrument_descriptor_; + metric_data.start_ts = last_collection_ts; + metric_data.end_ts = collection_ts; + result_to_export->GetAllEnteries( + [&metric_data](const MetricAttributes &attributes, Aggregation &aggregation) { + PointDataAttributes point_data_attr; + point_data_attr.point_data = aggregation.ToPoint(); + point_data_attr.attributes = attributes; + metric_data.point_data_attr_.push_back(point_data_attr); + return true; + }); + return callback(metric_data); +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index ec753111ff..a32826b145 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -13,82 +13,80 @@ using namespace opentelemetry::sdk::metrics; namespace nostd = opentelemetry::nostd; TEST(Aggregation, LongSumAggregation) { - LongSumAggregation aggr(true); - auto data = aggr.Collect(); + LongSumAggregation aggr; + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto sum_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(sum_data.value_)); EXPECT_EQ(nostd::get(sum_data.value_), 0l); - EXPECT_EQ(sum_data.is_monotonic_, true); EXPECT_NO_THROW(aggr.Aggregate(12l, {})); EXPECT_NO_THROW(aggr.Aggregate(0l, {})); - sum_data = nostd::get(aggr.Collect()); + sum_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(sum_data.value_), 12l); } TEST(Aggregation, DoubleSumAggregation) { - DoubleSumAggregation aggr(true); - auto data = aggr.Collect(); + DoubleSumAggregation aggr; + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto sum_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(sum_data.value_)); EXPECT_EQ(nostd::get(sum_data.value_), 0); - EXPECT_EQ(sum_data.is_monotonic_, true); EXPECT_NO_THROW(aggr.Aggregate(12.0, {})); EXPECT_NO_THROW(aggr.Aggregate(1.0, {})); - sum_data = nostd::get(aggr.Collect()); + sum_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(sum_data.value_), 13.0); } TEST(Aggregation, LongLastValueAggregation) { LongLastValueAggregation aggr; - auto data = aggr.Collect(); + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto lastvalue_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(lastvalue_data.value_)); EXPECT_EQ(lastvalue_data.is_lastvalue_valid_, false); EXPECT_NO_THROW(aggr.Aggregate(12l, {})); EXPECT_NO_THROW(aggr.Aggregate(1l, {})); - lastvalue_data = nostd::get(aggr.Collect()); + lastvalue_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(lastvalue_data.value_), 1.0); } TEST(Aggregation, DoubleLastValueAggregation) { DoubleLastValueAggregation aggr; - auto data = aggr.Collect(); + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto lastvalue_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(lastvalue_data.value_)); EXPECT_EQ(lastvalue_data.is_lastvalue_valid_, false); EXPECT_NO_THROW(aggr.Aggregate(12.0, {})); EXPECT_NO_THROW(aggr.Aggregate(1.0, {})); - lastvalue_data = nostd::get(aggr.Collect()); + lastvalue_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(lastvalue_data.value_), 1.0); } TEST(Aggregation, LongHistogramAggregation) { LongHistogramAggregation aggr; - auto data = aggr.Collect(); + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto histogram_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(histogram_data.sum_)); - ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); + ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); EXPECT_EQ(nostd::get(histogram_data.sum_), 0); EXPECT_EQ(histogram_data.count_, 0); EXPECT_NO_THROW(aggr.Aggregate(12l, {})); // lies in fourth bucket EXPECT_NO_THROW(aggr.Aggregate(100l, {})); // lies in eight bucket - histogram_data = nostd::get(aggr.Collect()); + histogram_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(histogram_data.sum_), 112); EXPECT_EQ(histogram_data.count_, 2); EXPECT_EQ(histogram_data.counts_[3], 1); EXPECT_EQ(histogram_data.counts_[7], 1); EXPECT_NO_THROW(aggr.Aggregate(13l, {})); // lies in fourth bucket EXPECT_NO_THROW(aggr.Aggregate(252l, {})); // lies in ninth bucket - histogram_data = nostd::get(aggr.Collect()); + histogram_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(histogram_data.count_, 4); EXPECT_EQ(histogram_data.counts_[3], 2); EXPECT_EQ(histogram_data.counts_[8], 1); @@ -97,23 +95,23 @@ TEST(Aggregation, LongHistogramAggregation) TEST(Aggregation, DoubleHistogramAggregation) { DoubleHistogramAggregation aggr; - auto data = aggr.Collect(); + auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto histogram_data = nostd::get(data); ASSERT_TRUE(nostd::holds_alternative(histogram_data.sum_)); - ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); + ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); EXPECT_EQ(nostd::get(histogram_data.sum_), 0); EXPECT_EQ(histogram_data.count_, 0); EXPECT_NO_THROW(aggr.Aggregate(12.0, {})); // lies in fourth bucket EXPECT_NO_THROW(aggr.Aggregate(100.0, {})); // lies in eight bucket - histogram_data = nostd::get(aggr.Collect()); + histogram_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(nostd::get(histogram_data.sum_), 112); EXPECT_EQ(histogram_data.count_, 2); EXPECT_EQ(histogram_data.counts_[3], 1); EXPECT_EQ(histogram_data.counts_[7], 1); EXPECT_NO_THROW(aggr.Aggregate(13.0, {})); // lies in fourth bucket EXPECT_NO_THROW(aggr.Aggregate(252.0, {})); // lies in ninth bucket - histogram_data = nostd::get(aggr.Collect()); + histogram_data = nostd::get(aggr.ToPoint()); EXPECT_EQ(histogram_data.count_, 4); EXPECT_EQ(histogram_data.counts_[3], 2); EXPECT_EQ(histogram_data.counts_[8], 1); diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index 0527c7f0ca..f7c9914e3c 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -42,6 +42,10 @@ TEST(AsyncMetricStorageTest, BasicTests) InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; + auto sdk_start_ts = std::chrono::system_clock::now(); + // Some computation here + auto collection_ts = std::chrono::system_clock::now() + std::chrono::seconds(5); + std::vector> exporters; std::shared_ptr meter_context(new MeterContext(std::move(exporters))); std::unique_ptr metric_reader(new MockMetricReader(AggregationTemporality::kDelta)); @@ -53,7 +57,7 @@ TEST(AsyncMetricStorageTest, BasicTests) opentelemetry::sdk::metrics::AsyncMetricStorage storage( instr_desc, AggregationType::kSum, &measurement_fetch, new DefaultAttributesProcessor()); - storage.Collect(collector.get(), collectors, std::chrono::system_clock::now(), - std::chrono::system_clock::now(), metric_callback); + EXPECT_NO_THROW( + storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts, metric_callback)); } #endif \ No newline at end of file diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 015d3023ae..6ede67d032 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -40,9 +40,7 @@ class MockMetricReader : public MetricReader { public: virtual bool OnForceFlush(std::chrono::microseconds timeout) noexcept override { return true; } - virtual bool OnShutDown(std::chrono::microseconds timeout) noexcept override { return true; } - virtual void OnInitialized() noexcept override {} }; diff --git a/sdk/test/metrics/metric_reader_test.cc b/sdk/test/metrics/metric_reader_test.cc index 214f522118..68a3fcc5f5 100644 --- a/sdk/test/metrics/metric_reader_test.cc +++ b/sdk/test/metrics/metric_reader_test.cc @@ -17,9 +17,7 @@ class MockMetricReader : public MetricReader MockMetricReader(AggregationTemporality aggr_temporality) : MetricReader(aggr_temporality) {} virtual bool OnForceFlush(std::chrono::microseconds timeout) noexcept override { return true; } - virtual bool OnShutDown(std::chrono::microseconds timeout) noexcept override { return true; } - virtual void OnInitialized() noexcept override {} }; diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index c4bd154460..b0440fdbb3 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -12,23 +12,235 @@ # include using namespace opentelemetry::sdk::metrics; +using namespace opentelemetry::common; using M = std::map; -TEST(WritableMetricStorageTest, BasicTests) +class MockCollectorHandle : public CollectorHandle { - InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, +public: + MockCollectorHandle(AggregationTemporality temp) : temporality(temp) {} + + AggregationTemporality GetAggregationTemporality() noexcept override { return temporality; } + +private: + AggregationTemporality temporality; +}; + +class WritableMetricStorageTestFixture : public ::testing::TestWithParam +{}; + +TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) +{ + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + long expected_total_get_requests = 0; + long expected_total_put_requests = 0; + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; + std::map attributes_get = {{"RequestType", "GET"}}; + std::map attributes_put = {{"RequestType", "PUT"}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), NoExemplarReservoir::GetNoExemplarReservoir()); - EXPECT_NO_THROW(storage.RecordLong(10l, opentelemetry::context::Context{})); - EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::context::Context{})); + + storage.RecordLong(10l, KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{}); + expected_total_get_requests += 10; + + EXPECT_NO_THROW(storage.RecordLong( + 30l, KeyValueIterableView>(attributes_put), + opentelemetry::context::Context{})); + expected_total_put_requests += 30; + + storage.RecordLong(20l, KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{}); + expected_total_get_requests += 20; + + EXPECT_NO_THROW(storage.RecordLong( + 40l, KeyValueIterableView>(attributes_put), + opentelemetry::context::Context{})); + expected_total_put_requests += 40; + + std::shared_ptr collector(new MockCollectorHandle(temporality)); + std::vector> collectors; + collectors.push_back(collector); + + // Some computation here + auto collection_ts = std::chrono::system_clock::now(); + size_t count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { + for (auto data_attr : data.point_data_attr_) + { + auto data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "GET") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_get_requests); + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "PUT") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_put_requests); + count_attributes++; + } + } + return true; + }); + + // In case of delta temporarily, subsequent collection would contain new data points, so resetting + // the counts + if (temporality == AggregationTemporality::kDelta) + { + expected_total_get_requests = 0; + expected_total_put_requests = 0; + } + EXPECT_NO_THROW(storage.RecordLong( - 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + 50l, KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{})); + expected_total_get_requests += 50; + EXPECT_NO_THROW(storage.RecordLong( + 40l, KeyValueIterableView>(attributes_put), + opentelemetry::context::Context{})); + expected_total_put_requests += 40; + + collection_ts = std::chrono::system_clock::now(); + count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { + for (auto data_attr : data.point_data_attr_) + { + auto data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "GET") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_get_requests); + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "PUT") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_put_requests); + count_attributes++; + } + } + return true; + }); +} +INSTANTIATE_TEST_CASE_P(WritableMetricStorageTestLong, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative, + AggregationTemporality::kDelta)); + +TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) +{ + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + double expected_total_get_requests = 0; + double expected_total_put_requests = 0; + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, + InstrumentValueType::kDouble}; + std::map attributes_get = {{"RequestType", "GET"}}; + std::map attributes_put = {{"RequestType", "PUT"}}; + + opentelemetry::sdk::metrics::SyncMetricStorage storage( + instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir()); + + storage.RecordDouble(10.0, + KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{}); + expected_total_get_requests += 10; + + EXPECT_NO_THROW(storage.RecordDouble( + 30.0, KeyValueIterableView>(attributes_put), opentelemetry::context::Context{})); + expected_total_put_requests += 30; - EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}), - opentelemetry::context::Context{})); + storage.RecordDouble(20.0, + KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{}); + expected_total_get_requests += 20; + + EXPECT_NO_THROW(storage.RecordDouble( + 40.0, KeyValueIterableView>(attributes_put), + opentelemetry::context::Context{})); + expected_total_put_requests += 40; + + std::shared_ptr collector(new MockCollectorHandle(temporality)); + std::vector> collectors; + collectors.push_back(collector); + + // Some computation here + auto collection_ts = std::chrono::system_clock::now(); + size_t count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { + for (auto data_attr : data.point_data_attr_) + { + auto data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "GET") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_get_requests); + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "PUT") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_put_requests); + count_attributes++; + } + } + return true; + }); + + // In case of delta temporarily, subsequent collection would contain new data points, so resetting + // the counts + if (temporality == AggregationTemporality::kDelta) + { + expected_total_get_requests = 0; + expected_total_put_requests = 0; + } + + EXPECT_NO_THROW(storage.RecordDouble( + 50.0, KeyValueIterableView>(attributes_get), + opentelemetry::context::Context{})); + expected_total_get_requests += 50; + EXPECT_NO_THROW(storage.RecordDouble( + 40.0, KeyValueIterableView>(attributes_put), + opentelemetry::context::Context{})); + expected_total_put_requests += 40; + + collection_ts = std::chrono::system_clock::now(); + count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { + for (auto data_attr : data.point_data_attr_) + { + auto data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "GET") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_get_requests); + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("RequestType")->second) == "PUT") + { + EXPECT_EQ(opentelemetry::nostd::get(data.value_), expected_total_put_requests); + count_attributes++; + } + } + return true; + }); } +INSTANTIATE_TEST_CASE_P(WritableMetricStorageTestDouble, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative, + AggregationTemporality::kDelta)); + #endif From 76c664a20b8219b91ead69ded6b7ee873b1a85ba Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Fri, 1 Apr 2022 02:51:26 +0800 Subject: [PATCH 10/19] Rename `http_client_curl` to `opentelemetry_http_client_curl` (#1301) Signed-off-by: owent --- examples/http/CMakeLists.txt | 9 +++++---- exporters/elasticsearch/CMakeLists.txt | 2 +- exporters/jaeger/CMakeLists.txt | 2 +- exporters/otlp/CMakeLists.txt | 2 +- exporters/zipkin/CMakeLists.txt | 3 ++- ext/src/http/client/curl/CMakeLists.txt | 19 ++++++++++--------- ext/test/http/CMakeLists.txt | 6 ++++-- ext/test/w3c_tracecontext_test/CMakeLists.txt | 6 +++--- 8 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/http/CMakeLists.txt b/examples/http/CMakeLists.txt index 2eddcfe03f..a1181c93a4 100644 --- a/examples/http/CMakeLists.txt +++ b/examples/http/CMakeLists.txt @@ -10,10 +10,11 @@ else() add_executable(http_server server.cc) target_link_libraries( - http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl - opentelemetry_exporter_ostream_span ${CURL_LIBRARIES}) + http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + opentelemetry_http_client_curl opentelemetry_exporter_ostream_span + ${CURL_LIBRARIES}) target_link_libraries( - http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl - opentelemetry_exporter_ostream_span) + http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + opentelemetry_http_client_curl opentelemetry_exporter_ostream_span) endif() diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index 1538a6f44a..9b2833854e 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -10,7 +10,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_elasticsearch_logs - PUBLIC opentelemetry_trace opentelemetry_logs http_client_curl) + PUBLIC opentelemetry_trace opentelemetry_logs opentelemetry_http_client_curl) install( TARGETS opentelemetry_exporter_elasticsearch_logs diff --git a/exporters/jaeger/CMakeLists.txt b/exporters/jaeger/CMakeLists.txt index 0659c399b0..6d3d8b88fb 100644 --- a/exporters/jaeger/CMakeLists.txt +++ b/exporters/jaeger/CMakeLists.txt @@ -31,7 +31,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_jaeger_trace - PUBLIC opentelemetry_resources http_client_curl + PUBLIC opentelemetry_resources opentelemetry_http_client_curl PRIVATE thrift::thrift) if(MSVC) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 0332267922..1dba7ea1c7 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -46,7 +46,7 @@ if(WITH_OTLP_HTTP) PROPERTIES EXPORT_NAME otlp_http_client) target_link_libraries( opentelemetry_exporter_otlp_http_client - PUBLIC opentelemetry_sdk opentelemetry_proto http_client_curl + PUBLIC opentelemetry_sdk opentelemetry_proto opentelemetry_http_client_curl nlohmann_json::nlohmann_json) if(nlohmann_json_clone) add_dependencies(opentelemetry_exporter_otlp_http_client diff --git a/exporters/zipkin/CMakeLists.txt b/exporters/zipkin/CMakeLists.txt index b9591324fd..b82339f1ee 100644 --- a/exporters/zipkin/CMakeLists.txt +++ b/exporters/zipkin/CMakeLists.txt @@ -20,7 +20,8 @@ add_library(opentelemetry_exporter_zipkin_trace src/zipkin_exporter.cc target_link_libraries( opentelemetry_exporter_zipkin_trace - PUBLIC opentelemetry_trace http_client_curl nlohmann_json::nlohmann_json) + PUBLIC opentelemetry_trace opentelemetry_http_client_curl + nlohmann_json::nlohmann_json) install( TARGETS opentelemetry_exporter_zipkin_trace diff --git a/ext/src/http/client/curl/CMakeLists.txt b/ext/src/http/client/curl/CMakeLists.txt index 64486b96db..78a81cfe3e 100644 --- a/ext/src/http/client/curl/CMakeLists.txt +++ b/ext/src/http/client/curl/CMakeLists.txt @@ -1,22 +1,23 @@ find_package(CURL) if(CURL_FOUND) - add_library(http_client_curl http_client_factory_curl.cc http_client_curl.cc) + add_library(opentelemetry_http_client_curl http_client_factory_curl.cc + http_client_curl.cc) - set_target_properties(http_client_curl PROPERTIES EXPORT_NAME - http_client_curl) + set_target_properties(opentelemetry_http_client_curl + PROPERTIES EXPORT_NAME http_client_curl) if(TARGET CURL::libcurl) - target_link_libraries(http_client_curl PUBLIC opentelemetry_ext - CURL::libcurl) + target_link_libraries(opentelemetry_http_client_curl + PUBLIC opentelemetry_ext CURL::libcurl) else() - target_include_directories(http_client_curl + target_include_directories(opentelemetry_http_client_curl INTERFACE "${CURL_INCLUDE_DIRS}") - target_link_libraries(http_client_curl PUBLIC opentelemetry_ext - ${CURL_LIBRARIES}) + target_link_libraries(opentelemetry_http_client_curl + PUBLIC opentelemetry_ext ${CURL_LIBRARIES}) endif() install( - TARGETS http_client_curl + TARGETS opentelemetry_http_client_curl EXPORT "${PROJECT_NAME}-target" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/ext/test/http/CMakeLists.txt b/ext/test/http/CMakeLists.txt index 13b7c8206e..341648085d 100644 --- a/ext/test/http/CMakeLists.txt +++ b/ext/test/http/CMakeLists.txt @@ -7,10 +7,12 @@ if(CURL_FOUND) ${CMAKE_THREAD_LIBS_INIT}) if(TARGET CURL::libcurl) - target_link_libraries(${FILENAME} CURL::libcurl http_client_curl) + target_link_libraries(${FILENAME} opentelemetry_http_client_curl + CURL::libcurl) else() include_directories(${CURL_INCLUDE_DIRS}) - target_link_libraries(${FILENAME} ${CURL_LIBRARIES} http_client_curl) + target_link_libraries(${FILENAME} ${CURL_LIBRARIES} + opentelemetry_http_client_curl) endif() gtest_add_tests( TARGET ${FILENAME} diff --git a/ext/test/w3c_tracecontext_test/CMakeLists.txt b/ext/test/w3c_tracecontext_test/CMakeLists.txt index 30e2f5d0f3..ea74a8eeb0 100644 --- a/ext/test/w3c_tracecontext_test/CMakeLists.txt +++ b/ext/test/w3c_tracecontext_test/CMakeLists.txt @@ -7,9 +7,9 @@ else() add_executable(w3c_tracecontext_test main.cc) target_link_libraries( w3c_tracecontext_test - PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl - opentelemetry_exporter_ostream_span ${CURL_LIBRARIES} - nlohmann_json::nlohmann_json) + PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + opentelemetry_http_client_curl opentelemetry_exporter_ostream_span + ${CURL_LIBRARIES} nlohmann_json::nlohmann_json) if(nlohmann_json_clone) add_dependencies(w3c_tracecontext_test nlohmann_json::nlohmann_json) endif() From 2034c9bcfa9cf6a995891efed36264088a333d37 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Fri, 1 Apr 2022 10:57:31 -0700 Subject: [PATCH 11/19] Don't show coverage annotation for pull requests (#1304) --- .github/.codecov.yaml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/.codecov.yaml b/.github/.codecov.yaml index e2c304a9b4..2b3003ff57 100644 --- a/.github/.codecov.yaml +++ b/.github/.codecov.yaml @@ -12,11 +12,7 @@ coverage: informational: true target: auto threshold: 10% - patch: - default: - informational: true - target: auto - threshold: 10% + patch: false parsers: gcov: From 33d9c628f2e7029b6b92733df69336f7e384c7e4 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 1 Apr 2022 14:07:44 -0700 Subject: [PATCH 12/19] Implement periodic exporting metric reader (#1286) --- exporters/ostream/BUILD | 6 +- .../export/periodic_exporting_metric_reader.h | 72 +++++++++++++ .../sdk/metrics/metric_exporter.h | 9 +- .../opentelemetry/sdk/metrics/metric_reader.h | 1 + sdk/src/metrics/CMakeLists.txt | 1 + .../periodic_exporting_metric_reader.cc | 101 ++++++++++++++++++ sdk/src/metrics/metric_reader.cc | 10 +- sdk/test/metrics/CMakeLists.txt | 3 +- sdk/test/metrics/meter_provider_sdk_test.cc | 3 +- .../periodic_exporting_metric_reader_test.cc | 81 ++++++++++++++ 10 files changed, 272 insertions(+), 15 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h create mode 100644 sdk/src/metrics/export/periodic_exporting_metric_reader.cc create mode 100644 sdk/test/metrics/periodic_exporting_metric_reader_test.cc diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index 19c0f7914d..f74d896344 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -43,7 +43,9 @@ cc_library( ], ) -#TODO MetricData is still changing, uncomment once it is final +# TODO - Uncomment once MetricData interface is finalised +#cc_library( +# name = "ostream_metric_exporter", # srcs = [ # "src/metric_exporter.cc", # ], @@ -70,7 +72,7 @@ cc_library( # deps = [ # ":ostream_metric_exporter", # "@com_google_googletest//:gtest_main", -#], +# ], #) cc_test( 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 new file mode 100644 index 0000000000..29125a6ea2 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -0,0 +1,72 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/metric_reader.h" +# include "opentelemetry/version.h" + +# include +# include +# include +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class MetricExporter; +/** + * 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 +{ + +public: + PeriodicExportingMetricReader( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &option, + AggregationTemporality aggregation_temporality = AggregationTemporality::kCumulative); + +private: + bool OnForceFlush(std::chrono::microseconds timeout) noexcept override; + + bool OnShutDown(std::chrono::microseconds timeout) noexcept override; + + void OnInitialized() noexcept override; + + std::unique_ptr exporter_; + std::chrono::milliseconds export_interval_millis_; + std::chrono::milliseconds export_timeout_millis_; + + void DoBackgroundWork(); + + /* The background worker thread */ + std::thread worker_thread_; + + /* Synchronization primitives */ + std::condition_variable cv_; + std::mutex cv_m_; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h index 3769259472..2488edd7ed 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h @@ -30,11 +30,9 @@ class MetricExporter /** * Exports a batch of metrics recordables. This method must not be called * concurrently for the same exporter instance. - * @param spans a span of unique pointers to metrics data + * @param data metrics data */ - virtual opentelemetry::sdk::common::ExportResult Export( - const nostd::span> - &records) noexcept = 0; + virtual opentelemetry::sdk::common::ExportResult Export(const MetricData &data) noexcept = 0; /** * Force flush the exporter. @@ -49,9 +47,6 @@ class MetricExporter */ virtual bool Shutdown( std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept = 0; - -private: - AggregationTemporality aggregation_temporality_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index 57690ac87c..7cf449e1e3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -57,6 +57,7 @@ class MetricReader virtual void OnInitialized() noexcept {} +protected: bool IsShutdown() const noexcept; private: diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index d8eff48cd5..b6656b5bf8 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -4,6 +4,7 @@ add_library( meter.cc meter_context.cc metric_reader.cc + export/periodic_exporting_metric_reader.cc state/metric_collector.cc state/sync_metric_storage.cc aggregation/histogram_aggregation.cc diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc new file mode 100644 index 0000000000..e9a91a9e06 --- /dev/null +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -0,0 +1,101 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" +# include "opentelemetry/sdk/common/global_log_handler.h" +# include "opentelemetry/sdk/metrics/metric_exporter.h" + +# include +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +PeriodicExportingMetricReader::PeriodicExportingMetricReader( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &option, + AggregationTemporality aggregation_temporality) + : MetricReader(aggregation_temporality), + exporter_{std::move(exporter)}, + export_interval_millis_{option.export_interval_millis}, + export_timeout_millis_{option.export_timeout_millis} +{ + if (export_interval_millis_ <= export_timeout_millis_) + { + OTEL_INTERNAL_LOG_WARN( + "[Periodic Exporting Metric Reader] Invalid configuration: " + "export_interval_millis_ should be less than export_timeout_millis_, using default values"); + export_interval_millis_ = kExportIntervalMillis; + export_timeout_millis_ = kExportTimeOutMillis; + } +} + +void PeriodicExportingMetricReader::OnInitialized() noexcept +{ + worker_thread_ = std::thread(&PeriodicExportingMetricReader::DoBackgroundWork, this); +} + +void PeriodicExportingMetricReader::DoBackgroundWork() +{ + std::unique_lock lk(cv_m_); + do + { + if (IsShutdown()) + { + break; + } + std::atomic cancel_export_for_timeout{false}; + auto start = std::chrono::steady_clock::now(); + auto future_receive = std::async(std::launch::async, [this, &cancel_export_for_timeout] { + Collect([this, &cancel_export_for_timeout](MetricData data) { + if (cancel_export_for_timeout) + { + OTEL_INTERNAL_LOG_ERROR( + "[Periodic Exporting Metric Reader] Collect took longer configured time: " + << export_timeout_millis_.count() << " ms, and timed out"); + return false; + } + this->exporter_->Export(data); + return true; + }); + }); + std::future_status status; + do + { + status = future_receive.wait_for(std::chrono::milliseconds(export_timeout_millis_)); + if (status == std::future_status::timeout) + { + cancel_export_for_timeout = true; + break; + } + } while (status != std::future_status::ready); + auto end = std::chrono::steady_clock::now(); + auto export_time_ms = std::chrono::duration_cast(end - start); + auto remaining_wait_interval_ms = export_interval_millis_ - export_time_ms; + cv_.wait_for(lk, remaining_wait_interval_ms); + } while (true); +} + +bool PeriodicExportingMetricReader::OnForceFlush(std::chrono::microseconds timeout) noexcept +{ + return exporter_->ForceFlush(timeout); +} + +bool PeriodicExportingMetricReader::OnShutDown(std::chrono::microseconds timeout) noexcept +{ + if (worker_thread_.joinable()) + { + cv_.notify_one(); + worker_thread_.join(); + } + return exporter_->Shutdown(timeout); +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/metric_reader.cc b/sdk/src/metrics/metric_reader.cc index 8238ad2a55..abf73d766a 100644 --- a/sdk/src/metrics/metric_reader.cc +++ b/sdk/src/metrics/metric_reader.cc @@ -20,6 +20,7 @@ MetricReader::MetricReader(AggregationTemporality aggregation_temporality) void MetricReader::SetMetricProducer(MetricProducer *metric_producer) { metric_producer_ = metric_producer; + OnInitialized(); } AggregationTemporality MetricReader::GetAggregationTemporality() const noexcept @@ -46,18 +47,21 @@ bool MetricReader::Collect(nostd::function_ref callback) noexc bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept { bool status = true; - if (IsShutdown()) { OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown - Cannot invoke shutdown twice!"); } + + { + const std::lock_guard locked(lock_); + shutdown_ = true; + } + if (!OnShutDown(timeout)) { status = false; OTEL_INTERNAL_LOG_WARN("MetricReader::OnShutDown Shutdown failed. Will not be tried again!"); } - const std::lock_guard locked(lock_); - shutdown_ = true; return status; } diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index fa1f22c73a..faf2f2b49f 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -11,7 +11,8 @@ foreach( observer_result_test sync_instruments_test async_instruments_test - metric_reader_test) + metric_reader_test + periodic_exporting_metric_reader_test) add_executable(${testname} "${testname}.cc") target_link_libraries( ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 6ede67d032..ec0a39b523 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -18,8 +18,7 @@ class MockMetricExporter : public MetricExporter public: MockMetricExporter() = default; - opentelemetry::sdk::common::ExportResult Export( - const opentelemetry::nostd::span> &records) noexcept override + opentelemetry::sdk::common::ExportResult Export(const MetricData &records) noexcept override { return opentelemetry::sdk::common::ExportResult::kSuccess; } diff --git a/sdk/test/metrics/periodic_exporting_metric_reader_test.cc b/sdk/test/metrics/periodic_exporting_metric_reader_test.cc new file mode 100644 index 0000000000..f13fbb0e04 --- /dev/null +++ b/sdk/test/metrics/periodic_exporting_metric_reader_test.cc @@ -0,0 +1,81 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" +# include "opentelemetry/sdk/metrics/export/metric_producer.h" +# include "opentelemetry/sdk/metrics/metric_exporter.h" + +# include + +using namespace opentelemetry; +using namespace opentelemetry::sdk::instrumentationlibrary; +using namespace opentelemetry::sdk::metrics; + +class MockPushMetricExporter : public MetricExporter +{ +public: + opentelemetry::sdk::common::ExportResult Export(const MetricData &record) noexcept override + { + records_.push_back(record); + return opentelemetry::sdk::common::ExportResult::kSuccess; + } + + bool ForceFlush( + std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override + { + return false; + } + + bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override + { + return true; + } + + size_t GetDataCount() { return records_.size(); } + +private: + std::vector records_; +}; + +class MockMetricProducer : public MetricProducer +{ +public: + MockMetricProducer(std::chrono::microseconds sleep_ms = std::chrono::microseconds::zero()) + : sleep_ms_{sleep_ms}, data_sent_size_(0) + {} + + bool Collect(nostd::function_ref callback) noexcept override + { + std::this_thread::sleep_for(sleep_ms_); + data_sent_size_++; + MetricData data; + callback(data); + return true; + } + + size_t GetDataCount() { return data_sent_size_; } + +private: + std::chrono::microseconds sleep_ms_; + size_t data_sent_size_; +}; + +TEST(PeriodicExporingMetricReader, BasicTests) +{ + std::unique_ptr exporter(new MockPushMetricExporter()); + PeriodicExportingMetricReaderOptions options; + options.export_timeout_millis = std::chrono::milliseconds(200); + options.export_interval_millis = std::chrono::milliseconds(500); + auto exporter_ptr = exporter.get(); + PeriodicExportingMetricReader reader(std::move(exporter), options); + MockMetricProducer producer; + reader.SetMetricProducer(&producer); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + reader.Shutdown(); + EXPECT_EQ(static_cast(exporter_ptr)->GetDataCount(), + static_cast(&producer)->GetDataCount()); +} + +#endif \ No newline at end of file From 48a40601061905542a52dd11109a868517a6e304 Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Tue, 5 Apr 2022 04:09:48 +0800 Subject: [PATCH 13/19] Add `async-changes` branch to pull_request of github action (#1309) Signed-off-by: owent --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a851728a2e..c29ee3084f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: push: branches: [ main ] pull_request: - branches: [ main ] + branches: [ main, async-changes ] jobs: cmake_test: From be75bbc862a81319f8f9cae11c14f79f2c776aa9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 4 Apr 2022 15:09:02 -0700 Subject: [PATCH 14/19] Add InstrumentationInfo and Resource to the metrics data to be exported. (#1299) --- .../sdk/metrics/async_instruments.h | 53 ++++++------------- .../sdk/metrics/export/metric_producer.h | 23 +++++++- sdk/include/opentelemetry/sdk/metrics/meter.h | 7 ++- .../sdk/metrics/metric_exporter.h | 2 +- .../opentelemetry/sdk/metrics/metric_reader.h | 4 +- .../sdk/metrics/state/async_metric_storage.h | 9 ++-- .../sdk/metrics/state/metric_collector.h | 2 +- .../sdk/metrics/state/metric_storage.h | 10 ++-- .../sdk/metrics/state/sync_metric_storage.h | 2 +- .../periodic_exporting_metric_reader.cc | 4 +- sdk/src/metrics/meter.cc | 29 +++++----- sdk/src/metrics/metric_reader.cc | 3 +- sdk/src/metrics/state/metric_collector.cc | 11 +++- sdk/src/metrics/state/sync_metric_storage.cc | 2 +- sdk/test/metrics/async_instruments_test.cc | 29 +++++----- sdk/test/metrics/async_metric_storage_test.cc | 2 +- sdk/test/metrics/meter_provider_sdk_test.cc | 2 +- sdk/test/metrics/metric_reader_test.cc | 2 +- .../periodic_exporting_metric_reader_test.cc | 8 +-- 19 files changed, 98 insertions(+), 106 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index f38ad6a3b4..8b1f76377b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -6,7 +6,6 @@ # include "opentelemetry/metrics/async_instruments.h" # include "opentelemetry/metrics/observer_result.h" # include "opentelemetry/nostd/string_view.h" -# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -19,22 +18,14 @@ class Asynchronous { public: Asynchronous(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") - : name_(name), - instrumentation_library_{instrumentation_library}, - callback_(callback), - description_(description), - unit_(unit) + : name_(name), callback_(callback), description_(description), unit_(unit) {} protected: std::string name_; - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library_; void (*callback_)(opentelemetry::metrics::ObserverResult &); std::string description_; std::string unit_; @@ -45,12 +36,10 @@ class LongObservableCounter : public opentelemetry::metrics::ObservableCounter &), nostd::string_view description = "", nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + : Asynchronous(name, callback, description, unit) {} }; @@ -60,12 +49,10 @@ class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter { public: DoubleObservableCounter(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + : Asynchronous(name, callback, description, unit) {} }; @@ -75,12 +62,10 @@ class LongObservableGauge : public opentelemetry::metrics::ObservableGauge { public: LongObservableGauge(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + : Asynchronous(name, callback, description, unit) {} }; @@ -90,12 +75,10 @@ class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge &), nostd::string_view description = "", nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + : Asynchronous(name, callback, description, unit) {} }; @@ -104,14 +87,11 @@ class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpD public Asynchronous { public: - LongObservableUpDownCounter( - nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - void (*callback)(opentelemetry::metrics::ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + LongObservableUpDownCounter(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, callback, description, unit) {} }; @@ -121,14 +101,11 @@ class DoubleObservableUpDownCounter public Asynchronous { public: - DoubleObservableUpDownCounter( - nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - void (*callback)(opentelemetry::metrics::ObserverResult &), - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, instrumentation_library, callback, description, unit) + DoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, callback, description, unit) {} }; diff --git a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h index 7c9ee57755..d3b38759ad 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h @@ -3,13 +3,33 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/sdk/resource/resource.h" + OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { +/** + * Metric Data to be exported along with resources and + * Instrumentation library. + */ +struct InstrumentationInfoMetrics +{ + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library_; + std::vector metric_data_; +}; + +struct ResourceMetrics +{ + const opentelemetry::sdk::resource::Resource *resource_; + std::vector instrumentation_info_metric_data_; +}; + /** * MetricProducer is the interface that is used to make metric data available to the * OpenTelemetry exporters. Implementations should be stateful, in that each call to @@ -27,7 +47,8 @@ class MetricProducer * * @return a status of completion of method. */ - virtual bool Collect(nostd::function_ref callback) noexcept = 0; + virtual bool Collect( + nostd::function_ref callback) noexcept = 0; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index c9e02918a0..4a6ea26aeb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -102,10 +102,9 @@ class Meter final : public opentelemetry::metrics::Meter const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() const noexcept; - /** collect metrics across all the meters **/ - bool Collect(CollectorHandle *collector, - opentelemetry::common::SystemTimestamp collect_ts, - nostd::function_ref callback) noexcept; + /** collect metrics across all the instruments configured for the meter **/ + std::vector Collect(CollectorHandle *collector, + opentelemetry::common::SystemTimestamp collect_ts) noexcept; private: // order of declaration is important here - instrumentation library should destroy after diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h index 2488edd7ed..b6b5acf3a9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h @@ -32,7 +32,7 @@ class MetricExporter * concurrently for the same exporter instance. * @param data metrics data */ - virtual opentelemetry::sdk::common::ExportResult Export(const MetricData &data) noexcept = 0; + virtual opentelemetry::sdk::common::ExportResult Export(const ResourceMetrics &data) noexcept = 0; /** * Force flush the exporter. diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index 7cf449e1e3..94924315f8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -6,6 +6,7 @@ # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/sdk/common/global_log_handler.h" # include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/sdk/metrics/export/metric_producer.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" @@ -18,7 +19,6 @@ namespace sdk namespace metrics { -class MetricProducer; /** * MetricReader defines the interface to collect metrics from SDK */ @@ -34,7 +34,7 @@ class MetricReader * Collect the metrics from SDK. * @return return the status of the operation. */ - bool Collect(nostd::function_ref callback) noexcept; + bool Collect(nostd::function_ref callback) noexcept; AggregationTemporality GetAggregationTemporality() const noexcept; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 31ab70c5b8..cfbf521538 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -4,16 +4,15 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/common/attributemap_hash.h" -# include "opentelemetry/sdk/metrics/instruments.h" - # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/observer_result.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include "opentelemetry/sdk/metrics/state/metric_collector.h" # include "opentelemetry/sdk/metrics/state/metric_storage.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" # include -# include "opentelemetry/sdk/metrics/observer_result.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -40,7 +39,7 @@ class AsyncMetricStorage : public MetricStorage nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref metric_collection_callback) noexcept override + nostd::function_ref metric_collection_callback) noexcept override { opentelemetry::sdk::metrics::ObserverResult ob_res(attributes_processor_); @@ -57,7 +56,7 @@ class AsyncMetricStorage : public MetricStorage // TBD -> read aggregation from hashmap, and perform metric collection MetricData metric_data; - if (metric_collection_callback(metric_data)) + if (metric_collection_callback(std::move(metric_data))) { return true; } diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h index 51c9cf6eff..20372f5209 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h @@ -41,7 +41,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle * * @return a status of completion of method. */ - bool Collect(nostd::function_ref callback) noexcept override; + bool Collect(nostd::function_ref callback) noexcept override; bool ForceFlush(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index 20f7d56140..e0ba55cbfe 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -25,7 +25,7 @@ class MetricStorage nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref callback) noexcept = 0; + nostd::function_ref callback) noexcept = 0; }; class WritableMetricStorage @@ -54,14 +54,10 @@ class NoopMetricStorage : public MetricStorage nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref callback) noexcept override + nostd::function_ref callback) noexcept override { MetricData metric_data; - if (callback(metric_data)) - { - return true; - } - return false; + return callback(std::move(metric_data)); } }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h index 8269d2fd3c..c16f33ede2 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -106,7 +106,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref callback) noexcept override; + nostd::function_ref callback) noexcept override; private: InstrumentDescriptor instrument_descriptor_; diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index e9a91a9e06..f11f84544f 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -51,7 +51,7 @@ void PeriodicExportingMetricReader::DoBackgroundWork() std::atomic cancel_export_for_timeout{false}; auto start = std::chrono::steady_clock::now(); auto future_receive = std::async(std::launch::async, [this, &cancel_export_for_timeout] { - Collect([this, &cancel_export_for_timeout](MetricData data) { + Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { if (cancel_export_for_timeout) { OTEL_INTERNAL_LOG_ERROR( @@ -59,7 +59,7 @@ void PeriodicExportingMetricReader::DoBackgroundWork() << export_timeout_millis_.count() << " ms, and timed out"); return false; } - this->exporter_->Export(data); + this->exporter_->Export(metric_data); return true; }); }); diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index fb74d55eb4..e7ca822b6d 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -64,7 +64,7 @@ nostd::shared_ptr> Meter::CreateLongObservableC nostd::string_view unit) noexcept { return nostd::shared_ptr>{ - new LongObservableCounter(name, GetInstrumentationLibrary(), callback, description, unit)}; + new LongObservableCounter(name, callback, description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleObservableCounter( @@ -74,7 +74,7 @@ nostd::shared_ptr> Meter::CreateDoubleObserva nostd::string_view unit) noexcept { return nostd::shared_ptr>{ - new DoubleObservableCounter(name, GetInstrumentationLibrary(), callback, description, unit)}; + new DoubleObservableCounter(name, callback, description, unit)}; } nostd::shared_ptr> Meter::CreateLongHistogram( @@ -112,7 +112,7 @@ nostd::shared_ptr> Meter::CreateLongObservableGau nostd::string_view unit) noexcept { return nostd::shared_ptr>{ - new LongObservableGauge(name, GetInstrumentationLibrary(), callback, description, unit)}; + new LongObservableGauge(name, callback, description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleObservableGauge( @@ -122,7 +122,7 @@ nostd::shared_ptr> Meter::CreateDoubleObservabl nostd::string_view unit) noexcept { return nostd::shared_ptr>{ - new DoubleObservableGauge(name, GetInstrumentationLibrary(), callback, description, unit)}; + new DoubleObservableGauge(name, callback, description, unit)}; } nostd::shared_ptr> Meter::CreateLongUpDownCounter( @@ -159,8 +159,8 @@ nostd::shared_ptr> Meter::CreateLongObser nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new LongObservableUpDownCounter( - name, GetInstrumentationLibrary(), callback, description, unit)}; + return nostd::shared_ptr>{ + new LongObservableUpDownCounter(name, callback, description, unit)}; } nostd::shared_ptr> @@ -170,8 +170,7 @@ Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, nostd::string_view unit) noexcept { return nostd::shared_ptr>{ - new DoubleObservableUpDownCounter(name, GetInstrumentationLibrary(), callback, description, - unit)}; + new DoubleObservableUpDownCounter(name, callback, description, unit)}; } const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() @@ -211,22 +210,20 @@ std::unique_ptr Meter::RegisterMetricStorage( } /** collect metrics across all the meters **/ -bool Meter::Collect(CollectorHandle *collector, - opentelemetry::common::SystemTimestamp collect_ts, - nostd::function_ref callback) noexcept +std::vector Meter::Collect(CollectorHandle *collector, + opentelemetry::common::SystemTimestamp collect_ts) noexcept { - std::vector data; + std::vector metric_data_list; for (auto &metric_storage : storage_registry_) { - // TBD - this needs to be asynchronous metric_storage.second->Collect(collector, meter_context_->GetCollectors(), meter_context_->GetSDKStartTime(), collect_ts, - [&callback](MetricData &metric_data) { - callback(metric_data); + [&metric_data_list](MetricData metric_data) { + metric_data_list.push_back(metric_data); return true; }); } - return true; + return metric_data_list; } } // namespace metrics diff --git a/sdk/src/metrics/metric_reader.cc b/sdk/src/metrics/metric_reader.cc index abf73d766a..eafcc60e7e 100644 --- a/sdk/src/metrics/metric_reader.cc +++ b/sdk/src/metrics/metric_reader.cc @@ -28,7 +28,8 @@ AggregationTemporality MetricReader::GetAggregationTemporality() const noexcept return aggregation_temporality_; } -bool MetricReader::Collect(nostd::function_ref callback) noexcept +bool MetricReader::Collect( + nostd::function_ref callback) noexcept { if (!metric_producer_) { diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index cd3b8eb6ca..6cae7bf12c 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -29,13 +29,20 @@ AggregationTemporality MetricCollector::GetAggregationTemporality() noexcept return metric_reader_->GetAggregationTemporality(); } -bool MetricCollector::Collect(nostd::function_ref callback) noexcept +bool MetricCollector::Collect( + nostd::function_ref callback) noexcept { + ResourceMetrics resource_metrics; for (auto &meter : meter_context_->GetMeters()) { auto collection_ts = std::chrono::system_clock::now(); - meter->Collect(this, collection_ts, callback); + InstrumentationInfoMetrics instrumentation_info_metrics; + instrumentation_info_metrics.metric_data_ = meter->Collect(this, collection_ts); + instrumentation_info_metrics.instrumentation_library_ = meter->GetInstrumentationLibrary(); + resource_metrics.instrumentation_info_metric_data_.push_back(instrumentation_info_metrics); } + resource_metrics.resource_ = &meter_context_->GetResource(); + callback(resource_metrics); return true; } diff --git a/sdk/src/metrics/state/sync_metric_storage.cc b/sdk/src/metrics/state/sync_metric_storage.cc index 5c14f8bbc3..f42de82b4b 100644 --- a/sdk/src/metrics/state/sync_metric_storage.cc +++ b/sdk/src/metrics/state/sync_metric_storage.cc @@ -15,7 +15,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, nostd::span> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, - nostd::function_ref callback) noexcept + nostd::function_ref callback) noexcept { opentelemetry::common::SystemTimestamp last_collection_ts = sdk_start_ts; auto aggregation_temporarily = collector->GetAggregationTemporality(); diff --git a/sdk/test/metrics/async_instruments_test.cc b/sdk/test/metrics/async_instruments_test.cc index ad3a81b031..ff9504f78c 100644 --- a/sdk/test/metrics/async_instruments_test.cc +++ b/sdk/test/metrics/async_instruments_test.cc @@ -3,16 +3,12 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/async_instruments.h" -# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include using namespace opentelemetry; -using namespace opentelemetry::sdk::instrumentationlibrary; using namespace opentelemetry::sdk::metrics; -auto instrumentation_library = InstrumentationLibrary::Create("opentelemetry-cpp", "0.1.0"); - using M = std::map; void asyc_generate_measurements_long(opentelemetry::metrics::ObserverResult &observer) {} @@ -22,44 +18,43 @@ void asyc_generate_measurements_double(opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW(LongObservableCounter counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_long, "description", "1")); + EXPECT_NO_THROW( + LongObservableCounter counter("long_counter", asyc_generate_meas_long, "description", "1")); } TEST(AsyncInstruments, DoubleObservableCounter) { auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW(DoubleObservableCounter counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_double, "description", "1")); + EXPECT_NO_THROW(DoubleObservableCounter counter("long_counter", asyc_generate_meas_double, + "description", "1")); } TEST(AsyncInstruments, LongObservableGauge) { auto asyc_generate_meas_long = [](opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW(LongObservableGauge counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_long, "description", "1")); + EXPECT_NO_THROW( + LongObservableGauge counter("long_counter", asyc_generate_meas_long, "description", "1")); } TEST(AsyncInstruments, DoubleObservableGauge) { auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW(DoubleObservableGauge counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_double, "description", "1")); + EXPECT_NO_THROW( + DoubleObservableGauge counter("long_counter", asyc_generate_meas_double, "description", "1")); } TEST(AsyncInstruments, LongObservableUpDownCounter) { auto asyc_generate_meas_long = [](opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW(LongObservableUpDownCounter counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_long, "description", "1")); + EXPECT_NO_THROW(LongObservableUpDownCounter counter("long_counter", asyc_generate_meas_long, + "description", "1")); } TEST(AsyncInstruments, DoubleObservableUpDownCounter) { auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; - EXPECT_NO_THROW( - DoubleObservableUpDownCounter counter("long_counter", instrumentation_library.get(), - asyc_generate_meas_double, "description", "1")); + EXPECT_NO_THROW(DoubleObservableUpDownCounter counter("long_counter", asyc_generate_meas_double, + "description", "1")); } #endif diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index f7c9914e3c..512e24472c 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -38,7 +38,7 @@ void measurement_fetch(opentelemetry::metrics::ObserverResult &observer_re TEST(AsyncMetricStorageTest, BasicTests) { - auto metric_callback = [](MetricData &metric_data) { return true; }; + auto metric_callback = [](MetricData &&metric_data) { return true; }; InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index ec0a39b523..3c059cc44c 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -18,7 +18,7 @@ class MockMetricExporter : public MetricExporter public: MockMetricExporter() = default; - opentelemetry::sdk::common::ExportResult Export(const MetricData &records) noexcept override + opentelemetry::sdk::common::ExportResult Export(const ResourceMetrics &records) noexcept override { return opentelemetry::sdk::common::ExportResult::kSuccess; } diff --git a/sdk/test/metrics/metric_reader_test.cc b/sdk/test/metrics/metric_reader_test.cc index 68a3fcc5f5..61a4364934 100644 --- a/sdk/test/metrics/metric_reader_test.cc +++ b/sdk/test/metrics/metric_reader_test.cc @@ -35,6 +35,6 @@ TEST(MetricReaderTest, BasicTests) std::shared_ptr meter_context2(new MeterContext(std::move(exporters))); MetricProducer *metric_producer = new MetricCollector(std::move(meter_context2), std::move(metric_reader2)); - EXPECT_NO_THROW(metric_producer->Collect([](MetricData data) { return true; })); + EXPECT_NO_THROW(metric_producer->Collect([](ResourceMetrics &metric_data) { return true; })); } #endif diff --git a/sdk/test/metrics/periodic_exporting_metric_reader_test.cc b/sdk/test/metrics/periodic_exporting_metric_reader_test.cc index f13fbb0e04..5219f31100 100644 --- a/sdk/test/metrics/periodic_exporting_metric_reader_test.cc +++ b/sdk/test/metrics/periodic_exporting_metric_reader_test.cc @@ -16,7 +16,7 @@ using namespace opentelemetry::sdk::metrics; class MockPushMetricExporter : public MetricExporter { public: - opentelemetry::sdk::common::ExportResult Export(const MetricData &record) noexcept override + opentelemetry::sdk::common::ExportResult Export(const ResourceMetrics &record) noexcept override { records_.push_back(record); return opentelemetry::sdk::common::ExportResult::kSuccess; @@ -36,7 +36,7 @@ class MockPushMetricExporter : public MetricExporter size_t GetDataCount() { return records_.size(); } private: - std::vector records_; + std::vector records_; }; class MockMetricProducer : public MetricProducer @@ -46,11 +46,11 @@ class MockMetricProducer : public MetricProducer : sleep_ms_{sleep_ms}, data_sent_size_(0) {} - bool Collect(nostd::function_ref callback) noexcept override + bool Collect(nostd::function_ref callback) noexcept override { std::this_thread::sleep_for(sleep_ms_); data_sent_size_++; - MetricData data; + ResourceMetrics data; callback(data); return true; } From 237a0b26372b8a75ae941272b78bc07f12692c7b Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Mon, 4 Apr 2022 20:41:02 -0700 Subject: [PATCH 15/19] Excempt should be applied on issue instead of PR (#1316) --- .github/workflows/stale.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 53ebe3259b..e55a93d08d 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -13,4 +13,4 @@ jobs: close-issue-message: 'Closed as inactive. Feel free to reopen if this is still an issue.' days-before-issue-stale: 60 days-before-issue-close: 7 - exempt-pr-labels: 'do-not-stale' + exempt-issue-labels: 'do-not-stale' From fd338cc9f96cd2ad2af7b3bed496c10c22098582 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 5 Apr 2022 23:05:14 -0700 Subject: [PATCH 16/19] Bump codecov/codecov-action from 2.1.0 to 3 (#1318) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c29ee3084f..3b0af15acb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -375,7 +375,7 @@ jobs: - name: run tests and generate report run: ./ci/do_ci.sh code.coverage - name: upload report - uses: codecov/codecov-action@v2.1.0 + uses: codecov/codecov-action@v3 with: file: /home/runner/build/coverage.info From 079dd3e7464a1fe3a86045f4793da26a4fd87122 Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 28 Apr 2022 21:57:43 +0800 Subject: [PATCH 17/19] Switch to multi_handle for `curl::HttpClient` Signed-off-by: owent --- CHANGELOG.md | 2 + exporters/ostream/BUILD | 59 +- .../ext/http/client/curl/http_client_curl.h | 191 +++-- .../http/client/curl/http_operation_curl.h | 530 ++++---------- ext/src/http/client/curl/BUILD | 1 + ext/src/http/client/curl/CMakeLists.txt | 5 +- ext/src/http/client/curl/http_client_curl.cc | 564 ++++++++++++++- .../http/client/curl/http_operation_curl.cc | 684 ++++++++++++++++++ ext/test/http/curl_http_test.cc | 224 +++++- 9 files changed, 1747 insertions(+), 513 deletions(-) create mode 100644 ext/src/http/client/curl/http_operation_curl.cc diff --git a/CHANGELOG.md b/CHANGELOG.md index b7d0597672..2cb294e721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ Increment the: * [SDK] Async Batch Span/Log processor with max async support ([#1306](https://github.com/open-telemetry/opentelemetry-cpp/pull/1306)) * [EXPORTER] OTLP http exporter allow concurrency session ([#1209](https://github.com/open-telemetry/opentelemetry-cpp/pull/1209)) +* [EXT] `curl::HttpClient` use `curl_multi_handle` instead of creating a thread + for every request and it's able to reuse connections now. ([#1317](https://github.com/open-telemetry/opentelemetry-cpp/pull/1317)) ## [1.3.0] 2022-04-11 diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index f74d896344..cca74d6693 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -43,37 +43,36 @@ cc_library( ], ) -# TODO - Uncomment once MetricData interface is finalised -#cc_library( -# name = "ostream_metric_exporter", -# srcs = [ -# "src/metric_exporter.cc", -# ], -# hdrs = [ -# "include/opentelemetry/exporters/ostream/metric_exporter.h", -# ], -# strip_include_prefix = "include", -# tags = [ -# "metrics", -# "ostream", -# ], -# deps = [ -# "//sdk/src/metrics", -# ], -#) +cc_library( + name = "ostream_metric_exporter", + srcs = [ + "src/metric_exporter.cc", + ], + hdrs = [ + "include/opentelemetry/exporters/ostream/metric_exporter.h", + ], + strip_include_prefix = "include", + tags = [ + "metrics", + "ostream", + ], + deps = [ + "//sdk/src/metrics", + ], +) -#cc_test( -# name = "ostream_metric_test", -# srcs = ["test/ostream_metric_test.cc"], -# tags = [ -# "ostream", -# "test", -# ], -# deps = [ -# ":ostream_metric_exporter", -# "@com_google_googletest//:gtest_main", -# ], -#) +cc_test( + name = "ostream_metric_test", + srcs = ["test/ostream_metric_test.cc"], + tags = [ + "ostream", + "test", + ], + deps = [ + ":ostream_metric_exporter", + "@com_google_googletest//:gtest_main", + ], +) cc_test( name = "ostream_metrics_test_deprecated", diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index ac263193bc..fa48a068a1 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -3,13 +3,19 @@ #pragma once -#include "http_operation_curl.h" +#include "opentelemetry/ext/http/client/curl/http_operation_curl.h" #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/ext/http/common/url_parser.h" +#include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/version.h" -#include +#include +#include +#include #include +#include +#include +#include #include OPENTELEMETRY_BEGIN_NAMESPACE @@ -24,6 +30,23 @@ namespace curl const opentelemetry::ext::http::client::StatusCode Http_Ok = 200; +class HttpCurlGlobalInitializer +{ +private: + HttpCurlGlobalInitializer(const HttpCurlGlobalInitializer &) = delete; + HttpCurlGlobalInitializer(HttpCurlGlobalInitializer &&) = delete; + + HttpCurlGlobalInitializer &operator=(const HttpCurlGlobalInitializer &) = delete; + HttpCurlGlobalInitializer &operator=(HttpCurlGlobalInitializer &&) = delete; + + HttpCurlGlobalInitializer(); + +public: + ~HttpCurlGlobalInitializer(); + + static nostd::shared_ptr GetInstance(); +}; + class Request : public opentelemetry::ext::http::client::Request { public: @@ -124,7 +147,8 @@ class Response : public opentelemetry::ext::http::client::Response class HttpClient; -class Session : public opentelemetry::ext::http::client::Session +class Session : public opentelemetry::ext::http::client::Session, + public std::enable_shared_from_this { public: Session(HttpClient &http_client, @@ -143,39 +167,16 @@ class Session : public opentelemetry::ext::http::client::Session } virtual void SendRequest( - std::shared_ptr callback) noexcept override - { - is_session_active_ = true; - std::string url = host_ + std::string(http_request_->uri_); - auto callback_ptr = callback.get(); - curl_operation_.reset(new HttpOperation( - http_request_->method_, url, callback_ptr, RequestMode::Async, http_request_->headers_, - http_request_->body_, false, http_request_->timeout_ms_)); - curl_operation_->SendAsync([this, callback](HttpOperation &operation) { - if (operation.WasAborted()) - { - // Manually cancelled - callback->OnEvent(opentelemetry::ext::http::client::SessionState::Cancelled, ""); - } - - if (operation.GetResponseCode() >= CURL_LAST) - { - // we have a http response - auto response = std::unique_ptr(new Response()); - response->headers_ = operation.GetResponseHeaders(); - response->body_ = operation.GetResponseBody(); - response->status_code_ = operation.GetResponseCode(); - callback->OnResponse(*response); - } - is_session_active_ = false; - }); - } + std::shared_ptr callback) noexcept override; virtual bool CancelSession() noexcept override; virtual bool FinishSession() noexcept override; - virtual bool IsSessionActive() noexcept override { return is_session_active_; } + virtual bool IsSessionActive() noexcept override + { + return is_session_active_.load(std::memory_order_acquire); + } void SetId(uint64_t session_id) { session_id_ = session_id; } @@ -188,19 +189,36 @@ class Session : public opentelemetry::ext::http::client::Session #ifdef ENABLE_TEST std::shared_ptr GetRequest() { return http_request_; } #endif + + inline HttpClient &GetHttpClient() noexcept { return http_client_; } + inline const HttpClient &GetHttpClient() const noexcept { return http_client_; } + + inline uint64_t GetSessionId() const noexcept { return session_id_; } + + inline const std::unique_ptr &GetOperation() const noexcept + { + return curl_operation_; + } + inline std::unique_ptr &GetOperation() noexcept { return curl_operation_; } + + /** + * Finish and cleanup the operation.It will remove curl easy handle in it from HttpClient + */ + void FinishOperation(); + private: std::shared_ptr http_request_; std::string host_; std::unique_ptr curl_operation_; uint64_t session_id_; HttpClient &http_client_; - bool is_session_active_; + std::atomic is_session_active_; }; class HttpClientSync : public opentelemetry::ext::http::client::HttpClientSync { public: - HttpClientSync() { curl_global_init(CURL_GLOBAL_ALL); } + HttpClientSync() : curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) {} opentelemetry::ext::http::client::Result Get( const nostd::string_view &url, @@ -208,7 +226,7 @@ class HttpClientSync : public opentelemetry::ext::http::client::HttpClientSync { opentelemetry::ext::http::client::Body body; HttpOperation curl_operation(opentelemetry::ext::http::client::Method::Get, url.data(), nullptr, - RequestMode::Sync, headers, body); + headers, body); curl_operation.SendSync(); auto session_state = curl_operation.GetSessionState(); if (curl_operation.WasAborted()) @@ -233,7 +251,7 @@ class HttpClientSync : public opentelemetry::ext::http::client::HttpClientSync const opentelemetry::ext::http::client::Headers &headers) noexcept override { HttpOperation curl_operation(opentelemetry::ext::http::client::Method::Post, url.data(), - nullptr, RequestMode::Sync, headers, body); + nullptr, headers, body); curl_operation.SendSync(); auto session_state = curl_operation.GetSessionState(); if (curl_operation.WasAborted()) @@ -253,72 +271,87 @@ class HttpClientSync : public opentelemetry::ext::http::client::HttpClientSync return opentelemetry::ext::http::client::Result(std::move(response), session_state); } - ~HttpClientSync() { curl_global_cleanup(); } + ~HttpClientSync() {} + +private: + nostd::shared_ptr curl_global_initializer_; }; class HttpClient : public opentelemetry::ext::http::client::HttpClient { public: // The call (curl_global_init) is not thread safe. Ensure this is called only once. - HttpClient() : next_session_id_{0} { curl_global_init(CURL_GLOBAL_ALL); } + HttpClient(); + ~HttpClient(); std::shared_ptr CreateSession( - nostd::string_view url) noexcept override + nostd::string_view url) noexcept override; + + bool CancelAllSessions() noexcept override; + + bool FinishAllSessions() noexcept override; + + void CleanupSession(uint64_t session_id); + + inline CURLM *GetMultiHandle() noexcept { return multi_handle_; } + + inline void SetMaxSessionsPerConnection(uint64_t max_sessions_per_connection) noexcept { - auto parsedUrl = common::UrlParser(std::string(url)); - if (!parsedUrl.success_) - { - return std::make_shared(*this); - } - auto session = - std::make_shared(*this, parsedUrl.scheme_, parsedUrl.host_, parsedUrl.port_); - auto session_id = ++next_session_id_; - session->SetId(session_id); - sessions_.insert({session_id, session}); - return session; + max_sessions_per_connection_ = max_sessions_per_connection; } - bool CancelAllSessions() noexcept override + inline uint64_t GetMaxSessionsPerConnection() const noexcept { - // CancelSession may change sessions_, we can not change a container while iterating it. - while (!sessions_.empty()) - { - std::map> sessions; - sessions.swap(sessions_); - for (auto &session : sessions) - { - session.second->CancelSession(); - } - } - return true; + return max_sessions_per_connection_; } - bool FinishAllSessions() noexcept override + void MaybeSpawnBackgroundThread(); + + void ScheduleAddSession(uint64_t session_id); + void ScheduleAbortSession(uint64_t session_id); + void ScheduleRemoveSession(uint64_t session_id, HttpCurlEasyResource &&resource); + +#ifdef ENABLE_TEST + void WaitBackgroundThreadExit() { - // FinishSession may change sessions_, we can not change a container while iterating it. - while (!sessions_.empty()) + std::unique_ptr background_thread; { - std::map> sessions; - sessions.swap(sessions_); - for (auto &session : sessions) - { - session.second->FinishSession(); - } + std::lock_guard lock_guard{background_thread_m_}; + background_thread.swap(background_thread_); } - return true; - } - void CleanupSession(uint64_t session_id) - { - // TBD = Need to be thread safe - sessions_.erase(session_id); + if (background_thread && background_thread->joinable()) + { + background_thread->join(); + } } - - ~HttpClient() { curl_global_cleanup(); } +#endif private: + void wakeupBackgroundThread(); + bool doAddSessions(); + bool doAbortSessions(); + bool doRemoveSessions(); + void resetMultiHandle(); + + std::mutex multi_handle_m_; + CURLM *multi_handle_; std::atomic next_session_id_; - std::map> sessions_; + uint64_t max_sessions_per_connection_; + + std::mutex sessions_m_; + std::recursive_mutex session_ids_m_; + std::unordered_map> sessions_; + std::unordered_set pending_to_add_session_ids_; + std::unordered_set pending_to_abort_session_ids_; + std::unordered_map pending_to_remove_session_handles_; + std::list> pending_to_remove_sessions_; + + std::mutex background_thread_m_; + std::unique_ptr background_thread_; + std::chrono::milliseconds scheduled_delay_milliseconds_; + + nostd::shared_ptr curl_global_initializer_; }; } // namespace curl diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h index 09cf8c7c2a..393d6393ed 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h @@ -3,7 +3,6 @@ #pragma once -#include "http_client_curl.h" #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/version.h" @@ -12,6 +11,7 @@ #include #include #include +#include #include #ifdef _WIN32 # include @@ -34,29 +34,89 @@ const std::chrono::milliseconds default_http_conn_timeout(5000); // ms const std::string http_status_regexp = "HTTP\\/\\d\\.\\d (\\d+)\\ .*"; const std::string http_header_regexp = "(.*)\\: (.*)\\n*"; -enum class RequestMode +class HttpClient; +class Session; + +struct HttpCurlEasyResource { - Sync, - Async + CURL *easy_handle; + curl_slist *headers_chunk; + + HttpCurlEasyResource(CURL *curl = nullptr, curl_slist *headers = nullptr) + : easy_handle{curl}, headers_chunk{headers} + {} + + HttpCurlEasyResource(HttpCurlEasyResource &&other) + : easy_handle{other.easy_handle}, headers_chunk{other.headers_chunk} + { + other.easy_handle = nullptr; + other.headers_chunk = nullptr; + } + + HttpCurlEasyResource &operator=(HttpCurlEasyResource &&other) + { + using std::swap; + swap(easy_handle, other.easy_handle); + swap(headers_chunk, other.headers_chunk); + + return *this; + } + + HttpCurlEasyResource(const HttpCurlEasyResource &other) = delete; + HttpCurlEasyResource &operator=(const HttpCurlEasyResource &other) = delete; }; class HttpOperation { -public: - void DispatchEvent(opentelemetry::ext::http::client::SessionState type, std::string reason = "") - { - if (request_mode_ == RequestMode::Async && callback_ != nullptr) - { - callback_->OnEvent(type, reason); - } - else - { - session_state_ = type; - } - } +private: + /** + * Old-school memory allocator + * + * @param contents + * @param size + * @param nmemb + * @param userp + * @return + */ + static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp); - std::atomic is_aborted_; // Set to 'true' when async callback is aborted - std::atomic is_finished_; // Set to 'true' when async callback is finished. + /** + * C++ STL std::vector allocator + * + * @param ptr + * @param size + * @param nmemb + * @param data + * @return + */ + static size_t WriteVectorHeaderCallback(void *ptr, size_t size, size_t nmemb, void *userp); + static size_t WriteVectorBodyCallback(void *ptr, size_t size, size_t nmemb, void *userp); + + static size_t ReadMemoryCallback(char *buffer, size_t size, size_t nitems, void *userp); + +#if LIBCURL_VERSION_NUM >= 0x075000 + static int PreRequestCallback(void *clientp, + char *conn_primary_ip, + char *conn_local_ip, + int conn_primary_port, + int conn_local_port); +#endif + +#if LIBCURL_VERSION_NUM >= 0x072000 + static int OnProgressCallback(void *clientp, + curl_off_t dltotal, + curl_off_t dlnow, + curl_off_t ultotal, + curl_off_t ulnow); +#else + static int OnProgressCallback(void *clientp, + double dltotal, + double dlnow, + double ultotal, + double ulnow); +#endif +public: + void DispatchEvent(opentelemetry::ext::http::client::SessionState type, std::string reason = ""); /** * Create local CURL instance for url and body @@ -71,8 +131,7 @@ class HttpOperation */ HttpOperation(opentelemetry::ext::http::client::Method method, std::string url, - opentelemetry::ext::http::client::EventHandler *callback, - RequestMode request_mode = RequestMode::Async, + opentelemetry::ext::http::client::EventHandler *event_handle, // Default empty headers and empty request body const opentelemetry::ext::http::client::Headers &request_headers = opentelemetry::ext::http::client::Headers(), @@ -80,238 +139,52 @@ class HttpOperation opentelemetry::ext::http::client::Body(), // Default connectivity and response size options bool is_raw_response = false, - std::chrono::milliseconds http_conn_timeout = default_http_conn_timeout) - : is_aborted_(false), - is_finished_(false), - // Optional connection params - is_raw_response_(is_raw_response), - http_conn_timeout_(http_conn_timeout), - request_mode_(request_mode), - curl_(nullptr), - // Result - res_(CURLE_OK), - callback_(callback), - method_(method), - url_(url), - // Local vars - request_headers_(request_headers), - request_body_(request_body), - sockfd_(0), - nread_(0) - { - /* get a curl handle */ - curl_ = curl_easy_init(); - if (!curl_) - { - res_ = CURLE_FAILED_INIT; - DispatchEvent(opentelemetry::ext::http::client::SessionState::CreateFailed); - return; - } - - curl_easy_setopt(curl_, CURLOPT_VERBOSE, 0); - - // Specify target URL - curl_easy_setopt(curl_, CURLOPT_URL, url_.c_str()); - - // TODO: support ssl cert verification for https request - curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, 0); // 1L - curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, 0); // 2L - - // Specify our custom headers - for (auto &kv : this->request_headers_) - { - std::string header = std::string(kv.first); - header += ": "; - header += std::string(kv.second); - headers_chunk_ = curl_slist_append(headers_chunk_, header.c_str()); - } - - if (headers_chunk_ != nullptr) - { - curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers_chunk_); - } - - DispatchEvent(opentelemetry::ext::http::client::SessionState::Created); - } + std::chrono::milliseconds http_conn_timeout = default_http_conn_timeout, + bool reuse_connection = false); /** * Destroy CURL instance */ - virtual ~HttpOperation() - { - // Given the request has not been aborted we should wait for completion here - // This guarantees the lifetime of this request. - if (result_.valid()) - { - result_.wait(); - } - // TBD - Need to be uncomment. This will callback instance is deleted. - // DispatchEvent(opentelemetry::ext::http::client::SessionState::Destroy); - res_ = CURLE_OK; - curl_easy_cleanup(curl_); - curl_slist_free_all(headers_chunk_); - ReleaseResponse(); - } + virtual ~HttpOperation(); /** * Finish CURL instance */ - virtual void Finish() - { - if (result_.valid() && !is_finished_) - { - result_.wait(); - is_finished_ = true; - } - } + virtual void Finish(); + + /** + * Cleanup all resource of curl + */ + void Cleanup(); + + /** + * Setup request + */ + CURLcode Setup(); /** * Send request synchronously */ - long Send() - { - ReleaseResponse(); - // Request buffer - const void *request = (request_body_.empty()) ? NULL : &request_body_[0]; - const size_t req_size = request_body_.size(); - if (!curl_) - { - res_ = CURLE_FAILED_INIT; - DispatchEvent(opentelemetry::ext::http::client::SessionState::SendFailed); - return res_; - } - - // TODO: control local port to use - // curl_easy_setopt(curl, CURLOPT_LOCALPORT, dcf_port); - - // Perform initial connect, handling the timeout if needed - curl_easy_setopt(curl_, CURLOPT_CONNECT_ONLY, 1L); - curl_easy_setopt(curl_, CURLOPT_TIMEOUT_MS, http_conn_timeout_.count()); - DispatchEvent(opentelemetry::ext::http::client::SessionState::Connecting); - res_ = curl_easy_perform(curl_); - if (CURLE_OK != res_) - { - DispatchEvent(opentelemetry::ext::http::client::SessionState::ConnectFailed, - curl_easy_strerror(res_)); // couldn't connect - stage 1 - return res_; - } - - /* Extract the socket from the curl handle - we'll need it for waiting. - * Note that this API takes a pointer to a 'long' while we use - * curl_socket_t for sockets otherwise. - */ - long sockextr = 0; - res_ = curl_easy_getinfo(curl_, CURLINFO_LASTSOCKET, &sockextr); - - if (CURLE_OK != res_) - { - DispatchEvent(opentelemetry::ext::http::client::SessionState::ConnectFailed, - curl_easy_strerror(res_)); // couldn't connect - stage 2 - return res_; - } - - /* wait for the socket to become ready for sending */ - sockfd_ = sockextr; - if (!WaitOnSocket(sockfd_, 0, static_cast(http_conn_timeout_.count())) || is_aborted_) - { - res_ = CURLE_OPERATION_TIMEDOUT; - DispatchEvent( - opentelemetry::ext::http::client::SessionState::ConnectFailed, - " Is aborted: " + std::to_string(is_aborted_.load())); // couldn't connect - stage 3 - return res_; - } - - DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); - // once connection is there - switch back to easy perform for HTTP post - curl_easy_setopt(curl_, CURLOPT_CONNECT_ONLY, 0); - - // send all data to our callback function - if (is_raw_response_) - { - curl_easy_setopt(curl_, CURLOPT_HEADER, true); - curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, (void *)&WriteMemoryCallback); - curl_easy_setopt(curl_, CURLOPT_WRITEDATA, (void *)&raw_response_); - } - else - { - curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, (void *)&WriteVectorCallback); - curl_easy_setopt(curl_, CURLOPT_HEADERDATA, (void *)&resp_headers_); - curl_easy_setopt(curl_, CURLOPT_WRITEDATA, (void *)&resp_body_); - } - - // TODO: only two methods supported for now - POST and GET - if (method_ == opentelemetry::ext::http::client::Method::Post) - { - // POST - curl_easy_setopt(curl_, CURLOPT_POST, true); - curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, (const char *)request); - curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, req_size); - } - else if (method_ == opentelemetry::ext::http::client::Method::Get) - { - // GET - } - else - { - res_ = CURLE_UNSUPPORTED_PROTOCOL; - return res_; - } - - // abort if slower than 4kb/sec during 30 seconds - curl_easy_setopt(curl_, CURLOPT_LOW_SPEED_TIME, 30L); - curl_easy_setopt(curl_, CURLOPT_LOW_SPEED_LIMIT, 4096); - DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); - - res_ = curl_easy_perform(curl_); - if (CURLE_OK != res_) - { - DispatchEvent(opentelemetry::ext::http::client::SessionState::SendFailed, - curl_easy_strerror(res_)); - return res_; - } - - /* Code snippet to parse raw HTTP response. This might come in handy - * if we ever consider to handle the raw upload instead of curl_easy_perform - ... - std::string resp((const char *)response); - std::regex http_status_regex(HTTP_STATUS_REGEXP); - std::smatch match; - if(std::regex_search(resp, match, http_status_regex)) - http_code = std::stol(match[1]); - ... - */ - - /* libcurl is nice enough to parse the http response code itself: */ - curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &res_); - // We got some response from server. Dump the contents. - DispatchEvent(opentelemetry::ext::http::client::SessionState::Response); - - // This function returns: - // - on success: HTTP status code. - // - on failure: CURL error code. - // The two sets of enums (CURLE, HTTP codes) - do not intersect, so we collapse them in one set. - return res_; - } + CURLcode Send(); - std::future &SendAsync(std::function callback = nullptr) - { - result_ = std::async(std::launch::async, [this, callback] { - long result = Send(); - if (callback != nullptr) - { - callback(*this); - } - return result; - }); - return result_; - } + /** + * Send request asynchronously + * @param session This operator must be binded to a Session + * @param callback callback when start async request success and got response + */ + CURLcode SendAsync(Session *session, std::function callback = nullptr); - void SendSync() { Send(); } + inline void SendSync() { Send(); } /** - * Get HTTP response code. This function returns CURL error code if HTTP response code is invalid. + * Get HTTP response code. This function returns 0. */ - uint16_t GetResponseCode() { return res_; } + inline StatusCode GetResponseCode() const noexcept + { + return static_cast(response_code_); + } + + CURLcode GetLastResultCode() { return last_curl_result_; } /** * Get last session state. @@ -321,198 +194,87 @@ class HttpOperation /** * Get whether or not response was programmatically aborted */ - bool WasAborted() { return is_aborted_.load(); } + bool WasAborted() { return is_aborted_.load(std::memory_order_acquire); } /** * Return a copy of resposne headers * * @return */ - Headers GetResponseHeaders() - { - Headers result; - if (resp_headers_.size() == 0) - return result; - - std::stringstream ss; - std::string headers((const char *)&resp_headers_[0], resp_headers_.size()); - ss.str(headers); - - std::string header; - while (std::getline(ss, header, '\n')) - { - // TODO - Regex below crashes with out-of-memory on CI docker container, so - // switching to string comparison. Need to debug and revert back. - - /*std::smatch match; - std::regex http_headers_regex(http_header_regexp); - if (std::regex_search(header, match, http_headers_regex)) - result.insert(std::pair( - static_cast(match[1]), static_cast(match[2]))); - */ - size_t pos = header.find(": "); - if (pos != std::string::npos) - result.insert( - std::pair(header.substr(0, pos), header.substr(pos + 2))); - } - return result; - } + Headers GetResponseHeaders(); /** * Return a copy of response body * * @return */ - std::vector GetResponseBody() { return resp_body_; } + inline const std::vector &GetResponseBody() const noexcept { return response_body_; } /** * Return a raw copy of response headers+body * * @return */ - std::vector GetRawResponse() { return raw_response_; } + inline const std::vector &GetRawResponse() const noexcept { return raw_response_; } /** * Release memory allocated for response */ - void ReleaseResponse() - { - resp_headers_.clear(); - resp_body_.clear(); - raw_response_.clear(); - } + void ReleaseResponse(); /** * Abort request in connecting or reading state. */ - void Abort() - { - is_aborted_ = true; - if (curl_ != nullptr) - { - // Simply close the socket - connection reset by peer - if (sockfd_) - { -#if defined(_WIN32) - ::closesocket(sockfd_); -#else - ::close(sockfd_); -#endif - sockfd_ = 0; - } - } - } + void Abort(); - CURL *GetHandle() { return curl_; } + /** + * Perform curl message, this function only can be called in the polling thread and it can only + * be called when got a CURLMSG_DONE. + * + * @param code + */ + void PerformCurlMessage(CURLcode code); -protected: - const bool is_raw_response_; // Do not split response headers from response body + inline CURL *GetCurlEasyHandle() noexcept { return curl_resource_.easy_handle; } + +private: + std::atomic is_aborted_; // Set to 'true' when async callback is aborted + std::atomic is_finished_; // Set to 'true' when async callback is finished. + std::atomic is_cleaned_; // Set to 'true' when async callback is cleaned. + const bool is_raw_response_; // Do not split response headers from response body + const bool reuse_connection_; // Reuse connection const std::chrono::milliseconds http_conn_timeout_; // Timeout for connect. Default: 5000ms - RequestMode request_mode_; - CURL *curl_; // Local curl instance - CURLcode res_; // Curl result OR HTTP status code if successful + HttpCurlEasyResource curl_resource_; + CURLcode last_curl_result_; // Curl result OR HTTP status code if successful - opentelemetry::ext::http::client::EventHandler *callback_; + opentelemetry::ext::http::client::EventHandler *event_handle_; // Request values opentelemetry::ext::http::client::Method method_; std::string url_; const Headers &request_headers_; const opentelemetry::ext::http::client::Body &request_body_; - struct curl_slist *headers_chunk_ = nullptr; + size_t request_nwrite_; opentelemetry::ext::http::client::SessionState session_state_; // Processed response headers and body - std::vector resp_headers_; - std::vector resp_body_; + long response_code_; + std::vector response_headers_; + std::vector response_body_; std::vector raw_response_; - // Socket parameters - curl_socket_t sockfd_; - - curl_off_t nread_; - size_t sendlen_ = 0; // # bytes sent by client - size_t acklen_ = 0; // # bytes ack by server - - std::future result_; - - /** - * Helper routine to wait for data on socket - * - * @param sockfd - * @param for_recv - * @param timeout_ms - * @return - */ - static int WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms) - { - struct timeval tv; - fd_set infd, outfd, errfd; - int res; - - tv.tv_sec = timeout_ms / 1000; - tv.tv_usec = (timeout_ms % 1000) * 1000; - - FD_ZERO(&infd); - FD_ZERO(&outfd); - FD_ZERO(&errfd); - - FD_SET(sockfd, &errfd); /* always check for error */ - - if (for_recv) - { - FD_SET(sockfd, &infd); - } - else - { - FD_SET(sockfd, &outfd); - } - - /* select() returns the number of signalled sockets or -1 */ - res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv); - return res; - } - - /** - * Old-school memory allocator - * - * @param contents - * @param size - * @param nmemb - * @param userp - * @return - */ - static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) + struct AsyncData { - std::vector *buf = static_cast *>(userp); - buf->insert(buf->end(), static_cast(contents), - static_cast(contents) + (size * nmemb)); - return size * nmemb; - } - - /** - * C++ STL std::vector allocator - * - * @param ptr - * @param size - * @param nmemb - * @param data - * @return - */ - static size_t WriteVectorCallback(void *ptr, - size_t size, - size_t nmemb, - std::vector *data) - { - if (data != nullptr) - { - const unsigned char *begin = (unsigned char *)(ptr); - const unsigned char *end = begin + size * nmemb; - data->insert(data->end(), begin, end); - } - return size * nmemb; - } + Session *session; // Owner Session + + std::thread::id callback_thread; + std::function callback; + std::atomic is_promise_running; + std::promise result_promise; + std::future result_future; + }; + std::unique_ptr async_data_; }; } // namespace curl } // namespace client diff --git a/ext/src/http/client/curl/BUILD b/ext/src/http/client/curl/BUILD index 33ab814b91..c0557fe99c 100644 --- a/ext/src/http/client/curl/BUILD +++ b/ext/src/http/client/curl/BUILD @@ -5,6 +5,7 @@ cc_library( srcs = [ "http_client_curl.cc", "http_client_factory_curl.cc", + "http_operation_curl.cc", ], copts = [ "-DWITH_CURL", diff --git a/ext/src/http/client/curl/CMakeLists.txt b/ext/src/http/client/curl/CMakeLists.txt index 78a81cfe3e..424f649f1a 100644 --- a/ext/src/http/client/curl/CMakeLists.txt +++ b/ext/src/http/client/curl/CMakeLists.txt @@ -1,7 +1,8 @@ find_package(CURL) if(CURL_FOUND) - add_library(opentelemetry_http_client_curl http_client_factory_curl.cc - http_client_curl.cc) + add_library( + opentelemetry_http_client_curl http_client_factory_curl.cc + http_client_curl.cc http_operation_curl.cc) set_target_properties(opentelemetry_http_client_curl PROPERTIES EXPORT_NAME http_client_curl) diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 74ad86ea4b..7f1b17e56b 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -3,16 +3,572 @@ #include "opentelemetry/ext/http/client/curl/http_client_curl.h" -bool opentelemetry::ext::http::client::curl::Session::CancelSession() noexcept +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ +namespace curl +{ + +HttpCurlGlobalInitializer::HttpCurlGlobalInitializer() +{ + curl_global_init(CURL_GLOBAL_ALL); +} + +HttpCurlGlobalInitializer::~HttpCurlGlobalInitializer() +{ + curl_global_cleanup(); +} + +nostd::shared_ptr HttpCurlGlobalInitializer::GetInstance() +{ + static nostd::shared_ptr shared_initializer{ + new HttpCurlGlobalInitializer()}; + return shared_initializer; +} + +void Session::SendRequest( + std::shared_ptr callback) noexcept { - curl_operation_->Abort(); + is_session_active_.store(true, std::memory_order_release); + std::string url = host_ + std::string(http_request_->uri_); + auto callback_ptr = callback.get(); + bool reuse_connection = false; + if (http_client_.GetMaxSessionsPerConnection() > 0) + { + reuse_connection = session_id_ % http_client_.GetMaxSessionsPerConnection() != 0; + } + + curl_operation_.reset(new HttpOperation(http_request_->method_, url, callback_ptr, + http_request_->headers_, http_request_->body_, false, + http_request_->timeout_ms_, reuse_connection)); + bool success = + CURLE_OK == curl_operation_->SendAsync(this, [this, callback](HttpOperation &operation) { + if (operation.WasAborted()) + { + // Manually cancelled + callback->OnEvent(opentelemetry::ext::http::client::SessionState::Cancelled, ""); + } + + if (operation.GetSessionState() == opentelemetry::ext::http::client::SessionState::Response) + { + // we have a http response + auto response = std::unique_ptr(new Response()); + response->headers_ = operation.GetResponseHeaders(); + response->body_ = operation.GetResponseBody(); + response->status_code_ = operation.GetResponseCode(); + callback->OnResponse(*response); + } + is_session_active_.store(false, std::memory_order_release); + }); + + if (success) + { + http_client_.MaybeSpawnBackgroundThread(); + } + else if (callback) + { + callback->OnEvent(opentelemetry::ext::http::client::SessionState::CreateFailed, ""); + is_session_active_.store(false, std::memory_order_release); + } +} + +bool Session::CancelSession() noexcept +{ + if (curl_operation_) + { + curl_operation_->Abort(); + } http_client_.CleanupSession(session_id_); return true; } -bool opentelemetry::ext::http::client::curl::Session::FinishSession() noexcept +bool Session::FinishSession() noexcept { - curl_operation_->Finish(); + if (curl_operation_) + { + curl_operation_->Finish(); + } http_client_.CleanupSession(session_id_); return true; } + +void Session::FinishOperation() +{ + if (curl_operation_) + { + curl_operation_->Cleanup(); + } +} + +HttpClient::HttpClient() + : next_session_id_{0}, + max_sessions_per_connection_{8}, + scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) +{ + multi_handle_ = curl_multi_init(); +} + +HttpClient::~HttpClient() +{ + while (true) + { + std::unique_ptr background_thread; + { + std::lock_guard lock_guard{background_thread_m_}; + background_thread.swap(background_thread_); + } + + // Force to abort all sessions + CancelAllSessions(); + + if (!background_thread) + { + break; + } + if (background_thread->joinable()) + { + background_thread->join(); + } + } + { + std::lock_guard lock_guard{multi_handle_m_}; + curl_multi_cleanup(multi_handle_); + } +} + +std::shared_ptr HttpClient::CreateSession( + nostd::string_view url) noexcept +{ + auto parsedUrl = common::UrlParser(std::string(url)); + if (!parsedUrl.success_) + { + return std::make_shared(*this); + } + auto session = + std::make_shared(*this, parsedUrl.scheme_, parsedUrl.host_, parsedUrl.port_); + auto session_id = ++next_session_id_; + session->SetId(session_id); + + std::lock_guard lock_guard{sessions_m_}; + sessions_.insert({session_id, session}); + + // FIXME: Session may leak if it do not SendRequest + return session; +} + +bool HttpClient::CancelAllSessions() noexcept +{ + // CancelSession may change sessions_, we can not change a container while iterating it. + while (true) + { + std::unordered_map> sessions; + { + std::lock_guard lock_guard{sessions_m_}; + sessions.swap(sessions_); + } + + if (sessions.empty()) + { + break; + } + + for (auto &session : sessions) + { + session.second->CancelSession(); + } + } + return true; +} + +bool HttpClient::FinishAllSessions() noexcept +{ + // FinishSession may change sessions_, we can not change a container while iterating it. + while (true) + { + std::unordered_map> sessions; + { + std::lock_guard lock_guard{sessions_m_}; + sessions.swap(sessions_); + } + + if (sessions.empty()) + { + break; + } + + for (auto &session : sessions) + { + session.second->FinishSession(); + } + } + return true; +} + +void HttpClient::CleanupSession(uint64_t session_id) +{ + std::shared_ptr session; + { + std::lock_guard lock_guard{sessions_m_}; + auto it = sessions_.find(session_id); + if (it != sessions_.end()) + { + session = it->second; + sessions_.erase(it); + } + } + + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_add_session_ids_.erase(session_id); + + if (session) + { + if (pending_to_remove_session_handles_.end() != + pending_to_remove_session_handles_.find(session_id)) + { + pending_to_remove_sessions_.emplace_back(std::move(session)); + } + else if (session->IsSessionActive() && session->GetOperation()) + { + session->FinishOperation(); + } + } + } +} + +void HttpClient::MaybeSpawnBackgroundThread() +{ + std::lock_guard lock_guard{background_thread_m_}; + if (background_thread_) + { + return; + } + + background_thread_.reset(new std::thread( + [](HttpClient *self) { + int still_running = 1; + while (true) + { + CURLMsg *msg; + int queued; + CURLMcode mc = curl_multi_perform(self->multi_handle_, &still_running); + // According to https://curl.se/libcurl/c/curl_multi_perform.html, when mc is not OK, we + // can not curl_multi_perform it again + if (mc != CURLM_OK) + { + self->resetMultiHandle(); + } + else if (still_running) + { + // curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util + // timeout to do the rest jobs +#if LIBCURL_VERSION_NUM >= 0x074200 + /* wait for activity, timeout or "nothing" */ + mc = curl_multi_poll(self->multi_handle_, nullptr, 0, + static_cast(self->scheduled_delay_milliseconds_.count()), + nullptr); +#else + mc = curl_multi_wait(self->multi_handle_, nullptr, 0, + static_cast(self->scheduled_delay_milliseconds_.count()), + nullptr); +#endif + } + + do + { + msg = curl_multi_info_read(self->multi_handle_, &queued); + if (msg == nullptr) + { + break; + } + + if (msg->msg == CURLMSG_DONE) + { + CURL *easy_handle = msg->easy_handle; + CURLcode result = msg->data.result; + Session *session = nullptr; + curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &session); + // If it's already moved into pending_to_remove_session_handles_, we just ingore this + // message. + if (nullptr != session && session->GetOperation()) + { + // Session can not be destroyed when calling PerformCurlMessage + auto hold_session = session->shared_from_this(); + session->GetOperation()->PerformCurlMessage(result); + } + } + } while (true); + + // Abort all pending easy handles + if (self->doAbortSessions()) + { + still_running = 1; + } + + // Remove all pending easy handles + if (self->doRemoveSessions()) + { + still_running = 1; + } + + // Add all pending easy handles + if (self->doAddSessions()) + { + still_running = 1; + } + + if (still_running == 0) + { + std::lock_guard lock_guard{self->background_thread_m_}; + // Double check, make sure no more pending sessions after locking background thread + // management + + // Abort all pending easy handles + if (self->doAbortSessions()) + { + still_running = 1; + } + + // Remove all pending easy handles + if (self->doRemoveSessions()) + { + still_running = 1; + } + + // Add all pending easy handles + if (self->doAddSessions()) + { + still_running = 1; + } + if (still_running == 0) + { + if (self->background_thread_) + { + self->background_thread_->detach(); + self->background_thread_.reset(); + } + break; + } + } + } + }, + this)); +} + +void HttpClient::ScheduleAddSession(uint64_t session_id) +{ + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_add_session_ids_.insert(session_id); + pending_to_remove_session_handles_.erase(session_id); + pending_to_abort_session_ids_.erase(session_id); + } + + wakeupBackgroundThread(); +} + +void HttpClient::ScheduleAbortSession(uint64_t session_id) +{ + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_abort_session_ids_.insert(session_id); + pending_to_add_session_ids_.erase(session_id); + } + + wakeupBackgroundThread(); +} + +void HttpClient::ScheduleRemoveSession(uint64_t session_id, HttpCurlEasyResource &&resource) +{ + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_add_session_ids_.erase(session_id); + pending_to_remove_session_handles_[session_id] = std::move(resource); + } + + wakeupBackgroundThread(); +} + +void HttpClient::wakeupBackgroundThread() +{ +// Before libcurl 7.68.0, we can only wait for timeout and do the rest jobs +// See https://curl.se/libcurl/c/curl_multi_wakeup.html +#if LIBCURL_VERSION_NUM >= 0x074400 + std::lock_guard lock_guard{multi_handle_m_}; + if (nullptr != multi_handle_) + { + curl_multi_wakeup(multi_handle_); + } +#endif +} + +bool HttpClient::doAddSessions() +{ + std::unordered_set pending_to_add_session_ids; + { + std::lock_guard session_id_lock_guard{session_ids_m_}; + pending_to_add_session_ids_.swap(pending_to_add_session_ids); + } + + bool has_data = false; + + std::lock_guard lock_guard{sessions_m_}; + for (auto &session_id : pending_to_add_session_ids) + { + auto session = sessions_.find(session_id); + if (session == sessions_.end()) + { + continue; + } + + if (!session->second->GetOperation()) + { + continue; + } + + CURL *easy_handle = session->second->GetOperation()->GetCurlEasyHandle(); + if (nullptr == easy_handle) + { + continue; + } + + curl_multi_add_handle(multi_handle_, easy_handle); + has_data = true; + } + + return has_data; +} + +bool HttpClient::doAbortSessions() +{ + std::list> abort_sessions; + std::unordered_set pending_to_abort_session_ids; + { + std::lock_guard session_id_lock_guard{session_ids_m_}; + pending_to_abort_session_ids_.swap(pending_to_abort_session_ids); + } + + { + std::lock_guard lock_guard{sessions_m_}; + for (auto &session_id : pending_to_abort_session_ids) + { + auto session = sessions_.find(session_id); + if (session == sessions_.end()) + { + continue; + } + + abort_sessions.push_back(session->second); + } + } + + bool has_data = false; + for (auto session : abort_sessions) + { + if (session->GetOperation()) + { + session->FinishOperation(); + has_data = true; + } + } + return has_data; +} + +bool HttpClient::doRemoveSessions() +{ + bool has_data = false; + bool should_continue; + do + { + std::unordered_map pending_to_remove_session_handles; + std::list> pending_to_remove_sessions; + { + std::lock_guard session_id_lock_guard{session_ids_m_}; + pending_to_remove_session_handles_.swap(pending_to_remove_session_handles); + pending_to_remove_sessions_.swap(pending_to_remove_sessions); + + // If user callback do not call CancelSession or FinishSession, We still need to remove it + // from sessions_ + std::lock_guard session_lock_guard{sessions_m_}; + for (auto &removing_handle : pending_to_remove_session_handles) + { + auto session = sessions_.find(removing_handle.first); + if (session != sessions_.end()) + { + pending_to_remove_sessions.emplace_back(std::move(session->second)); + sessions_.erase(session); + } + } + } + + for (auto &removing_handle : pending_to_remove_session_handles) + { + if (nullptr != removing_handle.second.headers_chunk) + { + curl_slist_free_all(removing_handle.second.headers_chunk); + } + + curl_multi_remove_handle(multi_handle_, removing_handle.second.easy_handle); + curl_easy_cleanup(removing_handle.second.easy_handle); + } + + for (auto &removing_session : pending_to_remove_sessions) + { + // This operation may add more pending_to_remove_session_handles + removing_session->FinishOperation(); + } + + should_continue = + !pending_to_remove_session_handles.empty() || !pending_to_remove_sessions.empty(); + if (should_continue) + { + has_data = true; + } + } while (should_continue); + + return has_data; +} + +void HttpClient::resetMultiHandle() +{ + std::list> sessions; + std::lock_guard session_lock_guard{sessions_m_}; + { + std::lock_guard session_id_lock_guard{session_ids_m_}; + for (auto &session : sessions_) + { + if (pending_to_add_session_ids_.end() == pending_to_add_session_ids_.find(session.first)) + { + sessions.push_back(session.second); + } + } + } + + for (auto &session : sessions) + { + session->CancelSession(); + session->FinishOperation(); + } + + doRemoveSessions(); + + // We will modify the multi_handle_, so we need to lock it + std::lock_guard lock_guard{multi_handle_m_}; + curl_multi_cleanup(multi_handle_); + + // Create a another multi handle to continue pending sessions + multi_handle_ = curl_multi_init(); +} + +} // namespace curl +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE diff --git a/ext/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc new file mode 100644 index 0000000000..cb32e5f916 --- /dev/null +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -0,0 +1,684 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/ext/http/client/curl/http_operation_curl.h" + +#include "opentelemetry/ext/http/client/curl/http_client_curl.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ +namespace curl +{ + +size_t HttpOperation::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) +{ + HttpOperation *self = reinterpret_cast(userp); + if (nullptr == self) + { + return 0; + } + + self->raw_response_.insert(self->raw_response_.end(), static_cast(contents), + static_cast(contents) + (size * nmemb)); + + if (self->WasAborted()) + { + return 0; + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + return size * nmemb; +} + +size_t HttpOperation::WriteVectorHeaderCallback(void *ptr, size_t size, size_t nmemb, void *userp) +{ + HttpOperation *self = reinterpret_cast(userp); + if (nullptr == self) + { + return 0; + } + + const unsigned char *begin = (unsigned char *)(ptr); + const unsigned char *end = begin + size * nmemb; + self->response_headers_.insert(self->response_headers_.end(), begin, end); + + if (self->WasAborted()) + { + return 0; + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + return size * nmemb; +} + +size_t HttpOperation::WriteVectorBodyCallback(void *ptr, size_t size, size_t nmemb, void *userp) +{ + HttpOperation *self = reinterpret_cast(userp); + if (nullptr == self) + { + return 0; + } + + const unsigned char *begin = (unsigned char *)(ptr); + const unsigned char *end = begin + size * nmemb; + self->response_body_.insert(self->response_body_.end(), begin, end); + + if (self->WasAborted()) + { + return 0; + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + return size * nmemb; +} + +size_t HttpOperation::ReadMemoryCallback(char *buffer, size_t size, size_t nitems, void *userp) +{ + HttpOperation *self = reinterpret_cast(userp); + if (nullptr == self) + { + return 0; + } + + if (self->WasAborted()) + { + return CURL_READFUNC_ABORT; + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + // EOF + if (self->request_nwrite_ >= self->request_body_.size()) + { + return 0; + } + + size_t nwrite = size * nitems; + if (nwrite > self->request_body_.size() - self->request_nwrite_) + { + nwrite = self->request_body_.size() - self->request_nwrite_; + } + + memcpy(buffer, &self->request_body_[self->request_nwrite_], nwrite); + self->request_nwrite_ += nwrite; + return nwrite; +} + +#if LIBCURL_VERSION_NUM >= 0x075000 +int HttpOperation::PreRequestCallback(void *clientp, char *, char *, int, int) +{ + HttpOperation *self = reinterpret_cast(clientp); + if (nullptr == self) + { + return CURL_PREREQFUNC_ABORT; + } + + if (self->GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + self->DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (self->WasAborted()) + { + return CURL_PREREQFUNC_ABORT; + } + + return CURL_PREREQFUNC_OK; +} +#endif + +#if LIBCURL_VERSION_NUM >= 0x072000 +int HttpOperation::OnProgressCallback(void *clientp, + curl_off_t dltotal, + curl_off_t dlnow, + curl_off_t ultotal, + curl_off_t ulnow) +{ + HttpOperation *self = reinterpret_cast(clientp); + if (nullptr == self) + { + return -1; + } + + if (self->WasAborted()) + { + return -1; + } + + // CURL_PROGRESSFUNC_CONTINUE is added in 7.68.0 +# if defined(CURL_PROGRESSFUNC_CONTINUE) + return CURL_PROGRESSFUNC_CONTINUE; +# else + return 0; +# endif +} +#else +int HttpOperation::OnProgressCallback(void *clientp, + double dltotal, + double dlnow, + double ultotal, + double ulnow) +{ + HttpOperation *self = reinterpret_cast(clientp); + if (nullptr == self) + { + return -1; + } + + if (self->WasAborted()) + { + return -1; + } + + return 0; +} +#endif + +void HttpOperation::DispatchEvent(opentelemetry::ext::http::client::SessionState type, + std::string reason) +{ + if (event_handle_ != nullptr) + { + event_handle_->OnEvent(type, reason); + } + + session_state_ = type; +} + +HttpOperation::HttpOperation(opentelemetry::ext::http::client::Method method, + std::string url, + opentelemetry::ext::http::client::EventHandler *event_handle, + // Default empty headers and empty request body + const opentelemetry::ext::http::client::Headers &request_headers, + const opentelemetry::ext::http::client::Body &request_body, + // Default connectivity and response size options + bool is_raw_response, + std::chrono::milliseconds http_conn_timeout, + bool reuse_connection) + : is_aborted_(false), + is_finished_(false), + is_cleaned_(false), + // Optional connection params + is_raw_response_(is_raw_response), + reuse_connection_(reuse_connection), + http_conn_timeout_(http_conn_timeout), + // Result + last_curl_result_(CURLE_OK), + event_handle_(event_handle), + method_(method), + url_(url), + // Local vars + request_headers_(request_headers), + request_body_(request_body), + request_nwrite_(0), + session_state_(opentelemetry::ext::http::client::SessionState::Created), + response_code_(0) +{ + /* get a curl handle */ + curl_resource_.easy_handle = curl_easy_init(); + if (!curl_resource_.easy_handle) + { + last_curl_result_ = CURLE_FAILED_INIT; + DispatchEvent(opentelemetry::ext::http::client::SessionState::CreateFailed, + curl_easy_strerror(last_curl_result_)); + return; + } + + // Specify our custom headers + if (!this->request_headers_.empty()) + { + for (auto &kv : this->request_headers_) + { + std::string header = std::string(kv.first); + header += ": "; + header += std::string(kv.second); + curl_resource_.headers_chunk = + curl_slist_append(curl_resource_.headers_chunk, header.c_str()); + } + } + + DispatchEvent(opentelemetry::ext::http::client::SessionState::Created); +} + +HttpOperation::~HttpOperation() +{ + // Given the request has not been aborted we should wait for completion here + // This guarantees the lifetime of this request. + switch (GetSessionState()) + { + case opentelemetry::ext::http::client::SessionState::Connecting: + case opentelemetry::ext::http::client::SessionState::Connected: + case opentelemetry::ext::http::client::SessionState::Sending: { + if (async_data_ && async_data_->result_future.valid()) + { + if (async_data_->callback_thread != std::this_thread::get_id()) + { + async_data_->result_future.wait(); + last_curl_result_ = async_data_->result_future.get(); + } + } + break; + } + default: + break; + } + + Cleanup(); +} + +void HttpOperation::Finish() +{ + if (is_finished_.exchange(true, std::memory_order_acq_rel)) + { + return; + } + + if (async_data_ && async_data_->result_future.valid()) + { + // We should not wait in callback from Cleanup() + if (async_data_->callback_thread != std::this_thread::get_id()) + { + async_data_->result_future.wait(); + last_curl_result_ = async_data_->result_future.get(); + } + } +} + +void HttpOperation::Cleanup() +{ + if (is_cleaned_.exchange(true, std::memory_order_acq_rel)) + { + return; + } + + switch (GetSessionState()) + { + case opentelemetry::ext::http::client::SessionState::Created: + case opentelemetry::ext::http::client::SessionState::Connecting: + case opentelemetry::ext::http::client::SessionState::Connected: + case opentelemetry::ext::http::client::SessionState::Sending: { + DispatchEvent(opentelemetry::ext::http::client::SessionState::Cancelled, + curl_easy_strerror(last_curl_result_)); + break; + } + default: + break; + } + + std::function callback; + + // Only cleanup async once even in recursive calls + if (async_data_) + { + // Just reset and move easy_handle to owner if in async mode + if (async_data_->session != nullptr) + { + auto session = async_data_->session; + async_data_->session = nullptr; + + if (curl_resource_.easy_handle != nullptr) + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PRIVATE, NULL); + curl_easy_reset(curl_resource_.easy_handle); + } + session->GetHttpClient().ScheduleRemoveSession(session->GetSessionId(), + std::move(curl_resource_)); + } + + callback.swap(async_data_->callback); + if (callback) + { + async_data_->callback_thread = std::this_thread::get_id(); + callback(*this); + async_data_->callback_thread = std::thread::id(); + } + + // Set value to promise to continue Finish() + if (true == async_data_->is_promise_running.exchange(false, std::memory_order_acq_rel)) + { + async_data_->result_promise.set_value(last_curl_result_); + } + + return; + } + + // Sync mode + if (curl_resource_.easy_handle != nullptr) + { + curl_easy_cleanup(curl_resource_.easy_handle); + curl_resource_.easy_handle = nullptr; + } + + if (curl_resource_.headers_chunk != nullptr) + { + curl_slist_free_all(curl_resource_.headers_chunk); + curl_resource_.headers_chunk = nullptr; + } +} + +CURLcode HttpOperation::Setup() +{ + if (!curl_resource_.easy_handle) + { + return CURLE_FAILED_INIT; + } + + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_VERBOSE, 0); + + // Specify target URL + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_URL, url_.c_str()); + + // TODO: support ssl cert verification for https request + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_SSL_VERIFYPEER, 0); // 1L + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_SSL_VERIFYHOST, 0); // 2L + + if (curl_resource_.headers_chunk != nullptr) + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_HTTPHEADER, curl_resource_.headers_chunk); + } + + // TODO: control local port to use + // curl_easy_setopt(curl, CURLOPT_LOCALPORT, dcf_port); + + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_TIMEOUT_MS, http_conn_timeout_.count()); + + // abort if slower than 4kb/sec during 30 seconds + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_LOW_SPEED_TIME, 30L); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_LOW_SPEED_LIMIT, 4096); + if (reuse_connection_) + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_FRESH_CONNECT, 0L); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_FORBID_REUSE, 0L); + } + else + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_FRESH_CONNECT, 1L); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_FORBID_REUSE, 1L); + } + + if (is_raw_response_) + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_HEADER, true); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_WRITEFUNCTION, + (void *)&HttpOperation::WriteMemoryCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_WRITEDATA, (void *)this); + } + else + { + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_WRITEFUNCTION, + (void *)&HttpOperation::WriteVectorBodyCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_WRITEDATA, (void *)this); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_HEADERFUNCTION, + (void *)&HttpOperation::WriteVectorHeaderCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_HEADERDATA, (void *)this); + } + + // TODO: only two methods supported for now - POST and GET + if (method_ == opentelemetry::ext::http::client::Method::Post) + { + // Request buffer + const curl_off_t req_size = static_cast(request_body_.size()); + // POST + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_POST, 1L); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_POSTFIELDS, NULL); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_POSTFIELDSIZE_LARGE, req_size); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_READFUNCTION, + (void *)&HttpOperation::ReadMemoryCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_READDATA, (void *)this); + } + else if (method_ == opentelemetry::ext::http::client::Method::Get) + { + // GET + } + else + { + return CURLE_UNSUPPORTED_PROTOCOL; + } + +#if LIBCURL_VERSION_NUM >= 0x072000 + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_XFERINFOFUNCTION, + (void *)&HttpOperation::OnProgressCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_XFERINFODATA, (void *)this); +#else + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PROGRESSFUNCTION, + (void *)&HttpOperation::OnProgressCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PROGRESSDATA, (void *)this); +#endif + +#if LIBCURL_VERSION_NUM >= 0x075000 + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PREREQFUNCTION, + (void *)&HttpOperation::PreRequestCallback); + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PREREQDATA, (void *)this); +#endif + + return CURLE_OK; +} + +CURLcode HttpOperation::Send() +{ + // If it is async sending, just return error + if (async_data_ && async_data_->is_promise_running.load(std::memory_order_acquire)) + { + return CURLE_FAILED_INIT; + } + + ReleaseResponse(); + + last_curl_result_ = Setup(); + if (last_curl_result_ != CURLE_OK) + { + DispatchEvent(opentelemetry::ext::http::client::SessionState::ConnectFailed, + curl_easy_strerror(last_curl_result_)); + return last_curl_result_; + } + + // Perform initial connect, handling the timeout if needed + // We can not use CURLOPT_CONNECT_ONLY because it will disable the reuse of connections. + DispatchEvent(opentelemetry::ext::http::client::SessionState::Connecting); + is_finished_.store(false, std::memory_order_release); + is_aborted_.store(false, std::memory_order_release); + is_cleaned_.store(false, std::memory_order_release); + + CURLcode code = curl_easy_perform(curl_resource_.easy_handle); + PerformCurlMessage(code); + if (CURLE_OK != code) + { + return code; + } + + return code; +} + +CURLcode HttpOperation::SendAsync(Session *session, std::function callback) +{ + if (nullptr == session) + { + return CURLE_FAILED_INIT; + } + + if (async_data_ && async_data_->is_promise_running.load(std::memory_order_acquire)) + { + return CURLE_FAILED_INIT; + } + else + { + async_data_.reset(new AsyncData()); + async_data_->is_promise_running.store(false, std::memory_order_release); + async_data_->session = nullptr; + } + + ReleaseResponse(); + + CURLcode code = Setup(); + last_curl_result_ = code; + if (code != CURLE_OK) + { + return code; + } + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_PRIVATE, session); + + DispatchEvent(opentelemetry::ext::http::client::SessionState::Connecting); + is_finished_.store(false, std::memory_order_release); + is_aborted_.store(false, std::memory_order_release); + is_cleaned_.store(false, std::memory_order_release); + + async_data_->session = session; + if (false == async_data_->is_promise_running.exchange(true, std::memory_order_acq_rel)) + { + async_data_->result_promise = std::promise(); + async_data_->result_future = async_data_->result_promise.get_future(); + } + async_data_->callback = std::move(callback); + + session->GetHttpClient().ScheduleAddSession(session->GetSessionId()); + return code; +} + +Headers HttpOperation::GetResponseHeaders() +{ + Headers result; + if (response_headers_.size() == 0) + return result; + + std::stringstream ss; + std::string headers((const char *)&response_headers_[0], response_headers_.size()); + ss.str(headers); + + std::string header; + while (std::getline(ss, header, '\n')) + { + // TODO - Regex below crashes with out-of-memory on CI docker container, so + // switching to string comparison. Need to debug and revert back. + + /*std::smatch match; + std::regex http_headers_regex(http_header_regexp); + if (std::regex_search(header, match, http_headers_regex)) + result.insert(std::pair( + static_cast(match[1]), static_cast(match[2]))); + */ + size_t pos = header.find(": "); + if (pos != std::string::npos) + result.insert( + std::pair(header.substr(0, pos), header.substr(pos + 2))); + } + return result; +} + +void HttpOperation::ReleaseResponse() +{ + response_headers_.clear(); + response_body_.clear(); + raw_response_.clear(); +} + +void HttpOperation::Abort() +{ + is_aborted_.store(true, std::memory_order_release); + if (curl_resource_.easy_handle != nullptr) + { + // Enable progress callback to abort from polling thread + curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_NOPROGRESS, 0L); + if (async_data_ && nullptr != async_data_->session) + { + async_data_->session->GetHttpClient().ScheduleAbortSession( + async_data_->session->GetSessionId()); + } + } +} + +void HttpOperation::PerformCurlMessage(CURLcode code) +{ + last_curl_result_ = code; + if (code != CURLE_OK) + { + switch (GetSessionState()) + { + case opentelemetry::ext::http::client::SessionState::Connecting: { + DispatchEvent(opentelemetry::ext::http::client::SessionState::ConnectFailed, + curl_easy_strerror(code)); // couldn't connect - stage 1 + break; + } + case opentelemetry::ext::http::client::SessionState::Connected: + case opentelemetry::ext::http::client::SessionState::Sending: { + if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + DispatchEvent(opentelemetry::ext::http::client::SessionState::SendFailed, + curl_easy_strerror(code)); + } + default: + break; + } + } + else if (curl_resource_.easy_handle != nullptr) + { + curl_easy_getinfo(curl_resource_.easy_handle, CURLINFO_RESPONSE_CODE, &response_code_); + } + + // Transform state + if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connecting) + { + DispatchEvent(opentelemetry::ext::http::client::SessionState::Connected); + } + + if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Connected) + { + DispatchEvent(opentelemetry::ext::http::client::SessionState::Sending); + } + + if (GetSessionState() == opentelemetry::ext::http::client::SessionState::Sending) + { + DispatchEvent(opentelemetry::ext::http::client::SessionState::Response); + } + + // Cleanup and unbind easy handle from multi handle, and finish callback + Cleanup(); +} + +} // namespace curl +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 7085a1da33..0bb5aa09a8 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -25,12 +25,27 @@ namespace nostd = opentelemetry::nostd; class CustomEventHandler : public http_client::EventHandler { public: - virtual void OnResponse(http_client::Response &response) noexcept override{}; + virtual void OnResponse(http_client::Response &response) noexcept override + { + got_response_ = true; + }; virtual void OnEvent(http_client::SessionState state, nostd::string_view reason) noexcept override - {} + { + switch (state) + { + case http_client::SessionState::ConnectFailed: + case http_client::SessionState::SendFailed: { + is_called_ = true; + break; + } + default: + break; + } + } virtual void OnConnecting(const http_client::SSLCertificate &) noexcept {} virtual ~CustomEventHandler() = default; bool is_called_ = false; + bool got_response_ = false; }; class GetEventHandler : public CustomEventHandler @@ -39,7 +54,8 @@ class GetEventHandler : public CustomEventHandler { ASSERT_EQ(200, response.GetStatusCode()); ASSERT_EQ(response.GetBody().size(), 0); - is_called_ = true; + is_called_ = true; + got_response_ = true; }; }; @@ -50,8 +66,32 @@ class PostEventHandler : public CustomEventHandler ASSERT_EQ(200, response.GetStatusCode()); std::string body(response.GetBody().begin(), response.GetBody().end()); ASSERT_EQ(body, "{'k1':'v1', 'k2':'v2', 'k3':'v3'}"); - is_called_ = true; + is_called_ = true; + got_response_ = true; + } +}; + +class FinishInCallbackHandler : public CustomEventHandler +{ +public: + FinishInCallbackHandler(std::shared_ptr session) : session_(session) {} + + void OnResponse(http_client::Response &response) noexcept override + { + ASSERT_EQ(200, response.GetStatusCode()); + ASSERT_EQ(response.GetBody().size(), 0); + is_called_ = true; + got_response_ = true; + + if (session_) + { + session_->FinishSession(); + session_.reset(); + } } + +private: + std::shared_ptr session_; }; class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS::HttpRequestCallback @@ -108,7 +148,7 @@ class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS::HttpRe response.headers["Content-Type"] = "text/plain"; response_status = 200; } - if (request.uri == "/post/") + else if (request.uri == "/post/") { std::unique_lock lk(mtx_requests); received_requests_.push_back(request); @@ -125,8 +165,10 @@ class BasicCurlHttpTests : public ::testing::Test, public HTTP_SERVER_NS::HttpRe bool waitForRequests(unsigned timeOutSec, unsigned expected_count = 1) { std::unique_lock lk(mtx_requests); - if (cv_got_events.wait_for(lk, std::chrono::milliseconds(1000 * timeOutSec), - [&] { return received_requests_.size() >= expected_count; })) + if (cv_got_events.wait_for(lk, std::chrono::milliseconds(1000 * timeOutSec), [&] { + // + return received_requests_.size() >= expected_count; + })) { return true; } @@ -201,6 +243,7 @@ TEST_F(BasicCurlHttpTests, SendGetRequest) ASSERT_TRUE(waitForRequests(30, 1)); session->FinishSession(); ASSERT_TRUE(handler->is_called_); + ASSERT_TRUE(handler->got_response_); } TEST_F(BasicCurlHttpTests, SendPostRequest) @@ -223,6 +266,7 @@ TEST_F(BasicCurlHttpTests, SendPostRequest) ASSERT_TRUE(waitForRequests(30, 1)); session->FinishSession(); ASSERT_TRUE(handler->is_called_); + ASSERT_TRUE(handler->got_response_); session_manager->CancelAllSessions(); session_manager->FinishAllSessions(); @@ -240,7 +284,8 @@ TEST_F(BasicCurlHttpTests, RequestTimeout) auto handler = std::make_shared(); session->SendRequest(handler); session->FinishSession(); - ASSERT_FALSE(handler->is_called_); + ASSERT_TRUE(handler->is_called_); + ASSERT_FALSE(handler->got_response_); } TEST_F(BasicCurlHttpTests, CurlHttpOperations) @@ -253,16 +298,16 @@ TEST_F(BasicCurlHttpTests, CurlHttpOperations) http_client::Headers headers = { {"name1", "value1_1"}, {"name1", "value1_2"}, {"name2", "value3"}, {"name3", "value3"}}; - curl::HttpOperation http_operations1(http_client::Method::Head, "/get", handler, - curl::RequestMode::Async, headers, body, true); + curl::HttpOperation http_operations1(http_client::Method::Head, "/get", handler, headers, body, + true); http_operations1.Send(); - curl::HttpOperation http_operations2(http_client::Method::Get, "/get", handler, - curl::RequestMode::Async, headers, body, true); + curl::HttpOperation http_operations2(http_client::Method::Get, "/get", handler, headers, body, + true); http_operations2.Send(); - curl::HttpOperation http_operations3(http_client::Method::Get, "/get", handler, - curl::RequestMode::Async, headers, body, false); + curl::HttpOperation http_operations3(http_client::Method::Get, "/get", handler, headers, body, + false); http_operations3.Send(); delete handler; } @@ -319,3 +364,154 @@ TEST_F(BasicCurlHttpTests, GetBaseUri) ASSERT_EQ(std::static_pointer_cast(session)->GetBaseUri(), "http://127.0.0.1:31339/"); } + +TEST_F(BasicCurlHttpTests, SendGetRequestAsync) +{ + curl::HttpClient http_client; + + for (int round = 0; round < 2; ++round) + { + received_requests_.clear(); + static constexpr const unsigned batch_count = 5; + std::shared_ptr sessions[batch_count]; + std::shared_ptr handlers[batch_count]; + for (unsigned i = 0; i < batch_count; ++i) + { + sessions[i] = http_client.CreateSession("http://127.0.0.1:19000/get/"); + auto request = sessions[i]->CreateRequest(); + request->SetMethod(http_client::Method::Get); + request->SetUri("get/"); + + handlers[i] = std::make_shared(); + + // Lock mtx_requests to prevent response, we will check IsSessionActive() in the end + std::unique_lock lock_requests(mtx_requests); + sessions[i]->SendRequest(handlers[i]); + ASSERT_TRUE(sessions[i]->IsSessionActive()); + } + + ASSERT_TRUE(waitForRequests(30, batch_count)); + + for (unsigned i = 0; i < batch_count; ++i) + { + sessions[i]->FinishSession(); + ASSERT_FALSE(sessions[i]->IsSessionActive()); + + ASSERT_TRUE(handlers[i]->is_called_); + ASSERT_TRUE(handlers[i]->got_response_); + } + + http_client.WaitBackgroundThreadExit(); + } +} + +TEST_F(BasicCurlHttpTests, SendGetRequestAsyncTimeout) +{ + received_requests_.clear(); + curl::HttpClient http_client; + + static constexpr const unsigned batch_count = 5; + std::shared_ptr sessions[batch_count]; + std::shared_ptr handlers[batch_count]; + for (unsigned i = 0; i < batch_count; ++i) + { + sessions[i] = http_client.CreateSession("http://222.222.222.200:19000/get/"); + auto request = sessions[i]->CreateRequest(); + request->SetMethod(http_client::Method::Get); + request->SetUri("get/"); + request->SetTimeoutMs(std::chrono::milliseconds(256)); + + handlers[i] = std::make_shared(); + + // Lock mtx_requests to prevent response, we will check IsSessionActive() in the end + std::unique_lock lock_requests(mtx_requests); + sessions[i]->SendRequest(handlers[i]); + ASSERT_TRUE(sessions[i]->IsSessionActive()); + } + + for (unsigned i = 0; i < batch_count; ++i) + { + sessions[i]->FinishSession(); + ASSERT_FALSE(sessions[i]->IsSessionActive()); + + ASSERT_TRUE(handlers[i]->is_called_); + ASSERT_FALSE(handlers[i]->got_response_); + } +} + +TEST_F(BasicCurlHttpTests, SendPostRequestAsync) +{ + curl::HttpClient http_client; + + for (int round = 0; round < 2; ++round) + { + received_requests_.clear(); + auto handler = std::make_shared(); + + static constexpr const unsigned batch_count = 5; + std::shared_ptr sessions[batch_count]; + for (auto &session : sessions) + { + session = http_client.CreateSession("http://127.0.0.1:19000/post/"); + auto request = session->CreateRequest(); + request->SetMethod(http_client::Method::Post); + request->SetUri("post/"); + + // Lock mtx_requests to prevent response, we will check IsSessionActive() in the end + std::unique_lock lock_requests(mtx_requests); + session->SendRequest(handler); + ASSERT_TRUE(session->IsSessionActive()); + } + + ASSERT_TRUE(waitForRequests(30, batch_count)); + + for (auto &session : sessions) + { + session->FinishSession(); + ASSERT_FALSE(session->IsSessionActive()); + } + + ASSERT_TRUE(handler->is_called_); + ASSERT_TRUE(handler->got_response_); + + http_client.WaitBackgroundThreadExit(); + } +} + +TEST_F(BasicCurlHttpTests, FinishInAsyncCallback) +{ + curl::HttpClient http_client; + + for (int round = 0; round < 2; ++round) + { + received_requests_.clear(); + static constexpr const unsigned batch_count = 5; + std::shared_ptr sessions[batch_count]; + std::shared_ptr handlers[batch_count]; + for (unsigned i = 0; i < batch_count; ++i) + { + sessions[i] = http_client.CreateSession("http://127.0.0.1:19000/get/"); + auto request = sessions[i]->CreateRequest(); + request->SetMethod(http_client::Method::Get); + request->SetUri("get/"); + + handlers[i] = std::make_shared(sessions[i]); + + // Lock mtx_requests to prevent response, we will check IsSessionActive() in the end + std::unique_lock lock_requests(mtx_requests); + sessions[i]->SendRequest(handlers[i]); + ASSERT_TRUE(sessions[i]->IsSessionActive()); + } + + http_client.WaitBackgroundThreadExit(); + ASSERT_TRUE(waitForRequests(300, batch_count)); + + for (unsigned i = 0; i < batch_count; ++i) + { + ASSERT_FALSE(sessions[i]->IsSessionActive()); + + ASSERT_TRUE(handlers[i]->is_called_); + ASSERT_TRUE(handlers[i]->got_response_); + } + } +} From 8e603fd08857f2afde681d8168e151395001239d Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 5 May 2022 12:40:43 +0800 Subject: [PATCH 18/19] Add `max_requests_per_connection` for all OTLP http exporters Signed-off-by: owent --- .../exporters/otlp/otlp_http_client.h | 11 ++-- .../exporters/otlp/otlp_http_exporter.h | 5 +- .../exporters/otlp/otlp_http_log_exporter.h | 5 +- exporters/otlp/src/otlp_http_client.cc | 8 ++- exporters/otlp/src/otlp_http_exporter.cc | 3 +- exporters/otlp/src/otlp_http_log_exporter.cc | 3 +- .../ext/http/client/curl/http_client_curl.h | 45 ++++++---------- .../ext/http/client/http_client.h | 2 + .../http/client/nosend/http_client_nosend.h | 53 +++++++------------ ext/src/http/client/curl/http_client_curl.cc | 5 ++ .../http/client/nosend/http_client_nosend.cc | 27 ++++++++++ 11 files changed, 95 insertions(+), 72 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 1b21ed9f90..288bf30630 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -79,7 +79,10 @@ struct OtlpHttpClientOptions OtlpHeaders http_headers = GetOtlpDefaultHeaders(); // Concurrent requests - std::size_t max_concurrent_requests = 8; + std::size_t max_concurrent_requests = 64; + + // Concurrent requests + std::size_t max_requests_per_connection = 8; inline OtlpHttpClientOptions(nostd::string_view input_url, HttpRequestContentType input_content_type, @@ -88,7 +91,8 @@ struct OtlpHttpClientOptions bool input_console_debug, std::chrono::system_clock::duration input_timeout, const OtlpHeaders &input_http_headers, - std::size_t input_concurrent_sessions = 8) + std::size_t input_concurrent_sessions = 64, + std::size_t input_max_requests_per_connection = 8) : url(input_url), content_type(input_content_type), json_bytes_mapping(input_json_bytes_mapping), @@ -96,7 +100,8 @@ struct OtlpHttpClientOptions console_debug(input_console_debug), timeout(input_timeout), http_headers(input_http_headers), - max_concurrent_requests(input_concurrent_sessions) + max_concurrent_requests(input_concurrent_sessions), + max_requests_per_connection(input_max_requests_per_connection) {} }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index a2f9eb3176..6dfe8f1f34 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -55,7 +55,10 @@ struct OtlpHttpExporterOptions #ifdef ENABLE_ASYNC_EXPORT // Concurrent requests // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-concurrent-requests - std::size_t max_concurrent_requests = 8; + std::size_t max_concurrent_requests = 64; + + // Concurrent requests + std::size_t max_requests_per_connection = 8; #endif }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h index 2bd8697076..91e4ccf714 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h @@ -55,7 +55,10 @@ struct OtlpHttpLogExporterOptions # ifdef ENABLE_ASYNC_EXPORT // Concurrent requests // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-concurrent-requests - std::size_t max_concurrent_requests = 8; + std::size_t max_concurrent_requests = 64; + + // Concurrent requests + std::size_t max_requests_per_connection = 8; # endif }; diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 1e5f128aaf..b2fc75838d 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -649,7 +649,9 @@ void ConvertListFieldToJson(nlohmann::json &value, OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : is_shutdown_(false), options_(options), http_client_(http_client::HttpClientFactory::Create()) -{} +{ + http_client_->SetMaxSessionsPerConnection(options_.max_requests_per_connection); +} OtlpHttpClient::~OtlpHttpClient() { @@ -682,7 +684,9 @@ OtlpHttpClient::~OtlpHttpClient() OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options, std::shared_ptr http_client) : is_shutdown_(false), options_(options), http_client_(http_client) -{} +{ + http_client_->SetMaxSessionsPerConnection(options_.max_requests_per_connection); +} // ----------------------------- HTTP Client methods ------------------------------ opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export( diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 8f51165cc8..f5b50162f3 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -34,7 +34,8 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) options.http_headers #ifdef ENABLE_ASYNC_EXPORT , - options.max_concurrent_requests + options.max_concurrent_requests, + options.max_requests_per_connection #endif ))) {} diff --git a/exporters/otlp/src/otlp_http_log_exporter.cc b/exporters/otlp/src/otlp_http_log_exporter.cc index 2e377699ed..d2430bf924 100644 --- a/exporters/otlp/src/otlp_http_log_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_exporter.cc @@ -36,7 +36,8 @@ OtlpHttpLogExporter::OtlpHttpLogExporter(const OtlpHttpLogExporterOptions &optio options.http_headers # ifdef ENABLE_ASYNC_EXPORT , - options.max_concurrent_requests + options.max_concurrent_requests, + options.max_requests_per_connection # endif ))) {} diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index fa48a068a1..888b7e676f 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -76,10 +76,7 @@ class Request : public opentelemetry::ext::http::client::Request AddHeader(name, value); } - virtual void SetUri(nostd::string_view uri) noexcept override - { - uri_ = static_cast(uri); - } + void SetUri(nostd::string_view uri) noexcept override { uri_ = static_cast(uri); } void SetTimeoutMs(std::chrono::milliseconds timeout_ms) noexcept override { @@ -99,14 +96,10 @@ class Response : public opentelemetry::ext::http::client::Response public: Response() : status_code_(Http_Ok) {} - virtual const opentelemetry::ext::http::client::Body &GetBody() const noexcept override - { - return body_; - } + const opentelemetry::ext::http::client::Body &GetBody() const noexcept override { return body_; } - virtual bool ForEachHeader( - nostd::function_ref callable) - const noexcept override + bool ForEachHeader(nostd::function_ref + callable) const noexcept override { for (const auto &header : headers_) { @@ -118,10 +111,9 @@ class Response : public opentelemetry::ext::http::client::Response return true; } - virtual bool ForEachHeader( - const nostd::string_view &name, - nostd::function_ref callable) - const noexcept override + bool ForEachHeader(const nostd::string_view &name, + nostd::function_ref + callable) const noexcept override { auto range = headers_.equal_range(static_cast(name)); for (auto it = range.first; it != range.second; ++it) @@ -134,7 +126,7 @@ class Response : public opentelemetry::ext::http::client::Response return true; } - virtual opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override + opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override { return status_code_; } @@ -166,14 +158,14 @@ class Session : public opentelemetry::ext::http::client::Session, return http_request_; } - virtual void SendRequest( + void SendRequest( std::shared_ptr callback) noexcept override; - virtual bool CancelSession() noexcept override; + bool CancelSession() noexcept override; - virtual bool FinishSession() noexcept override; + bool FinishSession() noexcept override; - virtual bool IsSessionActive() noexcept override + bool IsSessionActive() noexcept override { return is_session_active_.load(std::memory_order_acquire); } @@ -291,20 +283,17 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient bool FinishAllSessions() noexcept override; - void CleanupSession(uint64_t session_id); - - inline CURLM *GetMultiHandle() noexcept { return multi_handle_; } - - inline void SetMaxSessionsPerConnection(uint64_t max_sessions_per_connection) noexcept - { - max_sessions_per_connection_ = max_sessions_per_connection; - } + void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept override; inline uint64_t GetMaxSessionsPerConnection() const noexcept { return max_sessions_per_connection_; } + void CleanupSession(uint64_t session_id); + + inline CURLM *GetMultiHandle() noexcept { return multi_handle_; } + void MaybeSpawnBackgroundThread(); void ScheduleAddSession(uint64_t session_id); diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index e939962653..34564affc3 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -232,6 +232,8 @@ class HttpClient virtual bool FinishAllSessions() noexcept = 0; + virtual void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept = 0; + virtual ~HttpClient() = default; }; diff --git a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h index 405381c285..f32a075879 100644 --- a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h +++ b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h @@ -51,10 +51,7 @@ class Request : public opentelemetry::ext::http::client::Request void ReplaceHeader(nostd::string_view name, nostd::string_view value) noexcept override; - virtual void SetUri(nostd::string_view uri) noexcept override - { - uri_ = static_cast(uri); - } + void SetUri(nostd::string_view uri) noexcept override { uri_ = static_cast(uri); } void SetTimeoutMs(std::chrono::milliseconds timeout_ms) noexcept override { @@ -74,21 +71,16 @@ class Response : public opentelemetry::ext::http::client::Response public: Response() : status_code_(Http_Ok) {} - virtual const opentelemetry::ext::http::client::Body &GetBody() const noexcept override - { - return body_; - } + const opentelemetry::ext::http::client::Body &GetBody() const noexcept override { return body_; } - virtual bool ForEachHeader( - nostd::function_ref callable) - const noexcept override; + bool ForEachHeader(nostd::function_ref + callable) const noexcept override; - virtual bool ForEachHeader( - const nostd::string_view &name, - nostd::function_ref callable) - const noexcept override; + bool ForEachHeader(const nostd::string_view &name, + nostd::function_ref + callable) const noexcept override; - virtual opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override + opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override { return status_code_; } @@ -137,11 +129,11 @@ class Session : public opentelemetry::ext::http::client::Session (std::shared_ptr), (noexcept, override)); - virtual bool CancelSession() noexcept override; + bool CancelSession() noexcept override; - virtual bool FinishSession() noexcept override; + bool FinishSession() noexcept override; - virtual bool IsSessionActive() noexcept override { return is_session_active_; } + bool IsSessionActive() noexcept override { return is_session_active_; } void SetId(uint64_t session_id) { session_id_ = session_id; } @@ -164,27 +156,18 @@ class Session : public opentelemetry::ext::http::client::Session class HttpClient : public opentelemetry::ext::http::client::HttpClient { public: - HttpClient() { session_ = std::shared_ptr{new Session(*this)}; } + HttpClient(); std::shared_ptr CreateSession( - nostd::string_view) noexcept override - { - return session_; - } + nostd::string_view) noexcept override; - bool CancelAllSessions() noexcept override - { - session_->CancelSession(); - return true; - } + bool CancelAllSessions() noexcept override; - bool FinishAllSessions() noexcept override - { - session_->FinishSession(); - return true; - } + bool FinishAllSessions() noexcept override; + + void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept override; - void CleanupSession(uint64_t session_id) {} + void CleanupSession(uint64_t session_id); std::shared_ptr session_; }; diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 7f1b17e56b..20607cf582 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -211,6 +211,11 @@ bool HttpClient::FinishAllSessions() noexcept return true; } +void HttpClient::SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept +{ + max_sessions_per_connection_ = max_requests_per_connection; +} + void HttpClient::CleanupSession(uint64_t session_id) { std::shared_ptr session; diff --git a/ext/src/http/client/nosend/http_client_nosend.cc b/ext/src/http/client/nosend/http_client_nosend.cc index c2b1c6acf9..021af33095 100644 --- a/ext/src/http/client/nosend/http_client_nosend.cc +++ b/ext/src/http/client/nosend/http_client_nosend.cc @@ -63,6 +63,33 @@ bool Session::FinishSession() noexcept return true; } +HttpClient::HttpClient() +{ + session_ = std::shared_ptr{new Session(*this)}; +} + +std::shared_ptr HttpClient::CreateSession( + nostd::string_view) noexcept +{ + return session_; +} + +bool HttpClient::CancelAllSessions() noexcept +{ + session_->CancelSession(); + return true; +} + +bool HttpClient::FinishAllSessions() noexcept +{ + session_->FinishSession(); + return true; +} + +void HttpClient::SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept {} + +void HttpClient::CleanupSession(uint64_t session_id) {} + } // namespace nosend } // namespace client } // namespace http From 731565a7edb31b1399631192a7640eccea286c96 Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 5 May 2022 12:45:52 +0800 Subject: [PATCH 19/19] Fix style Signed-off-by: owent --- .../include/opentelemetry/exporters/otlp/otlp_http_client.h | 2 +- sdk/test/metrics/sync_metric_storage_test.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 288bf30630..edb0f59afc 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -91,7 +91,7 @@ struct OtlpHttpClientOptions bool input_console_debug, std::chrono::system_clock::duration input_timeout, const OtlpHeaders &input_http_headers, - std::size_t input_concurrent_sessions = 64, + std::size_t input_concurrent_sessions = 64, std::size_t input_max_requests_per_connection = 8) : url(input_url), content_type(input_content_type), diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index ea1db2be0c..7dfc4f9475 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -36,7 +36,7 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) long expected_total_get_requests = 0; long expected_total_put_requests = 0; InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, - InstrumentValueType::kLong}; + InstrumentValueType::kLong}; std::map attributes_get = {{"RequestType", "GET"}}; std::map attributes_put = {{"RequestType", "PUT"}}; @@ -142,7 +142,7 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) double expected_total_get_requests = 0; double expected_total_put_requests = 0; InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, - InstrumentValueType::kDouble}; + InstrumentValueType::kDouble}; std::map attributes_get = {{"RequestType", "GET"}}; std::map attributes_put = {{"RequestType", "PUT"}};