config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rabbitmqreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/url"
  8. "go.opentelemetry.io/collector/config/confighttp"
  9. "go.opentelemetry.io/collector/config/configopaque"
  10. "go.opentelemetry.io/collector/receiver/scraperhelper"
  11. "go.uber.org/multierr"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/metadata"
  13. )
  14. // Predefined error responses for configuration validation failures
  15. var (
  16. errMissingUsername = errors.New(`"username" not specified in config`)
  17. errMissingPassword = errors.New(`"password" not specified in config`)
  18. errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>:<port>`)
  19. )
  20. const defaultEndpoint = "http://localhost:15672"
  21. // Config defines the configuration for the various elements of the receiver agent.
  22. type Config struct {
  23. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  24. confighttp.HTTPClientSettings `mapstructure:",squash"`
  25. Username string `mapstructure:"username"`
  26. Password configopaque.String `mapstructure:"password"`
  27. metadata.MetricsBuilderConfig `mapstructure:",squash"`
  28. }
  29. // Validate validates the configuration by checking for missing or invalid fields
  30. func (cfg *Config) Validate() error {
  31. var err error
  32. if cfg.Username == "" {
  33. err = multierr.Append(err, errMissingUsername)
  34. }
  35. if cfg.Password == "" {
  36. err = multierr.Append(err, errMissingPassword)
  37. }
  38. _, parseErr := url.Parse(cfg.Endpoint)
  39. if parseErr != nil {
  40. wrappedErr := fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr)
  41. err = multierr.Append(err, wrappedErr)
  42. }
  43. return err
  44. }