|
| 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 example shows how to use the different modes to capture metrics. |
| 17 | +It shows the usage of the direct, bound and batch calling conventions. |
| 18 | +""" |
| 19 | +from opentelemetry import metrics |
| 20 | +from opentelemetry.sdk.metrics import ( |
| 21 | + MeterProvider, |
| 22 | + UpDownCounter, |
| 23 | + ValueRecorder, |
| 24 | +) |
| 25 | +from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter |
| 26 | +from opentelemetry.sdk.metrics.export.aggregate import ( |
| 27 | + HistogramAggregator, |
| 28 | + LastValueAggregator, |
| 29 | + MinMaxSumCountAggregator, |
| 30 | + SumAggregator, |
| 31 | +) |
| 32 | +from opentelemetry.sdk.metrics.view import View, ViewConfig |
| 33 | + |
| 34 | +# Use the meter type provided by the SDK package |
| 35 | +metrics.set_meter_provider(MeterProvider()) |
| 36 | +meter = metrics.get_meter(__name__) |
| 37 | +metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5) |
| 38 | + |
| 39 | +requests_counter = meter.create_metric( |
| 40 | + name="requests", |
| 41 | + description="number of requests", |
| 42 | + unit="1", |
| 43 | + value_type=int, |
| 44 | + metric_type=UpDownCounter, |
| 45 | +) |
| 46 | + |
| 47 | +requests_size = meter.create_metric( |
| 48 | + name="requests_size", |
| 49 | + description="size of requests", |
| 50 | + unit="1", |
| 51 | + value_type=int, |
| 52 | + metric_type=ValueRecorder, |
| 53 | +) |
| 54 | + |
| 55 | +# Views are used to define an aggregation type and label keys to aggregate by |
| 56 | +# for a given metric |
| 57 | + |
| 58 | +# Two views with the same metric and aggregation type but different label keys |
| 59 | +# With ViewConfig.LABEL_KEYS, all labels but the ones defined in label_keys are |
| 60 | +# dropped from the aggregation |
| 61 | +counter_view1 = View( |
| 62 | + requests_counter, |
| 63 | + SumAggregator, |
| 64 | + label_keys=["environment"], |
| 65 | + view_config=ViewConfig.LABEL_KEYS, |
| 66 | +) |
| 67 | +counter_view2 = View( |
| 68 | + requests_counter, |
| 69 | + MinMaxSumCountAggregator, |
| 70 | + label_keys=["os_type"], |
| 71 | + view_config=ViewConfig.LABEL_KEYS, |
| 72 | +) |
| 73 | +# This view has ViewConfig set to UNGROUPED, meaning all recorded metrics take |
| 74 | +# the labels directly without and consideration for label_keys |
| 75 | +counter_view3 = View( |
| 76 | + requests_counter, |
| 77 | + LastValueAggregator, |
| 78 | + label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED |
| 79 | + view_config=ViewConfig.UNGROUPED, |
| 80 | +) |
| 81 | +# This view uses the HistogramAggregator which accepts an option config |
| 82 | +# parameter to specify the bucket ranges |
| 83 | +size_view = View( |
| 84 | + requests_size, |
| 85 | + HistogramAggregator, |
| 86 | + label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED |
| 87 | + aggregator_config={"bounds": [20, 40, 60, 80, 100]}, |
| 88 | + view_config=ViewConfig.UNGROUPED, |
| 89 | +) |
| 90 | + |
| 91 | +# Register the views to the view manager to use the views. Views MUST be |
| 92 | +# registered before recording metrics. Metrics that are recorded without |
| 93 | +# views defined for them will use a default for that type of metric |
| 94 | +meter.register_view(counter_view1) |
| 95 | +meter.register_view(counter_view2) |
| 96 | +meter.register_view(counter_view3) |
| 97 | +meter.register_view(size_view) |
| 98 | + |
| 99 | +# The views will evaluate the labels passed into the record and aggregate upon |
| 100 | +# the unique labels that are generated |
| 101 | +# view1 labels will evaluate to {"environment": "staging"} |
| 102 | +# view2 labels will evaluate to {"os_type": linux} |
| 103 | +# view3 labels will evaluate to {"environment": "staging", "os_type": "linux"} |
| 104 | +requests_counter.add(100, {"environment": "staging", "os_type": "linux"}) |
| 105 | + |
| 106 | +# Since this is using the HistogramAggregator, the bucket counts will be reflected |
| 107 | +# with each record |
| 108 | +requests_size.record(25, {"test": "value"}) |
| 109 | +requests_size.record(-3, {"test": "value"}) |
| 110 | +requests_size.record(200, {"test": "value"}) |
| 111 | + |
| 112 | +input("...\n") |
0 commit comments