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

fix(tracing): Make sure a valid source is set on a transaction event #637

Merged
merged 3 commits into from May 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})
}
}