factory.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
  4. import (
  5. "context"
  6. "fmt"
  7. "os"
  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/hostmetricsreceiver/internal"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/loadscraper"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
  21. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
  22. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
  23. )
  24. // This file implements Factory for HostMetrics receiver.
  25. var (
  26. scraperFactories = map[string]internal.ScraperFactory{
  27. cpuscraper.TypeStr: &cpuscraper.Factory{},
  28. diskscraper.TypeStr: &diskscraper.Factory{},
  29. loadscraper.TypeStr: &loadscraper.Factory{},
  30. filesystemscraper.TypeStr: &filesystemscraper.Factory{},
  31. memoryscraper.TypeStr: &memoryscraper.Factory{},
  32. networkscraper.TypeStr: &networkscraper.Factory{},
  33. pagingscraper.TypeStr: &pagingscraper.Factory{},
  34. processesscraper.TypeStr: &processesscraper.Factory{},
  35. processscraper.TypeStr: &processscraper.Factory{},
  36. }
  37. )
  38. // NewFactory creates a new factory for host metrics receiver.
  39. func NewFactory() receiver.Factory {
  40. return receiver.NewFactory(
  41. metadata.Type,
  42. createDefaultConfig,
  43. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
  44. }
  45. func getScraperFactory(key string) (internal.ScraperFactory, bool) {
  46. if factory, ok := scraperFactories[key]; ok {
  47. return factory, true
  48. }
  49. return nil, false
  50. }
  51. // createDefaultConfig creates the default configuration for receiver.
  52. func createDefaultConfig() component.Config {
  53. return &Config{ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)}
  54. }
  55. // createMetricsReceiver creates a metrics receiver based on provided config.
  56. func createMetricsReceiver(
  57. ctx context.Context,
  58. set receiver.CreateSettings,
  59. cfg component.Config,
  60. consumer consumer.Metrics,
  61. ) (receiver.Metrics, error) {
  62. oCfg := cfg.(*Config)
  63. addScraperOptions, err := createAddScraperOptions(ctx, set, oCfg, scraperFactories)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return scraperhelper.NewScraperControllerReceiver(
  68. &oCfg.ScraperControllerSettings,
  69. set,
  70. consumer,
  71. addScraperOptions...,
  72. )
  73. }
  74. func createAddScraperOptions(
  75. ctx context.Context,
  76. set receiver.CreateSettings,
  77. config *Config,
  78. factories map[string]internal.ScraperFactory,
  79. ) ([]scraperhelper.ScraperControllerOption, error) {
  80. scraperControllerOptions := make([]scraperhelper.ScraperControllerOption, 0, len(config.Scrapers))
  81. for key, cfg := range config.Scrapers {
  82. hostMetricsScraper, ok, err := createHostMetricsScraper(ctx, set, key, cfg, factories)
  83. if err != nil {
  84. return nil, fmt.Errorf("failed to create scraper for key %q: %w", key, err)
  85. }
  86. if ok {
  87. scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddScraper(hostMetricsScraper))
  88. continue
  89. }
  90. return nil, fmt.Errorf("host metrics scraper factory not found for key: %q", key)
  91. }
  92. return scraperControllerOptions, nil
  93. }
  94. func createHostMetricsScraper(ctx context.Context, set receiver.CreateSettings, key string, cfg internal.Config, factories map[string]internal.ScraperFactory) (scraper scraperhelper.Scraper, ok bool, err error) {
  95. factory := factories[key]
  96. if factory == nil {
  97. ok = false
  98. return
  99. }
  100. ok = true
  101. scraper, err = factory.CreateMetricsScraper(ctx, set, cfg)
  102. return
  103. }
  104. type environment interface {
  105. Lookup(k string) (string, bool)
  106. }
  107. type osEnv struct{}
  108. var _ environment = (*osEnv)(nil)
  109. func (e *osEnv) Lookup(k string) (string, bool) {
  110. return os.LookupEnv(k)
  111. }