Skip to content

Commit 03b5c48

Browse files
committed
Optimize xpdata/context for persistent queue when only one value for key
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 336f54c commit 03b5c48

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

.chloggen/optimize-context.yaml

Lines changed: 25 additions & 0 deletions
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: xpdata
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Optimize xpdata/context for persistent queue when only one value for key
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13636]
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: [user]

pdata/xpdata/request/context.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ import (
1010
"go.opentelemetry.io/otel/trace"
1111

1212
"go.opentelemetry.io/collector/client"
13-
pdataint "go.opentelemetry.io/collector/pdata/internal"
14-
protocommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1"
15-
"go.opentelemetry.io/collector/pdata/pcommon"
13+
otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1"
1614
"go.opentelemetry.io/collector/pdata/xpdata/request/internal"
1715
)
1816

19-
var readOnlyState = pdataint.StateReadOnly
20-
2117
// encodeContext encodes the context into a map of strings.
2218
func encodeContext(ctx context.Context) internal.RequestContext {
2319
rc := internal.RequestContext{}
@@ -45,19 +41,25 @@ func encodeSpanContext(ctx context.Context, rc *internal.RequestContext) {
4541

4642
func encodeClientMetadata(ctx context.Context, rc *internal.RequestContext) {
4743
clientMetadata := client.FromContext(ctx).Metadata
48-
metadataMap, metadataFound := pcommon.Map{}, false
4944
for k := range clientMetadata.Keys() {
50-
if !metadataFound {
51-
metadataMap, metadataFound = pcommon.NewMap(), true
52-
}
53-
vals := metadataMap.PutEmptySlice(k)
54-
for i := 0; i < len(clientMetadata.Get(k)); i++ {
55-
vals.AppendEmpty().SetStr(clientMetadata.Get(k)[i])
45+
vals := clientMetadata.Get(k)
46+
switch len(vals) {
47+
case 1:
48+
rc.ClientMetadata = append(rc.ClientMetadata, otlpcommon.KeyValue{
49+
Key: k,
50+
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: vals[0]}},
51+
})
52+
default:
53+
metadataArray := make([]otlpcommon.AnyValue, 0, len(vals))
54+
for i := 0; i < len(vals); i++ {
55+
metadataArray = append(metadataArray, otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: vals[i]}})
56+
}
57+
rc.ClientMetadata = append(rc.ClientMetadata, otlpcommon.KeyValue{
58+
Key: k,
59+
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{Values: metadataArray}}},
60+
})
5661
}
5762
}
58-
if metadataFound {
59-
rc.ClientMetadata = *pdataint.GetOrigMap(pdataint.Map(metadataMap))
60-
}
6163
}
6264

6365
func encodeClientAddress(ctx context.Context, rc *internal.RequestContext) {
@@ -122,15 +124,21 @@ func decodeSpanContext(ctx context.Context, sc *internal.SpanContext) context.Co
122124
}))
123125
}
124126

125-
func decodeClientMetadata(clientMetadata []protocommon.KeyValue) map[string][]string {
127+
func decodeClientMetadata(clientMetadata []otlpcommon.KeyValue) map[string][]string {
126128
if len(clientMetadata) == 0 {
127129
return nil
128130
}
129131
metadataMap := make(map[string][]string, len(clientMetadata))
130-
for k, vals := range pcommon.Map(pdataint.NewMap(&clientMetadata, &readOnlyState)).All() {
131-
metadataMap[k] = make([]string, vals.Slice().Len())
132-
for i, v := range vals.Slice().All() {
133-
metadataMap[k][i] = v.Str()
132+
for _, kv := range clientMetadata {
133+
switch val := kv.Value.Value.(type) {
134+
case *otlpcommon.AnyValue_StringValue:
135+
metadataMap[kv.Key] = make([]string, 1)
136+
metadataMap[kv.Key][0] = val.StringValue
137+
case *otlpcommon.AnyValue_ArrayValue:
138+
metadataMap[kv.Key] = make([]string, len(val.ArrayValue.Values))
139+
for i, v := range val.ArrayValue.Values {
140+
metadataMap[kv.Key][i] = v.GetStringValue()
141+
}
134142
}
135143
}
136144
return metadataMap

0 commit comments

Comments
 (0)