diff --git a/.chloggen/optimize-context.yaml b/.chloggen/optimize-context.yaml new file mode 100644 index 00000000000..d2f884205f9 --- /dev/null +++ b/.chloggen/optimize-context.yaml @@ -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: xpdata + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Optimize xpdata/context for persistent queue when only one value for key + +# One or more tracking issues or pull requests related to the change +issues: [13636] + +# (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: [user] diff --git a/pdata/xpdata/request/context.go b/pdata/xpdata/request/context.go index 91c833731c1..a547f094039 100644 --- a/pdata/xpdata/request/context.go +++ b/pdata/xpdata/request/context.go @@ -10,14 +10,10 @@ import ( "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/collector/client" - pdataint "go.opentelemetry.io/collector/pdata/internal" - protocommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" - "go.opentelemetry.io/collector/pdata/pcommon" + otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" "go.opentelemetry.io/collector/pdata/xpdata/request/internal" ) -var readOnlyState = pdataint.StateReadOnly - // encodeContext encodes the context into a map of strings. func encodeContext(ctx context.Context) internal.RequestContext { rc := internal.RequestContext{} @@ -45,19 +41,25 @@ func encodeSpanContext(ctx context.Context, rc *internal.RequestContext) { func encodeClientMetadata(ctx context.Context, rc *internal.RequestContext) { clientMetadata := client.FromContext(ctx).Metadata - metadataMap, metadataFound := pcommon.Map{}, false for k := range clientMetadata.Keys() { - if !metadataFound { - metadataMap, metadataFound = pcommon.NewMap(), true - } - vals := metadataMap.PutEmptySlice(k) - for i := 0; i < len(clientMetadata.Get(k)); i++ { - vals.AppendEmpty().SetStr(clientMetadata.Get(k)[i]) + vals := clientMetadata.Get(k) + switch len(vals) { + case 1: + rc.ClientMetadata = append(rc.ClientMetadata, otlpcommon.KeyValue{ + Key: k, + Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: vals[0]}}, + }) + default: + metadataArray := make([]otlpcommon.AnyValue, 0, len(vals)) + for i := 0; i < len(vals); i++ { + metadataArray = append(metadataArray, otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: vals[i]}}) + } + rc.ClientMetadata = append(rc.ClientMetadata, otlpcommon.KeyValue{ + Key: k, + Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{Values: metadataArray}}}, + }) } } - if metadataFound { - rc.ClientMetadata = *pdataint.GetOrigMap(pdataint.Map(metadataMap)) - } } func encodeClientAddress(ctx context.Context, rc *internal.RequestContext) { @@ -122,15 +124,21 @@ func decodeSpanContext(ctx context.Context, sc *internal.SpanContext) context.Co })) } -func decodeClientMetadata(clientMetadata []protocommon.KeyValue) map[string][]string { +func decodeClientMetadata(clientMetadata []otlpcommon.KeyValue) map[string][]string { if len(clientMetadata) == 0 { return nil } metadataMap := make(map[string][]string, len(clientMetadata)) - for k, vals := range pcommon.Map(pdataint.NewMap(&clientMetadata, &readOnlyState)).All() { - metadataMap[k] = make([]string, vals.Slice().Len()) - for i, v := range vals.Slice().All() { - metadataMap[k][i] = v.Str() + for _, kv := range clientMetadata { + switch val := kv.Value.Value.(type) { + case *otlpcommon.AnyValue_StringValue: + metadataMap[kv.Key] = make([]string, 1) + metadataMap[kv.Key][0] = val.StringValue + case *otlpcommon.AnyValue_ArrayValue: + metadataMap[kv.Key] = make([]string, len(val.ArrayValue.Values)) + for i, v := range val.ArrayValue.Values { + metadataMap[kv.Key][i] = v.GetStringValue() + } } } return metadataMap