|
| 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 export metrics to the OT collector. |
| 17 | +""" |
| 18 | + |
| 19 | +from opentelemetry import metrics |
| 20 | +from opentelemetry.ext.opencensusexporter.metrics_exporter import ( |
| 21 | + OpenCensusMetricsExporter, |
| 22 | +) |
| 23 | +from opentelemetry.sdk.metrics import Counter, MeterProvider |
| 24 | +from opentelemetry.sdk.metrics.export.controller import PushController |
| 25 | + |
| 26 | +exporter = OpenCensusMetricsExporter( |
| 27 | + service_name="basic-service", endpoint="localhost:55678" |
| 28 | +) |
| 29 | + |
| 30 | +metrics.set_meter_provider(MeterProvider()) |
| 31 | +meter = metrics.get_meter(__name__) |
| 32 | +controller = PushController(meter, exporter, 5) |
| 33 | + |
| 34 | +requests_counter = meter.create_metric( |
| 35 | + name="requests", |
| 36 | + description="number of requests", |
| 37 | + unit="1", |
| 38 | + value_type=int, |
| 39 | + metric_type=Counter, |
| 40 | + label_keys=("environment",), |
| 41 | +) |
| 42 | + |
| 43 | +staging_labels = {"environment": "staging"} |
| 44 | +requests_counter.add(25, staging_labels) |
| 45 | + |
| 46 | +print("Metrics are available now at http://localhost:9090/graph") |
| 47 | +input("Press any key to exit...") |
0 commit comments