cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / telemetry / otlplog.go
otlplog.go 1.5K
1package telemetry23import (4 "context"5 "log/slog"6 "slices"7 "time"8)910// spanKey carries a span's ids (spanIDs) from emit to the OTLP log11// handler, so the log record of an operation links to its span.12type spanKey struct{}1314// otlpHandler is a slog.Handler that queues OTLP log records.15type otlpHandler struct {16 e *exporter17 level slog.Leveler18 attrs []keyValue19 prefix string20}2122func (h *otlpHandler) Enabled(_ context.Context, l slog.Level) bool {23 return l >= h.level.Level()24}2526func (h *otlpHandler) Handle(ctx context.Context, r slog.Record) error {27 rec := logRecord{28 TimeUnixNano: nanos(r.Time),29 ObservedTimeUnixNano: nanos(time.Now()),30 SeverityNumber: severity(r.Level),31 SeverityText: r.Level.String(),32 Body: str(r.Message),33 Attributes: slices.Clip(h.attrs),34 }35 ids, span := ctx.Value(spanKey{}).(spanIDs)36 if span {37 rec.TraceID, rec.SpanID = ids.trace, ids.span38 }39 r.Attrs(func(a slog.Attr) bool {40 // The record's own trace and span ids say these; parent_id stays.41 if !span || (a.Key != "trace_id" && a.Key != "span_id") {42 rec.Attributes = appendAttr(rec.Attributes, h.prefix, a)43 }44 return true45 })46 h.e.log(rec)47 return nil48}4950func (h *otlpHandler) WithAttrs(as []slog.Attr) slog.Handler {51 h2 := *h52 h2.attrs = slices.Clip(h.attrs)53 for _, a := range as {54 h2.attrs = appendAttr(h2.attrs, h.prefix, a)55 }56 return &h257}5859func (h *otlpHandler) WithGroup(name string) slog.Handler {60 if name == "" {61 return h62 }63 h2 := *h64 h2.prefix += name + "."65 return &h266}