Skip to content

Commit 20684c7

Browse files
authored
Push a new scope when a span is started (#886)
1 parent 2935366 commit 20684c7

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

tracing.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp
195195
clientOptions := span.clientOptions()
196196
if clientOptions.EnableTracing {
197197
hub := hubFromContext(ctx)
198+
if !span.IsTransaction() {
199+
// Push a new scope to stack for non transaction span
200+
hub.PushScope()
201+
}
198202
hub.Scope().SetSpan(&span)
199203
}
200204

@@ -355,6 +359,12 @@ func (s *Span) doFinish() {
355359
s.EndTime = monotonicTimeSince(s.StartTime)
356360
}
357361

362+
hub := hubFromContext(s.ctx)
363+
if !s.IsTransaction() {
364+
// Referenced to StartSpan function that pushes current non-transaction span to scope stack
365+
defer hub.PopScope()
366+
}
367+
358368
if !s.Sampled.Bool() {
359369
return
360370
}
@@ -370,7 +380,6 @@ func (s *Span) doFinish() {
370380
// TODO(tracing): add breadcrumbs
371381
// (see https://github.com/getsentry/sentry-python/blob/f6f3525f8812f609/sentry_sdk/tracing.py#L372)
372382

373-
hub := hubFromContext(s.ctx)
374383
hub.CaptureEvent(event)
375384
}
376385

tracing_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,3 +1019,71 @@ func TestSpanFinishConcurrentlyWithoutRaces(_ *testing.T) {
10191019

10201020
time.Sleep(50 * time.Millisecond)
10211021
}
1022+
1023+
func TestSpanScopeManagement(t *testing.T) {
1024+
// Initialize a test hub and client
1025+
transport := &TransportMock{}
1026+
client, err := NewClient(ClientOptions{
1027+
EnableTracing: true,
1028+
TracesSampleRate: 1.0,
1029+
Transport: transport,
1030+
})
1031+
if err != nil {
1032+
t.Fatal(err)
1033+
}
1034+
hub := NewHub(client, NewScope())
1035+
1036+
// Set the hub on the context
1037+
ctx := context.Background()
1038+
ctx = SetHubOnContext(ctx, hub)
1039+
1040+
// Start a parent span (transaction)
1041+
transaction := StartTransaction(ctx, "parent-operation")
1042+
defer transaction.Finish()
1043+
1044+
// Start a child span
1045+
childSpan := StartSpan(transaction.Context(), "child-operation")
1046+
// Finish the child span
1047+
defer childSpan.Finish()
1048+
1049+
subChildSpan := StartSpan(childSpan.Context(), "sub_child-operation")
1050+
subChildSpan.Finish()
1051+
1052+
// Capture an event after finishing the child span
1053+
// This event should be associated with the first child span
1054+
hub.CaptureMessage("Test event")
1055+
1056+
// Flush to ensure the event is sent
1057+
transport.Flush(time.Second)
1058+
1059+
// Verify that the event has the correct trace data
1060+
events := transport.Events()
1061+
if len(events) != 1 {
1062+
t.Fatalf("expected 2 event, got %d", len(events))
1063+
}
1064+
event := events[0]
1065+
1066+
// Extract the trace context from the event
1067+
traceCtx, ok := event.Contexts["trace"]
1068+
if !ok {
1069+
t.Fatalf("event does not have a trace context")
1070+
}
1071+
1072+
// Extract TraceID and SpanID from the trace context
1073+
traceID, ok := traceCtx["trace_id"].(TraceID)
1074+
if !ok {
1075+
t.Fatalf("trace_id not found")
1076+
}
1077+
spanID, ok := traceCtx["span_id"].(SpanID)
1078+
if !ok {
1079+
t.Fatalf("span_id not found")
1080+
}
1081+
1082+
// Verify that the IDs match the first child span IDs
1083+
if traceID != childSpan.TraceID {
1084+
t.Errorf("expected TraceID %s, got %s", transaction.TraceID, traceID)
1085+
}
1086+
if spanID != childSpan.SpanID {
1087+
t.Errorf("expected SpanID %s, got %s", transaction.SpanID, spanID)
1088+
}
1089+
}

0 commit comments

Comments
 (0)