Repository navigation
[Metrics SDK] Implement configurable cardinality limit #4188
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
Changes from all commits
fd95d28
43dbe46
4822e27
456b92e
3c5a4ee
ace38fc
099f8c1
a74a859
c1fa36e
7f8dc73
f03b34d
2e1d77c
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,46 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace metrics | ||
| { | ||
|
|
||
| /** | ||
| * Cardinality limits per instrument type for a MetricReader. | ||
| * | ||
| * All fields default to kDefaultCardinalityLimit (2000), which is the | ||
| * OpenTelemetry Metrics SDK specification default. | ||
| * | ||
| * To configure a reader-level limit, set the relevant fields and pass the | ||
| * struct to MetricReader::SetCardinalityLimits(). The sdk_builder converts | ||
| * the declarative CardinalityLimitsConfiguration (where 0 means "unset, use | ||
| * default_limit") into a fully-resolved CardinalityLimits before calling | ||
| * SetCardinalityLimits(), so SetCardinalityLimits() itself is a plain setter | ||
| * with no sentinel logic. | ||
| */ | ||
| struct CardinalityLimits | ||
| { | ||
| std::size_t default_limit = 2000; | ||
| std::size_t counter = 2000; | ||
| std::size_t gauge = 2000; | ||
| std::size_t histogram = 2000; | ||
| std::size_t observable_counter = 2000; | ||
| std::size_t observable_gauge = 2000; | ||
| std::size_t observable_up_down_counter = 2000; | ||
| std::size_t up_down_counter = 2000; | ||
| }; | ||
|
|
||
| /// The OpenTelemetry Metrics SDK spec default cardinality limit. | ||
| constexpr std::size_t kDefaultCardinalityLimit = 2000; | ||
|
|
||
| } // namespace metrics | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| #include "opentelemetry/sdk/configuration/batch_span_processor_configuration.h" | ||
| #include "opentelemetry/sdk/configuration/boolean_array_attribute_value_configuration.h" | ||
| #include "opentelemetry/sdk/configuration/boolean_attribute_value_configuration.h" | ||
| #include "opentelemetry/sdk/configuration/cardinality_limits_configuration.h" | ||
| #include "opentelemetry/sdk/configuration/composable_probability_sampler_configuration.h" | ||
| #include "opentelemetry/sdk/configuration/configuration.h" | ||
| #include "opentelemetry/sdk/configuration/configured_sdk.h" | ||
|
|
@@ -145,6 +146,8 @@ | |
| #include "opentelemetry/sdk/logs/processor.h" | ||
| #include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" | ||
| #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" | ||
| #include "opentelemetry/sdk/metrics/cardinality_limits.h" | ||
| #include "opentelemetry/sdk/metrics/exemplar/filter_type.h" | ||
| #include "opentelemetry/sdk/metrics/export/metric_producer.h" | ||
| #include "opentelemetry/sdk/metrics/instruments.h" | ||
| #include "opentelemetry/sdk/metrics/meter_config.h" | ||
|
|
@@ -180,10 +183,6 @@ | |
| #include "opentelemetry/version.h" | ||
| #include "src/common/wildcard_match.h" | ||
|
|
||
| #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW | ||
| # include "opentelemetry/sdk/metrics/exemplar/filter_type.h" | ||
| #endif | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
|
|
@@ -257,6 +256,40 @@ static opentelemetry::logs::Severity ToLogSeverity( | |
| return opentelemetry::logs::Severity::kInvalid; | ||
| } | ||
|
|
||
| /// Convert a CardinalityLimitsConfiguration (where 0 means "unset, use | ||
| /// default_limit") into a fully-resolved CardinalityLimits struct ready for | ||
| /// MetricReader::SetCardinalityLimits(). | ||
| static opentelemetry::sdk::metrics::CardinalityLimits ToCardinalityLimits( | ||
| const opentelemetry::sdk::configuration::CardinalityLimitsConfiguration &cfg) | ||
| { | ||
| opentelemetry::sdk::metrics::CardinalityLimits limits; | ||
| // If the caller supplied a non-zero default, use it; otherwise keep the | ||
| // SDK default (kDefaultCardinalityLimit = 2000) already in the struct. | ||
| const std::size_t d = cfg.default_limit != CardinalityLimitsConfiguration::kInheritDefault | ||
| ? cfg.default_limit | ||
| : limits.default_limit; | ||
| limits.default_limit = d; | ||
| limits.counter = cfg.counter != CardinalityLimitsConfiguration::kInheritDefault ? cfg.counter : d; | ||
| limits.gauge = cfg.gauge != CardinalityLimitsConfiguration::kInheritDefault ? cfg.gauge : d; | ||
| limits.histogram = | ||
| cfg.histogram != CardinalityLimitsConfiguration::kInheritDefault ? cfg.histogram : d; | ||
| limits.observable_counter = | ||
| cfg.observable_counter != CardinalityLimitsConfiguration::kInheritDefault | ||
| ? cfg.observable_counter | ||
| : d; | ||
| limits.observable_gauge = cfg.observable_gauge != CardinalityLimitsConfiguration::kInheritDefault | ||
| ? cfg.observable_gauge | ||
| : d; | ||
| limits.observable_up_down_counter = | ||
| cfg.observable_up_down_counter != CardinalityLimitsConfiguration::kInheritDefault | ||
| ? cfg.observable_up_down_counter | ||
| : d; | ||
| limits.up_down_counter = cfg.up_down_counter != CardinalityLimitsConfiguration::kInheritDefault | ||
| ? cfg.up_down_counter | ||
| : d; | ||
| return limits; | ||
| } | ||
|
|
||
| class ResourceAttributeValueSetter | ||
| : public opentelemetry::sdk::configuration::AttributeValueConfigurationVisitor | ||
| { | ||
|
|
@@ -1561,22 +1594,25 @@ std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePer | |
| OTEL_INTERNAL_LOG_WARN("metric producer not supported, ignoring"); | ||
| } | ||
|
|
||
| if (model->cardinality_limits != nullptr) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN("cardinality limits not supported, ignoring"); | ||
| } | ||
|
|
||
| const PeriodicMetricReaderBuilder *builder = registry_->GetPeriodicMetricReaderBuilder(); | ||
|
|
||
| if (builder != nullptr) | ||
| { | ||
| OTEL_INTERNAL_LOG_DEBUG("CreatePeriodicMetricReader() using registered builder"); | ||
| sdk = builder->Build(model, std::move(exporter_sdk)); | ||
| return sdk; | ||
| } | ||
| else | ||
| { | ||
| static const std::string die("No builder for PeriodicMetricReader"); | ||
| throw UnsupportedException(die); | ||
| } | ||
|
|
||
| static const std::string die("No builder for PeriodicMetricReader"); | ||
| throw UnsupportedException(die); | ||
| if (model->cardinality_limits != nullptr) | ||
| { | ||
| sdk->SetCardinalityLimits(ToCardinalityLimits(*model->cardinality_limits)); | ||
| } | ||
|
|
||
| return sdk; | ||
| } | ||
|
|
||
| std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePullMetricReader( | ||
|
|
@@ -1593,7 +1629,7 @@ std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> SdkBuilder::CreatePul | |
|
|
||
| if (model->cardinality_limits != nullptr) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN("cardinality limits not supported, ignoring"); | ||
| sdk->SetCardinalityLimits(ToCardinalityLimits(*model->cardinality_limits)); | ||
| } | ||
|
|
||
| return sdk; | ||
|
|
@@ -1700,6 +1736,20 @@ void SdkBuilder::AddView( | |
| sdk_aggregation_config = CreateAggregationConfig(stream->aggregation, sdk_aggregation_type); | ||
| } | ||
|
|
||
| // Apply aggregation_cardinality_limit from the view stream configuration | ||
| if (stream->aggregation_cardinality_limit != 0) | ||
| { | ||
| if (sdk_aggregation_config) | ||
| { | ||
| sdk_aggregation_config->cardinality_limit_ = stream->aggregation_cardinality_limit; | ||
| } | ||
| else | ||
| { | ||
| sdk_aggregation_config = std::make_shared<opentelemetry::sdk::metrics::AggregationConfig>( | ||
|
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. I think this breaks for histograms. When only
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. Apologies for missing histograms. I just confirmed that this rejects the view for any histogram/base2-exponential-histogram instrument when only aggregation_cardinality_limit is set. Fixed in #4314: AddView() now resolves the instrument's effective aggregation type (falling back to the instrument-derived default, same as ViewRegistry does) and builds the matching AggregationConfig subtype before applying the limit. Added a test that reproduces the rejection against the old code and verifies the fix. |
||
| stream->aggregation_cardinality_limit); | ||
| } | ||
| } | ||
|
|
||
| std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> sdk_attribute_processor; | ||
|
|
||
| if (stream->attribute_keys != nullptr) | ||
|
|
||
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.
It looks like reader-level limits are only stored, but never actually applied during collection. So if someone configures a reader limit of 10, the reader can still export more than 10 series. Should we really expose this configuration before it is enforced?
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.
Yes, I do agree on this one: reader-level limits are currently just plumbing with no enforcement, so a configured limit of 10 doesn't actually cap anything yet. I called this out as a known follow-up in the original PR description; I'd like to tackle enforcement in a separate PR since it touches the collection path per CollectorHandle, which is a bigger change than the histogram fix. Left the TODO in place for now to track it; happy to prioritize it if you'd rather not ship the config surface unenforced in the meantime.