| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"math" |
| 11 |
"strconv" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
|
| 19 |
"go.opentelemetry.io/otel/attribute" |
| 20 |
"go.opentelemetry.io/otel/codes" |
| 21 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 22 |
"go.opentelemetry.io/otel/trace/internal/telemetry" |
| 23 |
) |
| 24 |
|
| 25 |
const tName = "tracer.name" |
| 26 |
|
| 27 |
var ( |
| 28 |
attrs = []attribute.KeyValue{ |
| 29 |
attribute.Bool("bool", true), |
| 30 |
attribute.Int("int", -1), |
| 31 |
attribute.Int64("int64", 43), |
| 32 |
attribute.Float64("float64", 0.3), |
| 33 |
attribute.String("string", "value"), |
| 34 |
attribute.BoolSlice("bool slice", []bool{true, false, true}), |
| 35 |
attribute.IntSlice("int slice", []int{-1, -30, 328}), |
| 36 |
attribute.Int64Slice("int64 slice", []int64{1030, 0, 0}), |
| 37 |
attribute.Float64Slice("float64 slice", []float64{1e9}), |
| 38 |
attribute.StringSlice("string slice", []string{"one", "two"}), |
| 39 |
} |
| 40 |
|
| 41 |
tAttrs = []telemetry.Attr{ |
| 42 |
telemetry.Bool("bool", true), |
| 43 |
telemetry.Int("int", -1), |
| 44 |
telemetry.Int64("int64", 43), |
| 45 |
telemetry.Float64("float64", 0.3), |
| 46 |
telemetry.String("string", "value"), |
| 47 |
telemetry.Slice( |
| 48 |
"bool slice", |
| 49 |
telemetry.BoolValue(true), |
| 50 |
telemetry.BoolValue(false), |
| 51 |
telemetry.BoolValue(true), |
| 52 |
), |
| 53 |
telemetry.Slice("int slice", |
| 54 |
telemetry.IntValue(-1), |
| 55 |
telemetry.IntValue(-30), |
| 56 |
telemetry.IntValue(328), |
| 57 |
), |
| 58 |
telemetry.Slice("int64 slice", |
| 59 |
telemetry.Int64Value(1030), |
| 60 |
telemetry.Int64Value(0), |
| 61 |
telemetry.Int64Value(0), |
| 62 |
), |
| 63 |
telemetry.Slice("float64 slice", telemetry.Float64Value(1e9)), |
| 64 |
telemetry.Slice("string slice", |
| 65 |
telemetry.StringValue("one"), |
| 66 |
telemetry.StringValue("two"), |
| 67 |
), |
| 68 |
} |
| 69 |
|
| 70 |
spanContext0 = NewSpanContext(SpanContextConfig{ |
| 71 |
TraceID: TraceID{0x1}, |
| 72 |
SpanID: SpanID{0x1}, |
| 73 |
TraceFlags: FlagsSampled, |
| 74 |
}) |
| 75 |
spanContext1 = NewSpanContext(SpanContextConfig{ |
| 76 |
TraceID: TraceID{0x2}, |
| 77 |
SpanID: SpanID{0x2}, |
| 78 |
TraceFlags: FlagsSampled, |
| 79 |
}) |
| 80 |
|
| 81 |
link0 = Link{ |
| 82 |
SpanContext: spanContext0, |
| 83 |
Attributes: []attribute.KeyValue{ |
| 84 |
attribute.Int("n", 0), |
| 85 |
}, |
| 86 |
} |
| 87 |
link1 = Link{ |
| 88 |
SpanContext: spanContext1, |
| 89 |
Attributes: []attribute.KeyValue{ |
| 90 |
attribute.Int("n", 1), |
| 91 |
}, |
| 92 |
} |
| 93 |
|
| 94 |
tLink0 = &telemetry.SpanLink{ |
| 95 |
TraceID: telemetry.TraceID(spanContext0.TraceID()), |
| 96 |
SpanID: telemetry.SpanID(spanContext0.SpanID()), |
| 97 |
Flags: uint32(spanContext0.TraceFlags()), |
| 98 |
Attrs: []telemetry.Attr{telemetry.Int("n", 0)}, |
| 99 |
} |
| 100 |
tLink1 = &telemetry.SpanLink{ |
| 101 |
TraceID: telemetry.TraceID(spanContext1.TraceID()), |
| 102 |
SpanID: telemetry.SpanID(spanContext1.SpanID()), |
| 103 |
Flags: uint32(spanContext1.TraceFlags()), |
| 104 |
Attrs: []telemetry.Attr{telemetry.Int("n", 1)}, |
| 105 |
} |
| 106 |
) |
| 107 |
|
| 108 |
func TestTracerProviderInstance(t *testing.T) { |
| 109 |
t.Parallel() |
| 110 |
|
| 111 |
tp0, tp1 := newAutoTracerProvider(), newAutoTracerProvider() |
| 112 |
|
| 113 |
assert.Same(t, tracerProviderInstance, tp0) |
| 114 |
assert.Same(t, tracerProviderInstance, tp1) |
| 115 |
} |
| 116 |
|
| 117 |
func TestTracerProviderConcurrentSafe(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
const goroutines = 10 |
| 121 |
|
| 122 |
run := func(tp TracerProvider) <-chan struct{} { |
| 123 |
done := make(chan struct{}) |
| 124 |
go func(tp TracerProvider) { |
| 125 |
defer close(done) |
| 126 |
|
| 127 |
var wg sync.WaitGroup |
| 128 |
for i := range goroutines { |
| 129 |
wg.Add(1) |
| 130 |
go func(name, version string) { |
| 131 |
defer wg.Done() |
| 132 |
_ = tp.Tracer(name, WithInstrumentationVersion(version)) |
| 133 |
}("tracer"+strconv.Itoa(i%4), strconv.Itoa(i%2)) |
| 134 |
} |
| 135 |
|
| 136 |
wg.Wait() |
| 137 |
}(tp) |
| 138 |
return done |
| 139 |
} |
| 140 |
|
| 141 |
assert.NotPanics(t, func() { |
| 142 |
done0, done1 := run(newAutoTracerProvider()), run(newAutoTracerProvider()) |
| 143 |
|
| 144 |
<-done0 |
| 145 |
<-done1 |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
func TestSpanKindTransform(t *testing.T) { |
| 150 |
tests := map[SpanKind]telemetry.SpanKind{ |
| 151 |
SpanKind(-1): telemetry.SpanKind(0), |
| 152 |
SpanKindUnspecified: telemetry.SpanKind(0), |
| 153 |
SpanKind(math.MaxInt): telemetry.SpanKind(0), |
| 154 |
|
| 155 |
SpanKindInternal: telemetry.SpanKindInternal, |
| 156 |
SpanKindServer: telemetry.SpanKindServer, |
| 157 |
SpanKindClient: telemetry.SpanKindClient, |
| 158 |
SpanKindProducer: telemetry.SpanKindProducer, |
| 159 |
SpanKindConsumer: telemetry.SpanKindConsumer, |
| 160 |
} |
| 161 |
|
| 162 |
for in, want := range tests { |
| 163 |
assert.Equal(t, want, spanKind(in), in.String()) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestTracerStartPropagatesOrigCtx(t *testing.T) { |
| 168 |
t.Parallel() |
| 169 |
|
| 170 |
type ctxKey struct{} |
| 171 |
var key ctxKey |
| 172 |
val := "value" |
| 173 |
|
| 174 |
ctx := context.WithValue(t.Context(), key, val) |
| 175 |
ctx, _ = newAutoTracerProvider().Tracer(tName).Start(ctx, "span.name") |
| 176 |
|
| 177 |
assert.Equal(t, val, ctx.Value(key)) |
| 178 |
} |
| 179 |
|
| 180 |
func TestTracerStartReturnsNonNilSpan(t *testing.T) { |
| 181 |
t.Parallel() |
| 182 |
|
| 183 |
tr := newAutoTracerProvider().Tracer(tName) |
| 184 |
_, s := tr.Start(t.Context(), "span.name") |
| 185 |
assert.NotNil(t, s) |
| 186 |
} |
| 187 |
|
| 188 |
func TestTracerStartAddsSpanToCtx(t *testing.T) { |
| 189 |
t.Parallel() |
| 190 |
|
| 191 |
tr := newAutoTracerProvider().Tracer(tName) |
| 192 |
ctx, s := tr.Start(t.Context(), "span.name") |
| 193 |
|
| 194 |
assert.Same(t, s, SpanFromContext(ctx)) |
| 195 |
} |
| 196 |
|
| 197 |
func TestTracerConcurrentSafe(t *testing.T) { |
| 198 |
t.Parallel() |
| 199 |
|
| 200 |
const goroutines = 10 |
| 201 |
|
| 202 |
ctx := t.Context() |
| 203 |
run := func(tracer Tracer) <-chan struct{} { |
| 204 |
done := make(chan struct{}) |
| 205 |
|
| 206 |
go func(tr Tracer) { |
| 207 |
defer close(done) |
| 208 |
|
| 209 |
var wg sync.WaitGroup |
| 210 |
for i := range goroutines { |
| 211 |
wg.Add(1) |
| 212 |
go func(name string) { |
| 213 |
defer wg.Done() |
| 214 |
_, _ = tr.Start(ctx, name) |
| 215 |
}("span" + strconv.Itoa(i)) |
| 216 |
} |
| 217 |
|
| 218 |
wg.Wait() |
| 219 |
}(tracer) |
| 220 |
|
| 221 |
return done |
| 222 |
} |
| 223 |
|
| 224 |
assert.NotPanics(t, func() { |
| 225 |
tp := newAutoTracerProvider() |
| 226 |
done0, done1 := run(tp.Tracer("t0")), run(tp.Tracer("t1")) |
| 227 |
|
| 228 |
<-done0 |
| 229 |
<-done1 |
| 230 |
}) |
| 231 |
} |
| 232 |
|
| 233 |
func TestSpanCreation(t *testing.T) { |
| 234 |
const ( |
| 235 |
spanName = "span name" |
| 236 |
tracerName = "go.opentelemetry.io/otel/sdk/test" |
| 237 |
tracerVer = "v0.1.0" |
| 238 |
) |
| 239 |
|
| 240 |
ts := time.Now() |
| 241 |
|
| 242 |
tracer := newAutoTracerProvider().Tracer( |
| 243 |
tracerName, |
| 244 |
WithInstrumentationVersion(tracerVer), |
| 245 |
WithSchemaURL(semconv.SchemaURL), |
| 246 |
) |
| 247 |
|
| 248 |
assertTracer := func(traces *telemetry.Traces) func(*testing.T) { |
| 249 |
return func(t *testing.T) { |
| 250 |
t.Helper() |
| 251 |
|
| 252 |
rs := traces.ResourceSpans |
| 253 |
require.Len(t, rs, 1) |
| 254 |
sss := rs[0].ScopeSpans |
| 255 |
require.Len(t, sss, 1) |
| 256 |
ss := sss[0] |
| 257 |
assert.Equal(t, tracerName, ss.Scope.Name, "tracer name") |
| 258 |
assert.Equal(t, tracerVer, ss.Scope.Version, "tracer version") |
| 259 |
assert.Equal(t, semconv.SchemaURL, ss.SchemaURL, "tracer schema URL") |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
testcases := []struct { |
| 264 |
TestName string |
| 265 |
SpanName string |
| 266 |
Options []SpanStartOption |
| 267 |
Setup func(*testing.T) |
| 268 |
Eval func(*testing.T, context.Context, *autoSpan) |
| 269 |
}{ |
| 270 |
{ |
| 271 |
TestName: "SampledByDefault", |
| 272 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 273 |
t.Run("Tracer", assertTracer(s.traces)) |
| 274 |
|
| 275 |
assert.True(t, s.sampled.Load(), "not sampled by default.") |
| 276 |
}, |
| 277 |
}, |
| 278 |
{ |
| 279 |
TestName: "ParentSpanContext", |
| 280 |
Setup: func(t *testing.T) { |
| 281 |
orig := start |
| 282 |
t.Cleanup(func() { start = orig }) |
| 283 |
start = func(_ context.Context, _ *autoSpan, psc *SpanContext, _ *bool, _ *SpanContext) { |
| 284 |
*psc = spanContext0 |
| 285 |
} |
| 286 |
}, |
| 287 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 288 |
t.Run("Tracer", assertTracer(s.traces)) |
| 289 |
|
| 290 |
want := spanContext0.SpanID().String() |
| 291 |
got := s.span.ParentSpanID.String() |
| 292 |
assert.Equal(t, want, got) |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
TestName: "SpanContext", |
| 297 |
Setup: func(t *testing.T) { |
| 298 |
orig := start |
| 299 |
t.Cleanup(func() { start = orig }) |
| 300 |
start = func(_ context.Context, _ *autoSpan, _ *SpanContext, _ *bool, sc *SpanContext) { |
| 301 |
*sc = spanContext0 |
| 302 |
} |
| 303 |
}, |
| 304 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 305 |
t.Run("Tracer", assertTracer(s.traces)) |
| 306 |
|
| 307 |
str := func(i interface{ String() string }) string { |
| 308 |
return i.String() |
| 309 |
} |
| 310 |
assert.Equal(t, str(spanContext0.TraceID()), s.span.TraceID.String(), "trace ID") |
| 311 |
assert.Equal(t, str(spanContext0.SpanID()), s.span.SpanID.String(), "autoSpan ID") |
| 312 |
assert.Equal(t, uint32(spanContext0.TraceFlags()), s.span.Flags, "flags") |
| 313 |
assert.Equal(t, str(spanContext0.TraceState()), s.span.TraceState, "tracestate") |
| 314 |
}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
TestName: "NotSampled", |
| 318 |
Setup: func(t *testing.T) { |
| 319 |
orig := start |
| 320 |
t.Cleanup(func() { start = orig }) |
| 321 |
start = func(_ context.Context, _ *autoSpan, _ *SpanContext, s *bool, _ *SpanContext) { |
| 322 |
*s = false |
| 323 |
} |
| 324 |
}, |
| 325 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 326 |
assert.False(t, s.sampled.Load(), "sampled") |
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
TestName: "WithName", |
| 331 |
SpanName: spanName, |
| 332 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 333 |
t.Run("Tracer", assertTracer(s.traces)) |
| 334 |
assert.Equal(t, spanName, s.span.Name) |
| 335 |
}, |
| 336 |
}, |
| 337 |
{ |
| 338 |
TestName: "WithSpanKind", |
| 339 |
Options: []SpanStartOption{ |
| 340 |
WithSpanKind(SpanKindClient), |
| 341 |
}, |
| 342 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 343 |
t.Run("Tracer", assertTracer(s.traces)) |
| 344 |
assert.Equal(t, telemetry.SpanKindClient, s.span.Kind) |
| 345 |
}, |
| 346 |
}, |
| 347 |
{ |
| 348 |
TestName: "WithTimestamp", |
| 349 |
Options: []SpanStartOption{ |
| 350 |
WithTimestamp(ts), |
| 351 |
}, |
| 352 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 353 |
t.Run("Tracer", assertTracer(s.traces)) |
| 354 |
assert.Equal(t, ts, s.span.StartTime) |
| 355 |
}, |
| 356 |
}, |
| 357 |
{ |
| 358 |
TestName: "WithAttributes", |
| 359 |
Options: []SpanStartOption{ |
| 360 |
WithAttributes(attrs...), |
| 361 |
}, |
| 362 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 363 |
t.Run("Tracer", assertTracer(s.traces)) |
| 364 |
assert.Equal(t, tAttrs, s.span.Attrs) |
| 365 |
}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
TestName: "WithLinks", |
| 369 |
Options: []SpanStartOption{ |
| 370 |
WithLinks(link0, link1), |
| 371 |
}, |
| 372 |
Eval: func(t *testing.T, _ context.Context, s *autoSpan) { |
| 373 |
t.Run("Tracer", assertTracer(s.traces)) |
| 374 |
want := []*telemetry.SpanLink{tLink0, tLink1} |
| 375 |
assert.Equal(t, want, s.span.Links) |
| 376 |
}, |
| 377 |
}, |
| 378 |
} |
| 379 |
|
| 380 |
ctx := t.Context() |
| 381 |
for _, tc := range testcases { |
| 382 |
t.Run(tc.TestName, func(t *testing.T) { |
| 383 |
if tc.Setup != nil { |
| 384 |
tc.Setup(t) |
| 385 |
} |
| 386 |
|
| 387 |
c, sIface := tracer.Start(ctx, tc.SpanName, tc.Options...) |
| 388 |
require.IsType(t, &autoSpan{}, sIface) |
| 389 |
s := sIface.(*autoSpan) |
| 390 |
|
| 391 |
tc.Eval(t, c, s) |
| 392 |
}) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestSpanEnd(t *testing.T) { |
| 397 |
orig := ended |
| 398 |
t.Cleanup(func() { ended = orig }) |
| 399 |
|
| 400 |
var buf []byte |
| 401 |
ended = func(b []byte) { buf = b } |
| 402 |
|
| 403 |
timeNow := time.Unix(0, time.Now().UnixNano()) // No location. |
| 404 |
|
| 405 |
tests := []struct { |
| 406 |
Name string |
| 407 |
Options []SpanEndOption |
| 408 |
Eval func(*testing.T, time.Time) |
| 409 |
}{ |
| 410 |
{ |
| 411 |
Name: "Now", |
| 412 |
Eval: func(t *testing.T, ts time.Time) { |
| 413 |
assert.False(t, ts.IsZero(), "zero end time") |
| 414 |
}, |
| 415 |
}, |
| 416 |
{ |
| 417 |
Name: "WithTimestamp", |
| 418 |
Options: []SpanEndOption{ |
| 419 |
WithTimestamp(timeNow), |
| 420 |
}, |
| 421 |
Eval: func(t *testing.T, ts time.Time) { |
| 422 |
assert.Equal(t, timeNow, ts, "end time not set") |
| 423 |
}, |
| 424 |
}, |
| 425 |
} |
| 426 |
|
| 427 |
for _, test := range tests { |
| 428 |
t.Run(test.Name, func(t *testing.T) { |
| 429 |
s := spanBuilder{}.Build() |
| 430 |
s.End(test.Options...) |
| 431 |
|
| 432 |
assert.False(t, s.sampled.Load(), "ended autoSpan should not be sampled") |
| 433 |
require.NotNil(t, buf, "no span data emitted") |
| 434 |
|
| 435 |
var traces telemetry.Traces |
| 436 |
err := json.Unmarshal(buf, &traces) |
| 437 |
require.NoError(t, err) |
| 438 |
|
| 439 |
rs := traces.ResourceSpans |
| 440 |
require.Len(t, rs, 1) |
| 441 |
ss := rs[0].ScopeSpans |
| 442 |
require.Len(t, ss, 1) |
| 443 |
spans := ss[0].Spans |
| 444 |
require.Len(t, spans, 1) |
| 445 |
|
| 446 |
test.Eval(t, spans[0].EndTime) |
| 447 |
}) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
func TestSpanNilUnsampledGuards(t *testing.T) { |
| 452 |
run := func(fn func(s *autoSpan)) func(*testing.T) { |
| 453 |
return func(t *testing.T) { |
| 454 |
t.Helper() |
| 455 |
|
| 456 |
f := func(s *autoSpan) func() { return func() { fn(s) } } |
| 457 |
assert.NotPanics(t, f(nil), "nil span") |
| 458 |
assert.NotPanics(t, f(new(autoSpan)), "unsampled span") |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
t.Run("End", run(func(s *autoSpan) { s.End() })) |
| 463 |
t.Run("AddEvent", run(func(s *autoSpan) { s.AddEvent("event name") })) |
| 464 |
t.Run("AddLink", run(func(s *autoSpan) { s.AddLink(Link{}) })) |
| 465 |
t.Run("IsRecording", run(func(s *autoSpan) { _ = s.IsRecording() })) |
| 466 |
t.Run("RecordError", run(func(s *autoSpan) { s.RecordError(nil) })) |
| 467 |
t.Run("SpanContext", run(func(s *autoSpan) { _ = s.SpanContext() })) |
| 468 |
t.Run("SetStatus", run(func(s *autoSpan) { s.SetStatus(codes.Error, "test") })) |
| 469 |
t.Run("SetName", run(func(s *autoSpan) { s.SetName("span name") })) |
| 470 |
t.Run("SetAttributes", run(func(s *autoSpan) { s.SetAttributes(attrs...) })) |
| 471 |
t.Run("TracerProvider", run(func(s *autoSpan) { _ = s.TracerProvider() })) |
| 472 |
} |
| 473 |
|
| 474 |
func TestSpanAddLink(t *testing.T) { |
| 475 |
s := spanBuilder{ |
| 476 |
Options: []SpanStartOption{WithLinks(link0)}, |
| 477 |
}.Build() |
| 478 |
s.AddLink(link1) |
| 479 |
|
| 480 |
want := []*telemetry.SpanLink{tLink0, tLink1} |
| 481 |
assert.Equal(t, want, s.span.Links) |
| 482 |
} |
| 483 |
|
| 484 |
func TestSpanAddLinkLimit(t *testing.T) { |
| 485 |
tests := []struct { |
| 486 |
limit int |
| 487 |
want []*telemetry.SpanLink |
| 488 |
dropped uint32 |
| 489 |
}{ |
| 490 |
{0, nil, 2}, |
| 491 |
{1, []*telemetry.SpanLink{tLink1}, 1}, |
| 492 |
{2, []*telemetry.SpanLink{tLink0, tLink1}, 0}, |
| 493 |
{-1, []*telemetry.SpanLink{tLink0, tLink1}, 0}, |
| 494 |
} |
| 495 |
|
| 496 |
for _, test := range tests { |
| 497 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 498 |
orig := maxSpan.Links |
| 499 |
maxSpan.Links = test.limit |
| 500 |
t.Cleanup(func() { maxSpan.Links = orig }) |
| 501 |
|
| 502 |
builder := spanBuilder{} |
| 503 |
s := builder.Build() |
| 504 |
s.AddLink(link0) |
| 505 |
s.AddLink(link1) |
| 506 |
assert.Equal(t, test.want, s.span.Links, "AddLink") |
| 507 |
assert.Equal(t, test.dropped, s.span.DroppedLinks, "AddLink DroppedLinks") |
| 508 |
|
| 509 |
builder.Options = []SpanStartOption{ |
| 510 |
WithLinks(link0, link1), |
| 511 |
} |
| 512 |
s = builder.Build() |
| 513 |
assert.Equal(t, test.want, s.span.Links, "NewSpan") |
| 514 |
assert.Equal(t, test.dropped, s.span.DroppedLinks, "NewSpan DroppedLinks") |
| 515 |
}) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
func TestSpanLinkAttrLimit(t *testing.T) { |
| 520 |
tests := []struct { |
| 521 |
limit int |
| 522 |
want []telemetry.Attr |
| 523 |
dropped uint32 |
| 524 |
}{ |
| 525 |
{0, nil, uint32(len(tAttrs))}, |
| 526 |
{2, tAttrs[:2], uint32(len(tAttrs) - 2)}, |
| 527 |
{len(tAttrs), tAttrs, 0}, |
| 528 |
{-1, tAttrs, 0}, |
| 529 |
} |
| 530 |
|
| 531 |
link := Link{Attributes: attrs} |
| 532 |
for _, test := range tests { |
| 533 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 534 |
orig := maxSpan.LinkAttrs |
| 535 |
maxSpan.LinkAttrs = test.limit |
| 536 |
t.Cleanup(func() { maxSpan.LinkAttrs = orig }) |
| 537 |
|
| 538 |
builder := spanBuilder{} |
| 539 |
|
| 540 |
s := builder.Build() |
| 541 |
s.AddLink(link) |
| 542 |
|
| 543 |
require.Len(t, s.span.Links, 1) |
| 544 |
got := s.span.Links[0] |
| 545 |
assert.Equal(t, test.want, got.Attrs, "AddLink attrs") |
| 546 |
assert.Equal(t, test.dropped, got.DroppedAttrs, "dropped AddLink attrs") |
| 547 |
|
| 548 |
builder.Options = []SpanStartOption{WithLinks(link)} |
| 549 |
s = builder.Build() |
| 550 |
|
| 551 |
require.Len(t, s.span.Links, 1) |
| 552 |
got = s.span.Links[0] |
| 553 |
assert.Equal(t, test.want, got.Attrs, "NewSpan link attrs") |
| 554 |
assert.Equal(t, test.dropped, got.DroppedAttrs, "dropped NewSpan link attrs") |
| 555 |
}) |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
func TestSpanIsRecording(t *testing.T) { |
| 560 |
builder := spanBuilder{} |
| 561 |
s := builder.Build() |
| 562 |
assert.True(t, s.IsRecording(), "sampled span should be recorded") |
| 563 |
|
| 564 |
builder.NotSampled = true |
| 565 |
s = builder.Build() |
| 566 |
assert.False(t, s.IsRecording(), "unsampled span should not be recorded") |
| 567 |
} |
| 568 |
|
| 569 |
func TestSpanRecordError(t *testing.T) { |
| 570 |
s := spanBuilder{}.Build() |
| 571 |
|
| 572 |
var want []*telemetry.SpanEvent |
| 573 |
s.RecordError(nil) |
| 574 |
require.Equal(t, want, s.span.Events, "nil error recorded") |
| 575 |
|
| 576 |
ts := time.Now() |
| 577 |
err := errors.New("test") |
| 578 |
s.RecordError( |
| 579 |
err, |
| 580 |
WithTimestamp(ts), |
| 581 |
WithAttributes(attribute.Bool("testing", true)), |
| 582 |
) |
| 583 |
want = append(want, &telemetry.SpanEvent{ |
| 584 |
Name: semconv.ExceptionEventName, |
| 585 |
Time: ts, |
| 586 |
Attrs: []telemetry.Attr{ |
| 587 |
telemetry.Bool("testing", true), |
| 588 |
telemetry.String(string(semconv.ExceptionTypeKey), "*errors.errorString"), |
| 589 |
telemetry.String(string(semconv.ExceptionMessageKey), err.Error()), |
| 590 |
}, |
| 591 |
}) |
| 592 |
assert.Equal(t, want, s.span.Events, "nil error recorded") |
| 593 |
|
| 594 |
s.RecordError(err, WithStackTrace(true)) |
| 595 |
require.Len(t, s.span.Events, 2, "missing event") |
| 596 |
|
| 597 |
var hasST bool |
| 598 |
for _, attr := range s.span.Events[1].Attrs { |
| 599 |
if attr.Key == string(semconv.ExceptionStacktraceKey) { |
| 600 |
hasST = true |
| 601 |
break |
| 602 |
} |
| 603 |
} |
| 604 |
assert.True(t, hasST, "missing stacktrace attribute") |
| 605 |
} |
| 606 |
|
| 607 |
func TestAddEventLimit(t *testing.T) { |
| 608 |
const a, b, c = "a", "b", "c" |
| 609 |
|
| 610 |
ts := time.Now() |
| 611 |
|
| 612 |
evtA := &telemetry.SpanEvent{Name: "a", Time: ts} |
| 613 |
evtB := &telemetry.SpanEvent{Name: "b", Time: ts} |
| 614 |
evtC := &telemetry.SpanEvent{Name: "c", Time: ts} |
| 615 |
|
| 616 |
tests := []struct { |
| 617 |
limit int |
| 618 |
want []*telemetry.SpanEvent |
| 619 |
dropped uint32 |
| 620 |
}{ |
| 621 |
{0, nil, 3}, |
| 622 |
{1, []*telemetry.SpanEvent{evtC}, 2}, |
| 623 |
{2, []*telemetry.SpanEvent{evtB, evtC}, 1}, |
| 624 |
{3, []*telemetry.SpanEvent{evtA, evtB, evtC}, 0}, |
| 625 |
{-1, []*telemetry.SpanEvent{evtA, evtB, evtC}, 0}, |
| 626 |
} |
| 627 |
|
| 628 |
for _, test := range tests { |
| 629 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 630 |
orig := maxSpan.Events |
| 631 |
maxSpan.Events = test.limit |
| 632 |
t.Cleanup(func() { maxSpan.Events = orig }) |
| 633 |
|
| 634 |
builder := spanBuilder{} |
| 635 |
|
| 636 |
s := builder.Build() |
| 637 |
s.addEvent(a, ts, nil) |
| 638 |
s.addEvent(b, ts, nil) |
| 639 |
s.addEvent(c, ts, nil) |
| 640 |
|
| 641 |
assert.Equal(t, test.want, s.span.Events, "add event") |
| 642 |
assert.Equal(t, test.dropped, s.span.DroppedEvents, "dropped events") |
| 643 |
}) |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
func TestAddEventAttrLimit(t *testing.T) { |
| 648 |
tests := []struct { |
| 649 |
limit int |
| 650 |
want []telemetry.Attr |
| 651 |
dropped uint32 |
| 652 |
}{ |
| 653 |
{0, nil, uint32(len(tAttrs))}, |
| 654 |
{2, tAttrs[:2], uint32(len(tAttrs) - 2)}, |
| 655 |
{len(tAttrs), tAttrs, 0}, |
| 656 |
{-1, tAttrs, 0}, |
| 657 |
} |
| 658 |
|
| 659 |
for _, test := range tests { |
| 660 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 661 |
orig := maxSpan.EventAttrs |
| 662 |
maxSpan.EventAttrs = test.limit |
| 663 |
t.Cleanup(func() { maxSpan.EventAttrs = orig }) |
| 664 |
|
| 665 |
builder := spanBuilder{} |
| 666 |
|
| 667 |
s := builder.Build() |
| 668 |
s.addEvent("name", time.Now(), attrs) |
| 669 |
|
| 670 |
require.Len(t, s.span.Events, 1) |
| 671 |
got := s.span.Events[0] |
| 672 |
assert.Equal(t, test.want, got.Attrs, "event attrs") |
| 673 |
assert.Equal(t, test.dropped, got.DroppedAttrs, "dropped event attrs") |
| 674 |
}) |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
func TestSpanSpanContext(t *testing.T) { |
| 679 |
s := spanBuilder{SpanContext: spanContext0}.Build() |
| 680 |
assert.Equal(t, spanContext0, s.SpanContext()) |
| 681 |
} |
| 682 |
|
| 683 |
func TestSpanSetStatus(t *testing.T) { |
| 684 |
s := spanBuilder{}.Build() |
| 685 |
|
| 686 |
assert.Nil(t, s.span.Status, "empty status should not be set") |
| 687 |
|
| 688 |
const msg = "test" |
| 689 |
want := &telemetry.Status{Message: msg} |
| 690 |
|
| 691 |
for c, tCode := range map[codes.Code]telemetry.StatusCode{ |
| 692 |
codes.Error: telemetry.StatusCodeError, |
| 693 |
codes.Ok: telemetry.StatusCodeOK, |
| 694 |
codes.Unset: telemetry.StatusCodeUnset, |
| 695 |
} { |
| 696 |
want.Code = tCode |
| 697 |
s.SetStatus(c, msg) |
| 698 |
assert.Equalf(t, want, s.span.Status, "code: %s, msg: %s", c, msg) |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
func TestSpanSetName(t *testing.T) { |
| 703 |
const name = "span name" |
| 704 |
builder := spanBuilder{} |
| 705 |
|
| 706 |
s := builder.Build() |
| 707 |
s.SetName(name) |
| 708 |
assert.Equal(t, name, s.span.Name, "span name not set") |
| 709 |
|
| 710 |
builder.Name = "alt" |
| 711 |
s = builder.Build() |
| 712 |
s.SetName(name) |
| 713 |
assert.Equal(t, name, s.span.Name, "SetName did not overwrite") |
| 714 |
} |
| 715 |
|
| 716 |
func TestSpanSetAttributes(t *testing.T) { |
| 717 |
builder := spanBuilder{} |
| 718 |
|
| 719 |
s := builder.Build() |
| 720 |
s.SetAttributes(attrs...) |
| 721 |
assert.Equal(t, tAttrs, s.span.Attrs, "span attributes not set") |
| 722 |
|
| 723 |
builder.Options = []SpanStartOption{ |
| 724 |
WithAttributes(attrs[0].Key.Bool(!attrs[0].Value.AsBool())), |
| 725 |
} |
| 726 |
|
| 727 |
s = builder.Build() |
| 728 |
s.SetAttributes(attrs...) |
| 729 |
assert.Equal(t, tAttrs, s.span.Attrs, "SpanAttributes did not override") |
| 730 |
} |
| 731 |
|
| 732 |
func TestSpanAttributeLimits(t *testing.T) { |
| 733 |
tests := []struct { |
| 734 |
limit int |
| 735 |
want []telemetry.Attr |
| 736 |
dropped uint32 |
| 737 |
}{ |
| 738 |
{0, nil, uint32(len(tAttrs))}, |
| 739 |
{2, tAttrs[:2], uint32(len(tAttrs) - 2)}, |
| 740 |
{len(tAttrs), tAttrs, 0}, |
| 741 |
{-1, tAttrs, 0}, |
| 742 |
} |
| 743 |
|
| 744 |
for _, test := range tests { |
| 745 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 746 |
orig := maxSpan.Attrs |
| 747 |
maxSpan.Attrs = test.limit |
| 748 |
t.Cleanup(func() { maxSpan.Attrs = orig }) |
| 749 |
|
| 750 |
builder := spanBuilder{} |
| 751 |
|
| 752 |
s := builder.Build() |
| 753 |
s.SetAttributes(attrs...) |
| 754 |
assert.Equal(t, test.want, s.span.Attrs, "set span attributes") |
| 755 |
assert.Equal(t, test.dropped, s.span.DroppedAttrs, "dropped attrs") |
| 756 |
|
| 757 |
s.SetAttributes(attrs...) |
| 758 |
assert.Equal(t, test.want, s.span.Attrs, "set span attributes twice") |
| 759 |
assert.Equal(t, 2*test.dropped, s.span.DroppedAttrs, "2x dropped attrs") |
| 760 |
|
| 761 |
builder.Options = []SpanStartOption{WithAttributes(attrs...)} |
| 762 |
|
| 763 |
s = builder.Build() |
| 764 |
assert.Equal(t, test.want, s.span.Attrs, "new span attributes") |
| 765 |
assert.Equal(t, test.dropped, s.span.DroppedAttrs, "dropped attrs") |
| 766 |
}) |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
func TestSpanAttributeValueLimits(t *testing.T) { |
| 771 |
value := "hello world" |
| 772 |
|
| 773 |
aStr := attribute.String("string", value) |
| 774 |
aStrSlice := attribute.StringSlice("slice", []string{value, value}) |
| 775 |
|
| 776 |
eq := func(a, b []telemetry.Attr) bool { |
| 777 |
if len(a) != len(b) { |
| 778 |
return false |
| 779 |
} |
| 780 |
for i := range a { |
| 781 |
if !a[i].Equal(b[i]) { |
| 782 |
return false |
| 783 |
} |
| 784 |
} |
| 785 |
return true |
| 786 |
} |
| 787 |
|
| 788 |
tests := []struct { |
| 789 |
limit int |
| 790 |
want string |
| 791 |
}{ |
| 792 |
{0, ""}, |
| 793 |
{2, value[:2]}, |
| 794 |
{11, value}, |
| 795 |
{-1, value}, |
| 796 |
} |
| 797 |
for _, test := range tests { |
| 798 |
t.Run("Limit/"+strconv.Itoa(test.limit), func(t *testing.T) { |
| 799 |
orig := maxSpan.AttrValueLen |
| 800 |
maxSpan.AttrValueLen = test.limit |
| 801 |
t.Cleanup(func() { maxSpan.AttrValueLen = orig }) |
| 802 |
|
| 803 |
builder := spanBuilder{} |
| 804 |
|
| 805 |
want := []telemetry.Attr{ |
| 806 |
telemetry.String("string", test.want), |
| 807 |
telemetry.Slice( |
| 808 |
"slice", |
| 809 |
telemetry.StringValue(test.want), |
| 810 |
telemetry.StringValue(test.want), |
| 811 |
), |
| 812 |
} |
| 813 |
|
| 814 |
s := builder.Build() |
| 815 |
s.SetAttributes(aStr, aStrSlice) |
| 816 |
assert.Truef(t, eq(want, s.span.Attrs), "set span attributes: got %#v, want %#v", s.span.Attrs, want) |
| 817 |
|
| 818 |
s.AddEvent("test", WithAttributes(aStr, aStrSlice)) |
| 819 |
assert.Truef( |
| 820 |
t, |
| 821 |
eq(want, s.span.Events[0].Attrs), |
| 822 |
"span event attributes: got %#v, want %#v", |
| 823 |
s.span.Events[0].Attrs, |
| 824 |
want, |
| 825 |
) |
| 826 |
|
| 827 |
s.AddLink(Link{ |
| 828 |
Attributes: []attribute.KeyValue{aStr, aStrSlice}, |
| 829 |
}) |
| 830 |
assert.Truef( |
| 831 |
t, |
| 832 |
eq(want, s.span.Links[0].Attrs), |
| 833 |
"span link attributes: got %#v, want %#v", |
| 834 |
s.span.Links[0].Attrs, |
| 835 |
want, |
| 836 |
) |
| 837 |
|
| 838 |
builder.Options = []SpanStartOption{ |
| 839 |
WithAttributes(aStr, aStrSlice), |
| 840 |
WithLinks(Link{ |
| 841 |
Attributes: []attribute.KeyValue{aStr, aStrSlice}, |
| 842 |
}), |
| 843 |
} |
| 844 |
s = builder.Build() |
| 845 |
assert.Truef(t, eq(want, s.span.Attrs), "new span attributes: got %#v, want %#v", s.span.Attrs, want) |
| 846 |
assert.Truef( |
| 847 |
t, |
| 848 |
eq(want, s.span.Links[0].Attrs), |
| 849 |
"new span link attributes: got %#v, want %#v", |
| 850 |
s.span.Attrs, |
| 851 |
want, |
| 852 |
) |
| 853 |
}) |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
func TestSpanTracerProvider(t *testing.T) { |
| 858 |
var s autoSpan |
| 859 |
|
| 860 |
got := s.TracerProvider() |
| 861 |
assert.IsType(t, &autoTracerProvider{}, got) |
| 862 |
} |
| 863 |
|
| 864 |
type spanBuilder struct { |
| 865 |
Name string |
| 866 |
NotSampled bool |
| 867 |
SpanContext SpanContext |
| 868 |
Options []SpanStartOption |
| 869 |
} |
| 870 |
|
| 871 |
func (b spanBuilder) Build() *autoSpan { |
| 872 |
tracer := new(autoTracer) |
| 873 |
s := &autoSpan{spanContext: b.SpanContext} |
| 874 |
s.sampled.Store(!b.NotSampled) |
| 875 |
s.traces, s.span = tracer.traces( |
| 876 |
b.Name, |
| 877 |
NewSpanStartConfig(b.Options...), |
| 878 |
s.spanContext, |
| 879 |
SpanContext{}, |
| 880 |
) |
| 881 |
|
| 882 |
return s |
| 883 |
} |
| 884 |
|
| 885 |
func TestTruncate(t *testing.T) { |
| 886 |
type group struct { |
| 887 |
limit int |
| 888 |
input string |
| 889 |
expected string |
| 890 |
} |
| 891 |
|
| 892 |
tests := []struct { |
| 893 |
name string |
| 894 |
groups []group |
| 895 |
}{ |
| 896 |
// Edge case: limit is negative, no truncation should occur |
| 897 |
{ |
| 898 |
name: "NoTruncation", |
| 899 |
groups: []group{ |
| 900 |
{-1, "No truncation!", "No truncation!"}, |
| 901 |
}, |
| 902 |
}, |
| 903 |
|
| 904 |
// Edge case: string is already shorter than the limit, no truncation |
| 905 |
// should occur |
| 906 |
{ |
| 907 |
name: "ShortText", |
| 908 |
groups: []group{ |
| 909 |
{10, "Short text", "Short text"}, |
| 910 |
{15, "Short text", "Short text"}, |
| 911 |
{100, "Short text", "Short text"}, |
| 912 |
}, |
| 913 |
}, |
| 914 |
|
| 915 |
// Edge case: truncation happens with ASCII characters only |
| 916 |
{ |
| 917 |
name: "ASCIIOnly", |
| 918 |
groups: []group{ |
| 919 |
{1, "Hello World!", "H"}, |
| 920 |
{5, "Hello World!", "Hello"}, |
| 921 |
{12, "Hello World!", "Hello World!"}, |
| 922 |
}, |
| 923 |
}, |
| 924 |
|
| 925 |
// Truncation including multi-byte characters (UTF-8) |
| 926 |
{ |
| 927 |
name: "ValidUTF-8", |
| 928 |
groups: []group{ |
| 929 |
{7, "Hello, 世界", "Hello, "}, |
| 930 |
{8, "Hello, 世界", "Hello, 世"}, |
| 931 |
{2, "こんにちは", "こん"}, |
| 932 |
{3, "こんにちは", "こんに"}, |
| 933 |
{5, "こんにちは", "こんにちは"}, |
| 934 |
{12, "こんにちは", "こんにちは"}, |
| 935 |
}, |
| 936 |
}, |
| 937 |
|
| 938 |
// Truncation with invalid UTF-8 characters |
| 939 |
{ |
| 940 |
name: "InvalidUTF-8", |
| 941 |
groups: []group{ |
| 942 |
{11, "Invalid\x80text", "Invalidtext"}, |
| 943 |
// Do not modify invalid text if equal to limit. |
| 944 |
{11, "Valid text\x80", "Valid text\x80"}, |
| 945 |
// Do not modify invalid text if under limit. |
| 946 |
{15, "Valid text\x80", "Valid text\x80"}, |
| 947 |
{5, "Hello\x80World", "Hello"}, |
| 948 |
{11, "Hello\x80World\x80!", "HelloWorld!"}, |
| 949 |
{15, "Hello\x80World\x80Test", "HelloWorldTest"}, |
| 950 |
{15, "Hello\x80\x80\x80World\x80Test", "HelloWorldTest"}, |
| 951 |
{15, "\x80\x80\x80Hello\x80\x80\x80World\x80Test\x80\x80", "HelloWorldTest"}, |
| 952 |
}, |
| 953 |
}, |
| 954 |
|
| 955 |
// Truncation with mixed validn and invalid UTF-8 characters |
| 956 |
{ |
| 957 |
name: "MixedUTF-8", |
| 958 |
groups: []group{ |
| 959 |
{6, "€"[0:2] + "hello€€", "hello€"}, |
| 960 |
{6, "€" + "€"[0:2] + "hello", "€hello"}, |
| 961 |
{11, "Valid text\x80📜", "Valid text📜"}, |
| 962 |
{11, "Valid text📜\x80", "Valid text📜"}, |
| 963 |
{14, "😊 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"}, |
| 964 |
{14, "😊\x80 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"}, |
| 965 |
{14, "😊\x80 Hello\x80World🌍\x80🚀", "😊 HelloWorld🌍🚀"}, |
| 966 |
{14, "😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"}, |
| 967 |
{14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"}, |
| 968 |
}, |
| 969 |
}, |
| 970 |
|
| 971 |
// Edge case: empty string, should return empty string |
| 972 |
{ |
| 973 |
name: "Empty", |
| 974 |
groups: []group{ |
| 975 |
{5, "", ""}, |
| 976 |
}, |
| 977 |
}, |
| 978 |
|
| 979 |
// Edge case: limit is 0, should return an empty string |
| 980 |
{ |
| 981 |
name: "Zero", |
| 982 |
groups: []group{ |
| 983 |
{0, "Some text", ""}, |
| 984 |
{0, "", ""}, |
| 985 |
}, |
| 986 |
}, |
| 987 |
} |
| 988 |
|
| 989 |
for _, tt := range tests { |
| 990 |
for _, g := range tt.groups { |
| 991 |
t.Run(tt.name, func(t *testing.T) { |
| 992 |
t.Parallel() |
| 993 |
|
| 994 |
got := truncate(g.limit, g.input) |
| 995 |
assert.Equalf( |
| 996 |
t, g.expected, got, |
| 997 |
"input: %q([]rune%v))\ngot: %q([]rune%v)\nwant %q([]rune%v)", |
| 998 |
g.input, []rune(g.input), |
| 999 |
got, []rune(got), |
| 1000 |
g.expected, []rune(g.expected), |
| 1001 |
) |
| 1002 |
}) |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
func BenchmarkTruncate(b *testing.B) { |
| 1008 |
run := func(limit int, input string) func(b *testing.B) { |
| 1009 |
return func(b *testing.B) { |
| 1010 |
b.ReportAllocs() |
| 1011 |
b.RunParallel(func(pb *testing.PB) { |
| 1012 |
var out string |
| 1013 |
for pb.Next() { |
| 1014 |
out = truncate(limit, input) |
| 1015 |
} |
| 1016 |
_ = out |
| 1017 |
}) |
| 1018 |
} |
| 1019 |
} |
| 1020 |
b.Run("Unlimited", run(-1, "hello 😊 world 🌍🚀")) |
| 1021 |
b.Run("Zero", run(0, "Some text")) |
| 1022 |
b.Run("Short", run(10, "Short Text")) |
| 1023 |
b.Run("ASCII", run(5, "Hello, World!")) |
| 1024 |
b.Run("ValidUTF-8", run(10, "hello 😊 world 🌍🚀")) |
| 1025 |
b.Run("InvalidUTF-8", run(6, "€"[0:2]+"hello€€")) |
| 1026 |
b.Run("MixedUTF-8", run(14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80")) |
| 1027 |
} |
| 1028 |
|
| 1029 |
func TestSpanConcurrentSafe(t *testing.T) { |
| 1030 |
t.Parallel() |
| 1031 |
|
| 1032 |
const ( |
| 1033 |
nTracers = 2 |
| 1034 |
nSpans = 2 |
| 1035 |
nGoroutine = 10 |
| 1036 |
) |
| 1037 |
|
| 1038 |
runSpan := func(s Span) <-chan struct{} { |
| 1039 |
done := make(chan struct{}) |
| 1040 |
go func(span Span) { |
| 1041 |
defer close(done) |
| 1042 |
|
| 1043 |
var wg sync.WaitGroup |
| 1044 |
for i := range nGoroutine { |
| 1045 |
wg.Add(1) |
| 1046 |
go func(n int) { |
| 1047 |
defer wg.Done() |
| 1048 |
|
| 1049 |
_ = span.IsRecording() |
| 1050 |
_ = span.SpanContext() |
| 1051 |
_ = span.TracerProvider() |
| 1052 |
|
| 1053 |
span.AddEvent("event") |
| 1054 |
span.AddLink(Link{}) |
| 1055 |
span.RecordError(errors.New("err")) |
| 1056 |
span.SetStatus(codes.Error, "error") |
| 1057 |
span.SetName("span" + strconv.Itoa(n)) |
| 1058 |
span.SetAttributes(attribute.Bool("key", true)) |
| 1059 |
|
| 1060 |
span.End() |
| 1061 |
}(i) |
| 1062 |
} |
| 1063 |
|
| 1064 |
wg.Wait() |
| 1065 |
}(s) |
| 1066 |
return done |
| 1067 |
} |
| 1068 |
|
| 1069 |
runTracer := func(tr Tracer) <-chan struct{} { |
| 1070 |
done := make(chan struct{}) |
| 1071 |
go func(tracer Tracer) { |
| 1072 |
defer close(done) |
| 1073 |
|
| 1074 |
ctx := t.Context() |
| 1075 |
|
| 1076 |
var wg sync.WaitGroup |
| 1077 |
for i := range nSpans { |
| 1078 |
wg.Add(1) |
| 1079 |
go func(n int) { |
| 1080 |
defer wg.Done() |
| 1081 |
_, s := tracer.Start(ctx, "span"+strconv.Itoa(n)) |
| 1082 |
<-runSpan(s) |
| 1083 |
}(i) |
| 1084 |
} |
| 1085 |
|
| 1086 |
wg.Wait() |
| 1087 |
}(tr) |
| 1088 |
return done |
| 1089 |
} |
| 1090 |
|
| 1091 |
run := func(tp TracerProvider) <-chan struct{} { |
| 1092 |
done := make(chan struct{}) |
| 1093 |
go func(provider TracerProvider) { |
| 1094 |
defer close(done) |
| 1095 |
|
| 1096 |
var wg sync.WaitGroup |
| 1097 |
for i := range nTracers { |
| 1098 |
wg.Add(1) |
| 1099 |
go func(n int) { |
| 1100 |
defer wg.Done() |
| 1101 |
<-runTracer(provider.Tracer("tracer" + strconv.Itoa(n))) |
| 1102 |
}(i) |
| 1103 |
} |
| 1104 |
|
| 1105 |
wg.Wait() |
| 1106 |
}(tp) |
| 1107 |
return done |
| 1108 |
} |
| 1109 |
|
| 1110 |
assert.NotPanics(t, func() { |
| 1111 |
done0, done1 := run(newAutoTracerProvider()), run(newAutoTracerProvider()) |
| 1112 |
|
| 1113 |
<-done0 |
| 1114 |
<-done1 |
| 1115 |
}) |
| 1116 |
} |
| 1117 |
|
| 1118 |
func TestSpanLimit(t *testing.T) { |
| 1119 |
tests := []struct { |
| 1120 |
name string |
| 1121 |
get func(spanLimits) int |
| 1122 |
zero int |
| 1123 |
keys []string |
| 1124 |
}{ |
| 1125 |
{ |
| 1126 |
name: "AttributeValueLengthLimit", |
| 1127 |
get: func(sl spanLimits) int { return sl.AttrValueLen }, |
| 1128 |
zero: -1, |
| 1129 |
keys: []string{ |
| 1130 |
"OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", |
| 1131 |
"OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT", |
| 1132 |
}, |
| 1133 |
}, |
| 1134 |
{ |
| 1135 |
name: "AttributeCountLimit", |
| 1136 |
get: func(sl spanLimits) int { return sl.Attrs }, |
| 1137 |
zero: 128, |
| 1138 |
keys: []string{ |
| 1139 |
"OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", |
| 1140 |
"OTEL_ATTRIBUTE_COUNT_LIMIT", |
| 1141 |
}, |
| 1142 |
}, |
| 1143 |
{ |
| 1144 |
name: "EventCountLimit", |
| 1145 |
get: func(sl spanLimits) int { return sl.Events }, |
| 1146 |
zero: 128, |
| 1147 |
keys: []string{"OTEL_SPAN_EVENT_COUNT_LIMIT"}, |
| 1148 |
}, |
| 1149 |
{ |
| 1150 |
name: "EventAttributeCountLimit", |
| 1151 |
get: func(sl spanLimits) int { return sl.EventAttrs }, |
| 1152 |
zero: 128, |
| 1153 |
keys: []string{"OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"}, |
| 1154 |
}, |
| 1155 |
{ |
| 1156 |
name: "LinkCountLimit", |
| 1157 |
get: func(sl spanLimits) int { return sl.Links }, |
| 1158 |
zero: 128, |
| 1159 |
keys: []string{"OTEL_SPAN_LINK_COUNT_LIMIT"}, |
| 1160 |
}, |
| 1161 |
{ |
| 1162 |
name: "LinkAttributeCountLimit", |
| 1163 |
get: func(sl spanLimits) int { return sl.LinkAttrs }, |
| 1164 |
zero: 128, |
| 1165 |
keys: []string{"OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"}, |
| 1166 |
}, |
| 1167 |
} |
| 1168 |
|
| 1169 |
for _, test := range tests { |
| 1170 |
t.Run(test.name, func(t *testing.T) { |
| 1171 |
t.Run("Default", func(t *testing.T) { |
| 1172 |
assert.Equal(t, test.zero, test.get(newSpanLimits())) |
| 1173 |
}) |
| 1174 |
|
| 1175 |
t.Run("ValidValue", func(t *testing.T) { |
| 1176 |
for _, key := range test.keys { |
| 1177 |
t.Run(key, func(t *testing.T) { |
| 1178 |
t.Setenv(key, "43") |
| 1179 |
assert.Equal(t, 43, test.get(newSpanLimits())) |
| 1180 |
}) |
| 1181 |
} |
| 1182 |
}) |
| 1183 |
}) |
| 1184 |
} |
| 1185 |
} |