gRPC

Learn about the gRPC integration and how it adds support for the grpcio grpc client and server.

The gRPC integration instruments incoming unary-unary grpc 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 sentry-sdk from PyPI with the grpcio extra.

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

Add GRPCIntegration() to your integrations list, and enable tracing.

This will add appropriate intercepters to your server/client to capture errors and performance data.

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Copied
import grpc

import sentry_sdk
from sentry_sdk.integrations.grpc import GRPCIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
    integrations=[
        GRPCIntegration(),
    ],
)

...

# this works with synchronous servers:
server = grpc.server(thread_pool=...)

# ... and asynchronous servers:
server = grpc.aio.server()

Copied
import grpc

import sentry_sdk
from sentry_sdk.integrations.grpc import GRPCIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
    integrations=[
        GRPCIntegration(),
    ],
)

...

# this works with synchronous clients:
with grpc.insecure_channel("example.com:12345") as channel:
    ...

# ... and asynchronous clients:
async with grpc.aio.insecure_channel("example.com:12345") as channel:
    ...

Note:

In older versions of this integration you had to add the interceptors by hand. Since Python SDK version 1.35.0 you do not need to add any interceptors by hand but only add the GRPCIntegration as described above.

If you added the GRPCIntegration as described above, the server will create a transaction for each call to a function and send it to sentry.io.

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

Copied
import grpc

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

...

with sentry_sdk.start_transaction(op="function", name="testing_sentry"):
    with grpc.insecure_channel("example.com:12345") as channel:
        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.

The transaction created in the client will also be connected to the transaction on the server, giving you a full trace of your request.

If you use a framework (like Django, Flask, FastAPI, etc.) the transaction will be created for you automatically.

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

  • grpcio: 1.39+
  • Python: 3.7+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").