Skip to content

Commit

Permalink
feat: add TracesInstrumenter option
Browse files Browse the repository at this point in the history
This enables instrumentation code (sentryhttp, sentrygin, etc) to skip
trace instrumentation while still performing panic recovery.
  • Loading branch information
costela committed Oct 4, 2023
1 parent f505932 commit 98709e9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ TODO.md
# IDE system files
.idea
.vscode

/go.work*
16 changes: 16 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type ClientOptions struct {
TracesSampleRate float64
// Used to customize the sampling of traces, overrides TracesSampleRate.
TracesSampler TracesSampler
// Which instrumentation to use for tracing. Either "sentry" or "otel" are supported.
// Setting this to "otel" will ignore TracesSampleRate and TracesSampler and assume sampling is performed by otel.
TracesInstrumenter string
// The sample rate for profiling traces in the range [0.0, 1.0].
// This is relative to TracesSampleRate - it is a ratio of profiled traces out of all sampled traces.
ProfilesSampleRate float64
Expand Down Expand Up @@ -301,6 +304,19 @@ func NewClient(options ClientOptions) (*Client, error) {
options.MaxSpans = defaultMaxSpans
}

switch options.TracesInstrumenter {
case "":
options.TracesInstrumenter = "sentry"
case "sentry":

Check warning on line 310 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L310

Added line #L310 was not covered by tests
// noop
case "otel":
// sampling is performed by the OpenTelemetry SDK
options.TracesSampleRate = 1.0
options.TracesSampler = nil
default:
return nil, fmt.Errorf("invalid value for TracesInstrumenter (supported are 'sentry' and 'otel'): %q", options.TracesInstrumenter)

Check warning on line 317 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L312-L317

Added lines #L312 - L317 were not covered by tests
}

// SENTRYGODEBUG is a comma-separated list of key=value pairs (similar
// to GODEBUG). It is not a supported feature: recognized debug options
// may change any time.
Expand Down
22 changes: 12 additions & 10 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ func (h *handler) handle(c *gin.Context) {
sentry.WithTransactionSource(transactionSource),
}

transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request.Method, transactionName),
options...,
)
defer func() {
transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status())
transaction.Finish()
}()

c.Request = c.Request.WithContext(transaction.Context())
if hub.Client().Options().TracesInstrumenter == "sentry" {
transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request.Method, transactionName),
options...,
)
defer func() {
transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status())
transaction.Finish()
}()
c.Request = c.Request.WithContext(transaction.Context())
}

hub.Scope().SetRequest(c.Request)
c.Set(valuesKey, hub)
defer h.recoverWithSentry(hub, c.Request)
Expand Down
25 changes: 14 additions & 11 deletions http/sentryhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,20 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
}
// We don't mind getting an existing transaction back so we don't need to
// check if it is.
transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
options...,
)
defer transaction.Finish()
// TODO(tracing): if the next handler.ServeHTTP panics, store
// information on the transaction accordingly (status, tag,
// level?, ...).
r = r.WithContext(transaction.Context())

if hub.Client().Options().TracesInstrumenter == "sentry" {
// We don't mind getting an existing transaction back so we don't need to
// check if it is.
transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
options...,
)
defer transaction.Finish()
// TODO(tracing): if the next handler.ServeHTTP panics, store
// information on the transaction accordingly (status, tag,
// level?, ...).
r = r.WithContext(transaction.Context())
}
hub.Scope().SetRequest(r)
defer h.recoverWithSentry(hub, r)
// TODO(tracing): use custom response writer to intercept
Expand Down

0 comments on commit 98709e9

Please sign in to comment.