feat(logging): stamp trace and span ids on log records
This commit is contained in:
6 files changed
+53
-6
No files matched your search
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/agentworker"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error
|
||||
@@ -68,7 +69,7 @@ func runCommand(args []string, getenv func(string) string, stderr io.Writer, run
|
||||
return 1
|
||||
}
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(stderr, nil))
|
||||
log := slog.New(logging.WithTrace(slog.NewJSONHandler(stderr, nil)))
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ require (
|
||||
go.opentelemetry.io/otel v1.47.0 // indirect
|
||||
go.opentelemetry.io/otel/log v1.47.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.47.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.47.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/exp v0.0.0-20261013213113-2ab7f3cbc5fb // indirect
|
||||
@@ -100,6 +99,7 @@ require (
|
||||
go.lsp.dev/jsonrpc2 v1.0.1
|
||||
go.lsp.dev/protocol v1.0.1
|
||||
go.lsp.dev/uri v1.0.1
|
||||
go.opentelemetry.io/otel/trace v1.47.0
|
||||
golang.org/x/crypto v0.58.0
|
||||
golang.org/x/mod v0.42.0
|
||||
golang.org/x/net v0.61.0
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
@@ -158,7 +159,7 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
return &StartupError{Operation: operation, Err: err}
|
||||
}
|
||||
defer transport.Close()
|
||||
log = slog.New(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID))
|
||||
log = slog.New(logging.WithTrace(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID)))
|
||||
log.Info("system log publisher attached", "task", taskID)
|
||||
|
||||
subscription, err := transport.SubscribeTasks(ctx, taskID, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
|
||||
@@ -62,5 +62,5 @@ func Logging(cfg config.Config, component string) Logs {
|
||||
// Diagnostics captures a service's stderr log for its recent-log contract.
|
||||
func Diagnostics(log *slog.Logger, service string) (*slog.Logger, *logging.Feed) {
|
||||
feed := logging.NewFeed(1000)
|
||||
return slog.New(logging.NewFeedHandler(log.Handler(), feed)).With("service", service), feed
|
||||
return slog.New(logging.WithTrace(logging.NewFeedHandler(log.Handler(), feed))).With("service", service), feed
|
||||
}
|
||||
@@ -62,9 +62,9 @@ func New(opts Options) (*slog.Logger, io.Closer, error) {
|
||||
|
||||
func withFeed(handler slog.Handler, feed *Feed) slog.Handler {
|
||||
if feed == nil {
|
||||
return handler
|
||||
return WithTrace(handler)
|
||||
}
|
||||
return NewFeedHandler(handler, feed)
|
||||
return WithTrace(NewFeedHandler(handler, feed))
|
||||
}
|
||||
|
||||
func parseLevel(s string) slog.Level {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// stampedKey marks a context whose record already carries the ids, so a
|
||||
// traceHandler nested under another one does not stamp them twice.
|
||||
type stampedKey struct{}
|
||||
|
||||
// traceHandler stamps the trace and span ids of ctx's span on each record.
|
||||
// It must wrap every other handler: none of them reads ctx.
|
||||
type traceHandler struct{ inner slog.Handler }
|
||||
|
||||
// WithTrace wraps handler so records logged under a valid span carry
|
||||
// trace_id and span_id.
|
||||
func WithTrace(handler slog.Handler) slog.Handler {
|
||||
return traceHandler{inner: handler}
|
||||
}
|
||||
|
||||
func (h traceHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
return h.inner.Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h traceHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() && ctx.Value(stampedKey{}) == nil {
|
||||
record.AddAttrs(
|
||||
slog.String("trace_id", sc.TraceID().String()),
|
||||
slog.String("span_id", sc.SpanID().String()),
|
||||
)
|
||||
ctx = context.WithValue(ctx, stampedKey{}, true)
|
||||
}
|
||||
return h.inner.Handle(ctx, record)
|
||||
}
|
||||
|
||||
func (h traceHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return traceHandler{inner: h.inner.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h traceHandler) WithGroup(name string) slog.Handler {
|
||||
return traceHandler{inner: h.inner.WithGroup(name)}
|
||||
}
|
||||
Reference in new issue
Block a user