config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package carbonreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
  4. import (
  5. "fmt"
  6. "time"
  7. "go.opentelemetry.io/collector/config/confignet"
  8. "go.opentelemetry.io/collector/confmap"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/protocol"
  10. )
  11. const (
  12. // parserConfigSection is the name that must be used for the parser settings
  13. // in the configuration struct. The metadata mapstructure for the parser
  14. // should use the same string.
  15. parserConfigSection = "parser"
  16. )
  17. var _ confmap.Unmarshaler = (*Config)(nil)
  18. // Config defines configuration for the Carbon receiver.
  19. type Config struct {
  20. confignet.NetAddr `mapstructure:",squash"`
  21. // TCPIdleTimeout is the timout for idle TCP connections, it is ignored
  22. // if transport being used is UDP.
  23. TCPIdleTimeout time.Duration `mapstructure:"tcp_idle_timeout"`
  24. // Parser specifies a parser and the respective configuration to be used
  25. // by the receiver.
  26. Parser *protocol.Config `mapstructure:"parser"`
  27. }
  28. func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
  29. if componentParser == nil {
  30. // The section is empty nothing to do, using the default config.
  31. return nil
  32. }
  33. // Unmarshal but not exact yet so the different keys under config do not
  34. // trigger errors, this is needed so that the types of protocol and transport
  35. // are read.
  36. if err := componentParser.Unmarshal(cfg); err != nil {
  37. return err
  38. }
  39. // Unmarshal the protocol, so the type of config can be properly set.
  40. vParserCfg, errSub := componentParser.Sub(parserConfigSection)
  41. if errSub != nil {
  42. return errSub
  43. }
  44. if err := protocol.LoadParserConfig(vParserCfg, cfg.Parser); err != nil {
  45. return fmt.Errorf("error on %q section: %w", parserConfigSection, err)
  46. }
  47. // Unmarshal exact to validate the config keys.
  48. return componentParser.Unmarshal(cfg, confmap.WithErrorUnused())
  49. }