Skip to content

Commit 782749d

Browse files
[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 <[email protected]> Co-authored-by: Bogdan Drutu <[email protected]>
1 parent b0c12ba commit 782749d

File tree

6 files changed

+60
-4
lines changed

6 files changed

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

otelcol/collector_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ func TestCollectorRun(t *testing.T) {
391391
}{
392392
{file: "otelcol-noreaders.yaml"},
393393
{file: "otelcol-emptyreaders.yaml"},
394+
{file: "otelcol-multipleheaders.yaml"},
394395
}
395396

396397
for _, tt := range tests {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
receivers:
2+
nop:
3+
4+
exporters:
5+
nop:
6+
7+
service:
8+
telemetry:
9+
metrics:
10+
level: none
11+
traces:
12+
processors:
13+
- batch:
14+
exporter:
15+
otlp:
16+
endpoint: localhost:4318
17+
headers:
18+
first: val1
19+
second: val2
20+
protocol: http/protobuf
21+
pipelines:
22+
metrics:
23+
receivers: [nop]
24+
exporters: [nop]

service/telemetry/internal/migration/testdata/v0.2.0_metrics.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readers:
66
endpoint: 127.0.0.1:4317
77
headers:
88
"key1": "value1"
9+
"key2": "value2"
910
- pull:
1011
exporter:
1112
prometheus:

service/telemetry/internal/migration/v0.2.0.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ type logsConfigV020 struct {
3737
}
3838

3939
func headersV02ToV03(in configv02.Headers) []config.NameStringValuePair {
40-
headers := make([]config.NameStringValuePair, len(in))
41-
idx := 0
40+
headers := make([]config.NameStringValuePair, 0, len(in))
4241
for k, v := range in {
43-
headers[idx] = config.NameStringValuePair{
42+
headers = append(headers, config.NameStringValuePair{
4443
Name: k,
4544
Value: &v,
46-
}
45+
})
4746
}
4847
return headers
4948
}

service/telemetry/internal/migration/v0.2.0_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/require"
11+
config "go.opentelemetry.io/contrib/config/v0.3.0"
1112

1213
"go.opentelemetry.io/collector/config/configtelemetry"
1314
"go.opentelemetry.io/collector/confmap/confmaptest"
@@ -58,6 +59,11 @@ func TestUnmarshalMetricsConfigV020(t *testing.T) {
5859
require.Len(t, cfg.Readers, 2)
5960
// check the endpoint is prefixed w/ http
6061
require.Equal(t, "http://127.0.0.1:4317", *cfg.Readers[0].Periodic.Exporter.OTLP.Endpoint)
62+
require.ElementsMatch(t, []config.NameStringValuePair{{Name: "key1", Value: ptr("value1")}, {Name: "key2", Value: ptr("value2")}}, cfg.Readers[0].Periodic.Exporter.OTLP.Headers)
6163
// ensure defaults set in the original config object are not lost
6264
require.Equal(t, configtelemetry.LevelBasic, cfg.Level)
6365
}
66+
67+
func ptr[T any](v T) *T {
68+
return &v
69+
}

0 commit comments

Comments
 (0)