config.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package chronyreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "time"
  8. "go.opentelemetry.io/collector/component"
  9. "go.opentelemetry.io/collector/receiver/scraperhelper"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/chrony"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/metadata"
  12. )
  13. type Config struct {
  14. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  15. metadata.MetricsBuilderConfig `mapstructure:",squash"`
  16. // Endpoint is the published address or unix socket
  17. // that allows clients to connect to:
  18. // The allowed format is:
  19. // unix:///path/to/chronyd/unix.sock
  20. // udp://localhost:323
  21. //
  22. // The default value is unix:///var/run/chrony/chronyd.sock
  23. Endpoint string `mapstructure:"endpoint"`
  24. }
  25. var (
  26. _ component.Config = (*Config)(nil)
  27. errInvalidValue = errors.New("invalid value")
  28. )
  29. func newDefaultCongfig() component.Config {
  30. cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  31. cfg.Timeout = 10 * time.Second
  32. return &Config{
  33. ScraperControllerSettings: cfg,
  34. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  35. Endpoint: "unix:///var/run/chrony/chronyd.sock",
  36. }
  37. }
  38. func (c *Config) Validate() error {
  39. if c.Timeout < 1 {
  40. return fmt.Errorf("must have a positive timeout: %w", errInvalidValue)
  41. }
  42. _, _, err := chrony.SplitNetworkEndpoint(c.Endpoint)
  43. return err
  44. }