diff --git a/samples/otlptrace/README.md b/samples/otlptrace/README.md new file mode 100644 index 00000000..e016f876 --- /dev/null +++ b/samples/otlptrace/README.md @@ -0,0 +1,32 @@ +### OTLP Export Sample with GCP Auth +This example shows how to send traces to an OTLP (OpenTelemetry Protocol) endpoint that is protected by GCP authentication. The sample showcases the trace export using: + - gRPC + - http with protobuf + +#### Installation +Install the dependencies and libraries required to run the samples: + +```sh +# Move to the sample repository +cd samples/otlptrace + +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 OTEL_RESOURCE_ATTRIBUTES="gcp.project_id=" +export OTEL_EXPORTER_OTLP_ENDPOINT= + +cd samples/otlptrace && python3 example_grpc.py +``` +Other variations of the sample: + - `python3 example_http.py` - will run a program that will export traces using http/protobuf. diff --git a/samples/otlptrace/example_grpc.py b/samples/otlptrace/example_grpc.py new file mode 100644 index 00000000..3d234bc8 --- /dev/null +++ b/samples/otlptrace/example_grpc.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# Copyright 2024 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 trace +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( + OTLPSpanExporter, +) +from opentelemetry.sdk.resources import SERVICE_NAME, Resource +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor + +""" +This is a sample script that exports OTLP traces encoded as protobufs via gRPC. +""" + + +credentials, project_id = google.auth.default() +request = google.auth.transport.requests.Request() +credentials.refresh(request) +resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-grpc-sample"}) + +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, insecure=False) +) +trace_provider.add_span_processor(processor) +trace.set_tracer_provider(trace_provider) +tracer = trace.get_tracer("my.tracer.name") + + +def do_work(): + with tracer.start_as_current_span("span-grpc") as span: + # do some work that 'span' will track + print("doing some work...") + # When the 'with' block goes out of scope, 'span' is closed for you + + +def do_work_repeatedly(): + try: + while True: + do_work() + time.sleep(10) + except KeyboardInterrupt: + print("\nKeyboard Interrupt: Stopping work.") + + +do_work_repeatedly() diff --git a/samples/otlptrace/example_http.py b/samples/otlptrace/example_http.py index 1350ef21..854f7e20 100644 --- a/samples/otlptrace/example_http.py +++ b/samples/otlptrace/example_http.py @@ -30,9 +30,7 @@ "x-goog-user-project": credentials.quota_project_id, "Authorization": "Bearer " + credentials.token, } -resource = Resource.create(attributes={ - SERVICE_NAME: "otlp-gcp-http-sample" -}) +resource = Resource.create(attributes={SERVICE_NAME: "otlp-gcp-http-sample"}) trace_provider = TracerProvider(resource=resource) processor = BatchSpanProcessor(OTLPSpanExporter(headers=req_headers)) diff --git a/samples/otlptrace/requirements.txt b/samples/otlptrace/requirements.txt new file mode 100644 index 00000000..5c91c468 --- /dev/null +++ b/samples/otlptrace/requirements.txt @@ -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-http==1.24.0 +opentelemetry-exporter-otlp-proto-grpc==1.24.0 +grpcio==1.63.0