Skip to content

Commit 58370e5

Browse files
authored
Avoid using go embedded messages in Config (#12718)
This PR: * Helps with #12709, to avoid the situation where we need to add Unmarshal for a struct and if used as embedded will not work. * Helps with avoiding name conflicts between variables. Will allow for example to have a "Timeout" member in both QueueConfig and ClientConfig, even though that was a valid YAML config since `QueueConfig` is under "seding_queue". * This is a breaking change only if devs are created manually the OTLP configs which should be very rare, and unfortunately this is hard to do using deprecation steps. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent abcee1f commit 58370e5

15 files changed

+72
-48
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: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: otlpreceiver/otlpexporter/otlphttpexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Avoid using go embedded messages in Config
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12718]
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: [api]

exporter/otlpexporter/config.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ import (
1919

2020
// Config defines configuration for OTLP exporter.
2121
type Config struct {
22-
exporterhelper.TimeoutConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
23-
exporterhelper.QueueConfig `mapstructure:"sending_queue"`
24-
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
22+
TimeoutConfig exporterhelper.TimeoutConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
23+
QueueConfig exporterhelper.QueueConfig `mapstructure:"sending_queue"`
24+
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
25+
ClientConfig configgrpc.ClientConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
2526

2627
// Experimental: This configuration is at the early stage of development and may change without backward compatibility
2728
// until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved
2829
BatcherConfig exporterhelper.BatcherConfig `mapstructure:"batcher"`
29-
30-
configgrpc.ClientConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
3130
}
3231

3332
func (c *Config) Validate() error {
@@ -50,15 +49,15 @@ func (c *Config) Validate() error {
5049

5150
func (c *Config) sanitizedEndpoint() string {
5251
switch {
53-
case strings.HasPrefix(c.Endpoint, "http://"):
54-
return strings.TrimPrefix(c.Endpoint, "http://")
55-
case strings.HasPrefix(c.Endpoint, "https://"):
56-
return strings.TrimPrefix(c.Endpoint, "https://")
57-
case strings.HasPrefix(c.Endpoint, "dns://"):
52+
case strings.HasPrefix(c.ClientConfig.Endpoint, "http://"):
53+
return strings.TrimPrefix(c.ClientConfig.Endpoint, "http://")
54+
case strings.HasPrefix(c.ClientConfig.Endpoint, "https://"):
55+
return strings.TrimPrefix(c.ClientConfig.Endpoint, "https://")
56+
case strings.HasPrefix(c.ClientConfig.Endpoint, "dns://"):
5857
r := regexp.MustCompile("^dns://[/]?")
59-
return r.ReplaceAllString(c.Endpoint, "")
58+
return r.ReplaceAllString(c.ClientConfig.Endpoint, "")
6059
default:
61-
return c.Endpoint
60+
return c.ClientConfig.Endpoint
6261
}
6362
}
6463

exporter/otlpexporter/config_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ func TestUnmarshalInvalidConfig(t *testing.T) {
144144
func TestValidDNSEndpoint(t *testing.T) {
145145
factory := NewFactory()
146146
cfg := factory.CreateDefaultConfig().(*Config)
147-
cfg.Endpoint = "dns://authority/backend.example.com:4317"
147+
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317"
148148
assert.NoError(t, cfg.Validate())
149149
}
150150

151151
func TestSanitizeEndpoint(t *testing.T) {
152152
factory := NewFactory()
153153
cfg := factory.CreateDefaultConfig().(*Config)
154-
cfg.Endpoint = "dns://authority/backend.example.com:4317"
154+
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317"
155155
assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint())
156-
cfg.Endpoint = "dns:///backend.example.com:4317"
156+
cfg.ClientConfig.Endpoint = "dns:///backend.example.com:4317"
157157
assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint())
158-
cfg.Endpoint = "dns:////backend.example.com:4317"
158+
cfg.ClientConfig.Endpoint = "dns:////backend.example.com:4317"
159159
assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint())
160160
}

exporter/otlpexporter/factory_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreateDefaultConfig(t *testing.T) {
3434
assert.Equal(t, configretry.NewDefaultBackOffConfig(), ocfg.RetryConfig)
3535
assert.Equal(t, exporterhelper.NewDefaultQueueConfig(), ocfg.QueueConfig)
3636
assert.Equal(t, exporterhelper.NewDefaultTimeoutConfig(), ocfg.TimeoutConfig)
37-
assert.Equal(t, configcompression.TypeGzip, ocfg.Compression)
37+
assert.Equal(t, configcompression.TypeGzip, ocfg.ClientConfig.Compression)
3838
}
3939

4040
func TestCreateMetrics(t *testing.T) {

exporter/otlphttpexporter/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func (e *EncodingType) UnmarshalText(text []byte) error {
4545

4646
// Config defines configuration for OTLP/HTTP exporter.
4747
type Config struct {
48-
confighttp.ClientConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
49-
exporterhelper.QueueConfig `mapstructure:"sending_queue"`
50-
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
48+
ClientConfig confighttp.ClientConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
49+
QueueConfig exporterhelper.QueueConfig `mapstructure:"sending_queue"`
50+
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
5151

5252
// The URL to send traces to. If omitted the Endpoint + "/v1/traces" will be used.
5353
TracesEndpoint string `mapstructure:"traces_endpoint"`
@@ -66,7 +66,7 @@ var _ component.Config = (*Config)(nil)
6666

6767
// Validate checks if the exporter configuration is valid
6868
func (cfg *Config) Validate() error {
69-
if cfg.Endpoint == "" && cfg.TracesEndpoint == "" && cfg.MetricsEndpoint == "" && cfg.LogsEndpoint == "" {
69+
if cfg.ClientConfig.Endpoint == "" && cfg.TracesEndpoint == "" && cfg.MetricsEndpoint == "" && cfg.LogsEndpoint == "" {
7070
return errors.New("at least one endpoint must be specified")
7171
}
7272
return nil

exporter/otlphttpexporter/factory.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func composeSignalURL(oCfg *Config, signalOverrideURL string, signalName string,
6363
return "", fmt.Errorf("%s_endpoint must be a valid URL", signalName)
6464
}
6565
return signalOverrideURL, nil
66-
case oCfg.Endpoint == "":
66+
case oCfg.ClientConfig.Endpoint == "":
6767
return "", fmt.Errorf("either endpoint or %s_endpoint must be specified", signalName)
6868
default:
69-
if strings.HasSuffix(oCfg.Endpoint, "/") {
70-
return oCfg.Endpoint + signalVersion + "/" + signalName, nil
69+
if strings.HasSuffix(oCfg.ClientConfig.Endpoint, "/") {
70+
return oCfg.ClientConfig.Endpoint + signalVersion + "/" + signalName, nil
7171
}
72-
return oCfg.Endpoint + "/" + signalVersion + "/" + signalName, nil
72+
return oCfg.ClientConfig.Endpoint + "/" + signalVersion + "/" + signalName, nil
7373
}
7474
}
7575

exporter/otlphttpexporter/factory_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestCreateDefaultConfig(t *testing.T) {
3737
assert.Equal(t, 30*time.Second, ocfg.RetryConfig.MaxInterval, "default retry MaxInterval")
3838
assert.True(t, ocfg.QueueConfig.Enabled, "default sending queue is enabled")
3939
assert.Equal(t, EncodingProto, ocfg.Encoding)
40-
assert.Equal(t, configcompression.TypeGzip, ocfg.Compression)
40+
assert.Equal(t, configcompression.TypeGzip, ocfg.ClientConfig.Compression)
4141
}
4242

4343
func TestCreateMetrics(t *testing.T) {

exporter/otlphttpexporter/otlp.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ const (
6060
func newExporter(cfg component.Config, set exporter.Settings) (*baseExporter, error) {
6161
oCfg := cfg.(*Config)
6262

63-
if oCfg.Endpoint != "" {
64-
_, err := url.Parse(oCfg.Endpoint)
63+
if oCfg.ClientConfig.Endpoint != "" {
64+
_, err := url.Parse(oCfg.ClientConfig.Endpoint)
6565
if err != nil {
6666
return nil, errors.New("endpoint must be a valid URL")
6767
}

internal/e2e/otlphttp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func startLogs(t *testing.T, baseURL string, overrideURL string) exporter.Logs {
319319

320320
func createConfig(baseURL string, defaultCfg component.Config) *otlphttpexporter.Config {
321321
cfg := defaultCfg.(*otlphttpexporter.Config)
322-
cfg.Endpoint = baseURL
322+
cfg.ClientConfig.Endpoint = baseURL
323323
cfg.QueueConfig.Enabled = false
324324
cfg.RetryConfig.Enabled = false
325325
return cfg
@@ -351,7 +351,7 @@ func startLogsReceiver(t *testing.T, addr string, next consumer.Logs) {
351351

352352
func createReceiverConfig(addr string, defaultCfg component.Config) *otlpreceiver.Config {
353353
cfg := defaultCfg.(*otlpreceiver.Config)
354-
cfg.HTTP.Endpoint = addr
354+
cfg.HTTP.ServerConfig.Endpoint = addr
355355
cfg.GRPC = nil
356356
return cfg
357357
}

receiver/otlpreceiver/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
)
2323

2424
type HTTPConfig struct {
25-
*confighttp.ServerConfig `mapstructure:",squash"`
25+
ServerConfig confighttp.ServerConfig `mapstructure:",squash"`
2626

2727
// The URL path to receive traces on. If omitted "/v1/traces" will be used.
2828
TracesURLPath string `mapstructure:"traces_url_path,omitempty"`

receiver/otlpreceiver/config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestUnmarshalConfig(t *testing.T) {
119119
},
120120
},
121121
HTTP: &HTTPConfig{
122-
ServerConfig: &confighttp.ServerConfig{
122+
ServerConfig: confighttp.ServerConfig{
123123
Auth: &confighttp.AuthConfig{
124124
Authentication: configauth.Authentication{
125125
AuthenticatorID: component.MustNewID("test"),
@@ -164,7 +164,7 @@ func TestUnmarshalConfigUnix(t *testing.T) {
164164
Keepalive: configgrpc.NewDefaultKeepaliveServerConfig(),
165165
},
166166
HTTP: &HTTPConfig{
167-
ServerConfig: &confighttp.ServerConfig{
167+
ServerConfig: confighttp.ServerConfig{
168168
Endpoint: "/tmp/http_otlp.sock",
169169
CORS: confighttp.NewDefaultCORSConfig(),
170170
ResponseHeaders: map[string]configopaque.String{},

receiver/otlpreceiver/factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func createDefaultConfig() component.Config {
5858
Protocols: Protocols{
5959
GRPC: grpcCfg,
6060
HTTP: &HTTPConfig{
61-
ServerConfig: &httpCfg,
61+
ServerConfig: httpCfg,
6262
TracesURLPath: defaultTracesURLPath,
6363
MetricsURLPath: defaultMetricsURLPath,
6464
LogsURLPath: defaultLogsURLPath,

receiver/otlpreceiver/factory_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestCreateSameReceiver(t *testing.T) {
3939
factory := NewFactory()
4040
cfg := factory.CreateDefaultConfig().(*Config)
4141
cfg.GRPC.NetAddr.Endpoint = testutil.GetAvailableLocalAddress(t)
42-
cfg.HTTP.Endpoint = testutil.GetAvailableLocalAddress(t)
42+
cfg.HTTP.ServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
4343

4444
core, observer := observer.New(zapcore.DebugLevel)
4545
attrs := attribute.NewSet(
@@ -91,7 +91,7 @@ func TestCreateTraces(t *testing.T) {
9191
defaultServerConfig := confighttp.NewDefaultServerConfig()
9292
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
9393
defaultHTTPSettings := &HTTPConfig{
94-
ServerConfig: &defaultServerConfig,
94+
ServerConfig: defaultServerConfig,
9595
TracesURLPath: defaultTracesURLPath,
9696
MetricsURLPath: defaultMetricsURLPath,
9797
LogsURLPath: defaultLogsURLPath,
@@ -136,7 +136,7 @@ func TestCreateTraces(t *testing.T) {
136136
Protocols: Protocols{
137137
GRPC: defaultGRPCSettings,
138138
HTTP: &HTTPConfig{
139-
ServerConfig: &confighttp.ServerConfig{
139+
ServerConfig: confighttp.ServerConfig{
140140
Endpoint: "localhost:112233",
141141
},
142142
TracesURLPath: defaultTracesURLPath,
@@ -185,7 +185,7 @@ func TestCreateMetric(t *testing.T) {
185185
defaultServerConfig := confighttp.NewDefaultServerConfig()
186186
defaultServerConfig.Endpoint = "127.0.0.1:0"
187187
defaultHTTPSettings := &HTTPConfig{
188-
ServerConfig: &defaultServerConfig,
188+
ServerConfig: defaultServerConfig,
189189
TracesURLPath: defaultTracesURLPath,
190190
MetricsURLPath: defaultMetricsURLPath,
191191
LogsURLPath: defaultLogsURLPath,
@@ -230,7 +230,7 @@ func TestCreateMetric(t *testing.T) {
230230
Protocols: Protocols{
231231
GRPC: defaultGRPCSettings,
232232
HTTP: &HTTPConfig{
233-
ServerConfig: &confighttp.ServerConfig{
233+
ServerConfig: confighttp.ServerConfig{
234234
Endpoint: "327.0.0.1:1122",
235235
},
236236
MetricsURLPath: defaultMetricsURLPath,
@@ -279,7 +279,7 @@ func TestCreateLogs(t *testing.T) {
279279
defaultServerConfig := confighttp.NewDefaultServerConfig()
280280
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
281281
defaultHTTPSettings := &HTTPConfig{
282-
ServerConfig: &defaultServerConfig,
282+
ServerConfig: defaultServerConfig,
283283
TracesURLPath: defaultTracesURLPath,
284284
MetricsURLPath: defaultMetricsURLPath,
285285
LogsURLPath: defaultLogsURLPath,
@@ -324,7 +324,7 @@ func TestCreateLogs(t *testing.T) {
324324
Protocols: Protocols{
325325
GRPC: defaultGRPCSettings,
326326
HTTP: &HTTPConfig{
327-
ServerConfig: &confighttp.ServerConfig{
327+
ServerConfig: confighttp.ServerConfig{
328328
Endpoint: "327.0.0.1:1122",
329329
},
330330
LogsURLPath: defaultLogsURLPath,
@@ -373,7 +373,7 @@ func TestCreateProfiles(t *testing.T) {
373373
defaultServerConfig := confighttp.NewDefaultServerConfig()
374374
defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)
375375
defaultHTTPSettings := &HTTPConfig{
376-
ServerConfig: &defaultServerConfig,
376+
ServerConfig: defaultServerConfig,
377377
TracesURLPath: defaultTracesURLPath,
378378
MetricsURLPath: defaultMetricsURLPath,
379379
LogsURLPath: defaultLogsURLPath,
@@ -418,7 +418,7 @@ func TestCreateProfiles(t *testing.T) {
418418
Protocols: Protocols{
419419
GRPC: defaultGRPCSettings,
420420
HTTP: &HTTPConfig{
421-
ServerConfig: &confighttp.ServerConfig{
421+
ServerConfig: confighttp.ServerConfig{
422422
Endpoint: "localhost:112233",
423423
},
424424
},

receiver/otlpreceiver/otlp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (r *otlpReceiver) startHTTPServer(ctx context.Context, host component.Host)
166166
}
167167

168168
var err error
169-
if r.serverHTTP, err = r.cfg.HTTP.ToServer(ctx, host, r.settings.TelemetrySettings, httpMux, confighttp.WithErrorHandler(errorHandler)); err != nil {
169+
if r.serverHTTP, err = r.cfg.HTTP.ServerConfig.ToServer(ctx, host, r.settings.TelemetrySettings, httpMux, confighttp.WithErrorHandler(errorHandler)); err != nil {
170170
return err
171171
}
172172

receiver/otlpreceiver/otlp_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ func TestHTTPInvalidTLSCredentials(t *testing.T) {
755755
cfg := &Config{
756756
Protocols: Protocols{
757757
HTTP: &HTTPConfig{
758-
ServerConfig: &confighttp.ServerConfig{
758+
ServerConfig: confighttp.ServerConfig{
759759
Endpoint: testutil.GetAvailableLocalAddress(t),
760760
TLSSetting: &configtls.ServerConfig{
761761
Config: configtls.Config{
@@ -788,7 +788,7 @@ func testHTTPMaxRequestBodySize(t *testing.T, path string, contentType string, p
788788
cfg := &Config{
789789
Protocols: Protocols{
790790
HTTP: &HTTPConfig{
791-
ServerConfig: &confighttp.ServerConfig{
791+
ServerConfig: confighttp.ServerConfig{
792792
Endpoint: addr,
793793
MaxRequestBodySize: int64(size),
794794
},
@@ -833,7 +833,7 @@ func newGRPCReceiver(t *testing.T, settings component.TelemetrySettings, endpoin
833833

834834
func newHTTPReceiver(t *testing.T, settings component.TelemetrySettings, endpoint string, c consumertest.Consumer) component.Component {
835835
cfg := createDefaultConfig().(*Config)
836-
cfg.HTTP.Endpoint = endpoint
836+
cfg.HTTP.ServerConfig.Endpoint = endpoint
837837
cfg.GRPC = nil
838838
return newReceiver(t, settings, cfg, otlpReceiverID, c)
839839
}
@@ -1015,7 +1015,7 @@ func TestShutdown(t *testing.T) {
10151015
factory := NewFactory()
10161016
cfg := factory.CreateDefaultConfig().(*Config)
10171017
cfg.GRPC.NetAddr.Endpoint = endpointGrpc
1018-
cfg.HTTP.Endpoint = endpointHTTP
1018+
cfg.HTTP.ServerConfig.Endpoint = endpointHTTP
10191019
set := receivertest.NewNopSettings(metadata.Type)
10201020
set.ID = otlpReceiverID
10211021
r, err := NewFactory().CreateTraces(

0 commit comments

Comments
 (0)