-
Notifications
You must be signed in to change notification settings - Fork 587
[API/SDK] feat: exemplar filters #4191
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
Open
proost
wants to merge
9
commits into
open-telemetry:main
Choose a base branch
from
proost:feat-exemplar-filters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1cea021
feat: exemplar filters
proost ce49c80
doc: update changelog
proost c9418e6
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
proost 34cc7e6
test: change to mock object
proost 92c9406
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
proost 3363545
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
proost 0f4b8ce
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
proost f68f59b
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
proost 65d7375
Merge branch 'main' into feat-exemplar-filters
dbarker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
sdk/include/opentelemetry/sdk/metrics/exemplar/filtered_exemplar_reservoir.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is a nice addition for performance and reducing dynamic memory allocation. The
GetSpanmethod above is commonly used just to access the SpanContext from the span inside a full Context with this pattern:When the context does not have a span then one is dynamically allocated just to create an invalid span context.
Consider breaking this change out to a separate PR and replacing that pattern where appropriate.
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.
OK, I make a separate PR: #4254
After If the PR is merged, i will update here.