Skip to content

Commit eece48d

Browse files
committed
add samples for exemplar filter and custom reservoir factory
1 parent afd4e2c commit eece48d

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import time
15+
16+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
17+
from opentelemetry.sdk.metrics import Counter, MeterProvider
18+
from opentelemetry.sdk.metrics.export import (
19+
ConsoleMetricExporter,
20+
PeriodicExportingMetricReader,
21+
)
22+
from opentelemetry.sdk.metrics._internal.exemplar import (
23+
AlwaysOffExemplarFilter,
24+
AlwaysOnExemplarFilter,
25+
TraceBasedExemplarFilter,
26+
)
27+
28+
# Create an ExemplarFilter instance (e.g., TraceBasedExemplarFilter)
29+
exemplar_filter = TraceBasedExemplarFilter()
30+
31+
exporter = ConsoleMetricExporter()
32+
33+
reader = PeriodicExportingMetricReader(
34+
exporter,
35+
export_interval_millis=5_000,
36+
)
37+
38+
# Set up the MeterProvider with the ExemplarFilter
39+
provider = MeterProvider(
40+
metric_readers=[reader],
41+
exemplar_filter=exemplar_filter, # Pass the ExemplarFilter to the MeterProvider
42+
)
43+
set_meter_provider(provider)
44+
45+
meter = get_meter_provider().get_meter("exemplar-filter-example", "0.1.2")
46+
counter = meter.create_counter("my-counter")
47+
48+
for value in range(10):
49+
counter.add(value)
50+
time.sleep(2.0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import time
17+
18+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
19+
from opentelemetry.sdk.metrics import Counter, MeterProvider
20+
from opentelemetry.sdk.metrics.export import (
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
from opentelemetry.sdk.metrics.view import View
25+
from typing import Callable, Optional, Set, Type, Any, Sequence
26+
from opentelemetry.sdk.metrics._internal.aggregation import (
27+
Aggregation,
28+
DefaultAggregation,
29+
_Aggregation,
30+
_ExplicitBucketHistogramAggregation,
31+
_ExponentialBucketHistogramAggregation,
32+
)
33+
from opentelemetry.sdk.metrics._internal.exemplar import (
34+
AlignedHistogramBucketExemplarReservoir,
35+
ExemplarReservoir,
36+
ExemplarReservoirFactory,
37+
SimpleFixedSizeExemplarReservoir
38+
)
39+
40+
# Returns a factory for creating an exemplar reservoir based on the aggregation type and specified parameters
41+
def generalized_reservoir_factory(size: int = 1, boundaries: Sequence[float] = None) -> Callable[[Type[_Aggregation]], ExemplarReservoirFactory]:
42+
def factory(aggregationType: Type[_Aggregation]) -> ExemplarReservoirFactory:
43+
if issubclass(aggregationType, _ExplicitBucketHistogramAggregation):
44+
return lambda **kwargs: AlignedHistogramBucketExemplarReservoir(boundaries=boundaries or [], **{k: v for k, v in kwargs.items() if k != 'boundaries'})
45+
else:
46+
return lambda **kwargs: SimpleFixedSizeExemplarReservoir(size=size, **kwargs)
47+
48+
return factory
49+
50+
# Create a custom reservoir factory with specified parameters
51+
custom_reservoir_factory = generalized_reservoir_factory(size=10)
52+
53+
# Create a view with the custom reservoir factory
54+
change_reservoir_factory_view= View(
55+
instrument_name="my.counter",
56+
name="name",
57+
aggregation=DefaultAggregation(),
58+
exemplar_reservoir_factory=custom_reservoir_factory,
59+
)
60+
61+
# Use console exporter for the example
62+
exporter = ConsoleMetricExporter()
63+
64+
# Create a metric reader with stdout exporter
65+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
66+
provider = MeterProvider(
67+
metric_readers=[
68+
reader,
69+
],
70+
views=[
71+
change_reservoir_factory_view,
72+
],
73+
)
74+
set_meter_provider(provider)
75+
76+
meter = get_meter_provider().get_meter("reservoir-factory-change", "0.1.2")
77+
78+
my_counter = meter.create_counter("my.counter")
79+
80+
while 1:
81+
my_counter.add(random.randint(1, 10))
82+
time.sleep(random.random())

0 commit comments

Comments
 (0)