| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/trace/internal/env" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
func TestSettingSpanLimits(t *testing.T) { |
| 19 |
envLimits := func(val string) map[string]string { |
| 20 |
return map[string]string{ |
| 21 |
env.SpanAttributeValueLengthKey: val, |
| 22 |
env.SpanEventCountKey: val, |
| 23 |
env.SpanAttributeCountKey: val, |
| 24 |
env.SpanLinkCountKey: val, |
| 25 |
env.SpanEventAttributeCountKey: val, |
| 26 |
env.SpanLinkAttributeCountKey: val, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
limits := func(n int) *SpanLimits { |
| 31 |
lims := NewSpanLimits() |
| 32 |
lims.AttributeValueLengthLimit = n |
| 33 |
lims.AttributeCountLimit = n |
| 34 |
lims.EventCountLimit = n |
| 35 |
lims.LinkCountLimit = n |
| 36 |
lims.AttributePerEventCountLimit = n |
| 37 |
lims.AttributePerLinkCountLimit = n |
| 38 |
return &lims |
| 39 |
} |
| 40 |
|
| 41 |
tests := []struct { |
| 42 |
name string |
| 43 |
env map[string]string |
| 44 |
opt *SpanLimits |
| 45 |
rawOpt *SpanLimits |
| 46 |
want SpanLimits |
| 47 |
}{ |
| 48 |
{ |
| 49 |
name: "defaults", |
| 50 |
want: NewSpanLimits(), |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "env", |
| 54 |
env: envLimits("42"), |
| 55 |
want: *(limits(42)), |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "opt", |
| 59 |
opt: limits(42), |
| 60 |
want: *(limits(42)), |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "raw-opt", |
| 64 |
rawOpt: limits(42), |
| 65 |
want: *(limits(42)), |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "opt-override", |
| 69 |
env: envLimits("-2"), |
| 70 |
// Option take priority. |
| 71 |
opt: limits(43), |
| 72 |
want: *(limits(43)), |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "raw-opt-override", |
| 76 |
env: envLimits("-2"), |
| 77 |
// Option take priority. |
| 78 |
rawOpt: limits(43), |
| 79 |
want: *(limits(43)), |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "last-opt-wins", |
| 83 |
opt: limits(-2), |
| 84 |
rawOpt: limits(-3), |
| 85 |
want: *(limits(-3)), |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "env(unlimited)", |
| 89 |
// OTel spec says negative SpanLinkAttributeCountKey is invalid, |
| 90 |
// but since we will revert to the default (unlimited) which uses |
| 91 |
// negative values to signal this than this value is expected to |
| 92 |
// pass through. |
| 93 |
env: envLimits("-1"), |
| 94 |
want: *(limits(-1)), |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "opt(unlimited)", |
| 98 |
// Corrects to defaults. |
| 99 |
opt: limits(-1), |
| 100 |
want: NewSpanLimits(), |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "raw-opt(unlimited)", |
| 104 |
rawOpt: limits(-1), |
| 105 |
want: *(limits(-1)), |
| 106 |
}, |
| 107 |
} |
| 108 |
|
| 109 |
for _, test := range tests { |
| 110 |
t.Run(test.name, func(t *testing.T) { |
| 111 |
if test.env != nil { |
| 112 |
for k, v := range test.env { |
| 113 |
t.Setenv(k, v) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
var opts []TracerProviderOption |
| 118 |
if test.opt != nil { |
| 119 |
opts = append(opts, WithSpanLimits(*test.opt)) |
| 120 |
} |
| 121 |
if test.rawOpt != nil { |
| 122 |
opts = append(opts, WithRawSpanLimits(*test.rawOpt)) |
| 123 |
} |
| 124 |
|
| 125 |
assert.Equal(t, test.want, NewTracerProvider(opts...).spanLimits) |
| 126 |
}) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
type recorder []ReadOnlySpan |
| 131 |
|
| 132 |
func (*recorder) OnStart(context.Context, ReadWriteSpan) {} |
| 133 |
func (r *recorder) OnEnd(s ReadOnlySpan) { *r = append(*r, s) } |
| 134 |
func (*recorder) ForceFlush(context.Context) error { return nil } |
| 135 |
func (*recorder) Shutdown(context.Context) error { return nil } |
| 136 |
|
| 137 |
func testSpanLimits(t *testing.T, limits SpanLimits) ReadOnlySpan { |
| 138 |
rec := new(recorder) |
| 139 |
tp := NewTracerProvider(WithRawSpanLimits(limits), WithSpanProcessor(rec)) |
| 140 |
tracer := tp.Tracer("testSpanLimits") |
| 141 |
|
| 142 |
ctx := t.Context() |
| 143 |
a := []attribute.KeyValue{attribute.Bool("one", true), attribute.Bool("two", true)} |
| 144 |
l := trace.Link{ |
| 145 |
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{ |
| 146 |
TraceID: [16]byte{0x01}, |
| 147 |
SpanID: [8]byte{0x01}, |
| 148 |
}), |
| 149 |
Attributes: a, |
| 150 |
} |
| 151 |
_, span := tracer.Start(ctx, "span-name", trace.WithLinks(l, l)) |
| 152 |
span.SetAttributes( |
| 153 |
attribute.String("string", "abc"), |
| 154 |
attribute.StringSlice("stringSlice", []string{"abc", "def"}), |
| 155 |
attribute.String("euro", "€"), // this is a 3-byte rune |
| 156 |
) |
| 157 |
span.AddEvent("event 1", trace.WithAttributes(a...)) |
| 158 |
span.AddEvent("event 2", trace.WithAttributes(a...)) |
| 159 |
span.End() |
| 160 |
require.NoError(t, tp.Shutdown(ctx)) |
| 161 |
|
| 162 |
require.Len(t, *rec, 1, "exported spans") |
| 163 |
return (*rec)[0] |
| 164 |
} |
| 165 |
|
| 166 |
func TestSpanLimits(t *testing.T) { |
| 167 |
t.Run("AttributeValueLengthLimit", func(t *testing.T) { |
| 168 |
limits := NewSpanLimits() |
| 169 |
// Unlimited. |
| 170 |
limits.AttributeValueLengthLimit = -1 |
| 171 |
attrs := testSpanLimits(t, limits).Attributes() |
| 172 |
assert.Contains(t, attrs, attribute.String("string", "abc")) |
| 173 |
assert.Contains(t, attrs, attribute.StringSlice("stringSlice", []string{"abc", "def"})) |
| 174 |
assert.Contains(t, attrs, attribute.String("euro", "€")) |
| 175 |
|
| 176 |
limits.AttributeValueLengthLimit = 2 |
| 177 |
attrs = testSpanLimits(t, limits).Attributes() |
| 178 |
// Ensure string and string slice attributes are truncated. |
| 179 |
assert.Contains(t, attrs, attribute.String("string", "ab")) |
| 180 |
assert.Contains(t, attrs, attribute.StringSlice("stringSlice", []string{"ab", "de"})) |
| 181 |
assert.Contains(t, attrs, attribute.String("euro", "€")) |
| 182 |
|
| 183 |
limits.AttributeValueLengthLimit = 0 |
| 184 |
attrs = testSpanLimits(t, limits).Attributes() |
| 185 |
assert.Contains(t, attrs, attribute.String("string", "")) |
| 186 |
assert.Contains(t, attrs, attribute.StringSlice("stringSlice", []string{"", ""})) |
| 187 |
assert.Contains(t, attrs, attribute.String("euro", "")) |
| 188 |
}) |
| 189 |
|
| 190 |
t.Run("AttributeCountLimit", func(t *testing.T) { |
| 191 |
limits := NewSpanLimits() |
| 192 |
// Unlimited. |
| 193 |
limits.AttributeCountLimit = -1 |
| 194 |
assert.Len(t, testSpanLimits(t, limits).Attributes(), 3) |
| 195 |
|
| 196 |
limits.AttributeCountLimit = 1 |
| 197 |
assert.Len(t, testSpanLimits(t, limits).Attributes(), 1) |
| 198 |
|
| 199 |
// Ensure this can be disabled. |
| 200 |
limits.AttributeCountLimit = 0 |
| 201 |
assert.Empty(t, testSpanLimits(t, limits).Attributes()) |
| 202 |
}) |
| 203 |
|
| 204 |
t.Run("EventCountLimit", func(t *testing.T) { |
| 205 |
limits := NewSpanLimits() |
| 206 |
// Unlimited. |
| 207 |
limits.EventCountLimit = -1 |
| 208 |
assert.Len(t, testSpanLimits(t, limits).Events(), 2) |
| 209 |
|
| 210 |
limits.EventCountLimit = 1 |
| 211 |
assert.Len(t, testSpanLimits(t, limits).Events(), 1) |
| 212 |
|
| 213 |
// Ensure this can be disabled. |
| 214 |
limits.EventCountLimit = 0 |
| 215 |
assert.Empty(t, testSpanLimits(t, limits).Events()) |
| 216 |
}) |
| 217 |
|
| 218 |
t.Run("AttributePerEventCountLimit", func(t *testing.T) { |
| 219 |
limits := NewSpanLimits() |
| 220 |
// Unlimited. |
| 221 |
limits.AttributePerEventCountLimit = -1 |
| 222 |
for _, e := range testSpanLimits(t, limits).Events() { |
| 223 |
assert.Len(t, e.Attributes, 2) |
| 224 |
} |
| 225 |
|
| 226 |
limits.AttributePerEventCountLimit = 1 |
| 227 |
for _, e := range testSpanLimits(t, limits).Events() { |
| 228 |
assert.Len(t, e.Attributes, 1) |
| 229 |
} |
| 230 |
|
| 231 |
// Ensure this can be disabled. |
| 232 |
limits.AttributePerEventCountLimit = 0 |
| 233 |
for _, e := range testSpanLimits(t, limits).Events() { |
| 234 |
assert.Empty(t, e.Attributes) |
| 235 |
} |
| 236 |
}) |
| 237 |
|
| 238 |
t.Run("LinkCountLimit", func(t *testing.T) { |
| 239 |
limits := NewSpanLimits() |
| 240 |
// Unlimited. |
| 241 |
limits.LinkCountLimit = -1 |
| 242 |
assert.Len(t, testSpanLimits(t, limits).Links(), 2) |
| 243 |
|
| 244 |
limits.LinkCountLimit = 1 |
| 245 |
assert.Len(t, testSpanLimits(t, limits).Links(), 1) |
| 246 |
|
| 247 |
// Ensure this can be disabled. |
| 248 |
limits.LinkCountLimit = 0 |
| 249 |
assert.Empty(t, testSpanLimits(t, limits).Links()) |
| 250 |
}) |
| 251 |
|
| 252 |
t.Run("AttributePerLinkCountLimit", func(t *testing.T) { |
| 253 |
limits := NewSpanLimits() |
| 254 |
// Unlimited. |
| 255 |
limits.AttributePerLinkCountLimit = -1 |
| 256 |
for _, l := range testSpanLimits(t, limits).Links() { |
| 257 |
assert.Len(t, l.Attributes, 2) |
| 258 |
} |
| 259 |
|
| 260 |
limits.AttributePerLinkCountLimit = 1 |
| 261 |
for _, l := range testSpanLimits(t, limits).Links() { |
| 262 |
assert.Len(t, l.Attributes, 1) |
| 263 |
} |
| 264 |
|
| 265 |
// Ensure this can be disabled. |
| 266 |
limits.AttributePerLinkCountLimit = 0 |
| 267 |
for _, l := range testSpanLimits(t, limits).Links() { |
| 268 |
assert.Empty(t, l.Attributes) |
| 269 |
} |
| 270 |
}) |
| 271 |
} |