Skip to content

Commit

Permalink
fix: Truncate string slice attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericywl committed Jan 27, 2025
1 parent 58bbb4a commit d0ccb60
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 47 deletions.
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

0 comments on commit d0ccb60

Please sign in to comment.