Skip to content

Commit 800ba57

Browse files
committed
[Metrics SDK] Implement configurable cardinality limit
Fixes #3292 Implements configurable aggregation cardinality limits at view-level and reader-level as specified by the OpenTelemetry Metrics SDK spec. Changes: - cardinality_limits.h: New header with CardinalityLimits struct and constexpr kDefaultCardinalityLimit = 2000 - metric_reader.h: Include cardinality_limits.h; add GetCardinalityLimit() and SetCardinalityLimits() methods - metric_reader.cc: Implement per-instrument-type limit lookup (plain setter, no sentinel logic) - metric_collector.h/.cc: GetCardinalityLimit() in CollectorHandle defaults to CardinalityLimits::kDefaultCardinalityLimit; MetricCollector delegates to underlying MetricReader - meter_context.h/.cc: Remove GetReaderCardinalityLimit() — per-reader limits are accessed via GetCollectors() directly - sdk_builder.cc: Add ToCardinalityLimits() helper in anonymous namespace to convert CardinalityLimitsConfiguration (0 = unset) to CardinalityLimits; used by both periodic and pull reader creation paths - cardinality_limits_configuration.h: Initialize default_limit to 2000, per-instrument fields to 0 (unset sentinel); eliminates UB - CHANGELOG.md: Document the new feature - metric_reader_test.cc, metric_collector_test.cc: Unit tests; collector tests use GetCollectors() instead of removed GetReaderCardinalityLimit(); added ReaderCardinalityLimitEnforcedOnCollection integration test - sdk/test/metrics/BUILD: Add //sdk/src/metrics dep to all_tests
1 parent 43dbe46 commit 800ba57

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Increment the:
1818
* [RELEASE] Bump main branch to 1.29.0-dev
1919
[#4259](https://github.com/open-telemetry/opentelemetry-cpp/pull/4259)
2020

21+
* [ETW] Ensure spans own their names until they are ended
22+
[#4247](https://github.com/open-telemetry/opentelemetry-cpp/pull/4247)
23+
2124
* [Metrics SDK] Implement configurable cardinality limit
2225
[#4188](https://github.com/open-telemetry/opentelemetry-cpp/pull/4188)
2326

‎sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ class CollectorHandle
4141

4242
/**
4343
* Get the cardinality limit for a given instrument type.
44-
* Returns kAggregationCardinalityLimit (2000) by default.
4544
*
4645
* @param instrument_type The instrument type to get the limit for
4746
* @return The cardinality limit for the requested instrument type
4847
*/
49-
virtual std::size_t GetCardinalityLimit(InstrumentType instrument_type) noexcept
50-
{
51-
(void)instrument_type;
52-
return kDefaultCardinalityLimit;
53-
}
48+
virtual std::size_t GetCardinalityLimit(InstrumentType instrument_type) const noexcept = 0;
5449
};
5550

5651
/**
@@ -76,7 +71,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle
7671
AggregationTemporality GetAggregationTemporality(
7772
InstrumentType instrument_type) noexcept override;
7873

79-
std::size_t GetCardinalityLimit(InstrumentType instrument_type) noexcept override;
74+
std::size_t GetCardinalityLimit(InstrumentType instrument_type) const noexcept override;
8075

8176
/**
8277
* The callback to be called for each metric exporter. This will only be those

‎sdk/src/metrics/state/metric_collector.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ AggregationTemporality MetricCollector::GetAggregationTemporality(
4646
return aggregation_temporality;
4747
}
4848

49-
std::size_t MetricCollector::GetCardinalityLimit(InstrumentType instrument_type) noexcept
49+
std::size_t MetricCollector::GetCardinalityLimit(InstrumentType instrument_type) const noexcept
5050
{
5151
return metric_reader_->GetCardinalityLimit(instrument_type);
5252
}

‎sdk/test/metrics/common.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class MockCollectorHandle : public opentelemetry::sdk::metrics::CollectorHandle
6767
opentelemetry::sdk::metrics::AggregationTemporality GetAggregationTemporality(
6868
opentelemetry::sdk::metrics::InstrumentType) noexcept override;
6969

70+
std::size_t GetCardinalityLimit(
71+
opentelemetry::sdk::metrics::InstrumentType) const noexcept override
72+
{
73+
return opentelemetry::sdk::metrics::kDefaultCardinalityLimit;
74+
}
75+
7076
private:
7177
opentelemetry::sdk::metrics::AggregationTemporality temporality_;
7278
};

0 commit comments

Comments
 (0)