diff --git a/samples/otlpmetric/README.md b/samples/otlpmetric/README.md new file mode 100644 index 00000000..dcc9e963 --- /dev/null +++ b/samples/otlpmetric/README.md @@ -0,0 +1,30 @@ +### OTLP Export Sample with GCP Auth +This example shows how to send metrics to an OTLP (OpenTelemetry Protocol) endpoint that is protected by GCP authentication. The sample showcases the metric export using gRPC. + +#### Installation +Install the dependencies and libraries required to run the samples: + +```sh +# Move to the sample repository +cd samples/otlpmetric + +pip install -r requirements.txt +``` + +#### Prerequisites +Get Google credentials on your machine: + +```sh +gcloud auth application-default login +``` + +#### Run the Sample +```sh +# export necessary OTEL environment variables +export PROJECT_ID= +export OTEL_EXPORTER_OTLP_ENDPOINT= +export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=$PROJECT_ID,service.name=otlp-sample,service.instance.id=1" + +# from the samples/otlpmetric repository +python3 example.py +``` diff --git a/samples/otlpmetric/example.py b/samples/otlpmetric/example.py new file mode 100644 index 00000000..d8693a31 --- /dev/null +++ b/samples/otlpmetric/example.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import time + +import google.auth +import google.auth.transport.grpc +import google.auth.transport.requests +import grpc +from google.auth.transport.grpc import AuthMetadataPlugin +from opentelemetry import metrics +from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( + OTLPMetricExporter, +) +from opentelemetry.resourcedetector.gcp_resource_detector._detector import ( + GoogleCloudResourceDetector, +) +from opentelemetry.sdk.resources import SERVICE_NAME, Resource, get_aggregated_resources +from opentelemetry.sdk.metrics import MeterProvider +from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader + +""" +This is a sample script that exports OTLP metrics encoded as protobufs via gRPC. +""" + +credentials, project_id = google.auth.default() +request = google.auth.transport.requests.Request() +resource = get_aggregated_resources( + [GoogleCloudResourceDetector(raise_on_error=True)] +) + +auth_metadata_plugin = AuthMetadataPlugin( + credentials=credentials, request=request +) +channel_creds = grpc.composite_channel_credentials( + grpc.ssl_channel_credentials(), + grpc.metadata_call_credentials(auth_metadata_plugin), +) + +exporter = OTLPMetricExporter(credentials=channel_creds) +reader = PeriodicExportingMetricReader(exporter) +provider = MeterProvider(metric_readers=[reader],resource=resource) +meter = provider.get_meter("gcp.otlp.sample") +counter = meter.create_counter("sample.otlp.counter") + + +def do_work(): + counter.add(1) + # do some work that the 'counter' will track + print("doing some work...") + + +def do_work_repeatedly(): + try: + while True: + do_work() + time.sleep(1) + except KeyboardInterrupt: + print("\nKeyboard Interrupt: Stopping work.") + + +do_work_repeatedly() diff --git a/samples/otlpmetric/requirements.txt b/samples/otlpmetric/requirements.txt new file mode 100644 index 00000000..99dc3818 --- /dev/null +++ b/samples/otlpmetric/requirements.txt @@ -0,0 +1,7 @@ +# Dependencies require for trace export samples +opentelemetry-api==1.24.0 +opentelemetry-sdk==1.24.0 +google-auth==2.18.1 +opentelemetry-exporter-otlp-proto-grpc==1.24.0 +grpcio==1.63.0 +opentelemetry-resourcedetector-gcp==1.8.0