-
Notifications
You must be signed in to change notification settings - Fork 591
[SDK] feat: exemplar filters #4267
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
21c3ab6
b13986f
8dc295b
f99c77b
99d868a
a12d4c3
d793892
60b9440
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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW | ||
|
|
||
| # include <stdint.h> | ||
| # include <memory> | ||
| # include <utility> | ||
| # include <vector> | ||
|
|
||
| # include "opentelemetry/common/timestamp.h" | ||
| # include "opentelemetry/context/context.h" | ||
| # include "opentelemetry/nostd/shared_ptr.h" | ||
| # include "opentelemetry/sdk/metrics/data/exemplar_data.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/filter_type.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/reservoir.h" | ||
| # include "opentelemetry/trace/context.h" | ||
| # include "opentelemetry/trace/span_context.h" | ||
| # include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace metrics | ||
| { | ||
|
|
||
| /** | ||
| * A reservoir that pre-filters measurements according to an ExemplarFilterType before | ||
| * delegating eligible ones to a wrapped reservoir. | ||
| */ | ||
| class FilteredExemplarReservoir final : public ExemplarReservoir | ||
| { | ||
| public: | ||
| FilteredExemplarReservoir(ExemplarFilterType filter_type, | ||
| nostd::shared_ptr<ExemplarReservoir> reservoir) | ||
| : should_sample_(SelectFilter(filter_type)), reservoir_(std::move(reservoir)) | ||
| {} | ||
|
|
||
| void OfferMeasurement(int64_t value, | ||
| const MetricAttributes &attributes, | ||
| const opentelemetry::context::Context &context, | ||
| const opentelemetry::common::SystemTimestamp ×tamp) noexcept override | ||
| { | ||
| if (should_sample_(context)) | ||
| { | ||
| reservoir_->OfferMeasurement(value, attributes, context, timestamp); | ||
| } | ||
| } | ||
|
|
||
| void OfferMeasurement(double value, | ||
| const MetricAttributes &attributes, | ||
| const opentelemetry::context::Context &context, | ||
| const opentelemetry::common::SystemTimestamp ×tamp) noexcept override | ||
| { | ||
| if (should_sample_(context)) | ||
| { | ||
| reservoir_->OfferMeasurement(value, attributes, context, timestamp); | ||
| } | ||
| } | ||
|
|
||
| std::vector<std::shared_ptr<ExemplarData>> CollectAndReset( | ||
| const MetricAttributes &pointAttributes) noexcept override | ||
| { | ||
| return reservoir_->CollectAndReset(pointAttributes); | ||
| } | ||
|
|
||
| private: | ||
| using ShouldSampleFn = bool (*)(const opentelemetry::context::Context &context); | ||
|
|
||
| static bool AlwaysOn(const opentelemetry::context::Context & /* context */) noexcept | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| static bool AlwaysOff(const opentelemetry::context::Context & /* context */) noexcept | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| static bool TraceBased(const opentelemetry::context::Context &context) noexcept | ||
| { | ||
| const opentelemetry::trace::SpanContext span_context = | ||
| opentelemetry::trace::GetSpanContext(context); | ||
| return span_context.IsValid() && span_context.IsSampled(); | ||
| } | ||
|
|
||
| static ShouldSampleFn SelectFilter(ExemplarFilterType filter_type) noexcept | ||
| { | ||
| switch (filter_type) | ||
| { | ||
| case ExemplarFilterType::kAlwaysOn: | ||
| return &AlwaysOn; | ||
| case ExemplarFilterType::kAlwaysOff: | ||
| return &AlwaysOff; | ||
| case ExemplarFilterType::kTraceBased: | ||
| return &TraceBased; | ||
| } | ||
| return &TraceBased; // unreachable; all enumerators handled above | ||
| } | ||
|
|
||
| ShouldSampleFn should_sample_; | ||
| nostd::shared_ptr<ExemplarReservoir> reservoir_; | ||
| }; | ||
|
|
||
| } // namespace metrics | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
|
|
||
| #endif // ENABLE_METRICS_EXEMPLAR_PREVIEW | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
| # include "opentelemetry/common/macros.h" | ||
| # include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/filter_type.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/reservoir.h" | ||
| # include "opentelemetry/sdk/metrics/exemplar/simple_fixed_size_exemplar_reservoir.h" | ||
| # include "opentelemetry/version.h" | ||
|
|
||
|
|
@@ -48,7 +50,8 @@ static inline size_t GetSimpleFixedReservoirDefaultSize(const AggregationType ag | |
| static inline nostd::shared_ptr<ExemplarReservoir> GetExemplarReservoir( | ||
| const AggregationType agg_type, | ||
| const AggregationConfig *agg_config, | ||
| const InstrumentDescriptor &instrument_descriptor) | ||
| const InstrumentDescriptor &instrument_descriptor, | ||
| ExemplarFilterType filter_type) | ||
| { | ||
| if (agg_type == AggregationType::kHistogram) | ||
| { | ||
|
|
@@ -61,18 +64,21 @@ static inline nostd::shared_ptr<ExemplarReservoir> GetExemplarReservoir( | |
| // | ||
| if (histogram_agg_config != nullptr && histogram_agg_config->boundaries_.size() > 1) | ||
| { | ||
| return nostd::shared_ptr<ExemplarReservoir>(new AlignedHistogramBucketExemplarReservoir( | ||
| histogram_agg_config->boundaries_.size(), | ||
| AlignedHistogramBucketExemplarReservoir::GetHistogramCellSelector( | ||
| histogram_agg_config->boundaries_), | ||
| GetMapAndResetCellMethod(instrument_descriptor))); | ||
| return ExemplarReservoir::GetSimpleFilteredExemplarReservoir( | ||
| filter_type, | ||
| nostd::shared_ptr<ExemplarReservoir>(new AlignedHistogramBucketExemplarReservoir( | ||
| histogram_agg_config->boundaries_.size(), | ||
| AlignedHistogramBucketExemplarReservoir::GetHistogramCellSelector( | ||
| histogram_agg_config->boundaries_), | ||
| GetMapAndResetCellMethod(instrument_descriptor)))); | ||
| } | ||
| } | ||
|
|
||
| return nostd::shared_ptr<ExemplarReservoir>(new SimpleFixedSizeExemplarReservoir( | ||
| GetSimpleFixedReservoirDefaultSize(agg_type, agg_config), | ||
| SimpleFixedSizeExemplarReservoir::GetSimpleFixedSizeCellSelector(), | ||
| GetMapAndResetCellMethod(instrument_descriptor))); | ||
| return ExemplarReservoir::GetSimpleFilteredExemplarReservoir( | ||
|
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. For
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. For the performance, you are right. But according to spec, looks like current implementation is correct. Here is what i understand: If i missunderstand the spec, please let me know where I got it wrong. (spec is bit confused to me.) |
||
| filter_type, nostd::shared_ptr<ExemplarReservoir>(new SimpleFixedSizeExemplarReservoir( | ||
| GetSimpleFixedReservoirDefaultSize(agg_type, agg_config), | ||
| SimpleFixedSizeExemplarReservoir::GetSimpleFixedSizeCellSelector(), | ||
| GetMapAndResetCellMethod(instrument_descriptor)))); | ||
| } | ||
| } // namespace metrics | ||
| } // namespace sdk | ||
|
|
||
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 think
AlwaysOnstill does not fully work here. This forwards the measurement, but the real reservoir drops it later if there is no valid span context. Since trace/span ids are optional for exemplars,AlwaysOn + Context{}should still produce an exemplar without trace ids. Can we add a test that collects from the real reservoir and checks the returned exemplar is non-null?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.
Same point for the above #4267 (comment)