From 60e7b5c5c1100eb9850186893c605cdeef64e07d Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 2 Dec 2024 15:55:27 +0100 Subject: [PATCH] use unmarshall Signed-off-by: odubajDT --- receiver/zipkinreceiver/config.go | 24 ++++++- receiver/zipkinreceiver/config_test.go | 88 ++++++++------------------ receiver/zipkinreceiver/factory.go | 5 +- 3 files changed, 50 insertions(+), 67 deletions(-) diff --git a/receiver/zipkinreceiver/config.go b/receiver/zipkinreceiver/config.go index 0682f89f665ec..11ee26886fd68 100644 --- a/receiver/zipkinreceiver/config.go +++ b/receiver/zipkinreceiver/config.go @@ -8,6 +8,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confighttp" + "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/featuregate" ) @@ -18,7 +19,10 @@ var disallowHTTPDefaultProtocol = featuregate.GlobalRegistry().MustRegister( featuregate.WithRegisterFromVersion("v0.114.0"), ) -const deprecationConfigMsg = "the inline setting of http server parameters has been deprecated, please use .protocols.http parameter instead." +const ( + // Protocol values. + protoHTTP = "protocols::http" +) // Config defines configuration for Zipkin receiver. type Config struct { @@ -45,11 +49,25 @@ var _ component.Config = (*Config)(nil) func (cfg *Config) Validate() error { if isServerConfigDefined(cfg.ServerConfig) { if disallowHTTPDefaultProtocol.IsEnabled() { - return fmt.Errorf(deprecationConfigMsg) + return fmt.Errorf("the server config setup is disabled, please use protocols::http or enable it by setting zipkinreceiver.httpDefaultProtocol.disallow feaure gate to false") } if isServerConfigDefined(cfg.Protocols.HTTP) { - return fmt.Errorf("cannot use .protocols.http together with default server config setup") + return fmt.Errorf("cannot use protocols::http together with default server config setup") } + } + + return nil +} + +// Unmarshal a confmap.Conf into the config struct. +func (cfg *Config) Unmarshal(conf *confmap.Conf) error { + // first load the config normally + err := conf.Unmarshal(cfg) + if err != nil { + return err + } + + if !conf.IsSet(protoHTTP) { cfg.Protocols.HTTP = cfg.ServerConfig cfg.ServerConfig = confighttp.ServerConfig{} } diff --git a/receiver/zipkinreceiver/config_test.go b/receiver/zipkinreceiver/config_test.go index 24c3413c8853e..9eb96bcfcd7de 100644 --- a/receiver/zipkinreceiver/config_test.go +++ b/receiver/zipkinreceiver/config_test.go @@ -30,6 +30,10 @@ func TestValidateConfig(t *testing.T) { expected component.Config wantErr error }{ + { + id: component.NewID(metadata.Type), + expected: &Config{}, + }, { id: component.NewIDWithName(metadata.Type, "customname"), expected: &Config{ @@ -44,10 +48,6 @@ func TestValidateConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "customname"), disallowInline: true, - wantErr: fmt.Errorf(deprecationConfigMsg), - }, - { - id: component.NewIDWithName(metadata.Type, "protocols"), expected: &Config{ Protocols: ProtocolTypes{ HTTP: confighttp.ServerConfig{ @@ -67,30 +67,41 @@ func TestValidateConfig(t *testing.T) { }, ParseStringTags: false, }, - disallowInline: true, - }, - { - id: component.NewIDWithName(metadata.Type, "parse_strings"), - expected: &Config{ - ParseStringTags: true, - }, }, { - id: component.NewIDWithName(metadata.Type, "parse_strings"), + id: component.NewIDWithName(metadata.Type, "protocols"), expected: &Config{ - ParseStringTags: true, + Protocols: ProtocolTypes{ + HTTP: confighttp.ServerConfig{ + Endpoint: "localhost:8765", + }, + }, + ParseStringTags: false, }, disallowInline: true, }, + // { + // id: component.NewIDWithName(metadata.Type, "parse_strings"), + // expected: &Config{ + // ParseStringTags: true, + // }, + // }, + // { + // id: component.NewIDWithName(metadata.Type, "parse_strings"), + // expected: &Config{ + // ParseStringTags: true, + // }, + // disallowInline: true, + // }, { id: component.NewIDWithName(metadata.Type, "deprecated"), disallowInline: true, - wantErr: fmt.Errorf(deprecationConfigMsg), + wantErr: fmt.Errorf("the server config setup is disabled, please use protocols::http or enable it by setting zipkinreceiver.httpDefaultProtocol.disallow feaure gate to false"), }, { id: component.NewIDWithName(metadata.Type, "deprecated"), disallowInline: false, - wantErr: fmt.Errorf("cannot use .protocols.http together with default server config setup"), + wantErr: fmt.Errorf("cannot use protocols::http together with default server config setup"), }, } @@ -117,50 +128,3 @@ func TestValidateConfig(t *testing.T) { }) } } - -func TestLoadConfig(t *testing.T) { - t.Parallel() - - cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) - require.NoError(t, err) - - tests := []struct { - id component.ID - expected component.Config - wantErr bool - }{ - { - id: component.NewID(metadata.Type), - expected: createDefaultConfig(), - }, - { - id: component.NewIDWithName(metadata.Type, "parse_strings"), - expected: &Config{ - Protocols: ProtocolTypes{ - HTTP: confighttp.ServerConfig{ - Endpoint: defaultBindEndpoint, - }, - }, - ParseStringTags: true, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.id.String(), func(t *testing.T) { - factory := NewFactory() - cfg := factory.CreateDefaultConfig() - - sub, err := cm.Sub(tt.id.String()) - require.NoError(t, err) - require.NoError(t, sub.Unmarshal(cfg)) - - if tt.wantErr { - assert.Error(t, component.ValidateConfig(cfg)) - } else { - assert.NoError(t, component.ValidateConfig(cfg)) - assert.Equal(t, tt.expected, cfg) - } - }) - } -} diff --git a/receiver/zipkinreceiver/factory.go b/receiver/zipkinreceiver/factory.go index 47cb7a600955e..dde296e1ce497 100644 --- a/receiver/zipkinreceiver/factory.go +++ b/receiver/zipkinreceiver/factory.go @@ -18,8 +18,9 @@ import ( // This file implements factory for Zipkin receiver. const ( - defaultHTTPPort = 9411 - defaultBindEndpoint = "localhost:9411" + defaultHTTPPort = 9411 + defaultBindEndpoint = "localhost:9411" + deprecationConfigMsg = "the inline setting of http server parameters has been deprecated, please use protocols::http parameter instead." ) // NewFactory creates a new Zipkin receiver factory