cat / 012
clone

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

Write a span's ids to the JSON log with one allocation

Dami committed de03a0c
Cut trace_id, span_id and parent_id from one hex string, added by emit
rather than a wrapping handler, and carry the ids in the context only
for the OTLP log handler, which keeps parent_id as an attribute.
commit
de03a0c642cf9a778613fb2715dce7d7690f3ae7
parent
8a7c5a48163e0b1920242a70a008062497ec28d3
author
Dami <dami.etoile@gmail.com>
3 files changed, +35 -29
Minternal/telemetry/otlplog.go16+++++++++-----
Minternal/telemetry/telemetry.go13+++++++----
Minternal/telemetry/trace.go35+++++++++++++++---------------
M internal/telemetry/otlplog.go +10 -6
M internal/telemetry/telemetry.go +8 -5
M internal/telemetry/trace.go +17 -18
@@ -259,23 +259,22 @@ func ParentFrom(ctx context.Context) Parent {
259259 return p260260 }261261 262-// idsHandler adds a span's ids to its record in the JSON log, as263-// trace_id, span_id and, below a root, parent_id, so the file log holds264-// the same tree as the traces.265-type idsHandler struct{ slog.Handler }266-267-func (h idsHandler) Handle(ctx context.Context, r slog.Record) error {268- if ids, ok := ctx.Value(spanKey{}).(spanIDs); ok {269- r = r.Clone()270- r.AddAttrs(slog.String("trace_id", hex.EncodeToString(ids.trace[:])), slog.String("span_id", hex.EncodeToString(ids.span[:])))271- if !ids.parent.IsZero() {272- r.AddAttrs(slog.String("parent_id", hex.EncodeToString(ids.parent[:])))273- }262+// idAttrs appends a span's ids to attrs as trace_id, span_id and, below263+// a root, parent_id, so the JSON log holds the same tree as the traces.264+// The three are cut from one string, one allocation.265+func idAttrs(attrs []slog.Attr, ids spanIDs) []slog.Attr {266+ var b [64]byte267+ hex.Encode(b[:32], ids.trace[:])268+ hex.Encode(b[32:48], ids.span[:])269+ n := 48270+ if !ids.parent.IsZero() {271+ hex.Encode(b[48:], ids.parent[:])272+ n = 64274273 }275- return h.Handler.Handle(ctx, r)276-}277-278-func (h idsHandler) WithAttrs(as []slog.Attr) slog.Handler {279- return idsHandler{h.Handler.WithAttrs(as)}274+ s := string(b[:n])275+ attrs = append(attrs, slog.String("trace_id", s[:32]), slog.String("span_id", s[32:48]))276+ if n == 64 {277+ attrs = append(attrs, slog.String("parent_id", s[48:]))278+ }279+ return attrs280280 }281-func (h idsHandler) WithGroup(name string) slog.Handler { return idsHandler{h.Handler.WithGroup(name)} }