factory.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "context"
  6. "fmt"
  7. "go.opentelemetry.io/collector/component"
  8. "go.opentelemetry.io/collector/consumer"
  9. "go.opentelemetry.io/collector/receiver"
  10. "go.opentelemetry.io/collector/receiver/scraperhelper"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/chrony"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/metadata"
  13. )
  14. func NewFactory() receiver.Factory {
  15. return receiver.NewFactory(
  16. metadata.Type,
  17. newDefaultCongfig,
  18. receiver.WithMetrics(newMetricsReceiver, metadata.MetricsStability),
  19. )
  20. }
  21. func newMetricsReceiver(
  22. ctx context.Context,
  23. set receiver.CreateSettings,
  24. rCfg component.Config,
  25. consumer consumer.Metrics) (receiver.Metrics, error) {
  26. cfg, ok := rCfg.(*Config)
  27. if !ok {
  28. return nil, fmt.Errorf("wrong config provided: %w", errInvalidValue)
  29. }
  30. chronyc, err := chrony.New(cfg.Endpoint, cfg.Timeout)
  31. if err != nil {
  32. return nil, err
  33. }
  34. scraper, err := scraperhelper.NewScraper(
  35. metadata.Type,
  36. newScraper(ctx, chronyc, cfg, set).scrape,
  37. )
  38. if err != nil {
  39. return nil, err
  40. }
  41. return scraperhelper.NewScraperControllerReceiver(
  42. &cfg.ScraperControllerSettings,
  43. set,
  44. consumer,
  45. scraperhelper.AddScraper(scraper),
  46. )
  47. }