diff --git a/docs/examples/metrics/reader/preferred_exemplarfilter.py b/docs/examples/metrics/reader/preferred_exemplarfilter.py index 1840cebfcc..4bb98896be 100644 --- a/docs/examples/metrics/reader/preferred_exemplarfilter.py +++ b/docs/examples/metrics/reader/preferred_exemplarfilter.py @@ -13,19 +13,21 @@ # limitations under the License. import time +from opentelemetry import trace from opentelemetry.metrics import get_meter_provider, set_meter_provider -from opentelemetry.sdk.metrics import Counter, MeterProvider +from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics._internal.exemplar import ( - AlwaysOffExemplarFilter, - AlwaysOnExemplarFilter, TraceBasedExemplarFilter, ) from opentelemetry.sdk.metrics.export import ( ConsoleMetricExporter, PeriodicExportingMetricReader, ) +from opentelemetry.sdk.trace import TracerProvider # Create an ExemplarFilter instance (e.g., TraceBasedExemplarFilter) +# Default available values are AlwaysOffExemplarFilter, AlwaysOnExemplarFilter +# and TraceBasedExemplarFilter exemplar_filter = TraceBasedExemplarFilter() exporter = ConsoleMetricExporter() @@ -45,6 +47,11 @@ meter = get_meter_provider().get_meter("exemplar-filter-example", "0.1.2") counter = meter.create_counter("my-counter") -for value in range(10): - counter.add(value) - time.sleep(2.0) +# Create a trace and span as the default exemplar filter `TraceBasedExemplarFilter` +# will only store exemplar if a context exists +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) +with tracer.start_as_current_span("foo"): + for value in range(10): + counter.add(value) + time.sleep(2.0) diff --git a/docs/examples/metrics/views/change_reservoir_factory.py b/docs/examples/metrics/views/change_reservoir_factory.py index 843c1bf3a9..b87e9d92c6 100644 --- a/docs/examples/metrics/views/change_reservoir_factory.py +++ b/docs/examples/metrics/views/change_reservoir_factory.py @@ -14,21 +14,19 @@ import random import time -from typing import Any, Callable, Optional, Sequence, Set, Type +from typing import Type +from opentelemetry import trace from opentelemetry.metrics import get_meter_provider, set_meter_provider -from opentelemetry.sdk.metrics import Counter, MeterProvider +from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics._internal.aggregation import ( - Aggregation, DefaultAggregation, _Aggregation, _ExplicitBucketHistogramAggregation, - _ExponentialBucketHistogramAggregation, ) from opentelemetry.sdk.metrics._internal.exemplar import ( AlignedHistogramBucketExemplarReservoir, - ExemplarReservoir, - ExemplarReservoirFactory, + ExemplarReservoirBuilder, SimpleFixedSizeExemplarReservoir, ) from opentelemetry.sdk.metrics.export import ( @@ -36,30 +34,21 @@ PeriodicExportingMetricReader, ) from opentelemetry.sdk.metrics.view import View - - -# Returns a factory for creating an exemplar reservoir based on the aggregation type and specified parameters -def generalized_reservoir_factory( - size: int = 1, boundaries: Sequence[float] = None -) -> Callable[[Type[_Aggregation]], ExemplarReservoirFactory]: - def factory( - aggregationType: Type[_Aggregation], - ) -> ExemplarReservoirFactory: - if issubclass(aggregationType, _ExplicitBucketHistogramAggregation): - return lambda **kwargs: AlignedHistogramBucketExemplarReservoir( - boundaries=boundaries or [], - **{k: v for k, v in kwargs.items() if k != "boundaries"}, - ) - else: - return lambda **kwargs: SimpleFixedSizeExemplarReservoir( - size=size, **kwargs - ) - - return factory +from opentelemetry.sdk.trace import TracerProvider # Create a custom reservoir factory with specified parameters -custom_reservoir_factory = generalized_reservoir_factory(size=10) +def custom_reservoir_factory( + aggregationType: Type[_Aggregation], +) -> ExemplarReservoirBuilder: + if issubclass(aggregationType, _ExplicitBucketHistogramAggregation): + return AlignedHistogramBucketExemplarReservoir + else: + return lambda **kwargs: SimpleFixedSizeExemplarReservoir( + size=10, + **{k: v for k, v in kwargs.items() if k != "size"}, + ) + # Create a view with the custom reservoir factory change_reservoir_factory_view = View( @@ -88,6 +77,11 @@ def factory( my_counter = meter.create_counter("my.counter") -while 1: - my_counter.add(random.randint(1, 10)) - time.sleep(random.random()) +# Create a trace and span as the default exemplar filter `TraceBasedExemplarFilter` +# will only store exemplar if a context exists +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) +with tracer.start_as_current_span("foo"): + while 1: + my_counter.add(random.randint(1, 10)) + time.sleep(random.random())