diff --git a/README.md b/README.md index f4be740..e07a7d9 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,8 @@ On `log/otel` v1.6.x and earlier, `WithAttrs` attributes reach the log line only Span event string values are capped at 1 KiB and end with `…`. From `log/otel` v1.8.0, slice values (`[]string`, `[]int`, `[]int64`, `[]float64`, `[]bool`) also keep only their first 128 elements; the log line still gets the full record. +From `log/otel` v1.9.0, other span event values are encoded as slog's JSON handler encodes them for the log line, so redaction applies to both: after `slog.LogValuer` values are resolved, an error is written as `ERROR: ` plus `Error()`, and any other value goes through `encoding/json` (`MarshalJSON` and `MarshalText` apply, fields tagged `json:"-"` and unexported fields stay out). A value `encoding/json` rejects is recorded as `!ERROR: `. Earlier versions wrote these values with `%+v`. + The `otel` handler disables baggage logging by default. If you need baggage in logs, enable it explicitly and prefer an allow-list: diff --git a/otel/handler.go b/otel/handler.go index 875b2b4..e4165e2 100644 --- a/otel/handler.go +++ b/otel/handler.go @@ -13,10 +13,13 @@ package otel import ( + "bytes" "context" + "encoding/json" "fmt" "log/slog" "math" + "reflect" "strconv" "strings" "time" @@ -73,7 +76,13 @@ const ( // recorded as int64 nanoseconds; slog.Uint64 values as int64, or as a decimal // string when they exceed math.MaxInt64. String values longer than 1 KiB are // truncated and end with "…"; slice values ([]string, []int, []int64, -// []float64, []bool) keep their first 128 elements. Attributes added with +// []float64, []bool) keep their first 128 elements. Other values are encoded +// as slog's JSON handler encodes them for the log line, after slog.LogValuer +// values are resolved: an error that is not a json.Marshaler as "ERROR: " + +// Error(); anything else with encoding/json (json.Marshaler and +// encoding.TextMarshaler methods apply, fields tagged json:"-" and unexported +// fields stay out, a JSON string is unquoted). A value encoding/json rejects is +// recorded as "!ERROR: ", never as the raw value. Attributes added with // WithAttrs (logger.With) are recorded on every span event the derived handler // adds, before the record's own attributes, with the same keys, kinds and // caps; they are converted once, when WithAttrs is called. @@ -348,16 +357,64 @@ func (h OtelHandler) slogAttrToOtelAttr(attr slog.Attr, prefix string) []attribu return []attribute.KeyValue{attribute.Float64Slice(key, capSlice(v))} case []bool: return []attribute.KeyValue{attribute.BoolSlice(key, capSlice(v))} - case error: - return []attribute.KeyValue{attribute.String(key, capString(fmt.Sprintf("ERROR: %+v", v)))} default: - return []attribute.KeyValue{attribute.String(key, capString(fmt.Sprintf("%+v", v)))} + return []attribute.KeyValue{attribute.String(key, capString(anyString(v)))} } default: return []attribute.KeyValue{} } } +// anyString encodes a KindAny value for a span event the way slog's JSON +// handler encodes it for the log line, so values redacted there (json:"-" +// fields, MarshalJSON, MarshalText) are redacted on the span too. The order is +// documented on [OtelHandler]. A method that panics on a nil pointer receiver +// gives "", as in slog; any other panic gives "!PANIC: ". +func anyString(v any) string { + _, isJSONMarshaler := v.(json.Marshaler) + if err, ok := v.(error); ok && !isJSONMarshaler { + return "ERROR: " + encodeSafely(v, err.Error) + } + return encodeSafely(v, func() string { return jsonString(v) }) +} + +// jsonString encodes v as slog's JSON handler does (encoding/json without +// HTML escaping), unquoting the result when it is a JSON string. +func jsonString(v any) string { + var encoded bytes.Buffer + encoder := json.NewEncoder(&encoded) + encoder.SetEscapeHTML(false) + if err := encoder.Encode(v); err != nil { + return "!ERROR: " + err.Error() + } + text := bytes.TrimSuffix(encoded.Bytes(), []byte("\n")) + var unquoted string + if len(text) > 0 && text[0] == '"' && json.Unmarshal(text, &unquoted) == nil { + return unquoted + } + return string(text) +} + +// encodeSafely runs encode, turning a panic into text: "" when v is a nil +// pointer (a method that does not guard its receiver), as slog does, and +// "!PANIC: " otherwise. +func encodeSafely(v any, encode func() string) (s string) { + defer func() { + if r := recover(); r != nil { + s = recoveredString(v, r) + } + }() + return encode() +} + +// recoveredString describes a panic raised while encoding v. +func recoveredString(v any, recovered any) string { + if value := reflect.ValueOf(v); value.Kind() == reflect.Pointer && value.IsNil() { + return "" + } + return fmt.Sprintf("!PANIC: %v", recovered) +} + // joinKey appends key to the dot-joined group path in prefix. func joinKey(prefix, key string) string { if prefix == "" { diff --git a/otel/handler_benchmark_test.go b/otel/handler_benchmark_test.go index 66beed0..688376d 100644 --- a/otel/handler_benchmark_test.go +++ b/otel/handler_benchmark_test.go @@ -2,6 +2,7 @@ package otel import ( "context" + "errors" "io" "log/slog" "testing" @@ -31,6 +32,22 @@ func BenchmarkHandleSpanEvent(b *testing.B) { } } +// BenchmarkHandleSpanEventAnyValues measures span event encoding of KindAny +// values: a struct and an error. +func BenchmarkHandleSpanEventAnyValues(b *testing.B) { + logger := slog.New(New(slog.NewJSONHandler(io.Discard, nil))) + ctx := trace.ContextWithSpan(context.Background(), &discardSpan{}) + order := struct { + ID int + Status string + }{42, "ok"} + err := errors.New("payment declined") + b.ReportAllocs() + for b.Loop() { + logger.InfoContext(ctx, "order failed", "order", order, "error", err) + } +} + // discardSpan is a recording span that drops events, so the benchmark measures // the handler and not the recorder. type discardSpan struct { diff --git a/otel/handler_test.go b/otel/handler_test.go index 2773247..6444ba3 100644 --- a/otel/handler_test.go +++ b/otel/handler_test.go @@ -280,7 +280,7 @@ func TestSpanEventAttributeKinds(t *testing.T) { {"uint64 above int64 as string", slog.Uint64("k", math.MaxUint64), attribute.StringValue("18446744073709551615")}, {"string slice", slog.Any("k", []string{"a", "b"}), attribute.StringSliceValue([]string{"a", "b"})}, {"error", slog.Any("k", errors.New("boom")), attribute.StringValue("ERROR: boom")}, - {"any", slog.Any("k", struct{ A int }{1}), attribute.StringValue("{A:1}")}, + {"any as JSON", slog.Any("k", struct{ A int }{1}), attribute.StringValue(`{"A":1}`)}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -357,7 +357,7 @@ func TestSpanEventValuesAreCapped(t *testing.T) { {"short string unchanged", slog.String("k", "short"), "short"}, {"exactly at cap unchanged", slog.String("k", long[:maxAttributeValueLen]), long[:maxAttributeValueLen]}, {"long string", slog.String("k", long), wantCapped}, - {"long any", slog.Any("k", struct{ S string }{long}), "{S:" + long[:maxAttributeValueLen-3] + truncationMarker}, + {"long any", slog.Any("k", struct{ S string }{long}), `{"S":"` + long[:maxAttributeValueLen-6] + truncationMarker}, {"long error", slog.Any("k", errors.New(long)), "ERROR: " + long[:maxAttributeValueLen-7] + truncationMarker}, {"cut on a rune boundary", slog.String("k", "a"+strings.Repeat("é", maxAttributeValueLen)), "a" + strings.Repeat("é", (maxAttributeValueLen-1)/2) + truncationMarker}, } @@ -637,3 +637,85 @@ func hasAttr(attrs []attribute.KeyValue, key string) bool { } return false } + +type loginRequest struct { + User string + Password string `json:"-"` +} + +type redactingJSON string + +func (redactingJSON) MarshalJSON() ([]byte, error) { return []byte(`"[REDACTED]"`), nil } + +type redactingText string + +func (redactingText) MarshalText() ([]byte, error) { return []byte("[REDACTED]"), nil } + +type leakyStringer struct { + Updates chan int + password string +} + +func (s leakyStringer) String() string { return s.password } + +type valueMarshaler struct{} + +func (valueMarshaler) MarshalJSON() ([]byte, error) { return []byte(`"[REDACTED]"`), nil } + +type nilUnsafeError struct{ message string } + +func (e *nilUnsafeError) Error() string { return e.message } + +type panickingText struct{} + +func (panickingText) MarshalText() ([]byte, error) { panic("boom") } + +func TestSpanEventValuesFollowLogRedaction(t *testing.T) { + var typedNil *nilUnsafeError + tests := []struct { + name string + attr slog.Attr + want string + }{ + {"json dash field", slog.Any("k", loginRequest{User: "bob", Password: "hunter2"}), `{"User":"bob"}`}, + {"pointer to json dash field", slog.Any("k", &loginRequest{User: "bob", Password: "hunter2"}), `{"User":"bob"}`}, + {"MarshalJSON string is unquoted", slog.Any("k", redactingJSON("sk_live_123")), "[REDACTED]"}, + {"MarshalText", slog.Any("k", redactingText("sk_live_123")), "[REDACTED]"}, + {"unexported field stays out", slog.Any("k", struct{ token string }{"s3cr3t"}), "{}"}, + {"Stringer is not used when JSON fails", slog.Any("k", leakyStringer{password: "s3cr3t"}), "!ERROR: json: unsupported type: chan int"}, + {"typed nil marshaler", slog.Any("k", (*valueMarshaler)(nil)), "null"}, + {"no HTML escaping", slog.Any("k", struct{ URL string }{"a?b=1&c=<2>"}), `{"URL":"a?b=1&c=<2>"}`}, + {"map as JSON", slog.Any("k", map[string]int{"a": 1}), `{"a":1}`}, + {"nil", slog.Any("k", nil), "null"}, + {"typed nil error", slog.Any("k", error(typedNil)), "ERROR: "}, + {"panicking method", slog.Any("k", panickingText{}), "!PANIC: boom"}, + {"unsupported value", slog.Any("k", make(chan int)), "!ERROR: json: unsupported type: chan int"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + logger, ctx, span := newSpanLogger(t) + + logger.LogAttrs(ctx, slog.LevelInfo, "msg", tt.attr) + + if got := eventAttr(t, span, "k").AsString(); got != tt.want { + t.Fatalf("value = %q, want %q", got, tt.want) + } + }) + } +} + +func TestSpanEventAndLogLineHideJSONDashField(t *testing.T) { + var out bytes.Buffer + span := &recordingSpan{} + ctx := trace.ContextWithSpan(context.Background(), span) + logger := slog.New(New(slog.NewJSONHandler(&out, nil))) + + logger.InfoContext(ctx, "login", "req", loginRequest{User: "bob", Password: "hunter2"}) + + if strings.Contains(out.String(), "hunter2") { + t.Fatalf("log line leaks the password: %s", out.String()) + } + if got := eventAttr(t, span, "req").AsString(); strings.Contains(got, "hunter2") { + t.Fatalf("span event leaks the password: %q", got) + } +}