Skip to content

Commit 53010d9

Browse files
committed
feat: add otlp metric sample with gcp auth
1 parent 5dce5df commit 53010d9

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

samples/otlpmetric/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### OTLP Export Sample with GCP Auth
2+
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.
3+
4+
#### Installation
5+
Install the dependencies and libraries required to run the samples:
6+
7+
```sh
8+
# Move to the sample repository
9+
cd samples/otlpmetric
10+
11+
pip install -r requirements.txt
12+
```
13+
14+
#### Prerequisites
15+
Get Google credentials on your machine:
16+
17+
```sh
18+
gcloud auth application-default login
19+
```
20+
21+
#### Run the Sample
22+
```sh
23+
# export necessary OTEL environment variables
24+
export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=<project-id>"
25+
export OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint>
26+
27+
# from the samples/otlpmetric repository
28+
python3 example.py
29+
```

samples/otlpmetric/example.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 metric
24+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
25+
OTLPMetricExporter,
26+
)
27+
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
28+
from opentelemetry.sdk.metrics import MeterProvider
29+
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
30+
31+
"""
32+
This is a sample script that exports OTLP metrics encoded as protobufs via gRPC.
33+
"""
34+
35+
credentials, project_id = google.auth.default()
36+
request = google.auth.transport.requests.Request()
37+
resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-grpc-sample"})
38+
39+
auth_metadata_plugin = AuthMetadataPlugin(
40+
credentials=credentials, request=request
41+
)
42+
channel_creds = grpc.composite_channel_credentials(
43+
grpc.ssl_channel_credentials(),
44+
grpc.metadata_call_credentials(auth_metadata_plugin),
45+
)
46+
47+
exporter = OTLPMetricExporter(credentials=channel_creds)
48+
reader = PeriodicExportingMetricReader(exporter)
49+
provider = MeterProvider(metric_readers=[reader],resource=resource)
50+
meter = provider.get_meter("gcp.otlp.sample")
51+
counter = meter.create_counter("sample.otlp.counter")
52+
53+
54+
def do_work():
55+
counter.add(1)
56+
# do some work that the 'counter' will track
57+
print("doing some work...")
58+
59+
60+
def do_work_repeatedly():
61+
try:
62+
while True:
63+
do_work()
64+
time.sleep(1)
65+
except KeyboardInterrupt:
66+
print("\nKeyboard Interrupt: Stopping work.")
67+
68+
69+
do_work_repeatedly()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Dependencies require for trace export samples
2+
opentelemetry-api==1.24.0
3+
opentelemetry-sdk==1.24.0
4+
google-auth==2.18.1
5+
opentelemetry-exporter-otlp-proto-grpc==1.24.0
6+
grpcio==1.63.0

0 commit comments

Comments
 (0)