-
Notifications
You must be signed in to change notification settings - Fork 233
feat: sanitize utf8 strings with binary in spans #2485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SkArchon
merged 15 commits into
main
from
milinda/eng-8726-otel-error-traces-export-invalid-utf-8-in-string-field
Feb 23, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1593336
feat: refactor and add sanitizeUtf8 processor
SkArchon d158512
fix: updates
SkArchon 8da4582
fix: review comments
SkArchon 4239e47
Merge remote-tracking branch 'origin/main' into milinda/eng-8726-otel…
SkArchon a876dbc
fix: updates
SkArchon 9f98551
fix: review comments
SkArchon b480371
fix: review comments
SkArchon b29063d
fix: updates
SkArchon 880f8d7
Merge branch 'main' into milinda/eng-8726-otel-error-traces-export-in…
SkArchon fefcc56
fix: tests
SkArchon 37f7462
fix: review comments
SkArchon 1adc217
Merge remote-tracking branch 'origin/main' into milinda/eng-8726-otel…
SkArchon 5a3ec21
fix: review comments
SkArchon 035a5fd
Merge branch 'main' into milinda/eng-8726-otel-error-traces-export-in…
SkArchon 8ac3639
Merge branch 'main' into milinda/eng-8726-otel-error-traces-export-in…
SkArchon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/wundergraph/cosmo/router-tests/testenv" | ||
| "github.com/wundergraph/cosmo/router/core" | ||
| "github.com/wundergraph/cosmo/router/pkg/config" | ||
| "github.com/wundergraph/cosmo/router/pkg/trace/tracetest" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.17.0" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| func TestAttributeProcessorIntegration(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("SanitizeUTF8 logs warning when invalid UTF-8 is detected", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| exporter := tracetest.NewInMemoryExporter(t) | ||
|
|
||
| // Create a string with invalid UTF-8 bytes | ||
| invalidUTF8Value := string([]byte{0x80, 0x81, 0x82}) | ||
| sanitizedValue := strings.ToValidUTF8(invalidUTF8Value, "\ufffd") | ||
| attrKey := "custom.invalid_utf8_attr" | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| TraceExporter: exporter, | ||
| TracingSanitizeUTF8: &config.SanitizeUTF8Config{ | ||
| Enabled: true, | ||
| LogSanitizations: true, | ||
| }, | ||
| // Add a custom tracing attribute with invalid UTF-8 as default value | ||
| CustomTracingAttributes: []config.CustomAttribute{ | ||
| { | ||
| Key: attrKey, | ||
| Default: invalidUTF8Value, | ||
| }, | ||
| }, | ||
| LogObservation: testenv.LogObservationConfig{ | ||
| Enabled: true, | ||
| LogLevel: zapcore.WarnLevel, | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ | ||
| Query: `query { employees { id } }`, | ||
| }) | ||
| require.Contains(t, res.Body, `"employees"`) | ||
|
|
||
| // Verify that spans were created | ||
| sn := exporter.GetSpans().Snapshots() | ||
| require.NotEmpty(t, sn) | ||
|
|
||
| // Verify that the invalid UTF-8 attribute was sanitized (replaced with U+FFFD) | ||
| sanitizedAttr := attribute.String(attrKey, sanitizedValue) | ||
| require.Contains(t, sn[0].Attributes(), sanitizedAttr) | ||
|
|
||
| // Verify that the warning log was emitted | ||
| logEntries := xEnv.Observer().FilterMessageSnippet("Invalid UTF-8 in span attribute").All() | ||
| require.GreaterOrEqual(t, len(logEntries), 1) | ||
|
|
||
| // Verify the log contains the attribute key | ||
| logEntry := logEntries[0] | ||
| contextMap := logEntry.ContextMap() | ||
| require.Equal(t, attrKey, contextMap["key"]) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("SanitizeUTF8 does not log when logging is disabled", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| exporter := tracetest.NewInMemoryExporter(t) | ||
|
|
||
| // Create a string with invalid UTF-8 bytes | ||
| invalidUTF8Value := string([]byte{0x80, 0x81, 0x82}) | ||
| sanitizedValue := strings.ToValidUTF8(invalidUTF8Value, "\ufffd") | ||
| attrKey := "custom.invalid_utf8_attr_no_log" | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| TraceExporter: exporter, | ||
| TracingSanitizeUTF8: &config.SanitizeUTF8Config{ | ||
| Enabled: true, | ||
| LogSanitizations: false, // Logging disabled | ||
| }, | ||
| CustomTracingAttributes: []config.CustomAttribute{ | ||
| { | ||
| Key: attrKey, | ||
| Default: invalidUTF8Value, | ||
| }, | ||
| }, | ||
| LogObservation: testenv.LogObservationConfig{ | ||
| Enabled: true, | ||
| LogLevel: zapcore.WarnLevel, | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ | ||
| Query: `query { employees { id } }`, | ||
| }) | ||
| require.Contains(t, res.Body, `"employees"`) | ||
|
|
||
| // Verify that spans were created | ||
| sn := exporter.GetSpans().Snapshots() | ||
| require.NotEmpty(t, sn) | ||
|
|
||
| // Verify that the invalid UTF-8 attribute was still sanitized | ||
| sanitizedAttr := attribute.String(attrKey, sanitizedValue) | ||
| require.Contains(t, sn[0].Attributes(), sanitizedAttr) | ||
|
|
||
| // Verify that NO warning log was emitted for the sanitization | ||
| logEntries := xEnv.Observer().FilterMessageSnippet("Invalid UTF-8 in span attribute").All() | ||
| require.Empty(t, logEntries) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("SanitizeUTF8 disabled leaves invalid UTF-8 unchanged", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| exporter := tracetest.NewInMemoryExporter(t) | ||
|
|
||
| // Create a string with invalid UTF-8 bytes | ||
| invalidUTF8Value := string([]byte{0x80, 0x81, 0x82}) | ||
| attrKey := "custom.invalid_utf8_unchanged" | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| TraceExporter: exporter, | ||
| TracingSanitizeUTF8: &config.SanitizeUTF8Config{ | ||
| Enabled: false, // Disabled | ||
| }, | ||
| CustomTracingAttributes: []config.CustomAttribute{ | ||
| { | ||
| Key: attrKey, | ||
| Default: invalidUTF8Value, | ||
| }, | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ | ||
| Query: `query { employees { id } }`, | ||
| }) | ||
| require.Contains(t, res.Body, `"employees"`) | ||
|
|
||
| // Verify that spans were created | ||
| sn := exporter.GetSpans().Snapshots() | ||
| require.NotEmpty(t, sn) | ||
|
|
||
| // Verify that the invalid UTF-8 attribute was NOT sanitized | ||
| require.Contains(t, sn[0].Attributes(), attribute.String(attrKey, invalidUTF8Value)) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("IPAnonymization redacts IP attributes", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| exporter := tracetest.NewInMemoryExporter(t) | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| TraceExporter: exporter, | ||
| IPAnonymization: &core.IPAnonymizationConfig{ | ||
| Enabled: true, | ||
| Method: core.Redact, | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ | ||
| Query: `query { employees { id } }`, | ||
| }) | ||
| require.Contains(t, res.Body, `"employees"`) | ||
|
|
||
| sn := exporter.GetSpans().Snapshots() | ||
| require.NotEmpty(t, sn) | ||
|
|
||
| // Check that IP addresses are redacted (checks both http.client_ip and net.sock.peer.addr) | ||
| redactedIPCount := 0 | ||
| for _, span := range sn { | ||
| for _, attr := range span.Attributes() { | ||
| if attr.Key == semconv.HTTPClientIPKey || attr.Key == semconv.NetSockPeerAddrKey { | ||
| redactedIPCount++ | ||
| require.Equal(t, "[REDACTED]", attr.Value.AsString()) | ||
| } | ||
| } | ||
| } | ||
| require.Positive(t, redactedIPCount) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("IPAnonymization hashes IP attributes", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| exporter := tracetest.NewInMemoryExporter(t) | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| TraceExporter: exporter, | ||
| IPAnonymization: &core.IPAnonymizationConfig{ | ||
| Enabled: true, | ||
| Method: core.Hash, | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{ | ||
| Query: `query { employees { id } }`, | ||
| }) | ||
| require.Contains(t, res.Body, `"employees"`) | ||
|
|
||
| sn := exporter.GetSpans().Snapshots() | ||
| require.NotEmpty(t, sn) | ||
|
|
||
| // Check that IP addresses are hashed (64 char hex) in spans that have them | ||
| hashedIPCount := 0 | ||
| for _, span := range sn { | ||
| for _, attr := range span.Attributes() { | ||
| if attr.Key == semconv.HTTPClientIPKey || attr.Key == semconv.NetSockPeerAddrKey { | ||
| hashedIPCount++ | ||
| value := attr.Value.AsString() | ||
| require.Len(t, value, 64) | ||
| require.NotEqual(t, "[REDACTED]", value) | ||
| } | ||
| } | ||
| } | ||
| require.Positive(t, hashedIPCount) | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.