66package tracer
77
88import (
9+ "context"
910 "testing"
11+ "time"
12+
13+ "github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
1014
1115 "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
1217)
1318
1419// TestTrackDataStreamsTransactionPublicAPI verifies that TrackDataStreamsTransaction correctly
@@ -24,5 +29,73 @@ func TestTrackDataStreamsTransactionPublicAPI(t *testing.T) {
2429 assert .NotNil (t , tr .GetDataStreamsProcessor (), "DSM processor should be non-nil when DD_DATA_STREAMS_ENABLED=true" )
2530
2631 // Should not panic and should reach the processor.
27- TrackDataStreamsTransaction ("msg-001" , "ingested" )
32+ TrackDataStreamsTransaction (context .Background (), "msg-001" , "ingested" )
33+ }
34+
35+ // TestTrackDataStreamsTransactionTagsSpan verifies that the active span in the context
36+ // is tagged with the DSM transaction ID and checkpoint name.
37+ func TestTrackDataStreamsTransactionTagsSpan (t * testing.T ) {
38+ t .Setenv ("DD_DATA_STREAMS_ENABLED" , "true" )
39+ t .Setenv ("DD_INSTRUMENTATION_TELEMETRY_ENABLED" , "false" )
40+ Start (withNoopStats ())
41+ defer Stop ()
42+
43+ span , ctx := StartSpanFromContext (context .Background (), "test.op" )
44+ defer span .Finish ()
45+
46+ TrackDataStreamsTransaction (ctx , "tx-span-tag" , "processed" )
47+
48+ s , ok := SpanFromContext (ctx )
49+ require .True (t , ok )
50+ meta := s .getMetadata ()
51+ assert .Equal (t , "tx-span-tag" , meta [ext .DSMTransactionID ])
52+ assert .Equal (t , "processed" , meta [ext .DSMTransactionCheckpoint ])
53+ }
54+
55+ // TestTrackDataStreamsTransactionNoSpanInContextNoops verifies that when the context
56+ // contains no span, tagging is silently skipped and the function does not panic.
57+ func TestTrackDataStreamsTransactionNoSpanInContextNoops (t * testing.T ) {
58+ t .Setenv ("DD_DATA_STREAMS_ENABLED" , "true" )
59+ t .Setenv ("DD_INSTRUMENTATION_TELEMETRY_ENABLED" , "false" )
60+ Start (withNoopStats ())
61+ defer Stop ()
62+
63+ assert .NotPanics (t , func () {
64+ TrackDataStreamsTransaction (context .Background (), "tx-no-span" , "ingested" )
65+ })
66+ }
67+
68+ // TestTrackDataStreamsTransactionAtDelegatesToProcessor verifies that
69+ // TrackDataStreamsTransactionAt forwards the provided time to the processor.
70+ func TestTrackDataStreamsTransactionAtDelegatesToProcessor (t * testing.T ) {
71+ t .Setenv ("DD_DATA_STREAMS_ENABLED" , "true" )
72+ t .Setenv ("DD_INSTRUMENTATION_TELEMETRY_ENABLED" , "false" )
73+ Start (withNoopStats ())
74+ defer Stop ()
75+
76+ fixedTime := time .Date (2024 , 1 , 15 , 12 , 0 , 0 , 0 , time .UTC )
77+ assert .NotPanics (t , func () {
78+ TrackDataStreamsTransactionAt (context .Background (), "tx-at-001" , "delivered" , fixedTime )
79+ })
80+ }
81+
82+ // TestTrackDataStreamsTransactionAtTagsSpan verifies that TrackDataStreamsTransactionAt
83+ // also tags the active span in the context.
84+ func TestTrackDataStreamsTransactionAtTagsSpan (t * testing.T ) {
85+ t .Setenv ("DD_DATA_STREAMS_ENABLED" , "true" )
86+ t .Setenv ("DD_INSTRUMENTATION_TELEMETRY_ENABLED" , "false" )
87+ Start (withNoopStats ())
88+ defer Stop ()
89+
90+ span , ctx := StartSpanFromContext (context .Background (), "test.op" )
91+ defer span .Finish ()
92+
93+ fixedTime := time .Date (2024 , 1 , 15 , 12 , 0 , 0 , 0 , time .UTC )
94+ TrackDataStreamsTransactionAt (ctx , "tx-at-span" , "delivered" , fixedTime )
95+
96+ s , ok := SpanFromContext (ctx )
97+ require .True (t , ok )
98+ meta := s .getMetadata ()
99+ assert .Equal (t , "tx-at-span" , meta [ext .DSMTransactionID ])
100+ assert .Equal (t , "delivered" , meta [ext .DSMTransactionCheckpoint ])
28101}
0 commit comments