OpenTelemetry gRPC Integration

Module contents

Usage Client

import logging

import grpc

from opentelemetry import trace
from opentelemetry.ext.grpc import GrpcInstrumentorClient, client_interceptor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleExportSpanProcessor,
)

try:
    from .gen import helloworld_pb2, helloworld_pb2_grpc
except ImportError:
    from gen import helloworld_pb2, helloworld_pb2_grpc

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(ConsoleSpanExporter())
)
instrumentor = GrpcInstrumentorClient()
instrumentor.instrument()

def run():
    with grpc.insecure_channel("localhost:50051") as channel:

        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name="YOU"))

    print("Greeter client received: " + response.message)


if __name__ == "__main__":
    logging.basicConfig()
    run()

Usage Server

import logging
from concurrent import futures

import grpc

from opentelemetry import trace
from opentelemetry.ext.grpc import GrpcInstrumentorServer, server_interceptor
from opentelemetry.ext.grpc.grpcext import intercept_server
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleExportSpanProcessor,
)

try:
    from .gen import helloworld_pb2, helloworld_pb2_grpc
except ImportError:
    from gen import helloworld_pb2, helloworld_pb2_grpc

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(ConsoleSpanExporter())
)
grpc_server_instrumentor = GrpcInstrumentorServer()
grpc_server_instrumentor.instrument()


class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)


def serve():

    server = grpc.server(futures.ThreadPoolExecutor())

    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port("[::]:50051")
    server.start()
    server.wait_for_termination()


if __name__ == "__main__":
    logging.basicConfig()
    serve()
class opentelemetry.ext.grpc.GrpcInstrumentorServer[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

wrapper_fn(original_func, instance, args, kwargs)[source]
class opentelemetry.ext.grpc.GrpcInstrumentorClient[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

wrapper_fn(original_func, instance, args, kwargs)[source]
opentelemetry.ext.grpc.client_interceptor(tracer_provider=None)[source]

Create a gRPC client channel interceptor.

Parameters

tracer – The tracer to use to create client-side spans.

Returns

An invocation-side interceptor object.

opentelemetry.ext.grpc.server_interceptor(tracer_provider=None)[source]

Create a gRPC server interceptor.

Parameters

tracer – The tracer to use to create server-side spans.

Returns

A service-side interceptor object.