Skip to content

Commit cf1a228

Browse files
committed
Avoid using go embedded messages in Config
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 622ce90 commit cf1a228

File tree

13 files changed

+69
-45
lines changed

13 files changed

+69
-45
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
@@ -20,15 +20,14 @@ import (
2020

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

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

3433
func (c *Config) Validate() error {
@@ -51,15 +50,15 @@ func (c *Config) Validate() error {
5150

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

exporter/otlpexporter/config_test.go

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

152152
func TestSanitizeEndpoint(t *testing.T) {
153153
factory := NewFactory()
154154
cfg := factory.CreateDefaultConfig().(*Config)
155-
cfg.Endpoint = "dns://authority/backend.example.com:4317"
155+
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317"
156156
assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint())
157-
cfg.Endpoint = "dns:///backend.example.com:4317"
157+
cfg.ClientConfig.Endpoint = "dns:///backend.example.com:4317"
158158
assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint())
159-
cfg.Endpoint = "dns:////backend.example.com:4317"
159+
cfg.ClientConfig.Endpoint = "dns:////backend.example.com:4317"
160160
assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint())
161161
}

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/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
}

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)