Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions samples/otlpmetric/README.md
Original file line number Diff line number Diff line change
@@ -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=<project-id>
export OTEL_EXPORTER_OTLP_ENDPOINT=<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
```
74 changes: 74 additions & 0 deletions samples/otlpmetric/example.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 7 additions & 0 deletions samples/otlpmetric/requirements.txt
Original file line number Diff line number Diff line change
@@ -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