gRPC

The gRPC integration instruments all incoming requests and outgoing unary-unary, unary-stream grpc requests using grpcio channels.

Use this integration to start or continue transactions for incoming grpc requests, create spans for outgoing requests, and ensure traces are properly propagated to downstream services.

Install

Install sentry-sdk from PyPI with the grpcio extra.

Copied
pip install --upgrade 'sentry-sdk[grpcio]'

Configure

Add appropriate interceptor to your grpc stub/server:

Server

Copied
import grpc

import sentry_sdk
from sentry_sdk.integrations.grpc.server import ServerInterceptor

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    traces_sample_rate=1.0,
)

...

server = grpc.server(
    thread_pool=...,
    interceptors=[ServerInterceptor()],
)

Client

Copied
import grpc

import sentry_sdk
from sentry_sdk.integrations.grpc.client import ClientInterceptor

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    traces_sample_rate=1.0,
)

...

with grpc.insecure_channel("example.com:12345") as channel:
    channel = grpc.intercept_channel(channel, *[ClientInterceptor()])

Verify

Server

If you added the interceptor as described above, the server will create a transaction that will show up in sentry.io.

It takes a couple of moments for the data to appear in sentry.io.

Client

Copied
import grpc

sentry_sdk.init(...)  # same as above

...

with sentry_sdk.start_transaction(op="function", name="grcp_client"):
    with grpc.insecure_channel("example.com:12345") as channel:
        channel = grpc.intercept_channel(channel, *[ClientInterceptor()])
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))

This will create a transaction called testing_sentry in the Performance section of sentry.io and will create a span for the call to the SayHello method on the server.

It takes a couple of moments for the data to appear in sentry.io.

Supported Versions

  • grpcio: 1.21.1+
  • Python: 3.5+
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").