github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/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 "github.com/instana/testify/assert" 10 instana "github.com/mier85/go-sensor" 11 "github.com/mier85/go-sensor/w3ctrace" 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 c := instana.NewSpanContext(parent) 91 92 assert.NotEmpty(t, c.TraceID) 93 assert.Equal(t, c.SpanID, c.TraceID) 94 assert.Empty(t, c.ParentID) 95 assert.False(t, c.Sampled) 96 assert.Equal(t, parent.Suppressed, c.Suppressed) 97 assert.Equal(t, instana.EUMCorrelationData{}, c.Correlation) 98 assert.False(t, c.W3CContext.IsZero()) 99 assert.Empty(t, c.Baggage) 100 }) 101 } 102 } 103 104 func TestNewSpanContext_FromW3CTraceContext(t *testing.T) { 105 parent := instana.SpanContext{ 106 W3CContext: w3ctrace.Context{ 107 RawParent: "00-00000000000000010000000000000002-0000000000000003-01", 108 RawState: "in=1234;5678,vendor1=data", 109 }, 110 } 111 112 c := instana.NewSpanContext(parent) 113 114 assert.NotEqual(t, parent.SpanID, c.SpanID) 115 assert.Equal(t, instana.SpanContext{ 116 TraceIDHi: 0x1, 117 TraceID: 0x2, 118 ParentID: 0x3, 119 SpanID: c.SpanID, 120 W3CContext: w3ctrace.Context{ 121 RawParent: "00-00000000000000010000000000000002-" + instana.FormatID(c.SpanID) + "-01", 122 RawState: "in=1234;5678,vendor1=data", 123 }, 124 ForeignTrace: true, 125 Links: []instana.SpanReference{ 126 {TraceID: "1234", SpanID: "5678"}, 127 }, 128 }, c) 129 } 130 131 func TestSpanContext_WithBaggageItem(t *testing.T) { 132 c := instana.SpanContext{ 133 TraceIDHi: 10, 134 TraceID: 1, 135 SpanID: 2, 136 ParentID: 3, 137 Sampled: true, 138 Baggage: map[string]string{ 139 "key1": "value1", 140 "key2": "value2", 141 }, 142 } 143 144 updated := c.WithBaggageItem("key3", "value3") 145 assert.Equal(t, instana.SpanContext{ 146 TraceIDHi: 10, 147 TraceID: 1, 148 SpanID: 2, 149 ParentID: 3, 150 Sampled: true, 151 Baggage: map[string]string{ 152 "key1": "value1", 153 "key2": "value2", 154 "key3": "value3", 155 }, 156 }, updated) 157 158 assert.Equal(t, instana.SpanContext{ 159 TraceIDHi: 10, 160 TraceID: 1, 161 SpanID: 2, 162 ParentID: 3, 163 Sampled: true, 164 Baggage: map[string]string{ 165 "key1": "value1", 166 "key2": "value2", 167 }, 168 }, c) 169 } 170 171 func TestSpanContext_IsZero(t *testing.T) { 172 examples := map[string]instana.SpanContext{ 173 "with 64-bit trace ID": {TraceID: 0x1}, 174 "with 128-bit trace ID": {TraceIDHi: 0x1}, 175 "with span ID": {SpanID: 0x1}, 176 "with w3c context": { 177 W3CContext: w3ctrace.New(w3ctrace.Parent{ 178 Version: w3ctrace.Version_Max, 179 TraceID: "abcd", 180 ParentID: "1234", 181 }), 182 }, 183 "with suppressed option": { 184 Suppressed: true, 185 }, 186 } 187 188 for name, sc := range examples { 189 t.Run(name, func(t *testing.T) { 190 assert.False(t, sc.IsZero()) 191 }) 192 } 193 194 t.Run("zero value", func(t *testing.T) { 195 assert.True(t, instana.SpanContext{}.IsZero()) 196 }) 197 } 198 199 func TestSpanContext_Clone(t *testing.T) { 200 c := instana.SpanContext{ 201 TraceIDHi: 10, 202 TraceID: 1, 203 SpanID: 2, 204 ParentID: 3, 205 Sampled: true, 206 Suppressed: true, 207 W3CContext: w3ctrace.New(w3ctrace.Parent{ 208 TraceID: "w3ctraceid", 209 ParentID: "w3cparentid", 210 }), 211 Baggage: map[string]string{ 212 "key1": "value1", 213 "key2": "value2", 214 }, 215 } 216 217 cloned := c.Clone() 218 assert.Equal(t, c, cloned) 219 assert.False(t, &c == &cloned) 220 assert.False(t, &c.Baggage == &cloned.Baggage) 221 } 222 223 func TestSpanContext_Clone_NoBaggage(t *testing.T) { 224 c := instana.SpanContext{ 225 TraceIDHi: 10, 226 TraceID: 1, 227 SpanID: 2, 228 ParentID: 3, 229 Sampled: true, 230 } 231 232 cloned := c.Clone() 233 assert.Equal(t, c, cloned) 234 }