| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/go-logr/logr" |
| 14 |
"github.com/go-logr/logr/testr" |
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
|
| 19 |
"go.opentelemetry.io/otel" |
| 20 |
"go.opentelemetry.io/otel/attribute" |
| 21 |
"go.opentelemetry.io/otel/internal/global" |
| 22 |
"go.opentelemetry.io/otel/log" |
| 23 |
"go.opentelemetry.io/otel/log/noop" |
| 24 |
"go.opentelemetry.io/otel/sdk/resource" |
| 25 |
) |
| 26 |
|
| 27 |
const envVarResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES" |
| 28 |
|
| 29 |
type processor struct { |
| 30 |
Name string |
| 31 |
Err error |
| 32 |
|
| 33 |
shutdownCalls int |
| 34 |
forceFlushCalls int |
| 35 |
|
| 36 |
records []Record |
| 37 |
} |
| 38 |
|
| 39 |
func newProcessor(name string) *processor { |
| 40 |
return &processor{Name: name} |
| 41 |
} |
| 42 |
|
| 43 |
func (*processor) Enabled(context.Context, EnabledParameters) bool { |
| 44 |
return true |
| 45 |
} |
| 46 |
|
| 47 |
func (p *processor) OnEmit(_ context.Context, r *Record) error { |
| 48 |
if p.Err != nil { |
| 49 |
return p.Err |
| 50 |
} |
| 51 |
|
| 52 |
p.records = append(p.records, *r) |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (p *processor) Shutdown(context.Context) error { |
| 57 |
p.shutdownCalls++ |
| 58 |
return p.Err |
| 59 |
} |
| 60 |
|
| 61 |
func (p *processor) ForceFlush(context.Context) error { |
| 62 |
p.forceFlushCalls++ |
| 63 |
return p.Err |
| 64 |
} |
| 65 |
|
| 66 |
type fltrProcessor struct { |
| 67 |
*processor |
| 68 |
|
| 69 |
enabled bool |
| 70 |
params []EnabledParameters |
| 71 |
} |
| 72 |
|
| 73 |
func newFltrProcessor(name string, enabled bool) *fltrProcessor { |
| 74 |
return &fltrProcessor{ |
| 75 |
processor: newProcessor(name), |
| 76 |
enabled: enabled, |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func (p *fltrProcessor) Enabled(_ context.Context, params EnabledParameters) bool { |
| 81 |
p.params = append(p.params, params) |
| 82 |
return p.enabled |
| 83 |
} |
| 84 |
|
| 85 |
func TestNewLoggerProviderConfiguration(t *testing.T) { |
| 86 |
t.Cleanup(func(orig otel.ErrorHandler) func() { |
| 87 |
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { |
| 88 |
t.Log(err) |
| 89 |
})) |
| 90 |
return func() { otel.SetErrorHandler(orig) } |
| 91 |
}(otel.GetErrorHandler())) |
| 92 |
|
| 93 |
res := resource.NewSchemaless(attribute.String("key", "value")) |
| 94 |
p0, p1 := newProcessor("0"), newProcessor("1") |
| 95 |
attrCntLim := 12 |
| 96 |
attrValLenLim := 21 |
| 97 |
|
| 98 |
testcases := []struct { |
| 99 |
name string |
| 100 |
envars map[string]string |
| 101 |
options []LoggerProviderOption |
| 102 |
want *LoggerProvider |
| 103 |
}{ |
| 104 |
{ |
| 105 |
name: "Defaults", |
| 106 |
want: &LoggerProvider{ |
| 107 |
resource: resource.Default(), |
| 108 |
attributeCountLimit: defaultAttrCntLim, |
| 109 |
attributeValueLengthLimit: defaultAttrValLenLim, |
| 110 |
}, |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "Options", |
| 114 |
options: []LoggerProviderOption{ |
| 115 |
WithResource(res), |
| 116 |
WithProcessor(p0), |
| 117 |
WithProcessor(p1), |
| 118 |
WithAttributeCountLimit(attrCntLim), |
| 119 |
WithAttributeValueLengthLimit(attrValLenLim), |
| 120 |
WithAllowKeyDuplication(), |
| 121 |
}, |
| 122 |
want: &LoggerProvider{ |
| 123 |
resource: res, |
| 124 |
processors: []Processor{p0, p1}, |
| 125 |
attributeCountLimit: attrCntLim, |
| 126 |
attributeValueLengthLimit: attrValLenLim, |
| 127 |
allowDupKeys: true, |
| 128 |
}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "Environment", |
| 132 |
envars: map[string]string{ |
| 133 |
envarAttrCntLim: strconv.Itoa(attrCntLim), |
| 134 |
envarAttrValLenLim: strconv.Itoa(attrValLenLim), |
| 135 |
}, |
| 136 |
want: &LoggerProvider{ |
| 137 |
resource: resource.Default(), |
| 138 |
attributeCountLimit: attrCntLim, |
| 139 |
attributeValueLengthLimit: attrValLenLim, |
| 140 |
}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "InvalidEnvironment", |
| 144 |
envars: map[string]string{ |
| 145 |
envarAttrCntLim: "invalid attributeCountLimit", |
| 146 |
envarAttrValLenLim: "invalid attributeValueLengthLimit", |
| 147 |
}, |
| 148 |
want: &LoggerProvider{ |
| 149 |
resource: resource.Default(), |
| 150 |
attributeCountLimit: defaultAttrCntLim, |
| 151 |
attributeValueLengthLimit: defaultAttrValLenLim, |
| 152 |
}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "Precedence", |
| 156 |
envars: map[string]string{ |
| 157 |
envarAttrCntLim: strconv.Itoa(100), |
| 158 |
envarAttrValLenLim: strconv.Itoa(101), |
| 159 |
}, |
| 160 |
options: []LoggerProviderOption{ |
| 161 |
// These override the environment variables. |
| 162 |
WithAttributeCountLimit(attrCntLim), |
| 163 |
WithAttributeValueLengthLimit(attrValLenLim), |
| 164 |
}, |
| 165 |
want: &LoggerProvider{ |
| 166 |
resource: resource.Default(), |
| 167 |
attributeCountLimit: attrCntLim, |
| 168 |
attributeValueLengthLimit: attrValLenLim, |
| 169 |
}, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
for _, tc := range testcases { |
| 174 |
t.Run(tc.name, func(t *testing.T) { |
| 175 |
for key, value := range tc.envars { |
| 176 |
t.Setenv(key, value) |
| 177 |
} |
| 178 |
assert.Equal(t, tc.want, NewLoggerProvider(tc.options...)) |
| 179 |
}) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func mergeResource(t *testing.T, r1, r2 *resource.Resource) *resource.Resource { |
| 184 |
r, err := resource.Merge(r1, r2) |
| 185 |
assert.NoError(t, err) |
| 186 |
return r |
| 187 |
} |
| 188 |
|
| 189 |
func TestWithResource(t *testing.T) { |
| 190 |
t.Setenv(envVarResourceAttributes, "key=value,rk5=7") |
| 191 |
|
| 192 |
cases := []struct { |
| 193 |
name string |
| 194 |
options []LoggerProviderOption |
| 195 |
want *resource.Resource |
| 196 |
msg string |
| 197 |
}{ |
| 198 |
{ |
| 199 |
name: "explicitly empty resource", |
| 200 |
options: []LoggerProviderOption{WithResource(resource.Empty())}, |
| 201 |
want: resource.Environment(), |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "uses default if no resource option", |
| 205 |
options: []LoggerProviderOption{}, |
| 206 |
want: resource.Default(), |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "explicit resource", |
| 210 |
options: []LoggerProviderOption{ |
| 211 |
WithResource(resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5))), |
| 212 |
}, |
| 213 |
want: mergeResource( |
| 214 |
t, |
| 215 |
resource.Environment(), |
| 216 |
resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)), |
| 217 |
), |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "last resource wins", |
| 221 |
options: []LoggerProviderOption{ |
| 222 |
WithResource(resource.NewSchemaless(attribute.String("rk1", "vk1"), attribute.Int64("rk2", 5))), |
| 223 |
WithResource(resource.NewSchemaless(attribute.String("rk3", "rv3"), attribute.Int64("rk4", 10))), |
| 224 |
}, |
| 225 |
want: mergeResource( |
| 226 |
t, |
| 227 |
resource.Environment(), |
| 228 |
resource.NewSchemaless(attribute.String("rk3", "rv3"), attribute.Int64("rk4", 10)), |
| 229 |
), |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "overlapping attributes with environment resource", |
| 233 |
options: []LoggerProviderOption{ |
| 234 |
WithResource(resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk5", 10))), |
| 235 |
}, |
| 236 |
want: mergeResource( |
| 237 |
t, |
| 238 |
resource.Environment(), |
| 239 |
resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk5", 10)), |
| 240 |
), |
| 241 |
}, |
| 242 |
} |
| 243 |
for _, tc := range cases { |
| 244 |
t.Run(tc.name, func(t *testing.T) { |
| 245 |
got := newProviderConfig(tc.options).resource |
| 246 |
if diff := cmp.Diff(got, tc.want); diff != "" { |
| 247 |
t.Errorf("WithResource:\n -got +want %s", diff) |
| 248 |
} |
| 249 |
}) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func TestLoggerProviderConcurrentSafe(t *testing.T) { |
| 254 |
const goRoutineN = 10 |
| 255 |
|
| 256 |
var wg sync.WaitGroup |
| 257 |
wg.Add(goRoutineN) |
| 258 |
|
| 259 |
p := NewLoggerProvider(WithProcessor(newProcessor("0"))) |
| 260 |
const name = "testLogger" |
| 261 |
ctx := t.Context() |
| 262 |
for range goRoutineN { |
| 263 |
go func() { |
| 264 |
defer wg.Done() |
| 265 |
|
| 266 |
_ = p.Logger(name) |
| 267 |
_ = p.Shutdown(ctx) |
| 268 |
_ = p.ForceFlush(ctx) |
| 269 |
}() |
| 270 |
} |
| 271 |
|
| 272 |
wg.Wait() |
| 273 |
} |
| 274 |
|
| 275 |
type logSink struct { |
| 276 |
logr.LogSink |
| 277 |
|
| 278 |
level int |
| 279 |
msg string |
| 280 |
keysAndValues []any |
| 281 |
} |
| 282 |
|
| 283 |
func (*logSink) Enabled(int) bool { return true } |
| 284 |
|
| 285 |
func (l *logSink) Info(level int, msg string, keysAndValues ...any) { |
| 286 |
l.level, l.msg, l.keysAndValues = level, msg, keysAndValues |
| 287 |
l.LogSink.Info(level, msg, keysAndValues...) |
| 288 |
} |
| 289 |
|
| 290 |
func TestLoggerProviderLogger(t *testing.T) { |
| 291 |
t.Run("InvalidName", func(t *testing.T) { |
| 292 |
l := &logSink{LogSink: testr.New(t).GetSink()} |
| 293 |
t.Cleanup(func(orig logr.Logger) func() { |
| 294 |
global.SetLogger(logr.New(l)) |
| 295 |
return func() { global.SetLogger(orig) } |
| 296 |
}(global.GetLogger())) |
| 297 |
|
| 298 |
_ = NewLoggerProvider().Logger("") |
| 299 |
assert.Equal(t, 1, l.level, "logged level") |
| 300 |
assert.Equal(t, "Invalid Logger name.", l.msg, "logged message") |
| 301 |
require.Len(t, l.keysAndValues, 2, "logged key values") |
| 302 |
assert.Empty(t, l.keysAndValues[1], "logged name") |
| 303 |
}) |
| 304 |
|
| 305 |
t.Run("Stopped", func(t *testing.T) { |
| 306 |
ctx := t.Context() |
| 307 |
p := NewLoggerProvider() |
| 308 |
_ = p.Shutdown(ctx) |
| 309 |
l := p.Logger("testing") |
| 310 |
|
| 311 |
assert.NotNil(t, l) |
| 312 |
assert.IsType(t, noop.Logger{}, l) |
| 313 |
}) |
| 314 |
|
| 315 |
t.Run("SameLoggers", func(t *testing.T) { |
| 316 |
p := NewLoggerProvider() |
| 317 |
|
| 318 |
l0, l1, l2 := p.Logger( |
| 319 |
"l0", |
| 320 |
), p.Logger( |
| 321 |
"l1", |
| 322 |
), p.Logger( |
| 323 |
"l0", |
| 324 |
log.WithInstrumentationAttributes(attribute.String("foo", "bar")), |
| 325 |
) |
| 326 |
assert.NotSame(t, l0, l1) |
| 327 |
assert.NotSame(t, l0, l2) |
| 328 |
assert.NotSame(t, l1, l2) |
| 329 |
|
| 330 |
l3, l4, l5 := p.Logger( |
| 331 |
"l0", |
| 332 |
), p.Logger( |
| 333 |
"l1", |
| 334 |
), p.Logger( |
| 335 |
"l0", |
| 336 |
log.WithInstrumentationAttributes(attribute.String("foo", "bar")), |
| 337 |
) |
| 338 |
assert.Same(t, l0, l3) |
| 339 |
assert.Same(t, l1, l4) |
| 340 |
assert.Same(t, l2, l5) |
| 341 |
}) |
| 342 |
} |
| 343 |
|
| 344 |
func TestLoggerProviderShutdown(t *testing.T) { |
| 345 |
t.Run("Once", func(t *testing.T) { |
| 346 |
proc := newProcessor("") |
| 347 |
p := NewLoggerProvider(WithProcessor(proc)) |
| 348 |
|
| 349 |
ctx := t.Context() |
| 350 |
require.NoError(t, p.Shutdown(ctx)) |
| 351 |
require.Equal(t, 1, proc.shutdownCalls, "processor Shutdown not called") |
| 352 |
|
| 353 |
require.NoError(t, p.Shutdown(ctx)) |
| 354 |
assert.Equal(t, 1, proc.shutdownCalls, "processor Shutdown called multiple times") |
| 355 |
}) |
| 356 |
|
| 357 |
t.Run("Error", func(t *testing.T) { |
| 358 |
proc := newProcessor("") |
| 359 |
proc.Err = assert.AnError |
| 360 |
p := NewLoggerProvider(WithProcessor(proc)) |
| 361 |
|
| 362 |
ctx := t.Context() |
| 363 |
assert.ErrorIs(t, p.Shutdown(ctx), assert.AnError, "processor error not returned") |
| 364 |
}) |
| 365 |
} |
| 366 |
|
| 367 |
func TestLoggerProviderForceFlush(t *testing.T) { |
| 368 |
t.Run("Stopped", func(t *testing.T) { |
| 369 |
proc := newProcessor("") |
| 370 |
p := NewLoggerProvider(WithProcessor(proc)) |
| 371 |
|
| 372 |
ctx := t.Context() |
| 373 |
require.NoError(t, p.ForceFlush(ctx)) |
| 374 |
require.Equal(t, 1, proc.forceFlushCalls, "processor ForceFlush not called") |
| 375 |
|
| 376 |
require.NoError(t, p.Shutdown(ctx)) |
| 377 |
|
| 378 |
require.NoError(t, p.ForceFlush(ctx)) |
| 379 |
assert.Equal(t, 1, proc.forceFlushCalls, "processor ForceFlush called after Shutdown") |
| 380 |
}) |
| 381 |
|
| 382 |
t.Run("Multi", func(t *testing.T) { |
| 383 |
proc := newProcessor("") |
| 384 |
p := NewLoggerProvider(WithProcessor(proc)) |
| 385 |
|
| 386 |
ctx := t.Context() |
| 387 |
require.NoError(t, p.ForceFlush(ctx)) |
| 388 |
require.Equal(t, 1, proc.forceFlushCalls, "processor ForceFlush not called") |
| 389 |
|
| 390 |
require.NoError(t, p.ForceFlush(ctx)) |
| 391 |
assert.Equal(t, 2, proc.forceFlushCalls, "processor ForceFlush not called multiple times") |
| 392 |
}) |
| 393 |
|
| 394 |
t.Run("Error", func(t *testing.T) { |
| 395 |
proc := newProcessor("") |
| 396 |
proc.Err = assert.AnError |
| 397 |
p := NewLoggerProvider(WithProcessor(proc)) |
| 398 |
|
| 399 |
ctx := t.Context() |
| 400 |
assert.ErrorIs(t, p.ForceFlush(ctx), assert.AnError, "processor error not returned") |
| 401 |
}) |
| 402 |
} |
| 403 |
|
| 404 |
func BenchmarkLoggerProviderLogger(b *testing.B) { |
| 405 |
p := NewLoggerProvider() |
| 406 |
names := make([]string, b.N) |
| 407 |
for i := 0; i < b.N; i++ { |
| 408 |
names[i] = fmt.Sprintf("%d logger", i) |
| 409 |
} |
| 410 |
|
| 411 |
b.ResetTimer() |
| 412 |
b.ReportAllocs() |
| 413 |
|
| 414 |
loggers := make([]log.Logger, b.N) |
| 415 |
for i := 0; i < b.N; i++ { |
| 416 |
loggers[i] = p.Logger(names[i]) |
| 417 |
} |
| 418 |
|
| 419 |
b.StopTimer() |
| 420 |
loggers[0].Enabled(b.Context(), log.EnabledParameters{}) |
| 421 |
} |