-
Notifications
You must be signed in to change notification settings - Fork 612
[SDK] fix: fixed size exemplar reservoir works correctly #4429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
33c6dd4
deb01d6
fd8e429
a978029
c7864c3
6c4e5f6
5e65718
ee3c93e
980ef1e
64c406f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW | ||
|
|
||
| # include <cstdint> | ||
| # include <memory> | ||
| # include <vector> | ||
|
|
||
|
|
@@ -68,6 +69,11 @@ class SimpleFixedSizeExemplarReservoir : public FixedSizeExemplarReservoir | |
| // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#simplefixedsizeexemplarreservoir | ||
| // | ||
|
|
||
| if (size_ == 0) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| size_t measurement_num = measurements_seen_++; | ||
| size_t index = static_cast<size_t>(-1); | ||
|
|
||
|
|
@@ -77,22 +83,26 @@ class SimpleFixedSizeExemplarReservoir : public FixedSizeExemplarReservoir | |
| } | ||
| else | ||
| { | ||
| size_t random_index = sdk::common::Random::GenerateRandom64() % measurement_num; | ||
|
|
||
| if (random_index < size_) | ||
| { | ||
| index = random_index; | ||
| } | ||
| return GetRandomCellIndex(size_, measurement_num, sdk::common::Random::GenerateRandom64()); | ||
| } | ||
|
|
||
| return static_cast<int>(index); | ||
| } | ||
|
|
||
| void reset() override {} | ||
| void reset() override { measurements_seen_ = 0; } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reset is correct, but the sampling calculation just above is still off by one.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I updated: c7864c3 |
||
|
|
||
| private: | ||
| static int GetRandomCellIndex(size_t size, | ||
| size_t measurement_num, | ||
| uint64_t random_value) noexcept | ||
| { | ||
| size_t random_index = random_value % (measurement_num + 1); | ||
| return random_index < size ? static_cast<int>(random_index) : -1; | ||
| } | ||
|
|
||
| size_t measurements_seen_ = 0; | ||
| size_t size_; | ||
| friend class SimpleFixedSizeCellSelectorTestPeer; | ||
| }; // class SimpleFixedSizeCellSelector | ||
|
|
||
| }; // class SimpleFixedSizeExemplarReservoir | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW | ||
|
|
||
| # include <benchmark/benchmark.h> | ||
| # include <cstdint> | ||
| # include <string> | ||
| # include <utility> | ||
|
|
||
| # include "opentelemetry/context/context.h" | ||
| # include "opentelemetry/nostd/shared_ptr.h" | ||
| # include "opentelemetry/sdk/metrics/data/exemplar_data.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/reservoir.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/reservoir_cell.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.h" | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using opentelemetry::context::Context; | ||
| using opentelemetry::nostd::shared_ptr; | ||
| using opentelemetry::sdk::metrics::ExemplarReservoir; | ||
| using opentelemetry::sdk::metrics::MetricAttributes; | ||
| using opentelemetry::sdk::metrics::ReservoirCell; | ||
| using opentelemetry::sdk::metrics::SimpleFixedSizeExemplarReservoir; | ||
|
|
||
| class SharedSimpleFixedSizeExemplarReservoirFixture : public benchmark::Fixture | ||
| { | ||
| public: | ||
| using benchmark::Fixture::SetUp; | ||
| using benchmark::Fixture::TearDown; | ||
|
|
||
| void SetUp(benchmark::State &state) override | ||
| { | ||
| if (state.thread_index() != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| auto selector = SimpleFixedSizeExemplarReservoir::GetSimpleFixedSizeCellSelector(1); | ||
| reservoir_ = shared_ptr<ExemplarReservoir>{ | ||
| new SimpleFixedSizeExemplarReservoir{1, selector, &ReservoirCell::GetAndResetDouble}}; | ||
|
|
||
| // Model a reservoir partway through a normal collection interval. This | ||
| // keeps the benchmark focused on the steady-state offer path. | ||
| for (int64_t i = 0; i < 1024; ++i) | ||
| { | ||
| reservoir_->OfferMeasurement(static_cast<double>(i), attributes_, context_); | ||
| } | ||
| } | ||
|
|
||
| void TearDown(benchmark::State &state) override | ||
| { | ||
| if (state.thread_index() == 0) | ||
| { | ||
| reservoir_ = nullptr; | ||
| } | ||
| } | ||
|
|
||
| protected: | ||
| shared_ptr<ExemplarReservoir> reservoir_; | ||
| MetricAttributes attributes_{{"http.request.method", "GET"}, | ||
| {"http.route", "/checkout"}, | ||
| {"http.response.status_code", int64_t{200}}, | ||
| {"service.version", "1.42.0"}}; | ||
| Context context_; | ||
| }; | ||
|
|
||
| BENCHMARK_DEFINE_F(SharedSimpleFixedSizeExemplarReservoirFixture, OfferMeasurement) | ||
| (benchmark::State &state) | ||
| { | ||
| double value = 10.0 + static_cast<double>(state.thread_index()); | ||
| for (auto _ : state) | ||
| { | ||
| reservoir_->OfferMeasurement(value, attributes_, context_); | ||
| value += 0.001; | ||
| } | ||
|
|
||
| benchmark::DoNotOptimize(value); | ||
| state.SetItemsProcessed(state.iterations()); | ||
| } | ||
|
|
||
| BENCHMARK_REGISTER_F(SharedSimpleFixedSizeExemplarReservoirFixture, OfferMeasurement) | ||
| ->ThreadRange(1, 8) | ||
| ->UseRealTime() | ||
| ->Unit(benchmark::kNanosecond); | ||
|
|
||
| } // namespace | ||
|
|
||
| BENCHMARK_MAIN(); | ||
|
|
||
| #endif // ENABLE_METRICS_EXEMPLAR_PREVIEW |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This serializes offers and collection for each reservoir. That is the right correctness fix, and the feature is still
ENABLE_METRICS_EXEMPLAR_PREVIEW-gated. It does add contention to the exemplar-enabled record path, so please mention that tradeoff in the PR description and open a follow-up to benchmark or explore a less contended design before exemplars become stable.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because resetting "reservoir_cell_selector_" is called in "CollectAndReset". "CollectAndReset" is never called in the this repo, But i can't find what is contract about thread-safe.
Absolutely right. I bit more digging out atomic operation way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm considering using atomic variable, But it is not easy to change it and it needs broader change of ReservoirCell and ReservoirCellSelector.
So i added benchmark to the description.
6c4e5f6