config.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package bigipreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver"
  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/bigipreceiver/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 = "https://localhost:443"
  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. }