|
15 | 15 | from logging import WARNING
|
16 | 16 | from os import environ
|
17 | 17 | from unittest import TestCase
|
18 |
| -from unittest.mock import patch |
| 18 | +from unittest.mock import Mock, patch |
19 | 19 |
|
20 | 20 | from requests import Session
|
21 | 21 | from requests.models import Response
|
|
40 | 40 | OTEL_EXPORTER_OTLP_HEADERS,
|
41 | 41 | OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
|
42 | 42 | OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
|
| 43 | + OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION, |
43 | 44 | OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
|
44 | 45 | OTEL_EXPORTER_OTLP_METRICS_HEADERS,
|
45 | 46 | OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE,
|
|
54 | 55 | ObservableUpDownCounter,
|
55 | 56 | UpDownCounter,
|
56 | 57 | )
|
| 58 | +from opentelemetry.sdk.metrics._internal.aggregation import ( |
| 59 | + _ExplicitBucketHistogramAggregation, |
| 60 | + _ExponentialBucketHistogramAggregation, |
| 61 | +) |
57 | 62 | from opentelemetry.sdk.metrics.export import (
|
58 | 63 | AggregationTemporality,
|
59 | 64 | MetricExportResult,
|
60 | 65 | MetricsData,
|
61 | 66 | ResourceMetrics,
|
62 | 67 | ScopeMetrics,
|
63 | 68 | )
|
| 69 | +from opentelemetry.sdk.metrics.view import DefaultAggregation |
64 | 70 | from opentelemetry.sdk.resources import Resource
|
65 | 71 | from opentelemetry.sdk.util.instrumentation import (
|
66 | 72 | InstrumentationScope as SDKInstrumentationScope,
|
@@ -424,3 +430,60 @@ def test_aggregation_temporality(self):
|
424 | 430 | otlp_metric_exporter._preferred_temporality[ObservableGauge],
|
425 | 431 | AggregationTemporality.CUMULATIVE,
|
426 | 432 | )
|
| 433 | + |
| 434 | + def test_exponential_explicit_bucket_histogram(self): |
| 435 | + |
| 436 | + histogram = Mock(spec=Histogram) |
| 437 | + default_aggregation = DefaultAggregation() |
| 438 | + |
| 439 | + self.assertIsInstance( |
| 440 | + default_aggregation._create_aggregation(histogram, Mock(), Mock()), |
| 441 | + _ExplicitBucketHistogramAggregation, |
| 442 | + ) |
| 443 | + |
| 444 | + with patch.dict( |
| 445 | + environ, |
| 446 | + { |
| 447 | + OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "base2_exponential_bucket_histogram" |
| 448 | + }, |
| 449 | + ): |
| 450 | + self.assertIsInstance( |
| 451 | + default_aggregation._create_aggregation( |
| 452 | + histogram, Mock(), Mock() |
| 453 | + ), |
| 454 | + _ExponentialBucketHistogramAggregation, |
| 455 | + ) |
| 456 | + |
| 457 | + with patch.dict( |
| 458 | + environ, |
| 459 | + {OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "abc"}, |
| 460 | + ): |
| 461 | + with self.assertLogs(level=WARNING) as log: |
| 462 | + self.assertIsInstance( |
| 463 | + default_aggregation._create_aggregation( |
| 464 | + histogram, Mock(), Mock() |
| 465 | + ), |
| 466 | + _ExplicitBucketHistogramAggregation, |
| 467 | + ) |
| 468 | + self.assertEqual( |
| 469 | + log.output[0], |
| 470 | + ( |
| 471 | + "WARNING:opentelemetry.sdk.metrics._internal.aggregation:" |
| 472 | + "Invalid value for OTEL_EXPORTER_OTLP_METRICS_DEFAULT_" |
| 473 | + "HISTOGRAM_AGGREGATION: abc, using explicit bucket " |
| 474 | + "histogram aggregation" |
| 475 | + ), |
| 476 | + ) |
| 477 | + |
| 478 | + with patch.dict( |
| 479 | + environ, |
| 480 | + { |
| 481 | + OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "explicit_bucket_histogram" |
| 482 | + }, |
| 483 | + ): |
| 484 | + self.assertIsInstance( |
| 485 | + default_aggregation._create_aggregation( |
| 486 | + histogram, Mock(), Mock() |
| 487 | + ), |
| 488 | + _ExplicitBucketHistogramAggregation, |
| 489 | + ) |
0 commit comments