2024-03-13 17:47:07 +01:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package log // import "go.opentelemetry.io/otel/sdk/log"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-22 07:39:01 -07:00
|
|
|
"time"
|
2024-03-13 17:47:07 +01:00
|
|
|
|
2024-03-18 16:07:43 -07:00
|
|
|
"go.opentelemetry.io/otel"
|
2024-03-13 17:47:07 +01:00
|
|
|
"go.opentelemetry.io/otel/log"
|
|
|
|
"go.opentelemetry.io/otel/log/embedded"
|
2024-03-17 22:57:43 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2024-08-22 09:12:23 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/log/internal/x"
|
2024-03-18 16:07:43 -07:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-03-13 17:47:07 +01:00
|
|
|
)
|
|
|
|
|
2024-03-22 07:39:01 -07:00
|
|
|
var now = time.Now
|
|
|
|
|
2024-03-13 17:47:07 +01:00
|
|
|
// Compile-time check logger implements log.Logger.
|
|
|
|
var _ log.Logger = (*logger)(nil)
|
|
|
|
|
|
|
|
type logger struct {
|
|
|
|
embedded.Logger
|
2024-03-18 16:07:43 -07:00
|
|
|
|
|
|
|
provider *LoggerProvider
|
|
|
|
instrumentationScope instrumentation.Scope
|
2024-03-13 17:47:07 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:57:43 -07:00
|
|
|
func newLogger(p *LoggerProvider, scope instrumentation.Scope) *logger {
|
2024-03-18 16:07:43 -07:00
|
|
|
return &logger{
|
|
|
|
provider: p,
|
|
|
|
instrumentationScope: scope,
|
|
|
|
}
|
2024-03-17 22:57:43 -07:00
|
|
|
}
|
|
|
|
|
2024-03-13 17:47:07 +01:00
|
|
|
func (l *logger) Emit(ctx context.Context, r log.Record) {
|
2024-03-18 16:07:43 -07:00
|
|
|
newRecord := l.newRecord(ctx, r)
|
|
|
|
for _, p := range l.provider.processors {
|
2024-08-01 10:13:43 +02:00
|
|
|
if err := p.OnEmit(ctx, &newRecord); err != nil {
|
2024-03-18 16:07:43 -07:00
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
}
|
2024-03-13 17:47:07 +01:00
|
|
|
}
|
2024-03-15 08:15:44 -07:00
|
|
|
|
2024-08-22 09:12:23 -07:00
|
|
|
// Enabled returns true if at least one Processor held by the LoggerProvider
|
2024-09-13 06:35:01 +02:00
|
|
|
// that created the logger will process param for the provided context and param.
|
2024-08-22 09:12:23 -07:00
|
|
|
//
|
2024-09-13 06:35:01 +02:00
|
|
|
// If it is not possible to definitively determine the param will be
|
2024-08-22 09:12:23 -07:00
|
|
|
// processed, true will be returned by default. A value of false will only be
|
2024-09-13 06:35:01 +02:00
|
|
|
// returned if it can be positively verified that no Processor will process.
|
|
|
|
func (l *logger) Enabled(ctx context.Context, param log.EnabledParameters) bool {
|
2024-08-22 09:12:23 -07:00
|
|
|
fltrs := l.provider.filterProcessors()
|
|
|
|
// If there are more Processors than FilterProcessors we cannot be sure
|
|
|
|
// that all Processors will drop the record. Therefore, return true.
|
|
|
|
//
|
|
|
|
// If all Processors are FilterProcessors, check if any is enabled.
|
2024-09-13 06:35:01 +02:00
|
|
|
return len(l.provider.processors) > len(fltrs) || anyEnabled(ctx, param, fltrs)
|
2024-08-22 09:12:23 -07:00
|
|
|
}
|
|
|
|
|
2024-09-13 06:35:01 +02:00
|
|
|
func anyEnabled(ctx context.Context, param log.EnabledParameters, fltrs []x.FilterProcessor) bool {
|
2024-08-22 09:12:23 -07:00
|
|
|
for _, f := range fltrs {
|
2024-09-13 06:35:01 +02:00
|
|
|
if f.Enabled(ctx, param) {
|
2024-08-22 09:12:23 -07:00
|
|
|
// At least one Processor will process the Record.
|
2024-03-18 16:07:43 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 09:12:23 -07:00
|
|
|
// No Processor will process the record
|
2024-03-18 16:07:43 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) newRecord(ctx context.Context, r log.Record) Record {
|
|
|
|
sc := trace.SpanContextFromContext(ctx)
|
|
|
|
|
|
|
|
newRecord := Record{
|
|
|
|
timestamp: r.Timestamp(),
|
|
|
|
observedTimestamp: r.ObservedTimestamp(),
|
|
|
|
severity: r.Severity(),
|
|
|
|
severityText: r.SeverityText(),
|
|
|
|
body: r.Body(),
|
|
|
|
|
|
|
|
traceID: sc.TraceID(),
|
|
|
|
spanID: sc.SpanID(),
|
|
|
|
traceFlags: sc.TraceFlags(),
|
|
|
|
|
|
|
|
resource: l.provider.resource,
|
|
|
|
scope: &l.instrumentationScope,
|
|
|
|
attributeValueLengthLimit: l.provider.attributeValueLengthLimit,
|
|
|
|
attributeCountLimit: l.provider.attributeCountLimit,
|
|
|
|
}
|
2024-03-22 07:39:01 -07:00
|
|
|
|
|
|
|
// This field SHOULD be set once the event is observed by OpenTelemetry.
|
|
|
|
if newRecord.observedTimestamp.IsZero() {
|
|
|
|
newRecord.observedTimestamp = now()
|
|
|
|
}
|
|
|
|
|
2024-03-18 16:07:43 -07:00
|
|
|
r.WalkAttributes(func(kv log.KeyValue) bool {
|
|
|
|
newRecord.AddAttributes(kv)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return newRecord
|
2024-03-15 08:15:44 -07:00
|
|
|
}
|