package otelpoc
import (
"context"
"strings"
"testing"
"unicode/utf8"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)
func limitsWithLen(limit int) sdktrace.SpanLimits {
lim := sdktrace.NewSpanLimits()
lim.AttributeValueLengthLimit = limit
lim.AttributeCountLimit = -1
lim.AttributePerEventCountLimit = -1
lim.AttributePerLinkCountLimit = -1
lim.EventCountLimit = -1
lim.LinkCountLimit = -1
return lim
}
func captureOneSpan(t *testing.T, limit int, attrs ...attribute.KeyValue) []attribute.KeyValue {
t.Helper()
sr := tracetest.NewSpanRecorder()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sr),
sdktrace.WithSpanLimits(limitsWithLen(limit)),
)
defer func() { _ = tp.Shutdown(context.Background()) }()
tr := tp.Tracer("poc")
_, span := tr.Start(context.Background(), "op")
span.SetAttributes(attrs...)
span.End()
ended := sr.Ended()
if len(ended) != 1 {
t.Fatalf("expected 1 ended span, got %d", len(ended))
}
return ended[0].Attributes()
}
func TestOT001_FFFDOnlyStringBypassesTraceLengthLimit(t *testing.T) {
const limit = 5
val := strings.Repeat("\uFFFD", 1000)
got := captureOneSpan(t, limit, attribute.String("k", val))
if len(got) != 1 {
t.Fatalf("expected 1 attribute, got %d", len(got))
}
stored := got[0].Value.AsString()
t.Logf("input: %d chars / %d bytes; stored: %d chars / %d bytes; limit=%d",
utf8.RuneCountInString(val), len(val),
utf8.RuneCountInString(stored), len(stored), limit)
if maxBytes := 3 * limit; len(stored) > maxBytes {
t.Fatalf("BYPASS: AttributeValueLengthLimit=%d but %d bytes stored (expected <=%d)",
limit, len(stored), maxBytes)
}
}
func TestOT001_FFFDMixedStringOverRetains(t *testing.T) {
const limit = 5
val := strings.Repeat("\uFFFD", 100) + strings.Repeat("A", 6)
got := captureOneSpan(t, limit, attribute.String("k", val))
stored := got[0].Value.AsString()
t.Logf("input: %d chars / %d bytes; stored: %d chars / %d bytes; limit=%d",
utf8.RuneCountInString(val), len(val),
utf8.RuneCountInString(stored), len(stored), limit)
if chars := utf8.RuneCountInString(stored); chars > limit {
t.Fatalf("BYPASS: AttributeValueLengthLimit=%d but %d chars / %d bytes stored",
limit, chars, len(stored))
}
}
func TestOT001_ASCIIControl(t *testing.T) {
const limit = 5
val := strings.Repeat("B", 20)
got := captureOneSpan(t, limit, attribute.String("k", val))
stored := got[0].Value.AsString()
t.Logf("input: %d chars / %d bytes; stored: %d chars / %d bytes; limit=%d",
utf8.RuneCountInString(val), len(val),
utf8.RuneCountInString(stored), len(stored), limit)
if len(stored) != limit {
t.Fatalf("control failed: ASCII value not truncated (stored %d bytes)", len(stored))
}
}
func TestOT001_EventAttributesNotLengthTruncated(t *testing.T) {
const limit = 5
val := strings.Repeat("C", 100)
sr := tracetest.NewSpanRecorder()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sr),
sdktrace.WithSpanLimits(limitsWithLen(limit)),
)
defer func() { _ = tp.Shutdown(context.Background()) }()
tr := tp.Tracer("poc")
_, span := tr.Start(context.Background(), "op")
span.AddEvent("ev", trace.WithAttributes(attribute.String("k", val)))
span.End()
ended := sr.Ended()
events := ended[0].Events()
if len(events) != 1 {
t.Fatalf("expected 1 event, got %d", len(events))
}
stored := events[0].Attributes[0].Value.AsString()
t.Logf("event attribute input: %d bytes; stored: %d bytes; limit=%d", len(val), len(stored), limit)
if len(stored) > limit {
t.Logf("CONFIRMED: event attribute not length-truncated (%d bytes > limit %d)", len(stored), limit)
}
}