Skip to content

Commit

Permalink
Add trace origin (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice committed Jul 3, 2024
1 parent 77f9517 commit dafeb07
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 7 deletions.
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 @@ const (
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 @@ type Span struct { //nolint: maligned // prefer readability over optimal memory
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 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
parent: parent,
}

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

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 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
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 @@ func WithSpanSampled(sampled Sampled) SpanOption {
}
}

// 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

0 comments on commit dafeb07

Please sign in to comment.