|
| 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 DropAggregation, View |
| 25 | + |
| 26 | +# Create a view matching the counter instrument `my.counter` |
| 27 | +# and configure the view to drop the aggregation. |
| 28 | +drop_aggregation_view = View( |
| 29 | + instrument_type=Counter, |
| 30 | + instrument_name="my.counter", |
| 31 | + aggregation=DropAggregation(), |
| 32 | +) |
| 33 | + |
| 34 | +exporter = ConsoleMetricExporter() |
| 35 | + |
| 36 | +reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000) |
| 37 | +provider = MeterProvider( |
| 38 | + metric_readers=[ |
| 39 | + reader, |
| 40 | + ], |
| 41 | + views=[ |
| 42 | + drop_aggregation_view, |
| 43 | + ], |
| 44 | +) |
| 45 | +set_meter_provider(provider) |
| 46 | + |
| 47 | +meter = get_meter_provider().get_meter("view-drop-aggregation", "0.1.2") |
| 48 | + |
| 49 | +my_counter = meter.create_counter("my.counter") |
| 50 | + |
| 51 | +while 1: |
| 52 | + my_counter.add(random.randint(1, 10)) |
| 53 | + time.sleep(random.random()) |
0 commit comments