| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xray |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"go.opentelemetry.io/otel" |
| 13 |
"go.opentelemetry.io/otel/propagation" |
| 14 |
"go.opentelemetry.io/otel/trace" |
| 15 |
) |
| 16 |
|
| 17 |
func TestExtract(t *testing.T) { |
| 18 |
type extractTestCase struct { |
| 19 |
name string |
| 20 |
headerVal string |
| 21 |
wantValid bool |
| 22 |
wantTraceID string |
| 23 |
wantSpanID string |
| 24 |
wantSampled bool |
| 25 |
} |
| 26 |
|
| 27 |
tests := []extractTestCase{ |
| 28 |
{ |
| 29 |
name: "Valid header - sampled", |
| 30 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=1", |
| 31 |
wantValid: true, |
| 32 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 33 |
wantSpanID: "1234567890abcdef", |
| 34 |
wantSampled: true, |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "Valid header - not sampled", |
| 38 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=0", |
| 39 |
wantValid: true, |
| 40 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 41 |
wantSpanID: "1234567890abcdef", |
| 42 |
wantSampled: false, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "Valid header - sample requested", |
| 46 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=?", |
| 47 |
wantValid: true, |
| 48 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 49 |
wantSpanID: "1234567890abcdef", |
| 50 |
wantSampled: false, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "Empty header - no trace info", |
| 54 |
headerVal: "", |
| 55 |
wantValid: false, |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "Malformed TraceID - too short", |
| 59 |
headerVal: "Root=1-abc-123;Parent=1234567890abcdef;Sampled=1", |
| 60 |
wantValid: false, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "Malformed TraceID - missing delimiters", |
| 64 |
headerVal: "Root=1abcdef121234567890abcdef12345678;Parent=1234567890abcdef;Sampled=1", |
| 65 |
wantValid: false, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "Invalid TraceID version", |
| 69 |
headerVal: "Root=2-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=1", |
| 70 |
wantValid: false, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "Invalid SpanID format", |
| 74 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=bad-spanid;Sampled=1", |
| 75 |
wantValid: false, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "Missing Sampled", |
| 79 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef", |
| 80 |
wantValid: true, |
| 81 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 82 |
wantSpanID: "1234567890abcdef", |
| 83 |
wantSampled: false, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "Unknown Sampled value", |
| 87 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=other", |
| 88 |
wantValid: true, |
| 89 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 90 |
wantSpanID: "1234567890abcdef", |
| 91 |
wantSampled: false, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "Malformed key-value pair - missing '='", |
| 95 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;BrokenKeyValue;Sampled=1", |
| 96 |
wantValid: false, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "Only Sampled key", |
| 100 |
headerVal: "Sampled=1", |
| 101 |
wantValid: false, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "Missing Root key", |
| 105 |
headerVal: "Parent=1234567890abcdef;Sampled=1", |
| 106 |
wantValid: false, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "Trailing semicolon", |
| 110 |
headerVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=1;", |
| 111 |
wantValid: true, |
| 112 |
wantTraceID: "abcdef121234567890abcdef12345678", |
| 113 |
wantSpanID: "1234567890abcdef", |
| 114 |
wantSampled: true, |
| 115 |
}, |
| 116 |
} |
| 117 |
|
| 118 |
for _, tc := range tests { |
| 119 |
t.Run(tc.name, func(t *testing.T) { |
| 120 |
carrier := propagation.MapCarrier{} |
| 121 |
if tc.headerVal != "" { |
| 122 |
carrier.Set("X-Amzn-Trace-Id", tc.headerVal) |
| 123 |
} |
| 124 |
|
| 125 |
// prop := P |
| 126 |
|
| 127 |
ctx := Propagator{}.Extract(t.Context(), carrier) |
| 128 |
sc := trace.SpanContextFromContext(ctx) |
| 129 |
|
| 130 |
if sc.IsValid() != tc.wantValid { |
| 131 |
t.Fatalf("expected valid=%v, got %v", tc.wantValid, sc.IsValid()) |
| 132 |
} |
| 133 |
|
| 134 |
if tc.wantValid { |
| 135 |
if got := sc.TraceID().String(); got != tc.wantTraceID { |
| 136 |
t.Errorf("expected TraceID %q, got %q", tc.wantTraceID, got) |
| 137 |
} |
| 138 |
if got := sc.SpanID().String(); got != tc.wantSpanID { |
| 139 |
t.Errorf("expected SpanID %q, got %q", tc.wantSpanID, got) |
| 140 |
} |
| 141 |
if got := sc.IsSampled(); got != tc.wantSampled { |
| 142 |
t.Log("name-->> ", tc.name) |
| 143 |
t.Errorf("expected sampled=%v, got %v", tc.wantSampled, got) |
| 144 |
} |
| 145 |
} |
| 146 |
}) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func TestInject(t *testing.T) { |
| 151 |
type injectTestCase struct { |
| 152 |
name string |
| 153 |
traceID string |
| 154 |
spanID string |
| 155 |
traceFlags trace.TraceFlags |
| 156 |
wantHeaderVal string |
| 157 |
wantHeaderSet bool |
| 158 |
} |
| 159 |
|
| 160 |
tests := []injectTestCase{ |
| 161 |
{ |
| 162 |
name: "Valid span context - sampled", |
| 163 |
traceID: "abcdef121234567890abcdef12345678", |
| 164 |
spanID: "1234567890abcdef", |
| 165 |
traceFlags: trace.FlagsSampled, |
| 166 |
wantHeaderVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=1", |
| 167 |
wantHeaderSet: true, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "Valid span context - not sampled", |
| 171 |
traceID: "abcdef121234567890abcdef12345678", |
| 172 |
spanID: "1234567890abcdef", |
| 173 |
traceFlags: 0, |
| 174 |
wantHeaderVal: "Root=1-abcdef12-1234567890abcdef12345678;Parent=1234567890abcdef;Sampled=0", |
| 175 |
wantHeaderSet: true, |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "Different trace and span IDs - sampled", |
| 179 |
traceID: "fedcba098765432100fedcba09876543", |
| 180 |
spanID: "fedcba0987654321", |
| 181 |
traceFlags: trace.FlagsSampled, |
| 182 |
wantHeaderVal: "Root=1-fedcba09-8765432100fedcba09876543;Parent=fedcba0987654321;Sampled=1", |
| 183 |
wantHeaderSet: true, |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "Minimum valid trace and span - not sampled", |
| 187 |
traceID: "00000000000000000000000000000001", |
| 188 |
spanID: "0000000000000001", |
| 189 |
traceFlags: 0, |
| 190 |
wantHeaderVal: "Root=1-00000000-000000000000000000000001;Parent=0000000000000001;Sampled=0", |
| 191 |
wantHeaderSet: true, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "Maximum valid trace and span - sampled", |
| 195 |
traceID: "ffffffffffffffffffffffffffffffff", |
| 196 |
spanID: "ffffffffffffffff", |
| 197 |
traceFlags: trace.FlagsSampled, |
| 198 |
wantHeaderVal: "Root=1-ffffffff-ffffffffffffffffffffffff;Parent=ffffffffffffffff;Sampled=1", |
| 199 |
wantHeaderSet: true, |
| 200 |
}, |
| 201 |
{ |
| 202 |
name: "Complex hex pattern - not sampled", |
| 203 |
traceID: "a1b2c3d4e5f6789012345678901abcde", |
| 204 |
spanID: "a1b2c3d4e5f67890", |
| 205 |
traceFlags: 0, |
| 206 |
wantHeaderVal: "Root=1-a1b2c3d4-e5f6789012345678901abcde;Parent=a1b2c3d4e5f67890;Sampled=0", |
| 207 |
wantHeaderSet: true, |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "Invalid trace ID - empty", |
| 211 |
traceID: "", |
| 212 |
spanID: "1234567890abcdef", |
| 213 |
traceFlags: trace.FlagsSampled, |
| 214 |
wantHeaderSet: false, |
| 215 |
}, |
| 216 |
{ |
| 217 |
name: "Invalid span ID - empty", |
| 218 |
traceID: "abcdef121234567890abcdef12345678", |
| 219 |
spanID: "", |
| 220 |
traceFlags: trace.FlagsSampled, |
| 221 |
wantHeaderSet: false, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "Both invalid - empty trace and span IDs", |
| 225 |
traceID: "", |
| 226 |
spanID: "", |
| 227 |
traceFlags: trace.FlagsSampled, |
| 228 |
wantHeaderSet: false, |
| 229 |
}, |
| 230 |
} |
| 231 |
|
| 232 |
for _, tc := range tests { |
| 233 |
t.Run(tc.name, func(t *testing.T) { |
| 234 |
t.Parallel() |
| 235 |
|
| 236 |
propagator := Propagator{} |
| 237 |
carrier := propagation.MapCarrier{} |
| 238 |
|
| 239 |
var ctx context.Context |
| 240 |
|
| 241 |
if tc.traceID != "" && tc.spanID != "" { |
| 242 |
traceID, err := trace.TraceIDFromHex(tc.traceID) |
| 243 |
if err != nil { |
| 244 |
t.Fatalf("failed to parse trace ID: %v", err) |
| 245 |
} |
| 246 |
|
| 247 |
spanID, err := trace.SpanIDFromHex(tc.spanID) |
| 248 |
if err != nil { |
| 249 |
t.Fatalf("failed to parse span ID: %v", err) |
| 250 |
} |
| 251 |
|
| 252 |
sc := trace.NewSpanContext(trace.SpanContextConfig{ |
| 253 |
TraceID: traceID, |
| 254 |
SpanID: spanID, |
| 255 |
TraceFlags: tc.traceFlags, |
| 256 |
}) |
| 257 |
|
| 258 |
ctx = trace.ContextWithSpanContext(t.Context(), sc) |
| 259 |
} else { |
| 260 |
ctx = t.Context() |
| 261 |
} |
| 262 |
|
| 263 |
propagator.Inject(ctx, carrier) |
| 264 |
|
| 265 |
headerVal := carrier.Get(traceHeaderKey) |
| 266 |
|
| 267 |
if tc.wantHeaderSet { |
| 268 |
if headerVal == "" { |
| 269 |
t.Errorf("expected header to be set, but it was empty") |
| 270 |
} |
| 271 |
if headerVal != tc.wantHeaderVal { |
| 272 |
t.Errorf("expected header value %q, got %q", tc.wantHeaderVal, headerVal) |
| 273 |
} |
| 274 |
} else if headerVal != "" { |
| 275 |
t.Errorf("expected no header to be set, but got %q", headerVal) |
| 276 |
} |
| 277 |
}) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func TestInjectWithNoSpanContext(t *testing.T) { |
| 282 |
t.Parallel() |
| 283 |
|
| 284 |
propagator := Propagator{} |
| 285 |
carrier := propagation.MapCarrier{} |
| 286 |
|
| 287 |
ctx := t.Context() |
| 288 |
|
| 289 |
propagator.Inject(ctx, carrier) |
| 290 |
|
| 291 |
headerVal := carrier.Get(traceHeaderKey) |
| 292 |
if headerVal != "" { |
| 293 |
t.Errorf("expected no header to be set when no span context exists, but got %q", headerVal) |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func TestInjectWithInvalidSpanContext(t *testing.T) { |
| 298 |
t.Parallel() |
| 299 |
|
| 300 |
propagator := Propagator{} |
| 301 |
carrier := propagation.MapCarrier{} |
| 302 |
|
| 303 |
sc := trace.SpanContext{} |
| 304 |
ctx := trace.ContextWithSpanContext(t.Context(), sc) |
| 305 |
|
| 306 |
propagator.Inject(ctx, carrier) |
| 307 |
|
| 308 |
headerVal := carrier.Get(traceHeaderKey) |
| 309 |
if headerVal != "" { |
| 310 |
t.Errorf("expected no header to be set when span context is invalid, but got %q", headerVal) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
func BenchmarkPropagatorExtract(b *testing.B) { |
| 315 |
propagator := Propagator{} |
| 316 |
|
| 317 |
ctx := b.Context() |
| 318 |
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody) |
| 319 |
|
| 320 |
req.Header.Set("Root", "1-8a3c60f7-d188f8fa79d48a391a778fa6") |
| 321 |
req.Header.Set("Parent", "53995c3f42cd8ad8") |
| 322 |
req.Header.Set("Sampled", "1") |
| 323 |
|
| 324 |
b.ResetTimer() |
| 325 |
for range b.N { |
| 326 |
_ = propagator.Extract(ctx, propagation.HeaderCarrier(req.Header)) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func BenchmarkPropagatorInject(b *testing.B) { |
| 331 |
propagator := Propagator{} |
| 332 |
tracer := otel.Tracer("test") |
| 333 |
|
| 334 |
req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody) |
| 335 |
ctx, _ := tracer.Start(b.Context(), "Parent operation...") |
| 336 |
|
| 337 |
b.ResetTimer() |
| 338 |
for range b.N { |
| 339 |
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header)) |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func TestPropagatorFields(t *testing.T) { |
| 344 |
propagator := Propagator{} |
| 345 |
assert.Len(t, propagator.Fields(), 1, "Fields() should return exactly one field") |
| 346 |
assert.Equal(t, []string{traceHeaderKey}, propagator.Fields()) |
| 347 |
} |