|
3 | 3 |
|
4 | 4 | #include <gtest/gtest.h> |
5 | 5 |
|
| 6 | +#include <cstddef> |
6 | 7 | #include <memory> |
7 | 8 | #include <string> |
8 | 9 | #include <utility> |
|
16 | 17 | #include "opentelemetry/sdk/configuration/always_on_sampler_configuration.h" |
17 | 18 | #include "opentelemetry/sdk/configuration/extension_push_metric_exporter_builder.h" |
18 | 19 | #include "opentelemetry/sdk/configuration/extension_push_metric_exporter_configuration.h" |
| 20 | +#include "opentelemetry/sdk/configuration/instrument_type.h" |
19 | 21 | #include "opentelemetry/sdk/configuration/logger_config_configuration.h" |
20 | 22 | #include "opentelemetry/sdk/configuration/logger_configurator_configuration.h" |
21 | 23 | #include "opentelemetry/sdk/configuration/logger_matcher_and_config_configuration.h" |
|
30 | 32 | #include "opentelemetry/sdk/configuration/span_limits_configuration.h" |
31 | 33 | #include "opentelemetry/sdk/configuration/trace_id_ratio_based_sampler_configuration.h" |
32 | 34 | #include "opentelemetry/sdk/configuration/tracer_provider_configuration.h" |
| 35 | +#include "opentelemetry/sdk/configuration/view_configuration.h" |
| 36 | +#include "opentelemetry/sdk/configuration/view_selector_configuration.h" |
| 37 | +#include "opentelemetry/sdk/configuration/view_stream_configuration.h" |
33 | 38 |
|
| 39 | +#include "opentelemetry/nostd/function_ref.h" |
| 40 | +#include "opentelemetry/nostd/variant.h" |
34 | 41 | #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" |
35 | 42 | #include "opentelemetry/sdk/instrumentationscope/scope_configurator.h" |
36 | 43 | #include "opentelemetry/sdk/logs/logger_config.h" |
| 44 | +#include "opentelemetry/sdk/metrics/aggregation/aggregation.h" |
| 45 | +#include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" |
| 46 | +#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" |
| 47 | +#include "opentelemetry/sdk/metrics/data/point_data.h" |
| 48 | +#include "opentelemetry/sdk/metrics/instruments.h" |
37 | 49 | #include "opentelemetry/sdk/metrics/metric_reader.h" |
| 50 | +#include "opentelemetry/sdk/metrics/view/view.h" |
| 51 | +#include "opentelemetry/sdk/metrics/view/view_registry.h" |
38 | 52 | #include "opentelemetry/sdk/resource/resource.h" |
39 | 53 | #include "opentelemetry/sdk/trace/sampler.h" |
40 | 54 | #include "opentelemetry/sdk/trace/span_limits.h" |
@@ -260,3 +274,110 @@ TEST(SdkBuilder, CreatePeriodicMetricReader) |
260 | 274 | EXPECT_EQ(captured->timeout, model.timeout); |
261 | 275 | EXPECT_TRUE(captured->exporter != nullptr); |
262 | 276 | } |
| 277 | + |
| 278 | +namespace |
| 279 | +{ |
| 280 | + |
| 281 | +// Builds a ViewConfiguration selecting the given instrument type, with only |
| 282 | +// aggregation_cardinality_limit set on the stream (no explicit `aggregation` block). |
| 283 | +std::unique_ptr<config_sdk::ViewConfiguration> MakeCardinalityOnlyViewConfig( |
| 284 | + config_sdk::InstrumentType instrument_type, |
| 285 | + std::size_t cardinality_limit) |
| 286 | +{ |
| 287 | + auto model = std::make_unique<config_sdk::ViewConfiguration>(); |
| 288 | + model->selector = std::make_unique<config_sdk::ViewSelectorConfiguration>(); |
| 289 | + model->selector->instrument_type = instrument_type; |
| 290 | + |
| 291 | + model->stream = std::make_unique<config_sdk::ViewStreamConfiguration>(); |
| 292 | + model->stream->aggregation_cardinality_limit = cardinality_limit; |
| 293 | + |
| 294 | + return model; |
| 295 | +} |
| 296 | + |
| 297 | +} // namespace |
| 298 | + |
| 299 | +TEST(SdkBuilder, AddViewHistogramCardinalityLimitOnly) |
| 300 | +{ |
| 301 | + namespace metrics_sdk = opentelemetry::sdk::metrics; |
| 302 | + |
| 303 | + auto model = MakeCardinalityOnlyViewConfig(config_sdk::InstrumentType::histogram, 42); |
| 304 | + |
| 305 | + auto registry = std::make_shared<config_sdk::Registry>(); |
| 306 | + config_sdk::SdkBuilder builder(registry); |
| 307 | + |
| 308 | + metrics_sdk::ViewRegistry view_registry; |
| 309 | + builder.AddView(&view_registry, model); |
| 310 | + |
| 311 | + metrics_sdk::InstrumentDescriptor instrument_descriptor{ |
| 312 | + "", "", "", metrics_sdk::InstrumentType::kHistogram, metrics_sdk::InstrumentValueType::kLong}; |
| 313 | + auto instrumentation_scope = scope_sdk::InstrumentationScope::Create(""); |
| 314 | + |
| 315 | + int matched = 0; |
| 316 | + view_registry.FindViews( |
| 317 | + instrument_descriptor, *instrumentation_scope, [&](const metrics_sdk::View &view) { |
| 318 | + matched++; |
| 319 | + // The view must not be rejected: it should carry a |
| 320 | + // HistogramAggregationConfig (not a plain AggregationConfig), since |
| 321 | + // the instrument's default aggregation for kHistogram is kHistogram. |
| 322 | + auto *aggregation_config = view.GetAggregationConfig(); |
| 323 | + EXPECT_NE(aggregation_config, nullptr); |
| 324 | + if (aggregation_config) |
| 325 | + { |
| 326 | + EXPECT_EQ(aggregation_config->GetType(), metrics_sdk::AggregationType::kHistogram); |
| 327 | + EXPECT_EQ(aggregation_config->cardinality_limit_, 42u); |
| 328 | + |
| 329 | + // Pin what users actually receive: building the aggregation from this config |
| 330 | + // must keep the SDK's default bucket boundaries, not silently collapse to a |
| 331 | + // single bucket. A default-constructed HistogramAggregationConfig has empty |
| 332 | + // boundaries_, which the aggregation takes literally (as opposed to a null |
| 333 | + // config pointer, which falls back to the default boundaries), so AddView() |
| 334 | + // must populate boundaries_ explicitly. |
| 335 | + auto aggregation = metrics_sdk::DefaultAggregation::CreateAggregation( |
| 336 | + metrics_sdk::AggregationType::kHistogram, instrument_descriptor, aggregation_config); |
| 337 | + EXPECT_NE(aggregation, nullptr); |
| 338 | + if (aggregation) |
| 339 | + { |
| 340 | + auto histogram_data = |
| 341 | + opentelemetry::nostd::get<metrics_sdk::HistogramPointData>(aggregation->ToPoint()); |
| 342 | + EXPECT_EQ(histogram_data.boundaries_.size(), 15u); |
| 343 | + EXPECT_EQ(histogram_data.counts_.size(), 16u); |
| 344 | + } |
| 345 | + } |
| 346 | + return true; |
| 347 | + }); |
| 348 | + |
| 349 | + EXPECT_EQ(matched, 1); |
| 350 | +} |
| 351 | + |
| 352 | +TEST(SdkBuilder, AddViewCounterCardinalityLimitOnly) |
| 353 | +{ |
| 354 | + namespace metrics_sdk = opentelemetry::sdk::metrics; |
| 355 | + |
| 356 | + auto model = MakeCardinalityOnlyViewConfig(config_sdk::InstrumentType::counter, 7); |
| 357 | + |
| 358 | + auto registry = std::make_shared<config_sdk::Registry>(); |
| 359 | + config_sdk::SdkBuilder builder(registry); |
| 360 | + |
| 361 | + metrics_sdk::ViewRegistry view_registry; |
| 362 | + builder.AddView(&view_registry, model); |
| 363 | + |
| 364 | + metrics_sdk::InstrumentDescriptor instrument_descriptor{ |
| 365 | + "", "", "", metrics_sdk::InstrumentType::kCounter, metrics_sdk::InstrumentValueType::kLong}; |
| 366 | + auto instrumentation_scope = scope_sdk::InstrumentationScope::Create(""); |
| 367 | + |
| 368 | + int matched = 0; |
| 369 | + view_registry.FindViews( |
| 370 | + instrument_descriptor, *instrumentation_scope, [&](const metrics_sdk::View &view) { |
| 371 | + matched++; |
| 372 | + auto *aggregation_config = view.GetAggregationConfig(); |
| 373 | + EXPECT_NE(aggregation_config, nullptr); |
| 374 | + if (aggregation_config) |
| 375 | + { |
| 376 | + EXPECT_EQ(aggregation_config->GetType(), metrics_sdk::AggregationType::kDefault); |
| 377 | + EXPECT_EQ(aggregation_config->cardinality_limit_, 7u); |
| 378 | + } |
| 379 | + return true; |
| 380 | + }); |
| 381 | + |
| 382 | + EXPECT_EQ(matched, 1); |
| 383 | +} |
0 commit comments