|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import time |
| 17 | + |
| 18 | +import google.auth |
| 19 | +import google.auth.transport.grpc |
| 20 | +import google.auth.transport.requests |
| 21 | +import grpc |
| 22 | +from google.auth.transport.grpc import AuthMetadataPlugin |
| 23 | +from opentelemetry import metrics |
| 24 | +from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( |
| 25 | + OTLPMetricExporter, |
| 26 | +) |
| 27 | +from opentelemetry.resourcedetector.gcp_resource_detector._detector import ( |
| 28 | + GoogleCloudResourceDetector, |
| 29 | +) |
| 30 | +from opentelemetry.sdk.resources import SERVICE_NAME, Resource, get_aggregated_resources |
| 31 | +from opentelemetry.sdk.metrics import MeterProvider |
| 32 | +from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader |
| 33 | + |
| 34 | +""" |
| 35 | +This is a sample script that exports OTLP metrics encoded as protobufs via gRPC. |
| 36 | +""" |
| 37 | + |
| 38 | +credentials, project_id = google.auth.default() |
| 39 | +request = google.auth.transport.requests.Request() |
| 40 | +resource = get_aggregated_resources( |
| 41 | + [GoogleCloudResourceDetector(raise_on_error=True)] |
| 42 | +) |
| 43 | + |
| 44 | +auth_metadata_plugin = AuthMetadataPlugin( |
| 45 | + credentials=credentials, request=request |
| 46 | +) |
| 47 | +channel_creds = grpc.composite_channel_credentials( |
| 48 | + grpc.ssl_channel_credentials(), |
| 49 | + grpc.metadata_call_credentials(auth_metadata_plugin), |
| 50 | +) |
| 51 | + |
| 52 | +exporter = OTLPMetricExporter(credentials=channel_creds) |
| 53 | +reader = PeriodicExportingMetricReader(exporter) |
| 54 | +provider = MeterProvider(metric_readers=[reader],resource=resource) |
| 55 | +meter = provider.get_meter("gcp.otlp.sample") |
| 56 | +counter = meter.create_counter("sample.otlp.counter") |
| 57 | + |
| 58 | + |
| 59 | +def do_work(): |
| 60 | + counter.add(1) |
| 61 | + # do some work that the 'counter' will track |
| 62 | + print("doing some work...") |
| 63 | + |
| 64 | + |
| 65 | +def do_work_repeatedly(): |
| 66 | + try: |
| 67 | + while True: |
| 68 | + do_work() |
| 69 | + time.sleep(1) |
| 70 | + except KeyboardInterrupt: |
| 71 | + print("\nKeyboard Interrupt: Stopping work.") |
| 72 | + |
| 73 | + |
| 74 | +do_work_repeatedly() |
0 commit comments