Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace origin #849

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions echo/sentryecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (h *handler) handle(next echo.HandlerFunc) echo.HandlerFunc {
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(transactionSource),
sentry.WithSpanOrigin(sentry.SpanOriginEcho),
}

transaction := sentry.StartTransaction(
Expand Down
1 change: 1 addition & 0 deletions fasthttp/sentryfasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (h *Handler) Handle(handler fasthttp.RequestHandler) fasthttp.RequestHandle
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(convertedHTTPRequest),
sentry.WithTransactionSource(sentry.SourceRoute),
sentry.WithSpanOrigin(sentry.SpanOriginFastHTTP),
}

method := string(ctx.Method())
Expand Down
1 change: 1 addition & 0 deletions fiber/sentryfiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (h *handler) handle(ctx *fiber.Ctx) error {
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(convertedHTTPRequest),
sentry.WithTransactionSource(transactionSource),
sentry.WithSpanOrigin(sentry.SpanOriginFiber),
}

transaction := sentry.StartTransaction(
Expand Down
1 change: 1 addition & 0 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (h *handler) handle(c *gin.Context) {
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request),
sentry.WithTransactionSource(transactionSource),
sentry.WithSpanOrigin(sentry.SpanOriginGin),
}

transaction := sentry.StartTransaction(ctx,
Expand Down
1 change: 1 addition & 0 deletions http/sentryhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
sentry.WithSpanOrigin(sentry.SpanOriginStdLib),
}

transaction := sentry.StartTransaction(ctx,
Expand Down
1 change: 1 addition & 0 deletions iris/sentryiris.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (h *handler) handle(ctx iris.Context) {
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(ctx.Request()),
sentry.WithTransactionSource(sentry.SourceRoute),
sentry.WithSpanOrigin(sentry.SpanOriginIris),
}

currentRoute := ctx.GetCurrentRoute()
Expand Down
1 change: 1 addition & 0 deletions negroni/sentrynegroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Ha
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(r),
sentry.WithTransactionSource(sentry.SourceURL),
sentry.WithSpanOrigin(sentry.SpanOriginNegroni),
}
// We don't mind getting an existing transaction back so we don't need to
// check if it is.
Expand Down
37 changes: 30 additions & 7 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
SentryBaggageHeader = "baggage"
)

// SpanOrigin indicates what created a trace or a span. See: https://develop.sentry.dev/sdk/performance/trace-origin/
type SpanOrigin string

const (
SpanOriginManual = "manual"
SpanOriginEcho = "auto.http.echo"
SpanOriginFastHTTP = "auto.http.fasthttp"
SpanOriginFiber = "auto.http.fiber"
SpanOriginGin = "auto.http.gin"
SpanOriginStdLib = "auto.http.stdlib"
SpanOriginIris = "auto.http.iris"
SpanOriginNegroni = "auto.http.negroni"
)

// A Span is the building block of a Sentry transaction. Spans build up a tree
// structure of timed operations. The span tree makes up a transaction event
// that is sent to Sentry when the root span is finished.
Expand All @@ -37,6 +51,7 @@
Data map[string]interface{} `json:"data,omitempty"`
Sampled Sampled `json:"-"`
Source TransactionSource `json:"-"`
Origin SpanOrigin `json:"origin,omitempty"`

// mu protects concurrent writes to map fields
mu sync.RWMutex
Expand Down Expand Up @@ -113,11 +128,19 @@
parent: parent,
}

_, err := rand.Read(span.SpanID[:])
if err != nil {
panic(err)

Check warning on line 133 in tracing.go

View check run for this annotation

Codecov / codecov/patch

tracing.go#L133

Added line #L133 was not covered by tests
}

if hasParent {
span.TraceID = parent.TraceID
span.ParentSpanID = parent.SpanID
span.Origin = parent.Origin
} else {
// Only set the Source if this is a transaction
span.Source = SourceCustom
span.Origin = SpanOriginManual

// Implementation note:
//
Expand Down Expand Up @@ -154,13 +177,6 @@
panic(err)
}
}
_, err := rand.Read(span.SpanID[:])
if err != nil {
panic(err)
}
if hasParent {
span.ParentSpanID = parent.SpanID
}

// Apply options to override defaults.
for _, option := range options {
Expand Down Expand Up @@ -870,6 +886,13 @@
}
}

// WithSpanOrigin sets the origin of the span.
func WithSpanOrigin(origin SpanOrigin) SpanOption {
return func(s *Span) {
s.Origin = origin
}
}

// ContinueFromRequest returns a span option that updates the span to continue
// an existing trace. If it cannot detect an existing trace in the request, the
// span will be left unchanged.
Expand Down
1 change: 1 addition & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestStartChild(t *testing.T) {
ParentSpanID: child.ParentSpanID,
Op: child.Op,
Sampled: SampledTrue,
Origin: SpanOriginManual,
},
},
TransactionInfo: &TransactionInfo{
Expand Down
Loading