Skip to content

Commit

Permalink
Move feature to exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Apr 20, 2023
1 parent 240320a commit 32e0d85
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
from typing import Dict, Optional, Any, Callable, List
from typing import Sequence, Mapping # noqa: F401

from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
)
from io import BytesIO
from time import sleep
from deprecated import deprecated

from opentelemetry.sdk.metrics._internal.aggregation import (
_ExponentialBucketHistogramAggregation,
_ExplicitBucketHistogramAggregation,
)

from opentelemetry.exporter.otlp.proto.common._internal import (
_get_resource_data,
)
Expand Down Expand Up @@ -205,10 +213,38 @@ def __init__(

instrument_class_temporality.update(preferred_temporality or {})

otel_exporter_otlp_metrics_default_histogram_aggregation = environ.get(
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
"explicit_bucket_histogram",
)

if otel_exporter_otlp_metrics_default_histogram_aggregation == (
"base2_exponential_bucket_histogram"
):

histogram_aggregation_type = _ExponentialBucketHistogramAggregation

else:

if otel_exporter_otlp_metrics_default_histogram_aggregation != (
"explicit_bucket_histogram"
):

_logger.warning(
(
"Invalid value for %s: %s, using explicit bucket "
"histogram aggregation"
),
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
histogram_aggregation_type,
)

histogram_aggregation_type = _ExplicitBucketHistogramAggregation

MetricExporter.__init__(
self,
preferred_temporality=instrument_class_temporality,
preferred_aggregation=preferred_aggregation,
preferred_aggregation={Histogram, histogram_aggregation_type},
)

def _export(self, serialized_data: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging import WARNING
from os import environ
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import Mock, patch

from requests import Session
from requests.models import Response
Expand All @@ -40,6 +40,7 @@
OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE,
Expand All @@ -54,13 +55,18 @@
ObservableUpDownCounter,
UpDownCounter,
)
from opentelemetry.sdk.metrics._internal.aggregation import (
_ExplicitBucketHistogramAggregation,
_ExponentialBucketHistogramAggregation,
)
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
MetricExportResult,
MetricsData,
ResourceMetrics,
ScopeMetrics,
)
from opentelemetry.sdk.metrics.view import DefaultAggregation
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.util.instrumentation import (
InstrumentationScope as SDKInstrumentationScope,
Expand Down Expand Up @@ -424,3 +430,60 @@ def test_aggregation_temporality(self):
otlp_metric_exporter._preferred_temporality[ObservableGauge],
AggregationTemporality.CUMULATIVE,
)

def test_exponential_explicit_bucket_histogram(self):

histogram = Mock(spec=Histogram)
default_aggregation = DefaultAggregation()

self.assertIsInstance(
default_aggregation._create_aggregation(histogram, Mock(), Mock()),
_ExplicitBucketHistogramAggregation,
)

with patch.dict(
environ,
{
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "base2_exponential_bucket_histogram"
},
):
self.assertIsInstance(
default_aggregation._create_aggregation(
histogram, Mock(), Mock()
),
_ExponentialBucketHistogramAggregation,
)

with patch.dict(
environ,
{OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "abc"},
):
with self.assertLogs(level=WARNING) as log:
self.assertIsInstance(
default_aggregation._create_aggregation(
histogram, Mock(), Mock()
),
_ExplicitBucketHistogramAggregation,
)
self.assertEqual(
log.output[0],
(
"WARNING:opentelemetry.sdk.metrics._internal.aggregation:"
"Invalid value for OTEL_EXPORTER_OTLP_METRICS_DEFAULT_"
"HISTOGRAM_AGGREGATION: abc, using explicit bucket "
"histogram aggregation"
),
)

with patch.dict(
environ,
{
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION: "explicit_bucket_histogram"
},
):
self.assertIsInstance(
default_aggregation._create_aggregation(
histogram, Mock(), Mock()
),
_ExplicitBucketHistogramAggregation,
)
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
The :envvar:`OTEL_METRICS_EXEMPLAR_FILTER` is the filter for which measurements can become Exemplars.
"""

_OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION = (
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION = (
"OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION"
)
"""
Expand Down

0 comments on commit 32e0d85

Please sign in to comment.