Skip to content

Commit b682a2f

Browse files
committed
Select histogram aggregation with environment variables
Fixes #3264
1 parent e003062 commit b682a2f

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Enable selection of histogram aggregation via environment variable
11+
([#3169](https://github.com/open-telemetry/opentelemetry-python/pull/3169))
1012
- Move Protobuf encoding to its own package
1113
([#3169](https://github.com/open-telemetry/opentelemetry-python/pull/3169))
1214
- Add experimental feature to detect resource detectors in auto instrumentation

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
"""

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from enum import IntEnum
2020
from logging import getLogger
2121
from math import inf
22+
from os import environ
2223
from threading import Lock
2324
from typing import Generic, List, Optional, Sequence, TypeVar
2425

@@ -33,6 +34,9 @@
3334
Synchronous,
3435
UpDownCounter,
3536
)
37+
from opentelemetry.sdk.environment_variables import (
38+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
39+
)
3640
from opentelemetry.sdk.metrics._internal.exponential_histogram.buckets import (
3741
Buckets,
3842
)
@@ -927,6 +931,29 @@ def _create_aggregation(
927931
)
928932

929933
if isinstance(instrument, Histogram):
934+
935+
histogram_type = environ.get(
936+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
937+
"explicit_bucket_histogram",
938+
)
939+
940+
if histogram_type == "base2_exponential_bucket_histogram":
941+
942+
return _ExponentialBucketHistogramAggregation(
943+
attributes, start_time_unix_nano
944+
)
945+
946+
if histogram_type != "explicit_bucket_histogram":
947+
948+
_logger.warning(
949+
(
950+
"Invalid value for %s: %s, using explicit bucket "
951+
"histogram aggregation"
952+
),
953+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
954+
histogram_type,
955+
)
956+
930957
return _ExplicitBucketHistogramAggregation(
931958
attributes, start_time_unix_nano
932959
)

opentelemetry-sdk/tests/metrics/test_aggregation.py

+65-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from logging import WARNING
1516
from math import inf
17+
from os import environ
1618
from time import sleep
1719
from typing import Union
1820
from unittest import TestCase
19-
from unittest.mock import Mock
21+
from unittest.mock import Mock, patch
2022

23+
from opentelemetry.metrics import Histogram
24+
from opentelemetry.sdk.environment_variables import (
25+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
26+
)
2127
from opentelemetry.sdk.metrics._internal.aggregation import (
2228
_ExplicitBucketHistogramAggregation,
29+
_ExponentialBucketHistogramAggregation,
2330
_LastValueAggregation,
2431
_SumAggregation,
2532
)
@@ -541,3 +548,60 @@ def test_observable_gauge(self):
541548
0,
542549
)
543550
self.assertIsInstance(aggregation, _LastValueAggregation)
551+
552+
def test_exponential_explicit_bucket_histogram(self):
553+
554+
histogram = Mock(spec=Histogram)
555+
default_aggregation = DefaultAggregation()
556+
557+
self.assertIsInstance(
558+
default_aggregation._create_aggregation(histogram, Mock(), Mock()),
559+
_ExplicitBucketHistogramAggregation,
560+
)
561+
562+
with patch.dict(
563+
environ,
564+
{
565+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "base2_exponential_bucket_histogram"
566+
},
567+
):
568+
self.assertIsInstance(
569+
default_aggregation._create_aggregation(
570+
histogram, Mock(), Mock()
571+
),
572+
_ExponentialBucketHistogramAggregation,
573+
)
574+
575+
with patch.dict(
576+
environ,
577+
{OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "abc"},
578+
):
579+
with self.assertLogs(level=WARNING) as log:
580+
self.assertIsInstance(
581+
default_aggregation._create_aggregation(
582+
histogram, Mock(), Mock()
583+
),
584+
_ExplicitBucketHistogramAggregation,
585+
)
586+
self.assertEqual(
587+
log.output[0],
588+
(
589+
"WARNING:opentelemetry.sdk.metrics._internal.aggregation:"
590+
"Invalid value for OTEL_EXPORTER_OTLP_METRICS_DEFAULT_"
591+
"HISTOGRAM_AGGREGATION: abc, using explicit bucket "
592+
"histogram aggregation"
593+
),
594+
)
595+
596+
with patch.dict(
597+
environ,
598+
{
599+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "explicit_bucket_histogram"
600+
},
601+
):
602+
self.assertIsInstance(
603+
default_aggregation._create_aggregation(
604+
histogram, Mock(), Mock()
605+
),
606+
_ExplicitBucketHistogramAggregation,
607+
)

0 commit comments

Comments
 (0)