OpenTelemetry gRPC Instrumentation
This library provides automatic instrumentation for gRPC clients and servers within the OpenTelemetry Python ecosystem. It's part of the `opentelemetry-python-contrib` repository, currently at version `0.61b0`. As a `contrib` package, its release cadence often aligns with the main OpenTelemetry Python project, receiving frequent updates to add features, fix bugs, and align with new OpenTelemetry specifications.
Warnings
- gotcha This instrumentation, like many in `opentelemetry-python-contrib`, is currently in beta (`0.61b0`). While generally stable, its API might undergo minor breaking changes or modifications in future releases before reaching a stable `1.0.0` version.
- gotcha The gRPC instrumentation must be enabled *before* gRPC channels or servers are initialized. If `GrpcInstrumentor().instrument()` is called after `grpc.insecure_channel()` or `grpc.server()` has already been used to create instances, those existing instances will not be instrumented.
- gotcha This instrumentation automatically patches standard gRPC methods. Custom gRPC interceptors or highly specialized gRPC setups might not be fully covered. In such cases, you may need to implement manual instrumentation using the OpenTelemetry API.
- gotcha The instrumentation relies on the presence of the `grpcio` package. While `opentelemetry-instrumentation-grpc` specifies it as a dependency, ensure your application's environment has a compatible version of `grpcio` installed.
Install
-
pip install opentelemetry-instrumentation-grpc opentelemetry-sdk
Imports
- GrpcInstrumentor
from opentelemetry.instrumentation.grpc import GrpcInstrumentor
- set_tracer_provider
from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry import trace
Quickstart
import grpc
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.grpc import GrpcInstrumentor
# 1. Configure OpenTelemetry Tracer Provider
resource = Resource.create({"service.name": "my-grpc-app"})
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(tracer_provider)
# 2. Initialize gRPC Instrumentation
GrpcInstrumentor().instrument()
# Now any gRPC client or server interactions will be automatically instrumented.
# Example (no actual gRPC server running, just showing the channel creation):
print("OpenTelemetry gRPC instrumentation initialized.")
print("Any grpc.insecure_channel or grpc.secure_channel calls from now on will be instrumented.")
try:
# This part would typically be your actual gRPC client/server code
channel = grpc.insecure_channel('localhost:50051')
# Example: call a method (this would require a real gRPC setup)
# stub = YourServiceStub(channel)
# response = stub.YourMethod(YourRequest())
print("Created a gRPC channel (if a server was running, it would be instrumented).")
channel.close()
except Exception as e:
print(f"An error occurred (expected if no gRPC server is running): {e}")
print("Check console for exported spans.")