| 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 |
"reflect" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel" |
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
"go.opentelemetry.io/otel/log/embedded" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 16 |
"go.opentelemetry.io/otel/trace" |
| 17 |
) |
| 18 |
|
| 19 |
var now = time.Now |
| 20 |
|
| 21 |
const ( |
| 22 |
exceptionTypeKey = string(semconv.ExceptionTypeKey) |
| 23 |
exceptionMessageKey = string(semconv.ExceptionMessageKey) |
| 24 |
exceptionStacktraceKey = string(semconv.ExceptionStacktraceKey) |
| 25 |
) |
| 26 |
|
| 27 |
// Compile-time check logger implements log.Logger. |
| 28 |
var _ log.Logger = (*logger)(nil) |
| 29 |
|
| 30 |
type logger struct { |
| 31 |
embedded.Logger |
| 32 |
|
| 33 |
provider *LoggerProvider |
| 34 |
instrumentationScope instrumentation.Scope |
| 35 |
|
| 36 |
// recCntIncr increments the count of log records created. It will be nil |
| 37 |
// if observability is disabled. |
| 38 |
recCntIncr func(context.Context) |
| 39 |
} |
| 40 |
|
| 41 |
func newLogger(p *LoggerProvider, scope instrumentation.Scope) *logger { |
| 42 |
l := &logger{ |
| 43 |
provider: p, |
| 44 |
instrumentationScope: scope, |
| 45 |
} |
| 46 |
|
| 47 |
var err error |
| 48 |
l.recCntIncr, err = newRecordCounterIncr() |
| 49 |
if err != nil { |
| 50 |
otel.Handle(err) |
| 51 |
} |
| 52 |
return l |
| 53 |
} |
| 54 |
|
| 55 |
func (l *logger) Emit(ctx context.Context, r log.Record) { |
| 56 |
newRecord := l.newRecord(ctx, r) |
| 57 |
for _, p := range l.provider.processors { |
| 58 |
if err := p.OnEmit(ctx, &newRecord); err != nil { |
| 59 |
otel.Handle(err) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Enabled returns true if at least one Processor held by the LoggerProvider |
| 65 |
// that created the logger will process for the provided context and param. |
| 66 |
// |
| 67 |
// If it is not possible to definitively determine the record will be |
| 68 |
// processed, true will be returned by default. A value of false will only be |
| 69 |
// returned if it can be positively verified that no Processor will process. |
| 70 |
func (l *logger) Enabled(ctx context.Context, param log.EnabledParameters) bool { |
| 71 |
p := EnabledParameters{ |
| 72 |
InstrumentationScope: l.instrumentationScope, |
| 73 |
Severity: param.Severity, |
| 74 |
EventName: param.EventName, |
| 75 |
} |
| 76 |
|
| 77 |
for _, processor := range l.provider.processors { |
| 78 |
if processor.Enabled(ctx, p) { |
| 79 |
// At least one Processor will process the Record. |
| 80 |
return true |
| 81 |
} |
| 82 |
} |
| 83 |
// No Processor will process the record. |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
func (l *logger) newRecord(ctx context.Context, r log.Record) Record { |
| 88 |
sc := trace.SpanContextFromContext(ctx) |
| 89 |
|
| 90 |
newRecord := Record{ |
| 91 |
eventName: r.EventName(), |
| 92 |
timestamp: r.Timestamp(), |
| 93 |
observedTimestamp: r.ObservedTimestamp(), |
| 94 |
severity: r.Severity(), |
| 95 |
severityText: r.SeverityText(), |
| 96 |
|
| 97 |
traceID: sc.TraceID(), |
| 98 |
spanID: sc.SpanID(), |
| 99 |
traceFlags: sc.TraceFlags(), |
| 100 |
|
| 101 |
resource: l.provider.resource, |
| 102 |
scope: &l.instrumentationScope, |
| 103 |
attributeValueLengthLimit: l.provider.attributeValueLengthLimit, |
| 104 |
attributeCountLimit: l.provider.attributeCountLimit, |
| 105 |
allowDupKeys: l.provider.allowDupKeys, |
| 106 |
} |
| 107 |
if l.recCntIncr != nil { |
| 108 |
l.recCntIncr(ctx) |
| 109 |
} |
| 110 |
|
| 111 |
// This ensures we deduplicate key-value collections in the log body |
| 112 |
newRecord.SetBody(r.Body()) |
| 113 |
|
| 114 |
// This field SHOULD be set once the event is observed by OpenTelemetry. |
| 115 |
if newRecord.observedTimestamp.IsZero() { |
| 116 |
newRecord.observedTimestamp = now() |
| 117 |
} |
| 118 |
|
| 119 |
hasExceptionAttr := false |
| 120 |
r.WalkAttributes(func(kv log.KeyValue) bool { |
| 121 |
switch kv.Key { |
| 122 |
case exceptionTypeKey, exceptionMessageKey, exceptionStacktraceKey: |
| 123 |
hasExceptionAttr = true |
| 124 |
} |
| 125 |
newRecord.AddAttributes(kv) |
| 126 |
return true |
| 127 |
}) |
| 128 |
|
| 129 |
if err := r.Err(); err != nil && !hasExceptionAttr { |
| 130 |
addExceptionAttrs(&newRecord, err) |
| 131 |
} |
| 132 |
|
| 133 |
return newRecord |
| 134 |
} |
| 135 |
|
| 136 |
func addExceptionAttrs(r *Record, err error) { |
| 137 |
var attrs [2]log.KeyValue |
| 138 |
n := 0 |
| 139 |
if msg := err.Error(); msg != "" { |
| 140 |
if r.attributeCountLimit > 0 && r.attributeCountLimit-r.AttributesLen() < n+1 { |
| 141 |
goto flush |
| 142 |
} |
| 143 |
attrs[n] = log.String(exceptionMessageKey, msg) |
| 144 |
n++ |
| 145 |
} |
| 146 |
if errType := errorType(err); errType != "" { |
| 147 |
if r.attributeCountLimit > 0 && r.attributeCountLimit-r.AttributesLen() < n+1 { |
| 148 |
goto flush |
| 149 |
} |
| 150 |
attrs[n] = log.String(exceptionTypeKey, errType) |
| 151 |
n++ |
| 152 |
} |
| 153 |
|
| 154 |
flush: |
| 155 |
if n > 0 { |
| 156 |
r.addAttrs(attrs[:n]) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func errorType(err error) string { |
| 161 |
if et, ok := err.(interface{ ErrorType() string }); ok { |
| 162 |
if s := et.ErrorType(); s != "" { |
| 163 |
return s |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
t := reflect.TypeOf(err) |
| 168 |
if t == nil { |
| 169 |
return "" |
| 170 |
} |
| 171 |
|
| 172 |
pkg, name := t.PkgPath(), t.Name() |
| 173 |
if pkg != "" && name != "" { |
| 174 |
return pkg + "." + name |
| 175 |
} |
| 176 |
|
| 177 |
// The type has no package path or name (predeclared, not-defined, |
| 178 |
// or alias for a not-defined type). |
| 179 |
// |
| 180 |
// The type has no package path or name (predeclared, not-defined, |
| 181 |
// or alias for a not-defined type). |
| 182 |
// |
| 183 |
// This is not guaranteed to be unique, but is a best effort. |
| 184 |
return t.String() |
| 185 |
} |