Skip to content

Commit 98709e9

Browse files
committed
feat: add TracesInstrumenter option
This enables instrumentation code (sentryhttp, sentrygin, etc) to skip trace instrumentation while still performing panic recovery.
1 parent f505932 commit 98709e9

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ TODO.md
1212
# IDE system files
1313
.idea
1414
.vscode
15+
16+
/go.work*

client.go

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ type ClientOptions struct {
133133
TracesSampleRate float64
134134
// Used to customize the sampling of traces, overrides TracesSampleRate.
135135
TracesSampler TracesSampler
136+
// Which instrumentation to use for tracing. Either "sentry" or "otel" are supported.
137+
// Setting this to "otel" will ignore TracesSampleRate and TracesSampler and assume sampling is performed by otel.
138+
TracesInstrumenter string
136139
// The sample rate for profiling traces in the range [0.0, 1.0].
137140
// This is relative to TracesSampleRate - it is a ratio of profiled traces out of all sampled traces.
138141
ProfilesSampleRate float64
@@ -301,6 +304,19 @@ func NewClient(options ClientOptions) (*Client, error) {
301304
options.MaxSpans = defaultMaxSpans
302305
}
303306

307+
switch options.TracesInstrumenter {
308+
case "":
309+
options.TracesInstrumenter = "sentry"
310+
case "sentry":
311+
// noop
312+
case "otel":
313+
// sampling is performed by the OpenTelemetry SDK
314+
options.TracesSampleRate = 1.0
315+
options.TracesSampler = nil
316+
default:
317+
return nil, fmt.Errorf("invalid value for TracesInstrumenter (supported are 'sentry' and 'otel'): %q", options.TracesInstrumenter)
318+
}
319+
304320
// SENTRYGODEBUG is a comma-separated list of key=value pairs (similar
305321
// to GODEBUG). It is not a supported feature: recognized debug options
306322
// may change any time.

gin/sentrygin.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ func (h *handler) handle(c *gin.Context) {
7979
sentry.WithTransactionSource(transactionSource),
8080
}
8181

82-
transaction := sentry.StartTransaction(ctx,
83-
fmt.Sprintf("%s %s", c.Request.Method, transactionName),
84-
options...,
85-
)
86-
defer func() {
87-
transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status())
88-
transaction.Finish()
89-
}()
90-
91-
c.Request = c.Request.WithContext(transaction.Context())
82+
if hub.Client().Options().TracesInstrumenter == "sentry" {
83+
transaction := sentry.StartTransaction(ctx,
84+
fmt.Sprintf("%s %s", c.Request.Method, transactionName),
85+
options...,
86+
)
87+
defer func() {
88+
transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status())
89+
transaction.Finish()
90+
}()
91+
c.Request = c.Request.WithContext(transaction.Context())
92+
}
93+
9294
hub.Scope().SetRequest(c.Request)
9395
c.Set(valuesKey, hub)
9496
defer h.recoverWithSentry(hub, c.Request)

http/sentryhttp.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,20 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
9999
sentry.ContinueFromRequest(r),
100100
sentry.WithTransactionSource(sentry.SourceURL),
101101
}
102-
// We don't mind getting an existing transaction back so we don't need to
103-
// check if it is.
104-
transaction := sentry.StartTransaction(ctx,
105-
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
106-
options...,
107-
)
108-
defer transaction.Finish()
109-
// TODO(tracing): if the next handler.ServeHTTP panics, store
110-
// information on the transaction accordingly (status, tag,
111-
// level?, ...).
112-
r = r.WithContext(transaction.Context())
102+
103+
if hub.Client().Options().TracesInstrumenter == "sentry" {
104+
// We don't mind getting an existing transaction back so we don't need to
105+
// check if it is.
106+
transaction := sentry.StartTransaction(ctx,
107+
fmt.Sprintf("%s %s", r.Method, r.URL.Path),
108+
options...,
109+
)
110+
defer transaction.Finish()
111+
// TODO(tracing): if the next handler.ServeHTTP panics, store
112+
// information on the transaction accordingly (status, tag,
113+
// level?, ...).
114+
r = r.WithContext(transaction.Context())
115+
}
113116
hub.Scope().SetRequest(r)
114117
defer h.recoverWithSentry(hub, r)
115118
// TODO(tracing): use custom response writer to intercept

0 commit comments

Comments
 (0)