Skip to content

Commit

Permalink
add exemplar filter as environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
czhang771 committed Sep 3, 2024
1 parent 0ea80dc commit e7e4227
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@
)
from opentelemetry.metrics import UpDownCounter as APIUpDownCounter
from opentelemetry.metrics import _Gauge as APIGauge
from opentelemetry.sdk.environment_variables import OTEL_SDK_DISABLED
from opentelemetry.sdk.environment_variables import (
OTEL_SDK_DISABLED,
OTEL_METRICS_EXEMPLAR_FILTER,
)
from opentelemetry.sdk.metrics._internal.exceptions import MetricsTimeoutError
from opentelemetry.sdk.metrics._internal.exemplar import (
ExemplarFilter,
TraceBasedExemplarFilter,
AlwaysOnExemplarFilter,
AlwaysOffExemplarFilter,
)
from opentelemetry.sdk.metrics._internal.instrument import (
_Counter,
Expand Down Expand Up @@ -394,11 +399,12 @@ def __init__(
self._atexit_handler = None
if resource is None:
resource = Resource.create({})
filter = environ.get(OTEL_METRICS_EXEMPLAR_FILTER, None)
self._sdk_config = SdkConfiguration(
exemplar_filter=(
TraceBasedExemplarFilter()
if exemplar_filter is None
else exemplar_filter
exemplar_filter
if exemplar_filter is not None
else self._get_exemplar_filter(filter)
),
resource=resource,
metric_readers=metric_readers,
Expand Down Expand Up @@ -556,3 +562,14 @@ def get_meter(
self._measurement_consumer,
)
return self._meters[info]

def _get_exemplar_filter(self, exemplar_filter: str) -> ExemplarFilter:
if not exemplar_filter or exemplar_filter == 'trace_based':
return TraceBasedExemplarFilter()
elif exemplar_filter == 'always_on':
return AlwaysOnExemplarFilter()
elif exemplar_filter == 'always_off':
return AlwaysOffExemplarFilter()
else:
raise Exception("Invalid exemplar filter.")

0 comments on commit e7e4227

Please sign in to comment.