mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-19 22:39:38 +00:00
logging: link exported logs to their trace
The tracing handler adds traceID and spanID to access logs. The otlp core now sets them as the record's trace context.
This commit is contained in:
parent
2e4b9bac3a
commit
ae6725fd1c
2 changed files with 52 additions and 8 deletions
|
|
@ -24,6 +24,7 @@ import (
|
|||
sdklog "go.opentelemetry.io/otel/sdk/log"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
|
|
@ -39,7 +40,8 @@ func init() {
|
|||
// OTEL_* environment variables, such as OTEL_EXPORTER_OTLP_ENDPOINT and
|
||||
// OTEL_EXPORTER_OTLP_PROTOCOL. Set the log's output to discard to only
|
||||
// export the entries. The fields of the filter format apply to exported
|
||||
// records too.
|
||||
// records too, and access logs are linked to the span of the tracing
|
||||
// handler.
|
||||
//
|
||||
// log {
|
||||
// output discard
|
||||
|
|
@ -140,25 +142,43 @@ func (c *otlpCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
|||
return c.Core.Write(ent, c.prepare(fields))
|
||||
}
|
||||
|
||||
// prepare applies the field filters the way FilterEncoder does.
|
||||
// prepare applies the field filters the way FilterEncoder does, and
|
||||
// links the record to the span whose IDs the tracing handler logged.
|
||||
func (c *otlpCore) prepare(fields []zapcore.Field) []zapcore.Field {
|
||||
if len(c.filters) == 0 {
|
||||
return fields
|
||||
}
|
||||
|
||||
prepared := make([]zapcore.Field, 0, len(fields))
|
||||
var traceID trace.TraceID
|
||||
var spanID trace.SpanID
|
||||
prepared := make([]zapcore.Field, 0, len(fields)+1)
|
||||
for _, f := range fields {
|
||||
if filter, ok := c.filters[f.Key]; ok {
|
||||
f = filter.Filter(f)
|
||||
} else if marsh, ok := f.Interface.(zapcore.ObjectMarshaler); ok && f.Type == zapcore.ObjectMarshalerType {
|
||||
} else if marsh, ok := f.Interface.(zapcore.ObjectMarshaler); ok && f.Type == zapcore.ObjectMarshalerType && len(c.filters) > 0 {
|
||||
f.Interface = logObjectMarshalerWrapper{
|
||||
enc: FilterEncoder{Fields: c.filters, keyPrefix: f.Key + ">"},
|
||||
marsh: marsh,
|
||||
}
|
||||
}
|
||||
|
||||
if f.Type == zapcore.StringType {
|
||||
switch f.Key {
|
||||
case "traceID":
|
||||
traceID, _ = trace.TraceIDFromHex(f.String)
|
||||
case "spanID":
|
||||
spanID, _ = trace.SpanIDFromHex(f.String)
|
||||
}
|
||||
}
|
||||
|
||||
prepared = append(prepared, f)
|
||||
}
|
||||
|
||||
if traceID.IsValid() && spanID.IsValid() {
|
||||
spanCtx := trace.NewSpanContext(trace.SpanContextConfig{TraceID: traceID, SpanID: spanID})
|
||||
// the bridge takes the record's span from a context field, which encoders skip
|
||||
prepared = append(prepared, zapcore.Field{
|
||||
Type: zapcore.SkipType,
|
||||
Interface: trace.ContextWithSpanContext(context.Background(), spanCtx),
|
||||
})
|
||||
}
|
||||
|
||||
return prepared
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ package logging
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -183,3 +184,26 @@ func TestOTLPCoreAppliesFieldFilters(t *testing.T) {
|
|||
t.Errorf("unexpected request attribute %v", request)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOTLPCoreLinksRecordsToSpans(t *testing.T) {
|
||||
collector := newOTLPCollector(t)
|
||||
core := provisionOTLPCore(t)
|
||||
|
||||
logger := zap.New(core)
|
||||
logger.Info("traced", zap.String("traceID", "4bf92f3577b34da6a3ce929d0e0e4736"), zap.String("spanID", "00f067aa0ba902b7"))
|
||||
logger.Info("untraced", zap.String("traceID", "not-hex"), zap.String("spanID", "00f067aa0ba902b7"))
|
||||
if err := core.Cleanup(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
traced, _ := collector.record("traced")
|
||||
if got := hex.EncodeToString(traced.GetTraceId()); got != "4bf92f3577b34da6a3ce929d0e0e4736" {
|
||||
t.Errorf("unexpected trace ID %q", got)
|
||||
}
|
||||
if got := hex.EncodeToString(traced.GetSpanId()); got != "00f067aa0ba902b7" {
|
||||
t.Errorf("unexpected span ID %q", got)
|
||||
}
|
||||
if untraced, _ := collector.record("untraced"); len(untraced.GetTraceId()) != 0 {
|
||||
t.Errorf("expected no trace ID for invalid IDs, got %x", untraced.GetTraceId())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue