| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/codes" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
func TestSetStatus(t *testing.T) { |
| 19 |
tests := []struct { |
| 20 |
name string |
| 21 |
span recordingSpan |
| 22 |
code codes.Code |
| 23 |
description string |
| 24 |
expected Status |
| 25 |
}{ |
| 26 |
{ |
| 27 |
"Error and description should overwrite Unset", |
| 28 |
recordingSpan{}, |
| 29 |
codes.Error, |
| 30 |
"description", |
| 31 |
Status{Code: codes.Error, Description: "description"}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
"Ok should overwrite Unset and ignore description", |
| 35 |
recordingSpan{}, |
| 36 |
codes.Ok, |
| 37 |
"description", |
| 38 |
Status{Code: codes.Ok}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
"Error and description should return error and overwrite description", |
| 42 |
recordingSpan{status: Status{Code: codes.Error, Description: "d1"}}, |
| 43 |
codes.Error, |
| 44 |
"d2", |
| 45 |
Status{Code: codes.Error, Description: "d2"}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
"Ok should overwrite error and remove description", |
| 49 |
recordingSpan{status: Status{Code: codes.Error, Description: "d1"}}, |
| 50 |
codes.Ok, |
| 51 |
"d2", |
| 52 |
Status{Code: codes.Ok}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
"Error and description should be ignored when already Ok", |
| 56 |
recordingSpan{status: Status{Code: codes.Ok}}, |
| 57 |
codes.Error, |
| 58 |
"d2", |
| 59 |
Status{Code: codes.Ok}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
"Ok should be noop when already Ok", |
| 63 |
recordingSpan{status: Status{Code: codes.Ok}}, |
| 64 |
codes.Ok, |
| 65 |
"d2", |
| 66 |
Status{Code: codes.Ok}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
"Unset should be noop when already Ok", |
| 70 |
recordingSpan{status: Status{Code: codes.Ok}}, |
| 71 |
codes.Unset, |
| 72 |
"d2", |
| 73 |
Status{Code: codes.Ok}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
"Unset should be noop when already Error", |
| 77 |
recordingSpan{status: Status{Code: codes.Error, Description: "d1"}}, |
| 78 |
codes.Unset, |
| 79 |
"d2", |
| 80 |
Status{Code: codes.Error, Description: "d1"}, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
for i := range tests { |
| 85 |
tc := &tests[i] |
| 86 |
t.Run(tc.name, func(t *testing.T) { |
| 87 |
tc.span.SetStatus(tc.code, tc.description) |
| 88 |
assert.Equal(t, tc.expected, tc.span.status) |
| 89 |
}) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestTruncateAttr(t *testing.T) { |
| 94 |
const key = "key" |
| 95 |
|
| 96 |
strAttr := attribute.String(key, "value") |
| 97 |
strSliceAttr := attribute.StringSlice(key, []string{"value-0", "value-1"}) |
| 98 |
|
| 99 |
tests := []struct { |
| 100 |
limit int |
| 101 |
attr, want attribute.KeyValue |
| 102 |
}{ |
| 103 |
{ |
| 104 |
limit: -1, |
| 105 |
attr: strAttr, |
| 106 |
want: strAttr, |
| 107 |
}, |
| 108 |
{ |
| 109 |
limit: -1, |
| 110 |
attr: strSliceAttr, |
| 111 |
want: strSliceAttr, |
| 112 |
}, |
| 113 |
{ |
| 114 |
limit: 0, |
| 115 |
attr: attribute.Bool(key, true), |
| 116 |
want: attribute.Bool(key, true), |
| 117 |
}, |
| 118 |
{ |
| 119 |
limit: 0, |
| 120 |
attr: attribute.BoolSlice(key, []bool{true, false}), |
| 121 |
want: attribute.BoolSlice(key, []bool{true, false}), |
| 122 |
}, |
| 123 |
{ |
| 124 |
limit: 0, |
| 125 |
attr: attribute.Int(key, 42), |
| 126 |
want: attribute.Int(key, 42), |
| 127 |
}, |
| 128 |
{ |
| 129 |
limit: 0, |
| 130 |
attr: attribute.IntSlice(key, []int{42, -1}), |
| 131 |
want: attribute.IntSlice(key, []int{42, -1}), |
| 132 |
}, |
| 133 |
{ |
| 134 |
limit: 0, |
| 135 |
attr: attribute.Int64(key, 42), |
| 136 |
want: attribute.Int64(key, 42), |
| 137 |
}, |
| 138 |
{ |
| 139 |
limit: 0, |
| 140 |
attr: attribute.Int64Slice(key, []int64{42, -1}), |
| 141 |
want: attribute.Int64Slice(key, []int64{42, -1}), |
| 142 |
}, |
| 143 |
{ |
| 144 |
limit: 0, |
| 145 |
attr: attribute.Float64(key, 42), |
| 146 |
want: attribute.Float64(key, 42), |
| 147 |
}, |
| 148 |
{ |
| 149 |
limit: 0, |
| 150 |
attr: attribute.Float64Slice(key, []float64{42, -1}), |
| 151 |
want: attribute.Float64Slice(key, []float64{42, -1}), |
| 152 |
}, |
| 153 |
{ |
| 154 |
limit: 0, |
| 155 |
attr: strAttr, |
| 156 |
want: attribute.String(key, ""), |
| 157 |
}, |
| 158 |
{ |
| 159 |
limit: 0, |
| 160 |
attr: strSliceAttr, |
| 161 |
want: attribute.StringSlice(key, []string{"", ""}), |
| 162 |
}, |
| 163 |
{ |
| 164 |
limit: 0, |
| 165 |
attr: attribute.Stringer(key, bytes.NewBufferString("value")), |
| 166 |
want: attribute.String(key, ""), |
| 167 |
}, |
| 168 |
{ |
| 169 |
limit: 1, |
| 170 |
attr: strAttr, |
| 171 |
want: attribute.String(key, "v"), |
| 172 |
}, |
| 173 |
{ |
| 174 |
limit: 1, |
| 175 |
attr: strSliceAttr, |
| 176 |
want: attribute.StringSlice(key, []string{"v", "v"}), |
| 177 |
}, |
| 178 |
{ |
| 179 |
limit: 5, |
| 180 |
attr: strAttr, |
| 181 |
want: strAttr, |
| 182 |
}, |
| 183 |
{ |
| 184 |
limit: 7, |
| 185 |
attr: strSliceAttr, |
| 186 |
want: strSliceAttr, |
| 187 |
}, |
| 188 |
{ |
| 189 |
limit: 6, |
| 190 |
attr: attribute.StringSlice(key, []string{"value", "value-1"}), |
| 191 |
want: attribute.StringSlice(key, []string{"value", "value-"}), |
| 192 |
}, |
| 193 |
{ |
| 194 |
limit: 128, |
| 195 |
attr: strAttr, |
| 196 |
want: strAttr, |
| 197 |
}, |
| 198 |
{ |
| 199 |
limit: 128, |
| 200 |
attr: strSliceAttr, |
| 201 |
want: strSliceAttr, |
| 202 |
}, |
| 203 |
} |
| 204 |
|
| 205 |
for _, test := range tests { |
| 206 |
name := fmt.Sprintf("%s->%s(limit:%d)", test.attr.Key, test.attr.Value.Emit(), test.limit) |
| 207 |
t.Run(name, func(t *testing.T) { |
| 208 |
assert.Equal(t, test.want, truncateAttr(test.limit, test.attr)) |
| 209 |
}) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestTruncate(t *testing.T) { |
| 214 |
type group struct { |
| 215 |
limit int |
| 216 |
input string |
| 217 |
expected string |
| 218 |
} |
| 219 |
|
| 220 |
tests := []struct { |
| 221 |
name string |
| 222 |
groups []group |
| 223 |
}{ |
| 224 |
// Edge case: limit is negative, no truncation should occur |
| 225 |
{ |
| 226 |
name: "NoTruncation", |
| 227 |
groups: []group{ |
| 228 |
{-1, "No truncation!", "No truncation!"}, |
| 229 |
}, |
| 230 |
}, |
| 231 |
|
| 232 |
// Edge case: string is already shorter than the limit, no truncation |
| 233 |
// should occur |
| 234 |
{ |
| 235 |
name: "ShortText", |
| 236 |
groups: []group{ |
| 237 |
{10, "Short text", "Short text"}, |
| 238 |
{15, "Short text", "Short text"}, |
| 239 |
{100, "Short text", "Short text"}, |
| 240 |
}, |
| 241 |
}, |
| 242 |
|
| 243 |
// Edge case: truncation happens with ASCII characters only |
| 244 |
{ |
| 245 |
name: "ASCIIOnly", |
| 246 |
groups: []group{ |
| 247 |
{1, "Hello World!", "H"}, |
| 248 |
{5, "Hello World!", "Hello"}, |
| 249 |
{12, "Hello World!", "Hello World!"}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
|
| 253 |
// Truncation including multi-byte characters (UTF-8) |
| 254 |
{ |
| 255 |
name: "ValidUTF-8", |
| 256 |
groups: []group{ |
| 257 |
{7, "Hello, 世界", "Hello, "}, |
| 258 |
{8, "Hello, 世界", "Hello, 世"}, |
| 259 |
{2, "こんにちは", "こん"}, |
| 260 |
{3, "こんにちは", "こんに"}, |
| 261 |
{5, "こんにちは", "こんにちは"}, |
| 262 |
{12, "こんにちは", "こんにちは"}, |
| 263 |
}, |
| 264 |
}, |
| 265 |
|
| 266 |
// Truncation with invalid UTF-8 characters |
| 267 |
{ |
| 268 |
name: "InvalidUTF-8", |
| 269 |
groups: []group{ |
| 270 |
{11, "Invalid\x80text", "Invalidtext"}, |
| 271 |
// Do not modify invalid text if equal to limit. |
| 272 |
{11, "Valid text\x80", "Valid text\x80"}, |
| 273 |
// Do not modify invalid text if under limit. |
| 274 |
{15, "Valid text\x80", "Valid text\x80"}, |
| 275 |
{5, "Hello\x80World", "Hello"}, |
| 276 |
{11, "Hello\x80World\x80!", "HelloWorld!"}, |
| 277 |
{15, "Hello\x80World\x80Test", "HelloWorldTest"}, |
| 278 |
{15, "Hello\x80\x80\x80World\x80Test", "HelloWorldTest"}, |
| 279 |
{15, "\x80\x80\x80Hello\x80\x80\x80World\x80Test\x80\x80", "HelloWorldTest"}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
|
| 283 |
// Truncation with mixed validn and invalid UTF-8 characters |
| 284 |
{ |
| 285 |
name: "MixedUTF-8", |
| 286 |
groups: []group{ |
| 287 |
{6, "€"[0:2] + "hello€€", "hello€"}, |
| 288 |
{6, "€" + "€"[0:2] + "hello", "€hello"}, |
| 289 |
{11, "Valid text\x80📜", "Valid text📜"}, |
| 290 |
{11, "Valid text📜\x80", "Valid text📜"}, |
| 291 |
{14, "😊 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"}, |
| 292 |
{14, "😊\x80 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"}, |
| 293 |
{14, "😊\x80 Hello\x80World🌍\x80🚀", "😊 HelloWorld🌍🚀"}, |
| 294 |
{14, "😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"}, |
| 295 |
{14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"}, |
| 296 |
}, |
| 297 |
}, |
| 298 |
|
| 299 |
// Edge case: empty string, should return empty string |
| 300 |
{ |
| 301 |
name: "Empty", |
| 302 |
groups: []group{ |
| 303 |
{5, "", ""}, |
| 304 |
}, |
| 305 |
}, |
| 306 |
|
| 307 |
// Edge case: limit is 0, should return an empty string |
| 308 |
{ |
| 309 |
name: "Zero", |
| 310 |
groups: []group{ |
| 311 |
{0, "Some text", ""}, |
| 312 |
{0, "", ""}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
} |
| 316 |
|
| 317 |
for _, tt := range tests { |
| 318 |
for _, g := range tt.groups { |
| 319 |
t.Run(tt.name, func(t *testing.T) { |
| 320 |
t.Parallel() |
| 321 |
|
| 322 |
got := truncate(g.limit, g.input) |
| 323 |
assert.Equalf( |
| 324 |
t, g.expected, got, |
| 325 |
"input: %q([]rune%v))\ngot: %q([]rune%v)\nwant %q([]rune%v)", |
| 326 |
g.input, []rune(g.input), |
| 327 |
got, []rune(got), |
| 328 |
g.expected, []rune(g.expected), |
| 329 |
) |
| 330 |
}) |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func BenchmarkTruncate(b *testing.B) { |
| 336 |
run := func(limit int, input string) func(b *testing.B) { |
| 337 |
return func(b *testing.B) { |
| 338 |
b.ReportAllocs() |
| 339 |
b.RunParallel(func(pb *testing.PB) { |
| 340 |
var out string |
| 341 |
for pb.Next() { |
| 342 |
out = truncate(limit, input) |
| 343 |
} |
| 344 |
_ = out |
| 345 |
}) |
| 346 |
} |
| 347 |
} |
| 348 |
b.Run("Unlimited", run(-1, "hello 😊 world 🌍🚀")) |
| 349 |
b.Run("Zero", run(0, "Some text")) |
| 350 |
b.Run("Short", run(10, "Short Text")) |
| 351 |
b.Run("ASCII", run(5, "Hello, World!")) |
| 352 |
b.Run("ValidUTF-8", run(10, "hello 😊 world 🌍🚀")) |
| 353 |
b.Run("InvalidUTF-8", run(6, "€"[0:2]+"hello€€")) |
| 354 |
b.Run("MixedUTF-8", run(14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80")) |
| 355 |
} |
| 356 |
|
| 357 |
func TestLogDropAttrs(t *testing.T) { |
| 358 |
orig := logDropAttrs |
| 359 |
t.Cleanup(func() { logDropAttrs = orig }) |
| 360 |
|
| 361 |
var called bool |
| 362 |
logDropAttrs = func() { called = true } |
| 363 |
|
| 364 |
s := &recordingSpan{} |
| 365 |
s.addDroppedAttr(1) |
| 366 |
assert.True(t, called, "logDropAttrs not called") |
| 367 |
|
| 368 |
called = false |
| 369 |
s.addDroppedAttr(1) |
| 370 |
assert.False(t, called, "logDropAttrs called multiple times for same Span") |
| 371 |
} |
| 372 |
|
| 373 |
func BenchmarkRecordingSpanSetAttributes(b *testing.B) { |
| 374 |
var attrs []attribute.KeyValue |
| 375 |
for i := range 100 { |
| 376 |
attr := attribute.String(fmt.Sprintf("hello.attrib%d", i), fmt.Sprintf("goodbye.attrib%d", i)) |
| 377 |
attrs = append(attrs, attr) |
| 378 |
} |
| 379 |
|
| 380 |
ctx := b.Context() |
| 381 |
for _, limit := range []bool{false, true} { |
| 382 |
b.Run(fmt.Sprintf("WithLimit/%t", limit), func(b *testing.B) { |
| 383 |
b.ReportAllocs() |
| 384 |
sl := NewSpanLimits() |
| 385 |
if limit { |
| 386 |
sl.AttributeCountLimit = 50 |
| 387 |
} |
| 388 |
tp := NewTracerProvider(WithSampler(AlwaysSample()), WithSpanLimits(sl)) |
| 389 |
tracer := tp.Tracer("tracer") |
| 390 |
|
| 391 |
b.ResetTimer() |
| 392 |
for n := 0; n < b.N; n++ { |
| 393 |
_, span := tracer.Start(ctx, "span") |
| 394 |
span.SetAttributes(attrs...) |
| 395 |
span.End() |
| 396 |
} |
| 397 |
}) |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
func BenchmarkSpanEnd(b *testing.B) { |
| 402 |
cases := []struct { |
| 403 |
name string |
| 404 |
env map[string]string |
| 405 |
}{ |
| 406 |
{ |
| 407 |
name: "Default", |
| 408 |
}, |
| 409 |
{ |
| 410 |
name: "ObservabilityEnabled", |
| 411 |
env: map[string]string{ |
| 412 |
"OTEL_GO_X_OBSERVABILITY": "True", |
| 413 |
}, |
| 414 |
}, |
| 415 |
} |
| 416 |
|
| 417 |
ctx := trace.ContextWithSpanContext(b.Context(), trace.SpanContext{}) |
| 418 |
|
| 419 |
for _, c := range cases { |
| 420 |
b.Run(c.name, func(b *testing.B) { |
| 421 |
for k, v := range c.env { |
| 422 |
b.Setenv(k, v) |
| 423 |
} |
| 424 |
|
| 425 |
tracer := NewTracerProvider().Tracer("") |
| 426 |
|
| 427 |
spans := make([]trace.Span, b.N) |
| 428 |
for i := 0; i < b.N; i++ { |
| 429 |
_, span := tracer.Start(ctx, "") |
| 430 |
spans[i] = span |
| 431 |
} |
| 432 |
|
| 433 |
b.ReportAllocs() |
| 434 |
b.ResetTimer() |
| 435 |
for i := 0; i < b.N; i++ { |
| 436 |
spans[i].End() |
| 437 |
} |
| 438 |
}) |
| 439 |
} |
| 440 |
} |