Skip to content
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

fix: Truncate string slice attributes in OTLP labels #434

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion input/otlp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func setLabel(key string, event *modelpb.APMEvent, v pcommon.Value) {
for i := 0; i < s.Len(); i++ {
r := s.At(i)
if r.Type() == pcommon.ValueTypeStr {
result = append(result, r.Str())
result = append(result, truncate(r.Str()))
}
}
modelpb.Labels(event.Labels).SetSlice(key, result)
Expand Down
117 changes: 71 additions & 46 deletions input/otlp/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package otlp_test

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -350,53 +351,77 @@ func TestResourceConventions(t *testing.T) {
// This test ensures that the values are properly translated,
// and that heterogeneous array elements are dropped without a panic.
func TestResourceLabels(t *testing.T) {
metadata := transformResourceMetadata(t, map[string]interface{}{
"string_value": "abc",
"bool_value": true,
"int_value": 123,
"float_value": 1.23,
"string_array": []interface{}{"abc", "def", true, 123, 1.23, nil},
"bool_array": []interface{}{true, false, "true", 123, 1.23, nil},
"int_array": []interface{}{123, 456, "abc", true, 1.23, nil},
"float_array": []interface{}{1.23, 4.56, "abc", true, 123, nil},
"empty_array": []interface{}{}, // Ensure that an empty array is ignored.
t.Run("drop invalid", func(t *testing.T) {
metadata := transformResourceMetadata(t, map[string]interface{}{
"string_value": "abc",
"bool_value": true,
"int_value": 123,
"float_value": 1.23,
"string_array": []interface{}{"abc", "def", true, 123, 1.23, nil},
"bool_array": []interface{}{true, false, "true", 123, 1.23, nil},
"int_array": []interface{}{123, 456, "abc", true, 1.23, nil},
"float_array": []interface{}{1.23, 4.56, "abc", true, 123, nil},
"empty_array": []interface{}{}, // Ensure that an empty array is ignored.
})
assert.Equal(t, modelpb.Labels{
"string_value": {
Global: true,
Value: "abc",
},
"bool_value": {
Global: true,
Value: "true",
},
"string_array": {
Global: true,
Values: []string{"abc", "def"},
},
"bool_array": {
Global: true,
Values: []string{"true", "false"},
},
}, modelpb.Labels(metadata.Labels))
assert.Equal(t, modelpb.NumericLabels{
"int_value": {
Global: true,
Value: 123,
},
"float_value": {
Global: true,
Value: 1.23,
},
"int_array": {
Global: true,
Values: []float64{123, 456},
},
"float_array": {
Global: true,
Values: []float64{1.23, 4.56},
},
}, modelpb.NumericLabels(metadata.NumericLabels))
})

t.Run("string truncate", func(t *testing.T) {
metadata := transformResourceMetadata(t, map[string]interface{}{
"string_value": strings.Repeat("a", 2000),
"string_array": []interface{}{strings.Repeat("a", 2000)},
})

assert.Equal(t,
// Expect to truncate to keywordLength
modelpb.Labels{
"string_value": {
Global: true,
Value: strings.Repeat("a", 1024),
},
"string_array": {
Global: true,
Values: []string{strings.Repeat("a", 1024)},
},
},
modelpb.Labels(metadata.Labels),
)
})
assert.Equal(t, modelpb.Labels{
"string_value": {
Global: true,
Value: "abc",
},
"bool_value": {
Global: true,
Value: "true",
},
"string_array": {
Global: true,
Values: []string{"abc", "def"},
},
"bool_array": {
Global: true,
Values: []string{"true", "false"},
},
}, modelpb.Labels(metadata.Labels))
assert.Equal(t, modelpb.NumericLabels{
"int_value": {
Global: true,
Value: 123,
},
"float_value": {
Global: true,
Value: 1.23,
},
"int_array": {
Global: true,
Values: []float64{123, 456},
},
"float_array": {
Global: true,
Values: []float64{1.23, 4.56},
},
}, modelpb.NumericLabels(metadata.NumericLabels))
}

func transformResourceMetadata(t *testing.T, resourceAttrs map[string]interface{}) *modelpb.APMEvent {
Expand Down
Loading