factory.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package apachesparkreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/apachesparkreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "go.opentelemetry.io/collector/component"
  8. "go.opentelemetry.io/collector/config/confighttp"
  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/apachesparkreceiver/internal/metadata"
  13. )
  14. var errConfigNotSpark = errors.New("config was not a Spark receiver config")
  15. // NewFactory creates a new receiver factory for Spark
  16. func NewFactory() receiver.Factory {
  17. return receiver.NewFactory(
  18. metadata.Type,
  19. createDefaultConfig,
  20. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
  21. }
  22. // createDefaultConfig creates a config for Spark with as many default values as possible
  23. func createDefaultConfig() component.Config {
  24. cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  25. cfg.CollectionInterval = defaultCollectionInterval
  26. return &Config{
  27. ScraperControllerSettings: cfg,
  28. HTTPClientSettings: confighttp.HTTPClientSettings{
  29. Endpoint: defaultEndpoint,
  30. },
  31. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  32. }
  33. }
  34. // createMetricsReceiver creates the metric receiver for Spark
  35. func createMetricsReceiver(
  36. _ context.Context,
  37. params receiver.CreateSettings,
  38. config component.Config,
  39. consumer consumer.Metrics,
  40. ) (receiver.Metrics, error) {
  41. sparkConfig, ok := config.(*Config)
  42. if !ok {
  43. return nil, errConfigNotSpark
  44. }
  45. sparkScraper := newSparkScraper(params.Logger, sparkConfig, params)
  46. scraper, err := scraperhelper.NewScraper(metadata.Type, sparkScraper.scrape,
  47. scraperhelper.WithStart(sparkScraper.start))
  48. if err != nil {
  49. return nil, err
  50. }
  51. return scraperhelper.NewScraperControllerReceiver(&sparkConfig.ScraperControllerSettings, params,
  52. consumer, scraperhelper.AddScraper(scraper))
  53. }