130 lines
4.3 KiB
Go
130 lines
4.3 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/propagation"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
type captureHandler struct {
|
|
attrs []slog.Attr
|
|
}
|
|
|
|
func (h *captureHandler) Enabled(context.Context, slog.Level) bool { return true }
|
|
func (h *captureHandler) Handle(_ context.Context, record slog.Record) error {
|
|
record.Attrs(func(attr slog.Attr) bool {
|
|
h.attrs = append(h.attrs, attr)
|
|
return true
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (h *captureHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return &captureHandler{attrs: append(h.attrs, attrs...)}
|
|
}
|
|
func (h *captureHandler) WithGroup(string) slog.Handler { return h }
|
|
|
|
func TestSanitizerDropsIdentifiersAndRedactsErrors(t *testing.T) {
|
|
capture := &captureHandler{}
|
|
handler := &sanitizeHandler{inner: capture, service: "messenger-api", instance: "instance-1", minLevel: slog.LevelDebug}
|
|
record := slog.NewRecord(time.Now(), slog.LevelInfo, "request handled", 0)
|
|
record.AddAttrs(
|
|
slog.String("event", "http.request.completed"),
|
|
slog.String("path", "/api/v1/chats/33333333-3333-4333-8333-333333333333/messages"),
|
|
slog.String("query", "filename=secret.png"),
|
|
slog.String("user_id", "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"),
|
|
slog.String("object_key", "attachments/chat/attachment/original"),
|
|
slog.String("error", `parse "postgres://user:pass@db:5432/x": X-Amz-Signature=abc attachments/33333333-3333-4333-8333-333333333333/44444444-4444-4444-8444-444444444444/original`),
|
|
slog.String("request_id", "bad request id with spaces"),
|
|
)
|
|
if err := handler.Handle(context.Background(), record); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
seen := map[string]string{}
|
|
for _, attr := range capture.attrs {
|
|
seen[attr.Key] = attr.Value.String()
|
|
}
|
|
for _, forbidden := range []string{"path", "query", "user_id", "object_key"} {
|
|
if _, ok := seen[forbidden]; ok {
|
|
t.Fatalf("forbidden attribute %q was exported", forbidden)
|
|
}
|
|
}
|
|
if strings.Contains(seen["error"], "pass") || strings.Contains(seen["error"], "abc") {
|
|
t.Fatalf("error was not redacted: %q", seen["error"])
|
|
}
|
|
if strings.Contains(seen["error"], "33333333") || strings.Contains(seen["error"], "attachments/") {
|
|
t.Fatalf("object key/uuid was not redacted: %q", seen["error"])
|
|
}
|
|
if seen["request_id"] != "invalid" {
|
|
t.Fatalf("request_id was not normalized: %q", seen["request_id"])
|
|
}
|
|
if seen["service.name"] != "messenger-api" || seen["service.instance.id"] != "instance-1" {
|
|
t.Fatalf("service attributes missing: %#v", seen)
|
|
}
|
|
}
|
|
|
|
func TestValidTraceparent(t *testing.T) {
|
|
if !ValidTraceparent("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01") {
|
|
t.Fatal("valid traceparent rejected")
|
|
}
|
|
if ValidTraceparent("not-a-traceparent") || ValidTraceparent("00-X") {
|
|
t.Fatal("invalid traceparent accepted")
|
|
}
|
|
}
|
|
|
|
func TestInjectDropsOversizedTracestate(t *testing.T) {
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
|
ts, err := trace.ParseTraceState("a=" + strings.Repeat("a", 180) + ",b=" + strings.Repeat("b", 180) + ",c=" + strings.Repeat("c", 180))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
|
TraceID: trace.TraceID{1},
|
|
SpanID: trace.SpanID{1},
|
|
TraceState: ts,
|
|
})
|
|
traceparent, tracestate := Inject(trace.ContextWithSpanContext(context.Background(), sc))
|
|
if traceparent == "" {
|
|
t.Fatal("traceparent missing")
|
|
}
|
|
if tracestate != "" {
|
|
t.Fatalf("oversized tracestate was persisted: %d bytes", len(tracestate))
|
|
}
|
|
}
|
|
|
|
func TestInjectPreservesValidTracestate(t *testing.T) {
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
|
small, err := trace.ParseTraceState("vendor=abc123")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
|
TraceID: trace.TraceID{1},
|
|
SpanID: trace.SpanID{1},
|
|
TraceState: small,
|
|
})
|
|
_, kept := Inject(trace.ContextWithSpanContext(context.Background(), sc))
|
|
if kept != "vendor=abc123" {
|
|
t.Fatalf("valid tracestate dropped: %q", kept)
|
|
}
|
|
}
|
|
|
|
func TestSanitizeKeepsAllowlistedJobID(t *testing.T) {
|
|
const id = "11111111-1111-4111-8111-111111111111"
|
|
attr := slog.String("job_id", id)
|
|
clean, ok := sanitizeAttr(attr)
|
|
if !ok || clean.Value.String() != id {
|
|
t.Fatalf("job_id = %#v, want %q", clean, id)
|
|
}
|
|
if clean, ok := sanitizeAttr(slog.String("job_id", "not-a-uuid")); !ok || clean.Value.String() != "invalid" {
|
|
t.Fatalf("invalid job_id = %#v", clean)
|
|
}
|
|
}
|