Skip to content

Commit e69ca35

Browse files
authored
[service] ensure endpoint is prefixed w/ scheme (#12258)
#### Description Users can enter the OTLP endpoint w/o a scheme in the prefix. This causes issues with the URL parsing code in the config package. Fixes #12254 --------- Signed-off-by: Alex Boten <[email protected]>
1 parent a6aaf37 commit e69ca35

File tree

10 files changed

+129
-7
lines changed

10 files changed

+129
-7
lines changed

.chloggen/codeboten_fix-12254.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: 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: Preserve URL normalization logic that was present before.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12254]
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: []
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package migration // import "go.opentelemetry.io/collector/service/telemetry/internal/migration"
5+
6+
import "strings"
7+
8+
func normalizeEndpoint(endpoint string) *string {
9+
if !strings.HasPrefix(endpoint, "https://") && !strings.HasPrefix(endpoint, "http://") {
10+
endpoint = "http://" + endpoint
11+
}
12+
return &endpoint
13+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ processors:
1010
- simple:
1111
exporter:
1212
console: {}
13+
- simple:
14+
exporter:
15+
otlp:
16+
protocol: http/protobuf
17+
endpoint: http://127.0.0.1:4317
18+
headers:
19+
"key1": "value1"

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ processors:
1010
- simple:
1111
exporter:
1212
console: {}
13+
- simple:
14+
exporter:
15+
otlp:
16+
protocol: http/protobuf
17+
endpoint: http://127.0.0.1:4317
18+
headers:
19+
"key1": "value1"

service/telemetry/internal/migration/testdata/v0.3.0_logs.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ processors:
1111
- simple:
1212
exporter:
1313
console: {}
14+
- simple:
15+
exporter:
16+
otlp:
17+
protocol: http/protobuf
18+
endpoint: http://127.0.0.1:4317
19+
headers:
20+
- name: "key1"
21+
value: "value1"

service/telemetry/internal/migration/testdata/v0.3.0_traces.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ processors:
1111
- simple:
1212
exporter:
1313
console: {}
14+
- simple:
15+
exporter:
16+
otlp:
17+
protocol: http/protobuf
18+
endpoint: http://127.0.0.1:4317
19+
headers:
20+
- name: "key1"
21+
value: "value1"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func otlpV02ToV03(in *configv02.OTLP) *config.OTLP {
5757
ClientCertificate: in.ClientCertificate,
5858
ClientKey: in.ClientKey,
5959
Compression: in.Compression,
60-
Endpoint: &in.Endpoint,
60+
Endpoint: normalizeEndpoint(in.Endpoint),
6161
Insecure: in.Insecure,
6262
Protocol: &in.Protocol,
6363
Timeout: in.Timeout,
@@ -74,7 +74,7 @@ func otlpMetricV02ToV03(in *configv02.OTLPMetric) *config.OTLPMetric {
7474
ClientCertificate: in.ClientCertificate,
7575
ClientKey: in.ClientKey,
7676
Compression: in.Compression,
77-
Endpoint: &in.Endpoint,
77+
Endpoint: normalizeEndpoint(in.Endpoint),
7878
Insecure: in.Insecure,
7979
Protocol: &in.Protocol,
8080
Timeout: in.Timeout,

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func TestUnmarshalLogsConfigV020(t *testing.T) {
1818

1919
cfg := LogsConfigV030{}
2020
require.NoError(t, cm.Unmarshal(&cfg))
21-
require.Len(t, cfg.Processors, 2)
21+
require.Len(t, cfg.Processors, 3)
22+
// check the endpoint is prefixed w/ http
23+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[0].Batch.Exporter.OTLP.Endpoint)
24+
// check the endpoint is prefixed w/ http
25+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
2226
}
2327

2428
func TestUnmarshalTracesConfigV020(t *testing.T) {
@@ -27,7 +31,11 @@ func TestUnmarshalTracesConfigV020(t *testing.T) {
2731

2832
cfg := TracesConfigV030{}
2933
require.NoError(t, cm.Unmarshal(&cfg))
30-
require.Len(t, cfg.Processors, 2)
34+
require.Len(t, cfg.Processors, 3)
35+
// check the endpoint is prefixed w/ http
36+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[0].Batch.Exporter.OTLP.Endpoint)
37+
// check the endpoint is prefixed w/ http
38+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
3139
}
3240

3341
func TestUnmarshalMetricsConfigV020(t *testing.T) {
@@ -37,4 +45,6 @@ func TestUnmarshalMetricsConfigV020(t *testing.T) {
3745
cfg := MetricsConfigV030{}
3846
require.NoError(t, cm.Unmarshal(&cfg))
3947
require.Len(t, cfg.Readers, 2)
48+
// check the endpoint is prefixed w/ http
49+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Readers[0].Periodic.Exporter.OTLP.Endpoint)
4050
}

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ func (c *TracesConfigV030) Unmarshal(conf *confmap.Conf) error {
3939
// TODO: add a warning log to tell users to migrate their config
4040
return tracesConfigV02ToV03(v2TracesConfig, c)
4141
}
42+
// ensure endpoint normalization occurs
43+
for _, r := range c.Processors {
44+
if r.Batch != nil {
45+
if r.Batch.Exporter.OTLP != nil {
46+
r.Batch.Exporter.OTLP.Endpoint = normalizeEndpoint(*r.Batch.Exporter.OTLP.Endpoint)
47+
}
48+
}
49+
if r.Simple != nil {
50+
if r.Simple.Exporter.OTLP != nil && r.Simple.Exporter.OTLP.Endpoint != nil {
51+
r.Simple.Exporter.OTLP.Endpoint = normalizeEndpoint(*r.Simple.Exporter.OTLP.Endpoint)
52+
}
53+
}
54+
}
4255
return nil
4356
}
4457

@@ -70,6 +83,14 @@ func (c *MetricsConfigV030) Unmarshal(conf *confmap.Conf) error {
7083
// TODO: add a warning log to tell users to migrate their config
7184
return metricsConfigV02ToV03(v02, c)
7285
}
86+
// ensure endpoint normalization occurs
87+
for _, r := range c.Readers {
88+
if r.Periodic != nil {
89+
if r.Periodic.Exporter.OTLP != nil && r.Periodic.Exporter.OTLP.Endpoint != nil {
90+
r.Periodic.Exporter.OTLP.Endpoint = normalizeEndpoint(*r.Periodic.Exporter.OTLP.Endpoint)
91+
}
92+
}
93+
}
7394
return nil
7495
}
7596

