factory.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sshcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver"
  4. import (
  5. "context"
  6. "time"
  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/sshcheckreceiver/internal/configssh"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/metadata"
  13. )
  14. // NewFactory creates a new receiver factory
  15. func NewFactory() receiver.Factory {
  16. return receiver.NewFactory(
  17. metadata.Type,
  18. createDefaultConfig,
  19. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
  20. }
  21. func createDefaultConfig() component.Config {
  22. cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  23. cfg.CollectionInterval = 10 * time.Second
  24. return &Config{
  25. ScraperControllerSettings: cfg,
  26. SSHClientSettings: configssh.SSHClientSettings{
  27. Timeout: 10 * time.Second,
  28. },
  29. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  30. }
  31. }
  32. func createMetricsReceiver(_ context.Context, params receiver.CreateSettings, rConf component.Config, consumer consumer.Metrics) (receiver.Metrics, error) {
  33. // return error if sshcheckreceiver on Windows
  34. if !supportedOS() {
  35. return nil, errWindowsUnsupported
  36. }
  37. cfg, ok := rConf.(*Config)
  38. if !ok {
  39. return nil, errConfigNotSSHCheck
  40. }
  41. sshcheckScraper := newScraper(cfg, params)
  42. scraper, err := scraperhelper.NewScraper(metadata.Type, sshcheckScraper.scrape, scraperhelper.WithStart(sshcheckScraper.start))
  43. if err != nil {
  44. return nil, err
  45. }
  46. return scraperhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, params, consumer, scraperhelper.AddScraper(scraper))
  47. }