|
| 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 | +""" |
| 16 | +This module serves as an example for a simple application using metrics. |
| 17 | +
|
| 18 | +It shows: |
| 19 | +- How to configure a meter passing a stateful or stateless. |
| 20 | +- How to configure an exporter and how to create a controller. |
| 21 | +- How to create some metrics instruments and how to capture data with them. |
| 22 | +- How to use views to specify aggregation types for each metric instrument. |
| 23 | +""" |
| 24 | +import sys |
| 25 | +import time |
| 26 | + |
| 27 | +from opentelemetry import metrics |
| 28 | +from opentelemetry.sdk.metrics import MeterProvider |
| 29 | +from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter |
| 30 | + |
| 31 | +print( |
| 32 | + "Starting example, values will be printed to the console every 5 seconds." |
| 33 | +) |
| 34 | + |
| 35 | +# Stateful determines whether how metrics are collected: if true, metrics |
| 36 | +# accumulate over the process lifetime. If false, metrics are reset at the |
| 37 | +# beginning of each collection interval. |
| 38 | +stateful = True |
| 39 | + |
| 40 | +# Sets the global MeterProvider instance |
| 41 | +metrics.set_meter_provider(MeterProvider()) |
| 42 | + |
| 43 | +# The Meter is responsible for creating and recording metrics. Each meter has a |
| 44 | +# unique name, which we set as the module's name here. |
| 45 | +meter = metrics.get_meter(__name__) |
| 46 | + |
| 47 | +# Exporter to export metrics to the console |
| 48 | +exporter = ConsoleMetricsExporter() |
| 49 | + |
| 50 | +# start_pipeline will notify the MeterProvider to begin collecting/exporting |
| 51 | +# metrics with the given meter, exporter and interval in seconds |
| 52 | +metrics.get_meter_provider().start_pipeline(meter, exporter, 5) |
| 53 | + |
| 54 | +# Metric instruments allow to capture measurements |
| 55 | +requests_counter = meter.create_counter( |
| 56 | + name="requests", |
| 57 | + description="number of requests", |
| 58 | + unit="1", |
| 59 | + value_type=int, |
| 60 | +) |
| 61 | + |
| 62 | +requests_size = meter.create_valuerecorder( |
| 63 | + name="requests_size", |
| 64 | + description="size of requests", |
| 65 | + unit="1", |
| 66 | + value_type=int, |
| 67 | +) |
| 68 | + |
| 69 | +# Labels are used to identify key-values that are associated with a specific |
| 70 | +# metric that you want to record. These are useful for pre-aggregation and can |
| 71 | +# be used to store custom dimensions pertaining to a metric |
| 72 | +staging_labels = {"environment": "staging"} |
| 73 | +testing_labels = {"environment": "testing"} |
| 74 | + |
| 75 | +# Update the metric instruments using the direct calling convention |
| 76 | +requests_counter.add(25, staging_labels) |
| 77 | +requests_size.record(100, staging_labels) |
| 78 | +time.sleep(10) |
| 79 | + |
| 80 | +requests_counter.add(50, staging_labels) |
| 81 | +requests_size.record(5000, staging_labels) |
| 82 | +time.sleep(5) |
| 83 | + |
| 84 | +requests_counter.add(35, testing_labels) |
| 85 | +requests_size.record(2, testing_labels) |
| 86 | + |
| 87 | +input("...\n") |
0 commit comments