Remove impossible conditions from Unmarshaler, and validation from Unmarshal (#6284)

Signed-off-by: Bogdan <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2022-10-20 13:42:50 -07:00 committed by GitHub
parent fdc80a3bea
commit 7fa47b4927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 18 deletions

View File

@ -25,8 +25,8 @@ import (
const (
// Protocol values.
protoGRPC = "grpc"
protoHTTP = "http"
protoGRPC = "protocols::grpc"
protoHTTP = "protocols::http"
protocolsFieldName = "protocols"
)
@ -48,35 +48,25 @@ var _ confmap.Unmarshaler = (*Config)(nil)
// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
if cfg.GRPC == nil &&
cfg.HTTP == nil {
if cfg.GRPC == nil && cfg.HTTP == nil {
return errors.New("must specify at least one protocol when using the OTLP receiver")
}
return nil
}
// Unmarshal a confmap.Conf into the config struct.
func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
if componentParser == nil || len(componentParser.AllKeys()) == 0 {
return errors.New("empty config for OTLP receiver")
}
func (cfg *Config) Unmarshal(conf *confmap.Conf) error {
// first load the config normally
err := componentParser.Unmarshal(cfg, confmap.WithErrorUnused())
err := conf.Unmarshal(cfg, confmap.WithErrorUnused())
if err != nil {
return err
}
// next manually search for protocols in the confmap.Conf, if a protocol is not present it means it is disabled.
protocols, err := componentParser.Sub(protocolsFieldName)
if err != nil {
return err
}
if !protocols.IsSet(protoGRPC) {
if !conf.IsSet(protoGRPC) {
cfg.GRPC = nil
}
if !protocols.IsSet(protoHTTP) {
if !conf.IsSet(protoHTTP) {
cfg.HTTP = nil
}

View File

@ -198,5 +198,6 @@ func TestUnmarshalConfigEmptyProtocols(t *testing.T) {
func TestUnmarshalConfigEmpty(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.EqualError(t, config.UnmarshalReceiver(confmap.New(), cfg), "empty config for OTLP receiver")
assert.NoError(t, config.UnmarshalReceiver(confmap.New(), cfg))
assert.EqualError(t, cfg.Validate(), "must specify at least one protocol when using the OTLP receiver")
}