Skip to content

Commit

Permalink
Improve the example
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Sep 2, 2024
1 parent 0a13b62 commit 612404e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
19 changes: 13 additions & 6 deletions docs/examples/metrics/reader/preferred_exemplarfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
54 changes: 24 additions & 30 deletions docs/examples/metrics/views/change_reservoir_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,41 @@

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 (
ConsoleMetricExporter,
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(
Expand Down Expand Up @@ -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())

0 comments on commit 612404e

Please sign in to comment.