getsentry / sentry-go

The official Go SDK for Sentry (sentry.io)
https://docs.sentry.io/platforms/go/
MIT License
926 stars 214 forks source link

gRPC interceptors #240

Open rcmachado opened 4 years ago

rcmachado commented 4 years ago

Summary

Provide gRPC interceptors to automatically capture errors and panics from servers and clients, both for unary and stream requests.

Motivation

gRPC is a high-performance, open source universal RPC framework originally developed by Google. It supports languages/platforms such as Python, Ruby, Kotlin and Go.

Go SDK already has some integrations with standard net/http package and some popular frameworks (like Gin, Martini and others).

Similar to other frameworks, having standard Sentry integration will make it easier for users to integrate Sentry with gRPC.

Additional Context

gRPC has the concept of interceptor (middleware), which could make it easier to implement an official Sentry integration. An interceptor is a function with a specific signature/type:

// Server interceptors (middlewares)
type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)
type StreamServerInterceptor func(srv interface{}, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error

// Client interceptors (middlewares)
type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error
type StreamClientInterceptor func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error)

Using an interceptor is also straightforward (pseudo-code):

// Sentry initialization
err := sentry.Init(sentry.ClientOptions{
    // Either set your DSN here or set the SENTRY_DSN environment variable.
    Dsn: "https://7a0d561762b749e79de56c58bb0d898b@o24338.ingest.sentry.io/1307339",
    // Enable printing of SDK debug messages.
    // Useful when getting started or trying to figure something out.
    Debug: true,
})
if err != nil {
    log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
defer sentry.Flush(2 * time.Second)

// Initialize gRPC server
myServer := grpc.NewServer(
    grpc.StreamInterceptor(
        sentry.StreamServerInterceptor(),
    ),
    grpc.UnaryInterceptor(
        sentry.UnaryServerInterceptor(),
    ),
)
shouichi commented 3 years ago

Hi, I opened a draft pull request for this feature. Could someone please take a look? Any feedback would be appreciated. https://github.com/getsentry/sentry-go/pull/312

prasad-marne commented 3 years ago

Any updates on this. Should be very useful for anyone working with Grpc. Not sure why the PR is still in review

pablodz commented 3 years ago

Any update? x2 Still relevant

Glyphack commented 2 years ago

Hi, I've been using a solution for myself by integrating the Grpc Erro and status package with Sentry. I'm open to contribute it here but not sure if this issue is still active or not. I see an open PR which is not reviewed, is there any blocker?

cleptric commented 2 years ago

@Glyphack We're slowly getting back to the Go SDK, and we'll take a look at adding integration for gRPC after #486 for sure

github-actions[bot] commented 1 year ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

itsamirhn commented 1 year ago

Any Update?

pablodz commented 1 year ago

Any update? x3 Still relevant

Glyphack commented 1 year ago

Any update? x3 Still relevant

It'd be nice if you could just click the notifications to get updates, without posting a comment that emails everyone else uselessly. When there's nothing to add to the discussion there's no need to comment.

Anyway I have written this piece of code in the past based on Grpc status package: https://github.com/Glyphack/koal/blob/master/server/pkg/sentrygrpc/sentrygrpc.go