Skip to content

Commit 1b76dd2

Browse files
authored
Update OTLP trace example. (#386)
* Update OTLP trace example. * Do not list telemetry endpoint. * Remove manually adding quota project header.
1 parent 73b6ac1 commit 1b76dd2

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

samples/otlptrace/example_grpc.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@
3232
This is a sample script that exports OTLP traces encoded as protobufs via gRPC.
3333
"""
3434

35-
credentials, project_id = google.auth.default()
35+
credentials, _ = google.auth.default()
3636
request = google.auth.transport.requests.Request()
3737
resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-grpc-sample"})
3838

39-
auth_metadata_plugin = AuthMetadataPlugin(
40-
credentials=credentials, request=request
41-
)
39+
auth_metadata_plugin = AuthMetadataPlugin(credentials=credentials, request=request)
4240
channel_creds = grpc.composite_channel_credentials(
4341
grpc.ssl_channel_credentials(),
4442
grpc.metadata_call_credentials(auth_metadata_plugin),
4543
)
4644

4745
trace_provider = TracerProvider(resource=resource)
48-
processor = BatchSpanProcessor(OTLPSpanExporter(credentials=channel_creds))
46+
processor = BatchSpanProcessor(
47+
OTLPSpanExporter(
48+
credentials=channel_creds,
49+
)
50+
)
4951
trace_provider.add_span_processor(processor)
5052
trace.set_tracer_provider(trace_provider)
5153
tracer = trace.get_tracer("my.tracer.name")

samples/otlptrace/example_http.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
2020
OTLPSpanExporter,
2121
)
22+
from google.auth.transport.requests import AuthorizedSession
2223
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
2324
from opentelemetry.sdk.trace import TracerProvider
2425
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2526

26-
credentials, project_id = google.auth.default()
27-
request = google.auth.transport.requests.Request()
28-
credentials.refresh(request)
29-
req_headers = {
30-
"x-goog-user-project": credentials.quota_project_id,
31-
"Authorization": "Bearer " + credentials.token,
32-
}
33-
resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"})
34-
35-
trace_provider = TracerProvider(resource=resource)
36-
processor = BatchSpanProcessor(OTLPSpanExporter(headers=req_headers))
27+
credentials, _ = google.auth.default()
28+
trace_provider = TracerProvider(
29+
resource=Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"})
30+
)
31+
processor = BatchSpanProcessor(
32+
OTLPSpanExporter(
33+
session=AuthorizedSession(credentials),
34+
)
35+
)
3736
trace_provider.add_span_processor(processor)
3837
trace.set_tracer_provider(trace_provider)
3938
tracer = trace.get_tracer("my.tracer.name")

0 commit comments

Comments
 (0)