Skip to content

Commit eec094e

Browse files
authored
fix(tracing): Make sure a valid source is set on a transaction event (#637)
1 parent 5d969d0 commit eec094e

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

tracing.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ func (s *Span) toEvent() *Event {
519519
}
520520
contexts["trace"] = s.traceContext().Map()
521521

522+
// Make sure that the transaction source is valid
523+
transactionSource := s.Source
524+
if !transactionSource.isValid() {
525+
transactionSource = SourceCustom
526+
}
527+
522528
return &Event{
523529
Type: transactionType,
524530
Transaction: s.Name,
@@ -529,7 +535,7 @@ func (s *Span) toEvent() *Event {
529535
StartTime: s.StartTime,
530536
Spans: finished,
531537
TransactionInfo: &TransactionInfo{
532-
Source: s.Source,
538+
Source: transactionSource,
533539
},
534540
sdkMetaData: SDKMetaData{
535541
dsc: s.dynamicSamplingContext,
@@ -620,6 +626,24 @@ const (
620626
SourceTask TransactionSource = "task"
621627
)
622628

629+
// A set of all valid transaction sources.
630+
var allTransactionSources = map[TransactionSource]struct{}{
631+
SourceCustom: {},
632+
SourceURL: {},
633+
SourceRoute: {},
634+
SourceView: {},
635+
SourceComponent: {},
636+
SourceTask: {},
637+
}
638+
639+
// isValid returns 'true' if the given transaction source is a valid
640+
// source as recognized by the envelope protocol:
641+
// https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
642+
func (ts TransactionSource) isValid() bool {
643+
_, found := allTransactionSources[ts]
644+
return found
645+
}
646+
623647
// SpanStatus is the status of a span.
624648
type SpanStatus uint8
625649

@@ -788,7 +812,7 @@ type SpanOption func(s *Span)
788812
// starting a span affects the span tree as a whole, potentially overwriting a
789813
// name set previously.
790814
//
791-
// Deprecated: Use WithTransactionSource() instead.
815+
// Deprecated: Use WithTransactionName() instead.
792816
func TransactionName(name string) SpanOption {
793817
return WithTransactionName(name)
794818
}
@@ -826,6 +850,10 @@ func TransctionSource(source TransactionSource) SpanOption {
826850
}
827851

828852
// WithTransactionSource sets the source of the transaction name.
853+
//
854+
// Note: if the transaction source is not a valid source (as described
855+
// by the spec https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations),
856+
// it will be corrected to "custom" eventually, before the transaction is sent.
829857
func WithTransactionSource(source TransactionSource) SpanOption {
830858
return func(s *Span) {
831859
s.Source = source

tracing_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,49 @@ func TestDeprecatedSpanOptionSpanSampled(t *testing.T) {
890890
func TestDeprecatedSpanOptionTransctionSource(t *testing.T) {
891891
StartSpan(context.Background(), "op", TransctionSource("src"))
892892
}
893+
894+
func TestAdjustingTransactionSourceBeforeSending(t *testing.T) {
895+
tests := []struct {
896+
name string
897+
inputTransactionSource TransactionSource
898+
wantTransactionSource TransactionSource
899+
}{
900+
{
901+
name: "Invalid transaction source",
902+
inputTransactionSource: "invalidSource",
903+
wantTransactionSource: "custom",
904+
},
905+
{
906+
name: "Valid transaction source",
907+
inputTransactionSource: SourceTask,
908+
wantTransactionSource: "task",
909+
},
910+
{
911+
name: "Empty transaction source",
912+
inputTransactionSource: "",
913+
wantTransactionSource: "custom",
914+
},
915+
}
916+
917+
for _, tt := range tests {
918+
tt := tt
919+
t.Run(tt.name, func(t *testing.T) {
920+
transport := &TransportMock{}
921+
ctx := NewTestContext(ClientOptions{
922+
EnableTracing: true,
923+
TracesSampleRate: 1.0,
924+
Transport: transport,
925+
})
926+
transaction := StartTransaction(
927+
ctx,
928+
"Test Transaction",
929+
WithTransactionSource(tt.inputTransactionSource),
930+
)
931+
transaction.Finish()
932+
933+
event := transport.Events()[0]
934+
935+
assertEqual(t, event.TransactionInfo.Source, tt.wantTransactionSource)
936+
})
937+
}
938+
}

0 commit comments

Comments
 (0)