From f3c8493cadfb08046c3c602089f55fbe62fca3b0 Mon Sep 17 00:00:00 2001 From: Dylan Russell Date: Fri, 28 Feb 2025 15:38:16 +0000 Subject: [PATCH 1/3] Update OTLP trace example. --- samples/otlptrace/README.md | 3 +-- samples/otlptrace/example_grpc.py | 11 +++++++---- samples/otlptrace/example_http.py | 21 +++++++++++---------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/samples/otlptrace/README.md b/samples/otlptrace/README.md index 80eb3411..ea05ccb8 100644 --- a/samples/otlptrace/README.md +++ b/samples/otlptrace/README.md @@ -22,9 +22,8 @@ gcloud auth application-default login #### Run the Sample ```sh -# export necessary OTEL environment variables +# export necessary OTEL environment variable export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=" -export OTEL_EXPORTER_OTLP_ENDPOINT= # from the samples/otlptrace repository python3 example_grpc.py diff --git a/samples/otlptrace/example_grpc.py b/samples/otlptrace/example_grpc.py index 5a0be5b1..9e5d59be 100644 --- a/samples/otlptrace/example_grpc.py +++ b/samples/otlptrace/example_grpc.py @@ -36,16 +36,19 @@ request = google.auth.transport.requests.Request() resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-grpc-sample"}) -auth_metadata_plugin = AuthMetadataPlugin( - credentials=credentials, request=request -) +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), ) trace_provider = TracerProvider(resource=resource) -processor = BatchSpanProcessor(OTLPSpanExporter(credentials=channel_creds)) +processor = BatchSpanProcessor( + OTLPSpanExporter( + credentials=channel_creds, + endpoint="https://telemetry.googleapis.com:443/v1/traces", + ) +) trace_provider.add_span_processor(processor) trace.set_tracer_provider(trace_provider) tracer = trace.get_tracer("my.tracer.name") diff --git a/samples/otlptrace/example_http.py b/samples/otlptrace/example_http.py index 854f7e20..83d08ddb 100644 --- a/samples/otlptrace/example_http.py +++ b/samples/otlptrace/example_http.py @@ -19,21 +19,22 @@ from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( OTLPSpanExporter, ) +from google.auth.transport.requests import AuthorizedSession from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor credentials, project_id = google.auth.default() -request = google.auth.transport.requests.Request() -credentials.refresh(request) -req_headers = { - "x-goog-user-project": credentials.quota_project_id, - "Authorization": "Bearer " + credentials.token, -} -resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"}) - -trace_provider = TracerProvider(resource=resource) -processor = BatchSpanProcessor(OTLPSpanExporter(headers=req_headers)) +trace_provider = TracerProvider(resource=Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"})) +processor = BatchSpanProcessor( + OTLPSpanExporter( + headers={ + "x-goog-user-project": credentials.quota_project_id, + }, + session=AuthorizedSession(credentials), + endpoint="https://telemetry.googleapis.com:443/v1/traces", + ) +) trace_provider.add_span_processor(processor) trace.set_tracer_provider(trace_provider) tracer = trace.get_tracer("my.tracer.name") From 9585595473da630dedf4ffd2cced47bc09026d9a Mon Sep 17 00:00:00 2001 From: Dylan Russell Date: Fri, 28 Feb 2025 16:33:24 +0000 Subject: [PATCH 2/3] Do not list telemetry endpoint. --- samples/otlptrace/README.md | 3 ++- samples/otlptrace/example_grpc.py | 1 - samples/otlptrace/example_http.py | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/otlptrace/README.md b/samples/otlptrace/README.md index ea05ccb8..80eb3411 100644 --- a/samples/otlptrace/README.md +++ b/samples/otlptrace/README.md @@ -22,8 +22,9 @@ gcloud auth application-default login #### Run the Sample ```sh -# export necessary OTEL environment variable +# export necessary OTEL environment variables export OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=" +export OTEL_EXPORTER_OTLP_ENDPOINT= # from the samples/otlptrace repository python3 example_grpc.py diff --git a/samples/otlptrace/example_grpc.py b/samples/otlptrace/example_grpc.py index 9e5d59be..bec9cace 100644 --- a/samples/otlptrace/example_grpc.py +++ b/samples/otlptrace/example_grpc.py @@ -46,7 +46,6 @@ processor = BatchSpanProcessor( OTLPSpanExporter( credentials=channel_creds, - endpoint="https://telemetry.googleapis.com:443/v1/traces", ) ) trace_provider.add_span_processor(processor) diff --git a/samples/otlptrace/example_http.py b/samples/otlptrace/example_http.py index 83d08ddb..a07e1dd8 100644 --- a/samples/otlptrace/example_http.py +++ b/samples/otlptrace/example_http.py @@ -25,14 +25,15 @@ from opentelemetry.sdk.trace.export import BatchSpanProcessor credentials, project_id = google.auth.default() -trace_provider = TracerProvider(resource=Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"})) +trace_provider = TracerProvider( + resource=Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"}) +) processor = BatchSpanProcessor( OTLPSpanExporter( headers={ "x-goog-user-project": credentials.quota_project_id, }, session=AuthorizedSession(credentials), - endpoint="https://telemetry.googleapis.com:443/v1/traces", ) ) trace_provider.add_span_processor(processor) From 3083bf62c33bb3159431eb854c689ec140cdc547 Mon Sep 17 00:00:00 2001 From: Dylan Russell Date: Fri, 28 Feb 2025 18:50:11 +0000 Subject: [PATCH 3/3] Remove manually adding quota project header. --- samples/otlptrace/example_grpc.py | 2 +- samples/otlptrace/example_http.py | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/samples/otlptrace/example_grpc.py b/samples/otlptrace/example_grpc.py index bec9cace..80980f52 100644 --- a/samples/otlptrace/example_grpc.py +++ b/samples/otlptrace/example_grpc.py @@ -32,7 +32,7 @@ This is a sample script that exports OTLP traces encoded as protobufs via gRPC. """ -credentials, project_id = google.auth.default() +credentials, _ = google.auth.default() request = google.auth.transport.requests.Request() resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-grpc-sample"}) diff --git a/samples/otlptrace/example_http.py b/samples/otlptrace/example_http.py index a07e1dd8..a613beed 100644 --- a/samples/otlptrace/example_http.py +++ b/samples/otlptrace/example_http.py @@ -24,15 +24,12 @@ from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor -credentials, project_id = google.auth.default() +credentials, _ = google.auth.default() trace_provider = TracerProvider( resource=Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"}) ) processor = BatchSpanProcessor( OTLPSpanExporter( - headers={ - "x-goog-user-project": credentials.quota_project_id, - }, session=AuthorizedSession(credentials), ) )