[service] fix v0.2.0 to v0.3.0 conversion bug (#12438)

Converting headers from config schema v0.2.0 to v0.3.0 was causing a nil
dereferencing issue by incorrectly setting the name/value pair to a nil
pointer. Added a test in both the loading of the config in otelcol, as
well as the migration code unit test.

Fixes #12439

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Co-authored-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Alex Boten 2025-02-20 11:53:07 -08:00 committed by GitHub
parent b0c12ba6ca
commit 782749dd31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 60 additions and 4 deletions

View File

@ -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: bug_fix
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: service
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix crash at startup when converting from v0.2.0 to v0.3.0
# One or more tracking issues or pull requests related to the change
issues: [12438]
# (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: []

View File

@ -391,6 +391,7 @@ func TestCollectorRun(t *testing.T) {
}{
{file: "otelcol-noreaders.yaml"},
{file: "otelcol-emptyreaders.yaml"},
{file: "otelcol-multipleheaders.yaml"},
}
for _, tt := range tests {

View File

@ -0,0 +1,24 @@
receivers:
nop:
exporters:
nop:
service:
telemetry:
metrics:
level: none
traces:
processors:
- batch:
exporter:
otlp:
endpoint: localhost:4318
headers:
first: val1
second: val2
protocol: http/protobuf
pipelines:
metrics:
receivers: [nop]
exporters: [nop]

View File

@ -6,6 +6,7 @@ readers:
endpoint: 127.0.0.1:4317
headers:
"key1": "value1"
"key2": "value2"
- pull:
exporter:
prometheus:

View File

@ -37,13 +37,12 @@ type logsConfigV020 struct {
}
func headersV02ToV03(in configv02.Headers) []config.NameStringValuePair {
headers := make([]config.NameStringValuePair, len(in))
idx := 0
headers := make([]config.NameStringValuePair, 0, len(in))
for k, v := range in {
headers[idx] = config.NameStringValuePair{
headers = append(headers, config.NameStringValuePair{
Name: k,
Value: &v,
}
})
}
return headers
}

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
config "go.opentelemetry.io/contrib/config/v0.3.0"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap/confmaptest"
@ -58,6 +59,11 @@ func TestUnmarshalMetricsConfigV020(t *testing.T) {
require.Len(t, cfg.Readers, 2)
// check the endpoint is prefixed w/ http
require.Equal(t, "http://127.0.0.1:4317", *cfg.Readers[0].Periodic.Exporter.OTLP.Endpoint)
require.ElementsMatch(t, []config.NameStringValuePair{{Name: "key1", Value: ptr("value1")}, {Name: "key2", Value: ptr("value2")}}, cfg.Readers[0].Periodic.Exporter.OTLP.Headers)
// ensure defaults set in the original config object are not lost
require.Equal(t, configtelemetry.LevelBasic, cfg.Level)
}
func ptr[T any](v T) *T {
return &v
}