config.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package aerospikereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net"
  8. "strconv"
  9. "time"
  10. "go.opentelemetry.io/collector/config/configopaque"
  11. "go.opentelemetry.io/collector/config/configtls"
  12. "go.opentelemetry.io/collector/receiver/scraperhelper"
  13. "go.uber.org/multierr"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/internal/metadata"
  15. )
  16. var (
  17. errBadEndpoint = errors.New("endpoint must be specified as host:port")
  18. errBadPort = errors.New("invalid port in endpoint")
  19. errEmptyEndpoint = errors.New("endpoint must be specified")
  20. errEmptyEndpointTLSName = errors.New("endpoint TLSName must be specified")
  21. errEmptyPassword = errors.New("password must be set if username is set")
  22. errEmptyUsername = errors.New("username must be set if password is set")
  23. errNegativeTimeout = errors.New("timeout must be non-negative")
  24. errFailedTLSLoad = errors.New("failed to load TLS config")
  25. )
  26. // Config is the receiver configuration
  27. type Config struct {
  28. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  29. Endpoint string `mapstructure:"endpoint"`
  30. TLSName string `mapstructure:"tlsname"`
  31. Username string `mapstructure:"username"`
  32. Password configopaque.String `mapstructure:"password"`
  33. CollectClusterMetrics bool `mapstructure:"collect_cluster_metrics"`
  34. Timeout time.Duration `mapstructure:"timeout"`
  35. MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
  36. TLS *configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
  37. }
  38. // Validate validates the values of the given Config, and returns an error if validation fails
  39. func (c *Config) Validate() error {
  40. var allErrs error
  41. if c.Endpoint == "" {
  42. return multierr.Append(allErrs, errEmptyEndpoint)
  43. }
  44. host, portStr, err := net.SplitHostPort(c.Endpoint)
  45. if err != nil {
  46. return multierr.Append(allErrs, fmt.Errorf("%w: %s", errBadEndpoint, err.Error()))
  47. }
  48. if host == "" {
  49. allErrs = multierr.Append(allErrs, errBadEndpoint)
  50. }
  51. port, err := strconv.ParseInt(portStr, 10, 32)
  52. if err != nil {
  53. allErrs = multierr.Append(allErrs, fmt.Errorf("%w: %s", errBadPort, err.Error()))
  54. }
  55. if port < 0 || port > 65535 {
  56. allErrs = multierr.Append(allErrs, fmt.Errorf("%w: %d", errBadPort, port))
  57. }
  58. if c.Username != "" && c.Password == "" {
  59. allErrs = multierr.Append(allErrs, errEmptyPassword)
  60. }
  61. if c.Password != "" && c.Username == "" {
  62. allErrs = multierr.Append(allErrs, errEmptyUsername)
  63. }
  64. if c.Timeout.Milliseconds() < 0 {
  65. allErrs = multierr.Append(allErrs, fmt.Errorf("%w: must be positive", errNegativeTimeout))
  66. }
  67. if c.TLS != nil {
  68. _, err := c.TLS.LoadTLSConfig()
  69. if err != nil {
  70. allErrs = multierr.Append(allErrs, fmt.Errorf("%w: %s", errFailedTLSLoad, err.Error()))
  71. }
  72. }
  73. if c.TLS != nil && c.TLSName == "" {
  74. allErrs = multierr.Append(allErrs, fmt.Errorf("%w: when using TLS", errEmptyEndpointTLSName))
  75. }
  76. return allErrs
  77. }