Skip to content

Commit bdde9b4

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

File tree

10 files changed

+55
-31
lines changed

10 files changed

+55
-31
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/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

0 commit comments

Comments
 (0)