Skip to content

Commit

Permalink
use unmarshall
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Dec 6, 2024
1 parent cf96f92 commit 60e7b5c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 67 deletions.
24 changes: 21 additions & 3 deletions receiver/zipkinreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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{}
}
Expand Down
88 changes: 26 additions & 62 deletions receiver/zipkinreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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"),
},
}

Expand All @@ -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)
}
})
}
}
5 changes: 3 additions & 2 deletions receiver/zipkinreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60e7b5c

Please sign in to comment.