github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/span_context_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package instana_test 5 6 import ( 7 "testing" 8 9 instana "github.com/instana/go-sensor" 10 "github.com/instana/go-sensor/w3ctrace" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestNewRootSpanContext(t *testing.T) { 15 c := instana.NewRootSpanContext() 16 17 assert.NotEmpty(t, c.TraceID) 18 assert.Equal(t, c.SpanID, c.TraceID) 19 assert.False(t, c.Sampled) 20 assert.False(t, c.Suppressed) 21 assert.Empty(t, c.Baggage) 22 23 assert.Equal(t, instana.FormatLongID(c.TraceIDHi, c.TraceID), c.W3CContext.Parent().TraceID) 24 assert.Equal(t, instana.FormatID(c.SpanID), c.W3CContext.Parent().ParentID) 25 assert.Equal(t, w3ctrace.Flags{ 26 Sampled: true, 27 }, c.W3CContext.Parent().Flags) 28 } 29 30 func TestNewSpanContext(t *testing.T) { 31 examples := map[string]instana.SpanContext{ 32 "no w3c trace": { 33 TraceIDHi: 10, 34 TraceID: 1, 35 SpanID: 2, 36 ParentID: 3, 37 Sampled: true, 38 Suppressed: true, 39 Baggage: map[string]string{ 40 "key1": "value1", 41 "key2": "value2", 42 }, 43 }, 44 "with w3c trace, no instana state": { 45 TraceIDHi: 10, 46 TraceID: 1, 47 SpanID: 2, 48 W3CContext: w3ctrace.Context{ 49 RawParent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 50 RawState: "vendor1=data", 51 }, 52 }, 53 } 54 55 for name, parent := range examples { 56 t.Run(name, func(t *testing.T) { 57 c := instana.NewSpanContext(parent) 58 59 assert.Equal(t, parent.TraceIDHi, c.TraceIDHi) 60 assert.Equal(t, parent.TraceID, c.TraceID) 61 assert.Equal(t, parent.SpanID, c.ParentID) 62 assert.Equal(t, parent.Sampled, c.Sampled) 63 assert.Equal(t, parent.Suppressed, c.Suppressed) 64 assert.Equal(t, instana.FormatID(c.SpanID), c.W3CContext.Parent().ParentID) 65 assert.Equal(t, instana.EUMCorrelationData{}, c.Correlation) 66 assert.False(t, c.W3CContext.IsZero()) 67 assert.Equal(t, parent.Baggage, c.Baggage) 68 69 assert.NotEqual(t, parent.SpanID, c.SpanID) 70 assert.NotEmpty(t, c.SpanID) 71 assert.False(t, &c.Baggage == &parent.Baggage) 72 }) 73 } 74 } 75 76 func TestNewSpanContext_EmptyParent(t *testing.T) { 77 examples := map[string]instana.SpanContext{ 78 "zero value": {}, 79 "suppressed": {Suppressed: true}, 80 "with correlation data": { 81 Correlation: instana.EUMCorrelationData{ 82 Type: "web", 83 ID: "1", 84 }, 85 }, 86 } 87 88 for name, parent := range examples { 89 t.Run(name, func(t *testing.T) { 90 91 instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, nil) 92 defer instana.ShutdownSensor() 93 94 c := instana.NewSpanContext(parent) 95 96 assert.NotEmpty(t, c.TraceID) 97 assert.Equal(t, c.SpanID, c.TraceID) 98 assert.Empty(t, c.ParentID) 99 assert.False(t, c.Sampled) 100 assert.Equal(t, parent.Suppressed, c.Suppressed) 101 assert.Equal(t, instana.EUMCorrelationData{}, c.Correlation) 102 assert.False(t, c.W3CContext.IsZero()) 103 assert.Empty(t, c.Baggage) 104 }) 105 } 106 } 107 108 func TestNewSpanContext_FromW3CTraceContext(t *testing.T) { 109 parent := instana.SpanContext{ 110 W3CContext: w3ctrace.Context{ 111 RawParent: "00-00000000000000010000000000000002-0000000000000003-01", 112 RawState: "in=1234;5678,vendor1=data", 113 }, 114 } 115 116 instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, nil) 117 defer instana.ShutdownSensor() 118 119 c := instana.NewSpanContext(parent) 120 121 assert.NotEqual(t, parent.SpanID, c.SpanID) 122 assert.Equal(t, instana.SpanContext{ 123 TraceIDHi: 0x1, 124 TraceID: 0x2, 125 ParentID: 0x3, 126 SpanID: c.SpanID, 127 W3CContext: w3ctrace.Context{ 128 RawParent: "00-00000000000000010000000000000002-" + instana.FormatID(c.SpanID) + "-01", 129 RawState: "in=1234;5678,vendor1=data", 130 }, 131 ForeignTrace: true, 132 Links: []instana.SpanReference{ 133 {TraceID: "1234", SpanID: "5678"}, 134 }, 135 }, c) 136 } 137 138 func TestSpanContext_WithBaggageItem(t *testing.T) { 139 c := instana.SpanContext{ 140 TraceIDHi: 10, 141 TraceID: 1, 142 SpanID: 2, 143 ParentID: 3, 144 Sampled: true, 145 Baggage: map[string]string{ 146 "key1": "value1", 147 "key2": "value2", 148 }, 149 } 150 151 updated := c.WithBaggageItem("key3", "value3") 152 assert.Equal(t, instana.SpanContext{ 153 TraceIDHi: 10, 154 TraceID: 1, 155 SpanID: 2, 156 ParentID: 3, 157 Sampled: true, 158 Baggage: map[string]string{ 159 "key1": "value1", 160 "key2": "value2", 161 "key3": "value3", 162 }, 163 }, updated) 164 165 assert.Equal(t, instana.SpanContext{ 166 TraceIDHi: 10, 167 TraceID: 1, 168 SpanID: 2, 169 ParentID: 3, 170 Sampled: true, 171 Baggage: map[string]string{ 172 "key1": "value1", 173 "key2": "value2", 174 }, 175 }, c) 176 } 177 178 func TestSpanContext_IsZero(t *testing.T) { 179 examples := map[string]instana.SpanContext{ 180 "with 64-bit trace ID": {TraceID: 0x1}, 181 "with 128-bit trace ID": {TraceIDHi: 0x1}, 182 "with span ID": {SpanID: 0x1}, 183 "with w3c context": { 184 W3CContext: w3ctrace.New(w3ctrace.Parent{ 185 Version: w3ctrace.Version_Max, 186 TraceID: "abcd", 187 ParentID: "1234", 188 }), 189 }, 190 "with suppressed option": { 191 Suppressed: true, 192 }, 193 } 194 195 for name, sc := range examples { 196 t.Run(name, func(t *testing.T) { 197 assert.False(t, sc.IsZero()) 198 }) 199 } 200 201 t.Run("zero value", func(t *testing.T) { 202 assert.True(t, instana.SpanContext{}.IsZero()) 203 }) 204 } 205 206 func TestSpanContext_Clone(t *testing.T) { 207 c := instana.SpanContext{ 208 TraceIDHi: 10, 209 TraceID: 1, 210 SpanID: 2, 211 ParentID: 3, 212 Sampled: true, 213 Suppressed: true, 214 W3CContext: w3ctrace.New(w3ctrace.Parent{ 215 TraceID: "w3ctraceid", 216 ParentID: "w3cparentid", 217 }), 218 Baggage: map[string]string{ 219 "key1": "value1", 220 "key2": "value2", 221 }, 222 } 223 224 cloned := c.Clone() 225 assert.Equal(t, c, cloned) 226 assert.False(t, &c == &cloned) 227 assert.False(t, &c.Baggage == &cloned.Baggage) 228 } 229 230 func TestSpanContext_Clone_NoBaggage(t *testing.T) { 231 c := instana.SpanContext{ 232 TraceIDHi: 10, 233 TraceID: 1, 234 SpanID: 2, 235 ParentID: 3, 236 Sampled: true, 237 } 238 239 cloned := c.Clone() 240 assert.Equal(t, c, cloned) 241 }