Skip to content

otelslog: record.SetErr for error fields #8746

@pellared

Description

@pellared

otelslog currently treats errors fields ordinary fields

That makes exception/error data inconsistent with the OpenTelemetry log semantic conventions and harder to recognize as exception/error information.

The proposal is to change

func (h *Handler) convertRecord(r slog.Record) log.Record {
var record log.Record
record.SetTimestamp(r.Time)
record.SetBody(log.StringValue(r.Message))
const sevOffset = slog.Level(log.SeverityDebug) - slog.LevelDebug
record.SetSeverity(log.Severity(r.Level + sevOffset))
record.SetSeverityText(r.Level.String())
if h.source {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
record.AddAttributes(
log.String(string(semconv.CodeFilePathKey), f.File),
log.String(string(semconv.CodeFunctionNameKey), f.Function),
log.Int(string(semconv.CodeLineNumberKey), f.Line),
)
}
if h.attrs.Len() > 0 {
record.AddAttributes(h.attrs.KeyValues()...)
}
n := r.NumAttrs()
if h.group != nil {
if n > 0 {
buf := newKVBuffer(n)
r.Attrs(buf.AddAttr)
record.AddAttributes(h.group.KeyValue(buf.KeyValues()...))
} else {
// A Handler should not output groups if there are no attributes.
g := h.group.NextNonEmpty()
if g != nil {
record.AddAttributes(g.KeyValue())
}
}
} else if n > 0 {
buf := newKVBuffer(n)
r.Attrs(buf.AddAttr)
record.AddAttributes(buf.KeyValues()...)
}
return record
}

So that error is set as a field on the record instead of attributes.

Metadata

Metadata

Assignees

Labels

bridge: slogRelated to the slog bridgeenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions