Skip to content

Commit

Permalink
fix(tracing): Make sure a valid source is set on a transaction event (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyo committed May 15, 2023
1 parent 5d969d0 commit eec094e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
32 changes: 30 additions & 2 deletions tracing.go
Expand Up @@ -519,6 +519,12 @@ func (s *Span) toEvent() *Event {
}
contexts["trace"] = s.traceContext().Map()

// Make sure that the transaction source is valid
transactionSource := s.Source
if !transactionSource.isValid() {
transactionSource = SourceCustom
}

return &Event{
Type: transactionType,
Transaction: s.Name,
Expand All @@ -529,7 +535,7 @@ func (s *Span) toEvent() *Event {
StartTime: s.StartTime,
Spans: finished,
TransactionInfo: &TransactionInfo{
Source: s.Source,
Source: transactionSource,
},
sdkMetaData: SDKMetaData{
dsc: s.dynamicSamplingContext,
Expand Down Expand Up @@ -620,6 +626,24 @@ const (
SourceTask TransactionSource = "task"
)

// A set of all valid transaction sources.
var allTransactionSources = map[TransactionSource]struct{}{
SourceCustom: {},
SourceURL: {},
SourceRoute: {},
SourceView: {},
SourceComponent: {},
SourceTask: {},
}

// isValid returns 'true' if the given transaction source is a valid
// source as recognized by the envelope protocol:
// https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
func (ts TransactionSource) isValid() bool {
_, found := allTransactionSources[ts]
return found
}

// SpanStatus is the status of a span.
type SpanStatus uint8

Expand Down Expand Up @@ -788,7 +812,7 @@ type SpanOption func(s *Span)
// starting a span affects the span tree as a whole, potentially overwriting a
// name set previously.
//
// Deprecated: Use WithTransactionSource() instead.
// Deprecated: Use WithTransactionName() instead.
func TransactionName(name string) SpanOption {
return WithTransactionName(name)
}
Expand Down Expand Up @@ -826,6 +850,10 @@ func TransctionSource(source TransactionSource) SpanOption {
}

// WithTransactionSource sets the source of the transaction name.
//
// Note: if the transaction source is not a valid source (as described
// by the spec https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations),
// it will be corrected to "custom" eventually, before the transaction is sent.
func WithTransactionSource(source TransactionSource) SpanOption {
return func(s *Span) {
s.Source = source
Expand Down
46 changes: 46 additions & 0 deletions tracing_test.go
Expand Up @@ -890,3 +890,49 @@ func TestDeprecatedSpanOptionSpanSampled(t *testing.T) {
func TestDeprecatedSpanOptionTransctionSource(t *testing.T) {
StartSpan(context.Background(), "op", TransctionSource("src"))
}

func TestAdjustingTransactionSourceBeforeSending(t *testing.T) {
tests := []struct {
name string
inputTransactionSource TransactionSource
wantTransactionSource TransactionSource
}{
{
name: "Invalid transaction source",
inputTransactionSource: "invalidSource",
wantTransactionSource: "custom",
},
{
name: "Valid transaction source",
inputTransactionSource: SourceTask,
wantTransactionSource: "task",
},
{
name: "Empty transaction source",
inputTransactionSource: "",
wantTransactionSource: "custom",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Transport: transport,
})
transaction := StartTransaction(
ctx,
"Test Transaction",
WithTransactionSource(tt.inputTransactionSource),
)
transaction.Finish()

event := transport.Events()[0]

assertEqual(t, event.TransactionInfo.Source, tt.wantTransactionSource)
})
}
}

0 comments on commit eec094e

Please sign in to comment.