Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions sdk/test/metrics/cardinality_limit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ TEST_P(WritableMetricStorageCardinalityLimitTestFixture, LongCounterSumAggregati
count_attributes++;
if (data_attr.attributes.begin()->first == kAttributesLimitOverflowKey)
{
// Per the spec, the overflow data point MUST contain exactly one
// attribute, with the key `otel.metric.overflow` and the boolean
// value `true`.
EXPECT_EQ(data_attr.attributes.size(), 1u);
EXPECT_EQ(nostd::get<bool>(data_attr.attributes.begin()->second), true);
EXPECT_EQ(nostd::get<int64_t>(data.value_), record_value * 6);
overflow_present = true;
}
Expand All @@ -159,15 +164,22 @@ INSTANTIATE_TEST_SUITE_P(All,
WritableMetricStorageCardinalityLimitTestFixture,
::testing::Values(AggregationTemporality::kDelta));

// Pin the overflow attribute key to the value defined in the OpenTelemetry
// Metrics SDK specification. The previous value `otel.metrics.overflow`
// (plural) silently diverged from the spec for years because every other
// metrics test referenced the C++ symbol instead of the literal, so a typo
// in the constant could not be caught by tests. Keep this assertion against
// the literal string so any future drift is detected immediately.
// Pin the overflow attribute key and value to the contract defined in the
// OpenTelemetry Metrics SDK specification. The previous key value
// `otel.metrics.overflow` (plural) silently diverged from the spec for years
// because every other metrics test referenced the C++ symbol instead of the
// literal, so a typo in the constant could not be caught by tests. Keep this
// assertion against the literals so any future drift is detected immediately.
// Spec:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#cardinality-limits
TEST(CardinalityLimitOverflowAttribute, MatchesSpecLiteral)
{
EXPECT_EQ(opentelemetry::sdk::metrics::kAttributesLimitOverflowKey, "otel.metric.overflow");
EXPECT_EQ(opentelemetry::sdk::metrics::kAttributesLimitOverflowValue, true);
// The precomputed overflow attribute set MUST contain exactly the spec key
// mapped to the boolean value `true`.
ASSERT_EQ(opentelemetry::sdk::metrics::kOverflowAttributes.size(), 1u);
const auto &entry = *opentelemetry::sdk::metrics::kOverflowAttributes.begin();
EXPECT_EQ(entry.first, "otel.metric.overflow");
EXPECT_EQ(nostd::get<bool>(entry.second), true);
}
24 changes: 18 additions & 6 deletions sdk/test/metrics/sum_aggregation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,15 @@ TEST(HistogramToSumFilterAttributesWithCardinalityLimit, Double)
}
else
{
EXPECT_NE(md.point_data_attr_[i].attributes.end(),
md.point_data_attr_[i].attributes.find(
sdk::metrics::kAttributesLimitOverflowKey));
const auto overflow_it =
md.point_data_attr_[i].attributes.find(sdk::metrics::kAttributesLimitOverflowKey);
EXPECT_NE(md.point_data_attr_[i].attributes.end(), overflow_it);
if (overflow_it != md.point_data_attr_[i].attributes.end())
{
// Spec: overflow point MUST be tagged with the boolean value
// `true`.
EXPECT_EQ(opentelemetry::nostd::get<bool>(overflow_it->second), true);
}
}
}
}
Expand Down Expand Up @@ -390,9 +396,15 @@ TEST(CounterToSumFilterAttributesWithCardinalityLimit, Double)
}
else
{
EXPECT_NE(md.point_data_attr_[i].attributes.end(),
md.point_data_attr_[i].attributes.find(
sdk::metrics::kAttributesLimitOverflowKey));
const auto overflow_it =
md.point_data_attr_[i].attributes.find(sdk::metrics::kAttributesLimitOverflowKey);
EXPECT_NE(md.point_data_attr_[i].attributes.end(), overflow_it);
if (overflow_it != md.point_data_attr_[i].attributes.end())
{
// Spec: overflow point MUST be tagged with the boolean value
// `true`.
EXPECT_EQ(opentelemetry::nostd::get<bool>(overflow_it->second), true);
}
}
}
}
Expand Down
Loading