Skip to content
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
25 changes: 25 additions & 0 deletions .chloggen/client-metadata-keys.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: client

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for iterating over client metadata keys

# One or more tracking issues or pull requests related to the change
issues: [12804]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
7 changes: 7 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ package client // import "go.opentelemetry.io/collector/client"

import (
"context"
"iter"
"maps"
"net"
"strings"
)
Expand Down Expand Up @@ -148,6 +150,11 @@ func NewMetadata(md map[string][]string) Metadata {
}
}

// Keys returns an iterator for the metadata keys.
func (m Metadata) Keys() iter.Seq[string] {
return maps.Keys(m.data)
}

// Get gets the value of the key from metadata, returning a copy.
// The key lookup is case-insensitive.
func (m Metadata) Get(key string) []string {
Expand Down
3 changes: 3 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package client
import (
"context"
"net"
"slices"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestFromContext(t *testing.T) {
func TestMetadata(t *testing.T) {
source := map[string][]string{"test-key": {"test-val"}, "TEST-KEY-2": {"test-val"}}
md := NewMetadata(source)
assert.Equal(t, []string{"test-key", "test-key-2"}, slices.Sorted(md.Keys()))
assert.Equal(t, []string{"test-val"}, md.Get("test-key"))
assert.Equal(t, []string{"test-val"}, md.Get("test-KEY")) // case insensitive lookup
assert.Equal(t, []string{"test-val"}, md.Get("test-key-2")) // case insensitive lookup
Expand All @@ -93,5 +95,6 @@ func TestMetadata(t *testing.T) {

func TestUninstantiatedMetadata(t *testing.T) {
i := Info{}
assert.Empty(t, slices.Collect(i.Metadata.Keys()))
assert.Empty(t, i.Metadata.Get("test"))
}
Loading