@@ -1019,3 +1019,71 @@ func TestSpanFinishConcurrentlyWithoutRaces(_ *testing.T) {
1019
1019
1020
1020
time .Sleep (50 * time .Millisecond )
1021
1021
}
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