Skip to content

Commit 29e3d49

Browse files
authored
feat: add otlp metric sample with gcp auth (#371)
* feat: add otlp metric sample with gcp auth * populate required resource attributes
1 parent 5dce5df commit 29e3d49

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

samples/otlpmetric/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 PROJECT_ID=<project-id>
25+
export OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint>
26+
export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=$PROJECT_ID,service.name=otlp-sample,service.instance.id=1"
27+
28+
# from the samples/otlpmetric repository
29+
python3 example.py
30+
```

samples/otlpmetric/example.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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()

samples/otlpmetric/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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
7+
opentelemetry-resourcedetector-gcp==1.8.0

0 commit comments

Comments
 (0)