@@ -169,6 +190,18 @@ func (c *LogsConfigV030) Unmarshal(conf *confmap.Conf) error {
169190
// TODO: add a warning log to tell users to migrate their config
170191
return logsConfigV02ToV03(v02, c)
171192
}
172-
//
193+
// ensure endpoint normalization occurs
194+
for _, r := range c.Processors {
195+
if r.Batch != nil {
196+
if r.Batch.Exporter.OTLP != nil {
197+
r.Batch.Exporter.OTLP.Endpoint = normalizeEndpoint(*r.Batch.Exporter.OTLP.Endpoint)
198+
}
199+
}
200+
if r.Simple != nil {
201+
if r.Simple.Exporter.OTLP != nil && r.Simple.Exporter.OTLP.Endpoint != nil {
202+
r.Simple.Exporter.OTLP.Endpoint = normalizeEndpoint(*r.Simple.Exporter.OTLP.Endpoint)
203+
}
204+
}
205+
}
173206
return nil
174207
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func TestUnmarshalLogsConfigV030(t *testing.T) {
1818

1919
cfg := LogsConfigV030{}
2020
require.NoError(t, cm.Unmarshal(&cfg))
21-
require.Len(t, cfg.Processors, 2)
21+
require.Len(t, cfg.Processors, 3)
22+
// check the endpoint is prefixed w/ http
23+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[0].Batch.Exporter.OTLP.Endpoint)
24+
// check the endpoint is prefixed w/ http
25+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
2226
}
2327

2428
func TestUnmarshalTracesConfigV030(t *testing.T) {
@@ -27,7 +31,11 @@ func TestUnmarshalTracesConfigV030(t *testing.T) {
2731

2832
cfg := TracesConfigV030{}
2933
require.NoError(t, cm.Unmarshal(&cfg))
30-
require.Len(t, cfg.Processors, 2)
34+
require.Len(t, cfg.Processors, 3)
35+
// check the endpoint is prefixed w/ http
36+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[0].Batch.Exporter.OTLP.Endpoint)
37+
// check the endpoint is prefixed w/ http
38+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
3139
}
3240

3341
func TestUnmarshalMetricsConfigV030(t *testing.T) {
@@ -37,4 +45,7 @@ func TestUnmarshalMetricsConfigV030(t *testing.T) {
3745
cfg := MetricsConfigV030{}
3846
require.NoError(t, cm.Unmarshal(&cfg))
3947
require.Len(t, cfg.Readers, 2)
48+
49+
// check the endpoint is prefixed w/ http
50+
require.Equal(t, "http://127.0.0.1:4317", *cfg.Readers[0].Periodic.Exporter.OTLP.Endpoint)
4051
}

0 commit comments

Comments
 (0)