Skip to content

Commit 3068122

Browse files
authored
client: add Metadata.Keys method (#12805)
#### Description Add support for iterating over the client metadata keys. At the moment it is only possible to get the values for a known key, which makes it impossible to inspect metadata keys without prior knowledge of all possible keys, e.g. as described in open-telemetry/opentelemetry-collector-contrib#39157. #### Link to tracking issue Closes #12804 #### Testing Trivial addition, added unit tests. #### Documentation N/A
1 parent 6b9d125 commit 3068122

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

.chloggen/client-metadata-keys.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: client
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support for iterating over client metadata keys
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12804]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

client/client.go

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ package client // import "go.opentelemetry.io/collector/client"
7979

8080
import (
8181
"context"
82+
"iter"
83+
"maps"
8284
"net"
8385
"strings"
8486
)
@@ -148,6 +150,11 @@ func NewMetadata(md map[string][]string) Metadata {
148150
}
149151
}
150152

153+
// Keys returns an iterator for the metadata keys.
154+
func (m Metadata) Keys() iter.Seq[string] {
155+
return maps.Keys(m.data)
156+
}
157+
151158
// Get gets the value of the key from metadata, returning a copy.
152159
// The key lookup is case-insensitive.
153160
func (m Metadata) Get(key string) []string {

client/client_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package client
88
import (
99
"context"
1010
"net"
11+
"slices"
1112
"testing"
1213

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

9496
func TestUninstantiatedMetadata(t *testing.T) {
9597
i := Info{}
98+
assert.Empty(t, slices.Collect(i.Metadata.Keys()))
9699
assert.Empty(t, i.Metadata.Get("test"))
97100
}

0 commit comments

Comments
 (0)