Add HTTPMiddleware, HTTPTransport and LogHandler; extend TracerConfig with Insecure, Headers and Attributes; install W3C propagators; parent-based sampling; end-to-end OTLP export tests; update dependencies (Go 1.26). Breaking since v0.2.0: the OTLP connection uses TLS unless Insecure is set. Co-Authored-By: Claude <noreply@anthropic.com>
34 lines
877 B
Go
34 lines
877 B
Go
package obsx
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// LogHandler wraps h so that records logged with a context carrying a span
|
|
// (slog.InfoContext and friends) get trace_id and span_id attributes,
|
|
// linking logs to traces.
|
|
func LogHandler(h slog.Handler) slog.Handler { return logHandler{h} }
|
|
|
|
type logHandler struct{ slog.Handler }
|
|
|
|
func (h logHandler) Handle(ctx context.Context, r slog.Record) error {
|
|
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
|
|
r.AddAttrs(
|
|
slog.String("trace_id", sc.TraceID().String()),
|
|
slog.String("span_id", sc.SpanID().String()),
|
|
)
|
|
}
|
|
return h.Handler.Handle(ctx, r)
|
|
}
|
|
|
|
func (h logHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return logHandler{h.Handler.WithAttrs(attrs)}
|
|
}
|
|
|
|
func (h logHandler) WithGroup(name string) slog.Handler {
|
|
return logHandler{h.Handler.WithGroup(name)}
|
|
}
|