Skip to content
Open
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
17 changes: 11 additions & 6 deletions filter/otel/trace/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@ func (s *metadataSupplier) Get(key string) string {
if s.metadata == nil {
return ""
}
item, ok := s.metadata[key].([]string)
if !ok {
val, ok := s.metadata[key]
if !ok || val == nil {
return ""
}
if len(item) == 0 {
return ""
// Handle []string (produced by triple's generateAttachments and by Set below)
if item, ok := val.([]string); ok && len(item) > 0 {
return item[0]
}
// Handle string (defensive: for any externally-set string values)
if str, ok := val.(string); ok {
return str
}
return item[0]
return ""
}

func (s *metadataSupplier) Set(key string, value string) {
if s.metadata == nil {
s.metadata = map[string]any{}
}
s.metadata[key] = value
s.metadata[key] = []string{value}
}

func (s *metadataSupplier) Keys() []string {
Expand Down
40 changes: 39 additions & 1 deletion filter/otel/trace/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,29 @@ func Test_metadataSupplier_Get(t *testing.T) {
want: "",
},
{
name: "test",
name: "slice value",
metadata: map[string]any{
"key": []string{"test"},
},
key: "key",
want: "test",
},
{
name: "string value (defensive read)",
metadata: map[string]any{
"key": "test",
},
key: "key",
want: "test",
},
{
name: "non-string non-slice value",
metadata: map[string]any{
"key": 123,
},
key: "key",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -129,3 +145,25 @@ func Test_metadataSupplier_Get(t *testing.T) {
})
}
}

func Test_metadataSupplier_SetGetRoundTrip(t *testing.T) {
// This is the core bug scenario: Set stores a value, Get must be able to read it back.
s := &metadataSupplier{
metadata: map[string]any{},
}
s.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")

got := s.Get("traceparent")
if got != "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" {
t.Errorf("round-trip Get() = %q, want traceparent value", got)
}

// Verify the stored type is []string, consistent with triple's generateAttachments
stored, ok := s.metadata["traceparent"].([]string)
if !ok {
t.Errorf("Set stored type %T, want []string", s.metadata["traceparent"])
}
if len(stored) != 1 || stored[0] != "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" {
t.Errorf("stored value = %v, want [traceparent]", stored)
}
}
Loading