| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewSpanConfig(t *testing.T) { |
| 16 |
k1v1 := attribute.String("key1", "value1") |
| 17 |
k1v2 := attribute.String("key1", "value2") |
| 18 |
k2v2 := attribute.String("key2", "value2") |
| 19 |
|
| 20 |
timestamp0 := time.Unix(0, 0) |
| 21 |
timestamp1 := time.Unix(0, 0) |
| 22 |
|
| 23 |
link1 := Link{ |
| 24 |
SpanContext: SpanContext{traceID: TraceID([16]byte{1, 1}), spanID: SpanID{3}}, |
| 25 |
Attributes: []attribute.KeyValue{k1v1}, |
| 26 |
} |
| 27 |
link2 := Link{ |
| 28 |
SpanContext: SpanContext{traceID: TraceID([16]byte{1, 1}), spanID: SpanID{3}}, |
| 29 |
Attributes: []attribute.KeyValue{k1v2, k2v2}, |
| 30 |
} |
| 31 |
|
| 32 |
tests := []struct { |
| 33 |
options []SpanStartOption |
| 34 |
expected SpanConfig |
| 35 |
}{ |
| 36 |
{ |
| 37 |
// No non-zero-values should be set. |
| 38 |
[]SpanStartOption{}, |
| 39 |
SpanConfig{}, |
| 40 |
}, |
| 41 |
{ |
| 42 |
[]SpanStartOption{ |
| 43 |
WithAttributes(k1v1), |
| 44 |
}, |
| 45 |
SpanConfig{ |
| 46 |
attributes: []attribute.KeyValue{k1v1}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
// Multiple calls should append not overwrite. |
| 51 |
[]SpanStartOption{ |
| 52 |
WithAttributes(k1v1), |
| 53 |
WithAttributes(k1v2), |
| 54 |
WithAttributes(k2v2), |
| 55 |
}, |
| 56 |
SpanConfig{ |
| 57 |
// No uniqueness is guaranteed by the API. |
| 58 |
attributes: []attribute.KeyValue{k1v1, k1v2, k2v2}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
[]SpanStartOption{ |
| 63 |
WithAttributes(k1v1, k1v2, k2v2), |
| 64 |
}, |
| 65 |
SpanConfig{ |
| 66 |
// No uniqueness is guaranteed by the API. |
| 67 |
attributes: []attribute.KeyValue{k1v1, k1v2, k2v2}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
[]SpanStartOption{ |
| 72 |
WithTimestamp(timestamp0), |
| 73 |
}, |
| 74 |
SpanConfig{ |
| 75 |
timestamp: timestamp0, |
| 76 |
}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
[]SpanStartOption{ |
| 80 |
// Multiple calls overwrites with last-one-wins. |
| 81 |
WithTimestamp(timestamp0), |
| 82 |
WithTimestamp(timestamp1), |
| 83 |
}, |
| 84 |
SpanConfig{ |
| 85 |
timestamp: timestamp1, |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
[]SpanStartOption{ |
| 90 |
WithLinks(link1), |
| 91 |
}, |
| 92 |
SpanConfig{ |
| 93 |
links: []Link{link1}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
[]SpanStartOption{ |
| 98 |
// Multiple calls should append not overwrite. |
| 99 |
WithLinks(link1), |
| 100 |
WithLinks(link1, link2), |
| 101 |
}, |
| 102 |
SpanConfig{ |
| 103 |
// No uniqueness is guaranteed by the API. |
| 104 |
links: []Link{link1, link1, link2}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
[]SpanStartOption{ |
| 109 |
WithNewRoot(), |
| 110 |
}, |
| 111 |
SpanConfig{ |
| 112 |
newRoot: true, |
| 113 |
}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
[]SpanStartOption{ |
| 117 |
// Multiple calls should not change NewRoot state. |
| 118 |
WithNewRoot(), |
| 119 |
WithNewRoot(), |
| 120 |
}, |
| 121 |
SpanConfig{ |
| 122 |
newRoot: true, |
| 123 |
}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
[]SpanStartOption{ |
| 127 |
WithSpanKind(SpanKindConsumer), |
| 128 |
}, |
| 129 |
SpanConfig{ |
| 130 |
spanKind: SpanKindConsumer, |
| 131 |
}, |
| 132 |
}, |
| 133 |
{ |
| 134 |
[]SpanStartOption{ |
| 135 |
// Multiple calls overwrites with last-one-wins. |
| 136 |
WithSpanKind(SpanKindClient), |
| 137 |
WithSpanKind(SpanKindConsumer), |
| 138 |
}, |
| 139 |
SpanConfig{ |
| 140 |
spanKind: SpanKindConsumer, |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
// Everything should work together. |
| 145 |
[]SpanStartOption{ |
| 146 |
WithAttributes(k1v1), |
| 147 |
WithTimestamp(timestamp0), |
| 148 |
WithLinks(link1, link2), |
| 149 |
WithNewRoot(), |
| 150 |
WithSpanKind(SpanKindConsumer), |
| 151 |
}, |
| 152 |
SpanConfig{ |
| 153 |
attributes: []attribute.KeyValue{k1v1}, |
| 154 |
timestamp: timestamp0, |
| 155 |
links: []Link{link1, link2}, |
| 156 |
newRoot: true, |
| 157 |
spanKind: SpanKindConsumer, |
| 158 |
}, |
| 159 |
}, |
| 160 |
} |
| 161 |
for _, test := range tests { |
| 162 |
assert.Equal(t, test.expected, NewSpanStartConfig(test.options...)) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestSpanStartConfigAttributeMutability(t *testing.T) { |
| 167 |
a := attribute.String("a", "val") |
| 168 |
b := attribute.String("b", "val") |
| 169 |
attrs := []attribute.KeyValue{a, b} |
| 170 |
conf := NewSpanStartConfig(WithAttributes(attrs...)) |
| 171 |
|
| 172 |
// Mutating passed arg should not change configured attributes. |
| 173 |
attrs[0] = attribute.String("c", "val") |
| 174 |
|
| 175 |
want := SpanConfig{attributes: []attribute.KeyValue{a, b}} |
| 176 |
assert.Equal(t, want, conf) |
| 177 |
} |
| 178 |
|
| 179 |
func TestEndSpanConfig(t *testing.T) { |
| 180 |
timestamp := time.Unix(0, 0) |
| 181 |
|
| 182 |
tests := []struct { |
| 183 |
options []SpanEndOption |
| 184 |
expected SpanConfig |
| 185 |
}{ |
| 186 |
{ |
| 187 |
[]SpanEndOption{}, |
| 188 |
SpanConfig{}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
[]SpanEndOption{ |
| 192 |
WithStackTrace(true), |
| 193 |
}, |
| 194 |
SpanConfig{ |
| 195 |
stackTrace: true, |
| 196 |
}, |
| 197 |
}, |
| 198 |
{ |
| 199 |
[]SpanEndOption{ |
| 200 |
WithTimestamp(timestamp), |
| 201 |
}, |
| 202 |
SpanConfig{ |
| 203 |
timestamp: timestamp, |
| 204 |
}, |
| 205 |
}, |
| 206 |
} |
| 207 |
for _, test := range tests { |
| 208 |
assert.Equal(t, test.expected, NewSpanEndConfig(test.options...)) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestTracerConfig(t *testing.T) { |
| 213 |
v1 := "semver:0.0.1" |
| 214 |
v2 := "semver:1.0.0" |
| 215 |
schemaURL := "https://opentelemetry.io/schemas/1.21.0" |
| 216 |
attrs := attribute.NewSet( |
| 217 |
attribute.String("user", "alice"), |
| 218 |
attribute.Bool("admin", true), |
| 219 |
) |
| 220 |
|
| 221 |
c := NewTracerConfig( |
| 222 |
// Multiple calls should overwrite. |
| 223 |
WithInstrumentationVersion(v1), |
| 224 |
WithInstrumentationVersion(v2), |
| 225 |
WithSchemaURL(schemaURL), |
| 226 |
WithInstrumentationAttributes(attrs.ToSlice()...), |
| 227 |
) |
| 228 |
|
| 229 |
assert.Equal(t, v2, c.InstrumentationVersion(), "instrumentation version") |
| 230 |
assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL") |
| 231 |
assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes") |
| 232 |
} |
| 233 |
|
| 234 |
func TestWithInstrumentationAttributesNotLazy(t *testing.T) { |
| 235 |
attrs := []attribute.KeyValue{ |
| 236 |
attribute.String("service", "test"), |
| 237 |
attribute.Int("three", 3), |
| 238 |
} |
| 239 |
want := attribute.NewSet(attrs...) |
| 240 |
|
| 241 |
// WithInstrumentationAttributes is expected to immediately |
| 242 |
// create an immutable set from the attributes, so later changes |
| 243 |
// to attrs should not affect the config. |
| 244 |
opt := WithInstrumentationAttributes(attrs...) |
| 245 |
attrs[0] = attribute.String("service", "changed") |
| 246 |
|
| 247 |
c := NewTracerConfig(opt) |
| 248 |
assert.Equal(t, want, c.InstrumentationAttributes(), "instrumentation attributes") |
| 249 |
} |
| 250 |
|
| 251 |
func TestWithInstrumentationAttributeSet(t *testing.T) { |
| 252 |
attrs := attribute.NewSet( |
| 253 |
attribute.String("service", "test"), |
| 254 |
attribute.Int("three", 3), |
| 255 |
) |
| 256 |
|
| 257 |
c := NewTracerConfig( |
| 258 |
WithInstrumentationAttributeSet(attrs), |
| 259 |
) |
| 260 |
|
| 261 |
assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes") |
| 262 |
} |
| 263 |
|
| 264 |
// Save benchmark results to a file level var to avoid the compiler optimizing |
| 265 |
// away the actual work. |
| 266 |
var ( |
| 267 |
spanConfig SpanConfig |
| 268 |
eventConfig EventConfig |
| 269 |
) |
| 270 |
|
| 271 |
func BenchmarkNewTracerConfig(b *testing.B) { |
| 272 |
for _, bb := range []struct { |
| 273 |
name string |
| 274 |
options []TracerOption |
| 275 |
}{ |
| 276 |
{ |
| 277 |
name: "with no options", |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "with an instrumentation version", |
| 281 |
options: []TracerOption{ |
| 282 |
WithInstrumentationVersion("testing version"), |
| 283 |
}, |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "with a schema url", |
| 287 |
options: []TracerOption{ |
| 288 |
WithSchemaURL("testing URL"), |
| 289 |
}, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "with instrumentation attribute", |
| 293 |
options: []TracerOption{ |
| 294 |
WithInstrumentationAttributes(attribute.String("key", "value")), |
| 295 |
}, |
| 296 |
}, |
| 297 |
{ |
| 298 |
name: "with instrumentation attribute set", |
| 299 |
options: []TracerOption{ |
| 300 |
WithInstrumentationAttributeSet(attribute.NewSet(attribute.String("key", "value"))), |
| 301 |
}, |
| 302 |
}, |
| 303 |
} { |
| 304 |
b.Run(bb.name, func(b *testing.B) { |
| 305 |
b.ReportAllocs() |
| 306 |
b.ResetTimer() |
| 307 |
|
| 308 |
for b.Loop() { |
| 309 |
NewTracerConfig(bb.options...) |
| 310 |
} |
| 311 |
}) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func BenchmarkNewSpanStartConfig(b *testing.B) { |
| 316 |
for _, bb := range []struct { |
| 317 |
name string |
| 318 |
options []SpanStartOption |
| 319 |
}{ |
| 320 |
{ |
| 321 |
name: "with no options", |
| 322 |
}, |
| 323 |
{ |
| 324 |
name: "with attributes", |
| 325 |
options: []SpanStartOption{ |
| 326 |
WithAttributes(attribute.Bool("key", true)), |
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
name: "with attributes set multiple times", |
| 331 |
options: []SpanStartOption{ |
| 332 |
WithAttributes(attribute.Bool("key", true)), |
| 333 |
WithAttributes(attribute.Bool("secondKey", false)), |
| 334 |
}, |
| 335 |
}, |
| 336 |
{ |
| 337 |
name: "with a timestamp", |
| 338 |
options: []SpanStartOption{ |
| 339 |
WithTimestamp(time.Now()), |
| 340 |
}, |
| 341 |
}, |
| 342 |
{ |
| 343 |
name: "with links", |
| 344 |
options: []SpanStartOption{ |
| 345 |
WithLinks(Link{}), |
| 346 |
}, |
| 347 |
}, |
| 348 |
{ |
| 349 |
name: "with links set multiple times", |
| 350 |
options: []SpanStartOption{ |
| 351 |
WithLinks(Link{}), |
| 352 |
WithLinks(Link{}), |
| 353 |
}, |
| 354 |
}, |
| 355 |
{ |
| 356 |
name: "with new root", |
| 357 |
options: []SpanStartOption{ |
| 358 |
WithNewRoot(), |
| 359 |
}, |
| 360 |
}, |
| 361 |
{ |
| 362 |
name: "with span kind", |
| 363 |
options: []SpanStartOption{ |
| 364 |
WithSpanKind(SpanKindClient), |
| 365 |
}, |
| 366 |
}, |
| 367 |
} { |
| 368 |
b.Run(bb.name, func(b *testing.B) { |
| 369 |
b.ReportAllocs() |
| 370 |
b.ResetTimer() |
| 371 |
|
| 372 |
for i := 0; i < b.N; i++ { |
| 373 |
spanConfig = NewSpanStartConfig(bb.options...) |
| 374 |
} |
| 375 |
}) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func BenchmarkNewSpanEndConfig(b *testing.B) { |
| 380 |
for _, bb := range []struct { |
| 381 |
name string |
| 382 |
options []SpanEndOption |
| 383 |
}{ |
| 384 |
{ |
| 385 |
name: "with no options", |
| 386 |
}, |
| 387 |
{ |
| 388 |
name: "with a timestamp", |
| 389 |
options: []SpanEndOption{ |
| 390 |
WithTimestamp(time.Now()), |
| 391 |
}, |
| 392 |
}, |
| 393 |
{ |
| 394 |
name: "with stack trace", |
| 395 |
options: []SpanEndOption{ |
| 396 |
WithStackTrace(true), |
| 397 |
}, |
| 398 |
}, |
| 399 |
} { |
| 400 |
b.Run(bb.name, func(b *testing.B) { |
| 401 |
b.ReportAllocs() |
| 402 |
b.ResetTimer() |
| 403 |
|
| 404 |
for i := 0; i < b.N; i++ { |
| 405 |
spanConfig = NewSpanEndConfig(bb.options...) |
| 406 |
} |
| 407 |
}) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
func BenchmarkNewEventConfig(b *testing.B) { |
| 412 |
for _, bb := range []struct { |
| 413 |
name string |
| 414 |
options []EventOption |
| 415 |
}{ |
| 416 |
{ |
| 417 |
name: "with no options", |
| 418 |
}, |
| 419 |
{ |
| 420 |
name: "with attributes", |
| 421 |
options: []EventOption{ |
| 422 |
WithAttributes(attribute.Bool("key", true)), |
| 423 |
}, |
| 424 |
}, |
| 425 |
{ |
| 426 |
name: "with attributes set multiple times", |
| 427 |
options: []EventOption{ |
| 428 |
WithAttributes(attribute.Bool("key", true)), |
| 429 |
WithAttributes(attribute.Bool("secondKey", false)), |
| 430 |
}, |
| 431 |
}, |
| 432 |
{ |
| 433 |
name: "with a timestamp", |
| 434 |
options: []EventOption{ |
| 435 |
WithTimestamp(time.Now()), |
| 436 |
}, |
| 437 |
}, |
| 438 |
{ |
| 439 |
name: "with a stacktrace", |
| 440 |
options: []EventOption{ |
| 441 |
WithStackTrace(true), |
| 442 |
}, |
| 443 |
}, |
| 444 |
} { |
| 445 |
b.Run(bb.name, func(b *testing.B) { |
| 446 |
b.ReportAllocs() |
| 447 |
b.ResetTimer() |
| 448 |
|
| 449 |
for i := 0; i < b.N; i++ { |
| 450 |
eventConfig = NewEventConfig(bb.options...) |
| 451 |
} |
| 452 |
}) |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
func TestWithInstrumentationAttributesMerge(t *testing.T) { |
| 457 |
aliceAttr := attribute.String("user", "Alice") |
| 458 |
bobAttr := attribute.String("user", "Bob") |
| 459 |
adminAttr := attribute.Bool("admin", true) |
| 460 |
|
| 461 |
alice := attribute.NewSet(aliceAttr) |
| 462 |
bob := attribute.NewSet(bobAttr) |
| 463 |
aliceAdmin := attribute.NewSet(aliceAttr, adminAttr) |
| 464 |
bobAdmin := attribute.NewSet(bobAttr, adminAttr) |
| 465 |
|
| 466 |
t.Run("SameKey", func(t *testing.T) { |
| 467 |
c := NewTracerConfig( |
| 468 |
WithInstrumentationAttributes(aliceAttr), |
| 469 |
WithInstrumentationAttributes(bobAttr), |
| 470 |
) |
| 471 |
assert.Equal(t, bob, c.InstrumentationAttributes(), |
| 472 |
"Later values for the same key should overwrite earlier ones.") |
| 473 |
}) |
| 474 |
|
| 475 |
t.Run("DifferentKeys", func(t *testing.T) { |
| 476 |
c := NewTracerConfig( |
| 477 |
WithInstrumentationAttributes(aliceAttr), |
| 478 |
WithInstrumentationAttributes(adminAttr), |
| 479 |
) |
| 480 |
assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(), |
| 481 |
"Different keys should be merged") |
| 482 |
}) |
| 483 |
|
| 484 |
t.Run("Mixed", func(t *testing.T) { |
| 485 |
c := NewTracerConfig( |
| 486 |
WithInstrumentationAttributes(aliceAttr, adminAttr), |
| 487 |
WithInstrumentationAttributes(bobAttr), |
| 488 |
) |
| 489 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 490 |
"Combination of same and different keys should be merged.") |
| 491 |
}) |
| 492 |
|
| 493 |
t.Run("MergedEmpty", func(t *testing.T) { |
| 494 |
c := NewTracerConfig( |
| 495 |
WithInstrumentationAttributes(aliceAttr), |
| 496 |
WithInstrumentationAttributes(), |
| 497 |
) |
| 498 |
assert.Equal(t, alice, c.InstrumentationAttributes(), |
| 499 |
"Empty attributes should not affect existing ones.") |
| 500 |
}) |
| 501 |
|
| 502 |
t.Run("SameKeyWithSet", func(t *testing.T) { |
| 503 |
c := NewTracerConfig( |
| 504 |
WithInstrumentationAttributeSet(alice), |
| 505 |
WithInstrumentationAttributeSet(bob), |
| 506 |
) |
| 507 |
assert.Equal(t, bob, c.InstrumentationAttributes(), |
| 508 |
"Later values for the same key should overwrite earlier ones.") |
| 509 |
}) |
| 510 |
|
| 511 |
t.Run("DifferentKeysWithSet", func(t *testing.T) { |
| 512 |
c := NewTracerConfig( |
| 513 |
WithInstrumentationAttributeSet(alice), |
| 514 |
WithInstrumentationAttributeSet(attribute.NewSet(adminAttr)), |
| 515 |
) |
| 516 |
assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(), |
| 517 |
"Different keys should be merged.") |
| 518 |
}) |
| 519 |
|
| 520 |
t.Run("MixedWithSet", func(t *testing.T) { |
| 521 |
c := NewTracerConfig( |
| 522 |
WithInstrumentationAttributeSet(aliceAdmin), |
| 523 |
WithInstrumentationAttributeSet(bob), |
| 524 |
) |
| 525 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 526 |
"Combination of same and different keys should be merged.") |
| 527 |
}) |
| 528 |
|
| 529 |
t.Run("MergedEmptyWithSet", func(t *testing.T) { |
| 530 |
c := NewTracerConfig( |
| 531 |
WithInstrumentationAttributeSet(alice), |
| 532 |
WithInstrumentationAttributeSet(attribute.NewSet()), |
| 533 |
) |
| 534 |
assert.Equal(t, alice, c.InstrumentationAttributes(), |
| 535 |
"Empty attribute set should not affect existing ones.") |
| 536 |
}) |
| 537 |
|
| 538 |
t.Run("MixedAttributesAndSet", func(t *testing.T) { |
| 539 |
c := NewTracerConfig( |
| 540 |
WithInstrumentationAttributes(aliceAttr), |
| 541 |
WithInstrumentationAttributeSet(attribute.NewSet(bobAttr, adminAttr)), |
| 542 |
) |
| 543 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 544 |
"Attributes and attribute sets should be merged together.") |
| 545 |
}) |
| 546 |
} |