This is an automated email from the ASF dual-hosted git repository.

Alanxtl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/develop by this push:
     new 17c747867 fix(otel): fix metadataSupplier Get/Set type mismatch 
breaking trace propagation (#3433)
17c747867 is described below

commit 17c74786787d0a63b73747b2ccbeafd6b1eaf2a0
Author: aias00 <[email protected]>
AuthorDate: Wed Jul 8 10:56:39 2026 +0800

    fix(otel): fix metadataSupplier Get/Set type mismatch breaking trace 
propagation (#3433)
    
    Set stored values as string but Get expected []string, causing type
    assertion failure when OTel propagator Inject followed by Extract.
    Trace context (traceparent etc.) was lost during propagation.
    
    - Set: store []string{value} instead of string, consistent with
      triple's generateAttachments which stores http.Header values as
      []string
    - Get: add defensive string type handling alongside []string, matching
      RPCInvocation.GetAttachment's pattern for robustness
    
    Co-authored-by: Claude <[email protected]>
---
 filter/otel/trace/attachment.go      | 17 ++++++++++-----
 filter/otel/trace/attachment_test.go | 40 +++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/filter/otel/trace/attachment.go b/filter/otel/trace/attachment.go
index f6d88e76e..e057b7380 100644
--- a/filter/otel/trace/attachment.go
+++ b/filter/otel/trace/attachment.go
@@ -39,21 +39,28 @@ 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 {
+       switch v := val.(type) {
+       case []string:
+               if len(v) > 0 {
+                       return v[0]
+               }
+               return ""
+       case string:
+               return v
+       default:
                return ""
        }
-       return item[0]
 }
 
 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 {
diff --git a/filter/otel/trace/attachment_test.go 
b/filter/otel/trace/attachment_test.go
index b3f80df44..4cfca8db6 100644
--- a/filter/otel/trace/attachment_test.go
+++ b/filter/otel/trace/attachment_test.go
@@ -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) {
@@ -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)
+       }
+}

Reply via email to