|
| 1 | +# Copyright 2019, 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 | +""" |
| 16 | +This module serves as an example for a simple application using metrics |
| 17 | +Examples show how to recording affects the collection of metrics to be exported |
| 18 | +""" |
| 19 | +import time |
| 20 | + |
| 21 | +from opentelemetry import metrics |
| 22 | +from opentelemetry.sdk.metrics import Counter, Meter |
| 23 | +from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter |
| 24 | +from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher |
| 25 | +from opentelemetry.sdk.metrics.export.controller import PushController |
| 26 | + |
| 27 | +# Batcher used to collect all created metrics from meter ready for exporting |
| 28 | +# Pass in true/false to indicate whether the batcher is stateful. True |
| 29 | +# indicates the batcher computes checkpoints from over the process lifetime. |
| 30 | +# False indicates the batcher computes checkpoints which describe the updates |
| 31 | +# of a single collection period (deltas) |
| 32 | +batcher = UngroupedBatcher(True) |
| 33 | +# If a batcher is not provded, a default batcher is used |
| 34 | +# Meter is responsible for creating and recording metrics |
| 35 | +metrics.set_preferred_meter_implementation(lambda _: Meter(batcher)) |
| 36 | +meter = metrics.meter() |
| 37 | +# exporter to export metrics to the console |
| 38 | +exporter = ConsoleMetricsExporter() |
| 39 | +# controller collects metrics created from meter and exports it via the |
| 40 | +# exporter every interval |
| 41 | +controller = PushController(meter, exporter, 5) |
| 42 | + |
| 43 | +counter = meter.create_metric( |
| 44 | + "requests", "number of requests", 1, int, Counter, ("environment",) |
| 45 | +) |
| 46 | + |
| 47 | +counter2 = meter.create_metric( |
| 48 | + "clicks", "number of clicks", 1, int, Counter, ("environment",) |
| 49 | +) |
| 50 | + |
| 51 | +# Labelsets are used to identify key-values that are associated with a specific |
| 52 | +# metric that you want to record. These are useful for pre-aggregation and can |
| 53 | +# be used to store custom dimensions pertaining to a metric |
| 54 | +label_set = meter.get_label_set({"environment": "staging"}) |
| 55 | +label_set2 = meter.get_label_set({"environment": "testing"}) |
| 56 | + |
| 57 | +counter.add(25, label_set) |
| 58 | +# We sleep for 5 seconds, exported value should be 25 |
| 59 | +time.sleep(5) |
| 60 | + |
| 61 | +counter.add(50, label_set) |
| 62 | +# exported value should be 75 |
| 63 | +time.sleep(5) |
| 64 | + |
| 65 | +counter.add(35, label_set2) |
| 66 | +# should be two exported values 75 and 35, one for each labelset |
| 67 | +time.sleep(5) |
| 68 | + |
| 69 | +counter2.add(5, label_set) |
| 70 | +# should be three exported values, labelsets can be reused for different |
| 71 | +# metrics but will be recorded seperately, 75, 35 and 5 |
| 72 | +time.sleep(5) |
0 commit comments