Skip to content

Commit

Permalink
Merge pull request #18 from paulbrittain/add-option-to-set-operation-…
Browse files Browse the repository at this point in the history
…name

Add option to set operation name override.
  • Loading branch information
johnbellone authored Mar 7, 2024
2 parents 54d2413 + 3b66f2d commit c1696d0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
14 changes: 12 additions & 2 deletions client_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
ctx = sentry.SetHubOnContext(ctx, hub)
}

span := sentry.StartSpan(ctx, "grpc.client")
operationName := defaultClientOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}

span := sentry.StartSpan(ctx, operationName)
ctx = span.Context()
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
Expand Down Expand Up @@ -60,7 +65,12 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
ctx = sentry.SetHubOnContext(ctx, hub)
}

span := sentry.StartSpan(ctx, "grpc.client")
operationName := defaultClientOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}

span := sentry.StartSpan(ctx, operationName)
ctx = span.Context()
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
Expand Down
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ func (r *reportOnOption) Apply(o *options) {
func WithReportOn(r reporter) Option {
return &reportOnOption{ReportOn: r}
}

type operationNameOverride struct {
OperationNameOverride string
}

func (r *operationNameOverride) Apply(o *options) {
o.OperationNameOverride = r.OperationNameOverride
}

func WithOperationNameOverride(s string) Option {
return &operationNameOverride{OperationNameOverride: s}
}
18 changes: 12 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
"google.golang.org/grpc/status"
)

const (
defaultServerOperationName = "grpc.server"
defaultClientOperationName = "grpc.client"
)

var defaultOptions = &options{
Repanic: false,
WaitForDelivery: false,
ReportOn: ReportAlways,
Timeout: 1 * time.Second,
Repanic: false,
WaitForDelivery: false,
ReportOn: ReportAlways,
Timeout: 1 * time.Second,
OperationNameOverride: "",
}


type options struct {
// Repanic configures whether Sentry should repanic after recovery.
Repanic bool
Expand All @@ -26,8 +31,9 @@ type options struct {
Timeout time.Duration

ReportOn func(error) bool
}

OperationNameOverride string
}

func ReportAlways(error) bool {
return true
Expand Down
14 changes: 12 additions & 2 deletions server_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
ctx = sentry.SetHubOnContext(ctx, hub)
}

operationName := defaultServerOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}

md, _ := metadata.FromIncomingContext(ctx) // nil check in ContinueFromGrpcMetadata
span := sentry.StartSpan(ctx, "grpc.server", ContinueFromGrpcMetadata(md))
span := sentry.StartSpan(ctx, operationName, ContinueFromGrpcMetadata(md))
ctx = span.Context()
defer span.Finish()

Expand Down Expand Up @@ -79,8 +84,13 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
ctx = sentry.SetHubOnContext(ctx, hub)
}

operationName := defaultServerOperationName
if o.OperationNameOverride != "" {
operationName = o.OperationNameOverride
}

md, _ := metadata.FromIncomingContext(ctx) // nil check in ContinueFromGrpcMetadata
span := sentry.StartSpan(ctx, "grpc.server", ContinueFromGrpcMetadata(md))
span := sentry.StartSpan(ctx, operationName, ContinueFromGrpcMetadata(md))
ctx = span.Context()
defer span.Finish()

Expand Down

0 comments on commit c1696d0

Please sign in to comment.