Skip to content

Commit 32e0d85

Browse files
committed
Move feature to exporter
1 parent 240320a commit 32e0d85

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
1818
from typing import Dict, Optional, Any, Callable, List
1919
from typing import Sequence, Mapping # noqa: F401
2020

21+
from opentelemetry.sdk.environment_variables import (
22+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
23+
)
2124
from io import BytesIO
2225
from time import sleep
2326
from deprecated import deprecated
2427

28+
from opentelemetry.sdk.metrics._internal.aggregation import (
29+
_ExponentialBucketHistogramAggregation,
30+
_ExplicitBucketHistogramAggregation,
31+
)
32+
2533
from opentelemetry.exporter.otlp.proto.common._internal import (
2634
_get_resource_data,
2735
)
@@ -205,10 +213,38 @@ def __init__(
205213

206214
instrument_class_temporality.update(preferred_temporality or {})
207215

216+
otel_exporter_otlp_metrics_default_histogram_aggregation = environ.get(
217+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
218+
"explicit_bucket_histogram",
219+
)
220+
221+
if otel_exporter_otlp_metrics_default_histogram_aggregation == (
222+
"base2_exponential_bucket_histogram"
223+
):
224+
225+
histogram_aggregation_type = _ExponentialBucketHistogramAggregation
226+
227+
else:
228+
229+
if otel_exporter_otlp_metrics_default_histogram_aggregation != (
230+
"explicit_bucket_histogram"
231+
):
232+
233+
_logger.warning(
234+
(
235+
"Invalid value for %s: %s, using explicit bucket "
236+
"histogram aggregation"
237+
),
238+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
239+
histogram_aggregation_type,
240+
)
241+
242+
histogram_aggregation_type = _ExplicitBucketHistogramAggregation
243+
208244
MetricExporter.__init__(
209245
self,
210246
preferred_temporality=instrument_class_temporality,
211-
preferred_aggregation=preferred_aggregation,
247+
preferred_aggregation={Histogram, histogram_aggregation_type},
212248
)
213249

214250
def _export(self, serialized_data: str):

exporter/opentelemetry-exporter-otlp-proto-http/tests/metrics/test_otlp_metrics_exporter.py

+64-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from logging import WARNING
1616
from os import environ
1717
from unittest import TestCase
18-
from unittest.mock import patch
18+
from unittest.mock import Mock, patch
1919

2020
from requests import Session
2121
from requests.models import Response
@@ -40,6 +40,7 @@
4040
OTEL_EXPORTER_OTLP_HEADERS,
4141
OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
4242
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
43+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
4344
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
4445
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
4546
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE,
@@ -54,13 +55,18 @@
5455
ObservableUpDownCounter,
5556
UpDownCounter,
5657
)
58+
from opentelemetry.sdk.metrics._internal.aggregation import (
59+
_ExplicitBucketHistogramAggregation,
60+
_ExponentialBucketHistogramAggregation,
61+
)
5762
from opentelemetry.sdk.metrics.export import (
5863
AggregationTemporality,
5964
MetricExportResult,
6065
MetricsData,
6166
ResourceMetrics,
6267
ScopeMetrics,
6368
)
69+
from opentelemetry.sdk.metrics.view import DefaultAggregation
6470
from opentelemetry.sdk.resources import Resource
6571
from opentelemetry.sdk.util.instrumentation import (
6672
InstrumentationScope as SDKInstrumentationScope,
@@ -424,3 +430,60 @@ def test_aggregation_temporality(self):
424430
otlp_metric_exporter._preferred_temporality[ObservableGauge],
425431
AggregationTemporality.CUMULATIVE,
426432
)
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+
)

opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@
650650
The :envvar:`OTEL_METRICS_EXEMPLAR_FILTER` is the filter for which measurements can become Exemplars.
651651
"""
652652

653-
_OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION = (
653+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION = (
654654
"OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION"
655655
)
656656
"""

0 commit comments

Comments
 (0)