| 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 |
"errors" |
| 9 |
"strconv" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel" |
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/log" |
| 19 |
"go.opentelemetry.io/otel/metric" |
| 20 |
"go.opentelemetry.io/otel/sdk" |
| 21 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 22 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 23 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 24 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 25 |
"go.opentelemetry.io/otel/sdk/resource" |
| 26 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 27 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 28 |
"go.opentelemetry.io/otel/trace" |
| 29 |
) |
| 30 |
|
| 31 |
func TestLoggerEmit(t *testing.T) { |
| 32 |
nowDate := time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 33 |
|
| 34 |
nowSwap := now |
| 35 |
t.Cleanup(func() { |
| 36 |
now = nowSwap |
| 37 |
}) |
| 38 |
now = func() time.Time { |
| 39 |
return nowDate |
| 40 |
} |
| 41 |
|
| 42 |
p0, p1, p2WithError := newProcessor("0"), newProcessor("1"), newProcessor("2") |
| 43 |
p2WithError.Err = errors.New("error") |
| 44 |
|
| 45 |
r := log.Record{} |
| 46 |
r.SetEventName("testing.name") |
| 47 |
r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)) |
| 48 |
r.SetBody(log.StringValue("testing body value")) |
| 49 |
r.SetSeverity(log.SeverityInfo) |
| 50 |
r.SetSeverityText("testing text") |
| 51 |
r.AddAttributes( |
| 52 |
log.String("k1", "str"), |
| 53 |
log.Float64("k2", 1.0), |
| 54 |
) |
| 55 |
r.SetObservedTimestamp(time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)) |
| 56 |
|
| 57 |
rWithNoObservedTimestamp := r |
| 58 |
rWithNoObservedTimestamp.SetObservedTimestamp(time.Time{}) |
| 59 |
|
| 60 |
rWithAllowKeyDuplication := r |
| 61 |
rWithAllowKeyDuplication.AddAttributes( |
| 62 |
log.String("k1", "str1"), |
| 63 |
) |
| 64 |
rWithAllowKeyDuplication.SetBody(log.MapValue( |
| 65 |
log.Int64("1", 2), |
| 66 |
log.Int64("1", 3), |
| 67 |
)) |
| 68 |
|
| 69 |
rWithDuplicatesInBody := r |
| 70 |
rWithDuplicatesInBody.SetBody(log.MapValue( |
| 71 |
log.Int64("1", 2), |
| 72 |
log.Int64("1", 3), |
| 73 |
)) |
| 74 |
|
| 75 |
contextWithSpanContext := trace.ContextWithSpanContext( |
| 76 |
t.Context(), |
| 77 |
trace.NewSpanContext(trace.SpanContextConfig{ |
| 78 |
TraceID: trace.TraceID{0o1}, |
| 79 |
SpanID: trace.SpanID{0o2}, |
| 80 |
TraceFlags: 0x1, |
| 81 |
}), |
| 82 |
) |
| 83 |
|
| 84 |
testCases := []struct { |
| 85 |
name string |
| 86 |
logger *logger |
| 87 |
ctx context.Context |
| 88 |
record log.Record |
| 89 |
expectedRecords []Record |
| 90 |
}{ |
| 91 |
{ |
| 92 |
name: "NoProcessors", |
| 93 |
logger: newLogger(NewLoggerProvider(), instrumentation.Scope{}), |
| 94 |
ctx: t.Context(), |
| 95 |
record: r, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "WithProcessors", |
| 99 |
logger: newLogger(NewLoggerProvider( |
| 100 |
WithProcessor(p0), |
| 101 |
WithProcessor(p1), |
| 102 |
WithAttributeValueLengthLimit(3), |
| 103 |
WithAttributeCountLimit(2), |
| 104 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 105 |
), instrumentation.Scope{Name: "scope"}), |
| 106 |
ctx: t.Context(), |
| 107 |
record: r, |
| 108 |
expectedRecords: []Record{ |
| 109 |
{ |
| 110 |
eventName: r.EventName(), |
| 111 |
timestamp: r.Timestamp(), |
| 112 |
body: r.Body(), |
| 113 |
severity: r.Severity(), |
| 114 |
severityText: r.SeverityText(), |
| 115 |
observedTimestamp: r.ObservedTimestamp(), |
| 116 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 117 |
attributeValueLengthLimit: 3, |
| 118 |
attributeCountLimit: 2, |
| 119 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 120 |
front: [attributesInlineCount]log.KeyValue{ |
| 121 |
log.String("k1", "str"), |
| 122 |
log.Float64("k2", 1.0), |
| 123 |
}, |
| 124 |
nFront: 2, |
| 125 |
}, |
| 126 |
}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "WithProcessorsWithError", |
| 130 |
logger: newLogger(NewLoggerProvider( |
| 131 |
WithProcessor(p2WithError), |
| 132 |
WithAttributeValueLengthLimit(3), |
| 133 |
WithAttributeCountLimit(2), |
| 134 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 135 |
), instrumentation.Scope{Name: "scope"}), |
| 136 |
ctx: t.Context(), |
| 137 |
}, |
| 138 |
{ |
| 139 |
name: "WithTraceSpanInContext", |
| 140 |
logger: newLogger(NewLoggerProvider( |
| 141 |
WithProcessor(p0), |
| 142 |
WithProcessor(p1), |
| 143 |
WithAttributeValueLengthLimit(3), |
| 144 |
WithAttributeCountLimit(2), |
| 145 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 146 |
), instrumentation.Scope{Name: "scope"}), |
| 147 |
ctx: contextWithSpanContext, |
| 148 |
record: r, |
| 149 |
expectedRecords: []Record{ |
| 150 |
{ |
| 151 |
eventName: r.EventName(), |
| 152 |
timestamp: r.Timestamp(), |
| 153 |
body: r.Body(), |
| 154 |
severity: r.Severity(), |
| 155 |
severityText: r.SeverityText(), |
| 156 |
observedTimestamp: r.ObservedTimestamp(), |
| 157 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 158 |
attributeValueLengthLimit: 3, |
| 159 |
attributeCountLimit: 2, |
| 160 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 161 |
front: [attributesInlineCount]log.KeyValue{ |
| 162 |
log.String("k1", "str"), |
| 163 |
log.Float64("k2", 1.0), |
| 164 |
}, |
| 165 |
nFront: 2, |
| 166 |
traceID: trace.TraceID{0o1}, |
| 167 |
spanID: trace.SpanID{0o2}, |
| 168 |
traceFlags: 0x1, |
| 169 |
}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "WithNilContext", |
| 174 |
logger: newLogger(NewLoggerProvider( |
| 175 |
WithProcessor(p0), |
| 176 |
WithProcessor(p1), |
| 177 |
WithAttributeValueLengthLimit(3), |
| 178 |
WithAttributeCountLimit(2), |
| 179 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 180 |
), instrumentation.Scope{Name: "scope"}), |
| 181 |
ctx: t.Context(), |
| 182 |
record: r, |
| 183 |
expectedRecords: []Record{ |
| 184 |
{ |
| 185 |
eventName: r.EventName(), |
| 186 |
timestamp: r.Timestamp(), |
| 187 |
body: r.Body(), |
| 188 |
severity: r.Severity(), |
| 189 |
severityText: r.SeverityText(), |
| 190 |
observedTimestamp: r.ObservedTimestamp(), |
| 191 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 192 |
attributeValueLengthLimit: 3, |
| 193 |
attributeCountLimit: 2, |
| 194 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 195 |
front: [attributesInlineCount]log.KeyValue{ |
| 196 |
log.String("k1", "str"), |
| 197 |
log.Float64("k2", 1.0), |
| 198 |
}, |
| 199 |
nFront: 2, |
| 200 |
}, |
| 201 |
}, |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "NoObservedTimestamp", |
| 205 |
logger: newLogger(NewLoggerProvider( |
| 206 |
WithProcessor(p0), |
| 207 |
WithProcessor(p1), |
| 208 |
WithAttributeValueLengthLimit(3), |
| 209 |
WithAttributeCountLimit(2), |
| 210 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 211 |
), instrumentation.Scope{Name: "scope"}), |
| 212 |
ctx: t.Context(), |
| 213 |
record: rWithNoObservedTimestamp, |
| 214 |
expectedRecords: []Record{ |
| 215 |
{ |
| 216 |
eventName: rWithNoObservedTimestamp.EventName(), |
| 217 |
timestamp: rWithNoObservedTimestamp.Timestamp(), |
| 218 |
body: rWithNoObservedTimestamp.Body(), |
| 219 |
severity: rWithNoObservedTimestamp.Severity(), |
| 220 |
severityText: rWithNoObservedTimestamp.SeverityText(), |
| 221 |
observedTimestamp: nowDate, |
| 222 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 223 |
attributeValueLengthLimit: 3, |
| 224 |
attributeCountLimit: 2, |
| 225 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 226 |
front: [attributesInlineCount]log.KeyValue{ |
| 227 |
log.String("k1", "str"), |
| 228 |
log.Float64("k2", 1.0), |
| 229 |
}, |
| 230 |
nFront: 2, |
| 231 |
}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
{ |
| 235 |
name: "WithAllowKeyDuplication", |
| 236 |
logger: newLogger(NewLoggerProvider( |
| 237 |
WithProcessor(p0), |
| 238 |
WithProcessor(p1), |
| 239 |
WithAttributeValueLengthLimit(5), |
| 240 |
WithAttributeCountLimit(5), |
| 241 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 242 |
WithAllowKeyDuplication(), |
| 243 |
), instrumentation.Scope{Name: "scope"}), |
| 244 |
ctx: t.Context(), |
| 245 |
record: rWithAllowKeyDuplication, |
| 246 |
expectedRecords: []Record{ |
| 247 |
{ |
| 248 |
eventName: rWithAllowKeyDuplication.EventName(), |
| 249 |
timestamp: rWithAllowKeyDuplication.Timestamp(), |
| 250 |
body: rWithAllowKeyDuplication.Body(), |
| 251 |
severity: rWithAllowKeyDuplication.Severity(), |
| 252 |
severityText: rWithAllowKeyDuplication.SeverityText(), |
| 253 |
observedTimestamp: rWithAllowKeyDuplication.ObservedTimestamp(), |
| 254 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 255 |
attributeValueLengthLimit: 5, |
| 256 |
attributeCountLimit: 5, |
| 257 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 258 |
front: [attributesInlineCount]log.KeyValue{ |
| 259 |
log.String("k1", "str"), |
| 260 |
log.Float64("k2", 1.0), |
| 261 |
log.String("k1", "str1"), |
| 262 |
}, |
| 263 |
nFront: 3, |
| 264 |
allowDupKeys: true, |
| 265 |
}, |
| 266 |
}, |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "WithDuplicatesInBody", |
| 270 |
logger: newLogger(NewLoggerProvider( |
| 271 |
WithProcessor(p0), |
| 272 |
WithProcessor(p1), |
| 273 |
WithAttributeValueLengthLimit(5), |
| 274 |
WithAttributeCountLimit(5), |
| 275 |
WithResource(resource.NewSchemaless(attribute.String("key", "value"))), |
| 276 |
), instrumentation.Scope{Name: "scope"}), |
| 277 |
ctx: t.Context(), |
| 278 |
record: rWithDuplicatesInBody, |
| 279 |
expectedRecords: []Record{ |
| 280 |
{ |
| 281 |
eventName: rWithDuplicatesInBody.EventName(), |
| 282 |
timestamp: rWithDuplicatesInBody.Timestamp(), |
| 283 |
body: log.MapValue( |
| 284 |
log.Int64("1", 3), |
| 285 |
), |
| 286 |
severity: rWithDuplicatesInBody.Severity(), |
| 287 |
severityText: rWithDuplicatesInBody.SeverityText(), |
| 288 |
observedTimestamp: rWithDuplicatesInBody.ObservedTimestamp(), |
| 289 |
resource: resource.NewSchemaless(attribute.String("key", "value")), |
| 290 |
attributeValueLengthLimit: 5, |
| 291 |
attributeCountLimit: 5, |
| 292 |
scope: &instrumentation.Scope{Name: "scope"}, |
| 293 |
front: [attributesInlineCount]log.KeyValue{ |
| 294 |
log.String("k1", "str"), |
| 295 |
log.Float64("k2", 1.0), |
| 296 |
}, |
| 297 |
nFront: 2, |
| 298 |
}, |
| 299 |
}, |
| 300 |
}, |
| 301 |
} |
| 302 |
|
| 303 |
for _, tc := range testCases { |
| 304 |
t.Run(tc.name, func(t *testing.T) { |
| 305 |
// Clean up the records before the test. |
| 306 |
p0.records = nil |
| 307 |
p1.records = nil |
| 308 |
|
| 309 |
tc.logger.Emit(tc.ctx, tc.record) |
| 310 |
|
| 311 |
assert.Equal(t, tc.expectedRecords, p0.records) |
| 312 |
assert.Equal(t, tc.expectedRecords, p1.records) |
| 313 |
}) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
func TestNewRecordAddsExceptionAttrs(t *testing.T) { |
| 318 |
l := newLogger(NewLoggerProvider(), instrumentation.Scope{}) |
| 319 |
|
| 320 |
t.Run("AddsMissing", func(t *testing.T) { |
| 321 |
var in log.Record |
| 322 |
in.SetBody(log.StringValue("boom")) |
| 323 |
in.SetSeverity(log.SeverityError) |
| 324 |
in.SetErr(errors.New("boom")) |
| 325 |
got := l.newRecord(t.Context(), in) |
| 326 |
|
| 327 |
var gotAttrs []log.KeyValue |
| 328 |
got.WalkAttributes(func(kv log.KeyValue) bool { |
| 329 |
gotAttrs = append(gotAttrs, kv) |
| 330 |
return true |
| 331 |
}) |
| 332 |
|
| 333 |
assert.Len(t, gotAttrs, 2) |
| 334 |
assert.Contains(t, gotAttrs, log.String(string(semconv.ExceptionTypeKey), "*errors.errorString")) |
| 335 |
assert.Contains(t, gotAttrs, log.String(string(semconv.ExceptionMessageKey), "boom")) |
| 336 |
}) |
| 337 |
|
| 338 |
t.Run("ShortCircuitsAtAttributeLimit", func(t *testing.T) { |
| 339 |
var in log.Record |
| 340 |
in.SetBody(log.StringValue("boom")) |
| 341 |
in.SetSeverity(log.SeverityError) |
| 342 |
in.SetErr(errors.New("boom")) |
| 343 |
in.AddAttributes(log.String("k1", "v1")) |
| 344 |
|
| 345 |
lLimited := newLogger(NewLoggerProvider(WithAttributeCountLimit(2)), instrumentation.Scope{}) |
| 346 |
got := lLimited.newRecord(t.Context(), in) |
| 347 |
|
| 348 |
var gotType, gotMessage string |
| 349 |
got.WalkAttributes(func(kv log.KeyValue) bool { |
| 350 |
switch kv.Key { |
| 351 |
case string(semconv.ExceptionTypeKey): |
| 352 |
gotType = kv.Value.AsString() |
| 353 |
case string(semconv.ExceptionMessageKey): |
| 354 |
gotMessage = kv.Value.AsString() |
| 355 |
} |
| 356 |
return true |
| 357 |
}) |
| 358 |
|
| 359 |
assert.Empty(t, gotType) |
| 360 |
assert.Equal(t, "boom", gotMessage) |
| 361 |
}) |
| 362 |
|
| 363 |
t.Run("NoSlotsLeft", func(t *testing.T) { |
| 364 |
var in log.Record |
| 365 |
in.SetBody(log.StringValue("boom")) |
| 366 |
in.SetSeverity(log.SeverityError) |
| 367 |
in.SetErr(errors.New("boom")) |
| 368 |
in.AddAttributes(log.String("k1", "v1")) |
| 369 |
lLimited := newLogger(NewLoggerProvider(WithAttributeCountLimit(1)), instrumentation.Scope{}) |
| 370 |
got := lLimited.newRecord(t.Context(), in) |
| 371 |
|
| 372 |
var gotType, gotMessage string |
| 373 |
got.WalkAttributes(func(kv log.KeyValue) bool { |
| 374 |
switch kv.Key { |
| 375 |
case string(semconv.ExceptionTypeKey): |
| 376 |
gotType = kv.Value.AsString() |
| 377 |
case string(semconv.ExceptionMessageKey): |
| 378 |
gotMessage = kv.Value.AsString() |
| 379 |
} |
| 380 |
return true |
| 381 |
}) |
| 382 |
|
| 383 |
assert.Empty(t, gotType) |
| 384 |
assert.Empty(t, gotMessage) |
| 385 |
}) |
| 386 |
} |
| 387 |
|
| 388 |
func TestErrorType(t *testing.T) { |
| 389 |
t.Run("UsesErrorTypeMethod", func(t *testing.T) { |
| 390 |
err := errWithType{msg: "boom", typ: "custom.type"} |
| 391 |
assert.Equal(t, "custom.type", errorType(err)) |
| 392 |
}) |
| 393 |
|
| 394 |
t.Run("FallsBackWhenErrorTypeEmpty", func(t *testing.T) { |
| 395 |
err := errWithType{msg: "boom", typ: ""} |
| 396 |
assert.Equal(t, "go.opentelemetry.io/otel/sdk/log.errWithType", errorType(err)) |
| 397 |
}) |
| 398 |
|
| 399 |
t.Run("NilError", func(t *testing.T) { |
| 400 |
assert.Empty(t, errorType(nil)) |
| 401 |
}) |
| 402 |
|
| 403 |
t.Run("UnnamedType", func(t *testing.T) { |
| 404 |
var err error = struct{ baseErr }{} |
| 405 |
assert.Contains(t, errorType(err), "struct") |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
type errWithType struct { |
| 410 |
msg string |
| 411 |
typ string |
| 412 |
} |
| 413 |
|
| 414 |
func (e errWithType) Error() string { return e.msg } |
| 415 |
|
| 416 |
func (e errWithType) ErrorType() string { return e.typ } |
| 417 |
|
| 418 |
type baseErr struct{} |
| 419 |
|
| 420 |
func (baseErr) Error() string { return "boom" } |
| 421 |
|
| 422 |
func TestNewRecordSkipsExceptionWhenPresent(t *testing.T) { |
| 423 |
l := newLogger(NewLoggerProvider(), instrumentation.Scope{}) |
| 424 |
|
| 425 |
t.Run("ExistingMessage", func(t *testing.T) { |
| 426 |
var r log.Record |
| 427 |
r.SetBody(log.StringValue("boom")) |
| 428 |
r.SetSeverity(log.SeverityError) |
| 429 |
r.SetErr(errors.New("boom")) |
| 430 |
r.AddAttributes(log.String(string(semconv.ExceptionMessageKey), "existing.message")) |
| 431 |
|
| 432 |
got := l.newRecord(t.Context(), r) |
| 433 |
|
| 434 |
var gotType, gotMessage string |
| 435 |
got.WalkAttributes(func(kv log.KeyValue) bool { |
| 436 |
switch kv.Key { |
| 437 |
case string(semconv.ExceptionTypeKey): |
| 438 |
gotType = kv.Value.AsString() |
| 439 |
case string(semconv.ExceptionMessageKey): |
| 440 |
gotMessage = kv.Value.AsString() |
| 441 |
} |
| 442 |
return true |
| 443 |
}) |
| 444 |
|
| 445 |
assert.Equal(t, "existing.message", gotMessage) |
| 446 |
assert.Empty(t, gotType) |
| 447 |
}) |
| 448 |
|
| 449 |
t.Run("ExistingType", func(t *testing.T) { |
| 450 |
var r log.Record |
| 451 |
r.SetBody(log.StringValue("boom")) |
| 452 |
r.SetSeverity(log.SeverityError) |
| 453 |
r.SetErr(errors.New("boom")) |
| 454 |
r.AddAttributes(log.String(string(semconv.ExceptionTypeKey), "existing.type")) |
| 455 |
|
| 456 |
got := l.newRecord(t.Context(), r) |
| 457 |
|
| 458 |
var gotType, gotMessage string |
| 459 |
got.WalkAttributes(func(kv log.KeyValue) bool { |
| 460 |
switch kv.Key { |
| 461 |
case string(semconv.ExceptionTypeKey): |
| 462 |
gotType = kv.Value.AsString() |
| 463 |
case string(semconv.ExceptionMessageKey): |
| 464 |
gotMessage = kv.Value.AsString() |
| 465 |
} |
| 466 |
return true |
| 467 |
}) |
| 468 |
|
| 469 |
assert.Equal(t, "existing.type", gotType) |
| 470 |
assert.Empty(t, gotMessage) |
| 471 |
}) |
| 472 |
} |
| 473 |
|
| 474 |
func TestLoggerEnabled(t *testing.T) { |
| 475 |
p0 := newFltrProcessor("0", true) |
| 476 |
p1 := newFltrProcessor("1", true) |
| 477 |
p2WithDisabled := newFltrProcessor("2", false) |
| 478 |
|
| 479 |
testCases := []struct { |
| 480 |
name string |
| 481 |
logger *logger |
| 482 |
ctx context.Context |
| 483 |
param log.EnabledParameters |
| 484 |
expected bool |
| 485 |
expectedP0Params []EnabledParameters |
| 486 |
expectedP1Params []EnabledParameters |
| 487 |
expectedP2Params []EnabledParameters |
| 488 |
}{ |
| 489 |
{ |
| 490 |
name: "NoProcessors", |
| 491 |
logger: newLogger(NewLoggerProvider(), instrumentation.Scope{}), |
| 492 |
ctx: t.Context(), |
| 493 |
expected: false, |
| 494 |
}, |
| 495 |
{ |
| 496 |
name: "WithProcessors", |
| 497 |
logger: newLogger(NewLoggerProvider( |
| 498 |
WithProcessor(p0), |
| 499 |
WithProcessor(p1), |
| 500 |
), instrumentation.Scope{Name: "scope"}), |
| 501 |
ctx: t.Context(), |
| 502 |
param: log.EnabledParameters{ |
| 503 |
Severity: log.SeverityInfo, |
| 504 |
EventName: "test_event", |
| 505 |
}, |
| 506 |
expected: true, |
| 507 |
expectedP0Params: []EnabledParameters{{ |
| 508 |
InstrumentationScope: instrumentation.Scope{Name: "scope"}, |
| 509 |
Severity: log.SeverityInfo, |
| 510 |
EventName: "test_event", |
| 511 |
}}, |
| 512 |
expectedP1Params: nil, |
| 513 |
}, |
| 514 |
{ |
| 515 |
name: "WithDisabledProcessors", |
| 516 |
logger: newLogger(NewLoggerProvider( |
| 517 |
WithProcessor(p2WithDisabled), |
| 518 |
), instrumentation.Scope{}), |
| 519 |
ctx: t.Context(), |
| 520 |
expected: false, |
| 521 |
expectedP2Params: []EnabledParameters{{}}, |
| 522 |
}, |
| 523 |
{ |
| 524 |
name: "ContainsDisabledProcessor", |
| 525 |
logger: newLogger(NewLoggerProvider( |
| 526 |
WithProcessor(p2WithDisabled), |
| 527 |
WithProcessor(p0), |
| 528 |
), instrumentation.Scope{}), |
| 529 |
ctx: t.Context(), |
| 530 |
expected: true, |
| 531 |
expectedP2Params: []EnabledParameters{{}}, |
| 532 |
expectedP0Params: []EnabledParameters{{}}, |
| 533 |
}, |
| 534 |
{ |
| 535 |
name: "WithNilContext", |
| 536 |
logger: newLogger(NewLoggerProvider( |
| 537 |
WithProcessor(p0), |
| 538 |
WithProcessor(p1), |
| 539 |
), instrumentation.Scope{}), |
| 540 |
ctx: nil, |
| 541 |
expected: true, |
| 542 |
expectedP0Params: []EnabledParameters{{}}, |
| 543 |
expectedP1Params: nil, |
| 544 |
}, |
| 545 |
} |
| 546 |
|
| 547 |
for _, tc := range testCases { |
| 548 |
t.Run(tc.name, func(t *testing.T) { |
| 549 |
// Clean up the records before the test. |
| 550 |
p0.params = nil |
| 551 |
p1.params = nil |
| 552 |
p2WithDisabled.params = nil |
| 553 |
|
| 554 |
assert.Equal(t, tc.expected, tc.logger.Enabled(tc.ctx, tc.param)) |
| 555 |
assert.Equal(t, tc.expectedP0Params, p0.params) |
| 556 |
assert.Equal(t, tc.expectedP1Params, p1.params) |
| 557 |
assert.Equal(t, tc.expectedP2Params, p2WithDisabled.params) |
| 558 |
}) |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
func TestLoggerObservability(t *testing.T) { |
| 563 |
testCases := []struct { |
| 564 |
name string |
| 565 |
enabled bool |
| 566 |
records []log.Record |
| 567 |
wantLogRecordCount int64 |
| 568 |
}{ |
| 569 |
{ |
| 570 |
name: "Disabled", |
| 571 |
enabled: false, |
| 572 |
records: []log.Record{{}, {}}, |
| 573 |
wantLogRecordCount: 0, |
| 574 |
}, |
| 575 |
{ |
| 576 |
name: "Enabled", |
| 577 |
enabled: true, |
| 578 |
records: []log.Record{{}, {}, {}, {}, {}}, |
| 579 |
wantLogRecordCount: 5, |
| 580 |
}, |
| 581 |
} |
| 582 |
|
| 583 |
for _, tc := range testCases { |
| 584 |
t.Run(tc.name, func(t *testing.T) { |
| 585 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", strconv.FormatBool(tc.enabled)) |
| 586 |
prev := otel.GetMeterProvider() |
| 587 |
t.Cleanup(func() { |
| 588 |
otel.SetMeterProvider(prev) |
| 589 |
}) |
| 590 |
r := sdkmetric.NewManualReader() |
| 591 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(r)) |
| 592 |
otel.SetMeterProvider(mp) |
| 593 |
l := newLogger(NewLoggerProvider(), instrumentation.Scope{}) |
| 594 |
|
| 595 |
for _, record := range tc.records { |
| 596 |
l.Emit(t.Context(), record) |
| 597 |
} |
| 598 |
|
| 599 |
gotMetrics := new(metricdata.ResourceMetrics) |
| 600 |
assert.NoError(t, r.Collect(t.Context(), gotMetrics)) |
| 601 |
if tc.wantLogRecordCount == 0 { |
| 602 |
assert.Empty(t, gotMetrics.ScopeMetrics) |
| 603 |
return |
| 604 |
} |
| 605 |
|
| 606 |
require.Len(t, gotMetrics.ScopeMetrics, 1) |
| 607 |
sm := gotMetrics.ScopeMetrics[0] |
| 608 |
assert.Equal(t, instrumentation.Scope{ |
| 609 |
Name: "go.opentelemetry.io/otel/sdk/log", |
| 610 |
Version: sdk.Version(), |
| 611 |
SchemaURL: semconv.SchemaURL, |
| 612 |
}, sm.Scope) |
| 613 |
|
| 614 |
wantMetric := metricdata.Metrics{ |
| 615 |
Name: otelconv.SDKLogCreated{}.Name(), |
| 616 |
Description: otelconv.SDKLogCreated{}.Description(), |
| 617 |
Unit: otelconv.SDKLogCreated{}.Unit(), |
| 618 |
Data: metricdata.Sum[int64]{ |
| 619 |
DataPoints: []metricdata.DataPoint[int64]{{Value: tc.wantLogRecordCount}}, |
| 620 |
Temporality: metricdata.CumulativeTemporality, |
| 621 |
IsMonotonic: true, |
| 622 |
}, |
| 623 |
} |
| 624 |
metricdatatest.AssertEqual(t, wantMetric, sm.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 625 |
}) |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func TestNewLoggerObservabilityErrorHandled(t *testing.T) { |
| 630 |
errHandler := otel.GetErrorHandler() |
| 631 |
t.Cleanup(func() { |
| 632 |
otel.SetErrorHandler(errHandler) |
| 633 |
}) |
| 634 |
|
| 635 |
var errs []error |
| 636 |
eh := otel.ErrorHandlerFunc(func(e error) { errs = append(errs, e) }) |
| 637 |
otel.SetErrorHandler(eh) |
| 638 |
|
| 639 |
orig := otel.GetMeterProvider() |
| 640 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 641 |
otel.SetMeterProvider(&errMeterProvider{err: assert.AnError}) |
| 642 |
|
| 643 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 644 |
l := newLogger(NewLoggerProvider(), instrumentation.Scope{}) |
| 645 |
_ = l |
| 646 |
require.Len(t, errs, 1) |
| 647 |
assert.ErrorIs(t, errs[0], assert.AnError) |
| 648 |
} |
| 649 |
|
| 650 |
type errMeterProvider struct { |
| 651 |
metric.MeterProvider |
| 652 |
|
| 653 |
err error |
| 654 |
} |
| 655 |
|
| 656 |
func (mp *errMeterProvider) Meter(string, ...metric.MeterOption) metric.Meter { |
| 657 |
return &errMeter{err: mp.err} |
| 658 |
} |
| 659 |
|
| 660 |
type errMeter struct { |
| 661 |
metric.Meter |
| 662 |
|
| 663 |
err error |
| 664 |
} |
| 665 |
|
| 666 |
func (m *errMeter) Int64Counter(string, ...metric.Int64CounterOption) (metric.Int64Counter, error) { |
| 667 |
return nil, m.err |
| 668 |
} |
| 669 |
|
| 670 |
func (m *errMeter) Int64UpDownCounter(string, ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) { |
| 671 |
return nil, m.err |
| 672 |
} |