factory.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "context"
  6. "errors"
  7. "time"
  8. "go.opentelemetry.io/collector/component"
  9. "go.opentelemetry.io/collector/consumer"
  10. "go.opentelemetry.io/collector/receiver"
  11. "go.opentelemetry.io/collector/receiver/scraperhelper"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata"
  13. )
  14. var errConfigNotHTTPCheck = errors.New("config was not a HTTP check receiver config")
  15. // NewFactory creates a new receiver factory
  16. func NewFactory() receiver.Factory {
  17. return receiver.NewFactory(
  18. metadata.Type,
  19. createDefaultConfig,
  20. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
  21. }
  22. func createDefaultConfig() component.Config {
  23. cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  24. cfg.CollectionInterval = 60 * time.Second
  25. return &Config{
  26. ScraperControllerSettings: cfg,
  27. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  28. Targets: []*targetConfig{},
  29. }
  30. }
  31. func createMetricsReceiver(_ context.Context, params receiver.CreateSettings, rConf component.Config, consumer consumer.Metrics) (receiver.Metrics, error) {
  32. cfg, ok := rConf.(*Config)
  33. if !ok {
  34. return nil, errConfigNotHTTPCheck
  35. }
  36. httpcheckScraper := newScraper(cfg, params)
  37. scraper, err := scraperhelper.NewScraper(metadata.Type, httpcheckScraper.scrape, scraperhelper.WithStart(httpcheckScraper.start))
  38. if err != nil {
  39. return nil, err
  40. }
  41. return scraperhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, params, consumer, scraperhelper.AddScraper(scraper))
  42. }