config.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/url"
  8. "go.opentelemetry.io/collector/config/confighttp"
  9. "go.opentelemetry.io/collector/receiver/scraperhelper"
  10. "go.uber.org/multierr"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata"
  12. )
  13. // Predefined error responses for configuration validation failures
  14. var (
  15. errMissingEndpoint = errors.New(`"endpoint" must be specified`)
  16. errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>[:<port>]`)
  17. )
  18. // Config defines the configuration for the various elements of the receiver agent.
  19. type Config struct {
  20. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  21. metadata.MetricsBuilderConfig `mapstructure:",squash"`
  22. Targets []*targetConfig `mapstructure:"targets"`
  23. }
  24. type targetConfig struct {
  25. confighttp.HTTPClientSettings `mapstructure:",squash"`
  26. Method string `mapstructure:"method"`
  27. }
  28. // Validate validates the configuration by checking for missing or invalid fields
  29. func (cfg *targetConfig) Validate() error {
  30. var err error
  31. if cfg.Endpoint == "" {
  32. err = multierr.Append(err, errMissingEndpoint)
  33. } else {
  34. _, parseErr := url.ParseRequestURI(cfg.Endpoint)
  35. if parseErr != nil {
  36. err = multierr.Append(err, fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr))
  37. }
  38. }
  39. return err
  40. }
  41. // Validate validates the configuration by checking for missing or invalid fields
  42. func (cfg *Config) Validate() error {
  43. var err error
  44. if len(cfg.Targets) == 0 {
  45. err = multierr.Append(err, errors.New("no targets configured"))
  46. }
  47. for _, target := range cfg.Targets {
  48. err = multierr.Append(err, target.Validate())
  49. }
  50. return err
  51. }