|
| 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