factory.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package gitproviderreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitproviderreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  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/gitproviderreceiver/internal"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitproviderreceiver/internal/metadata"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitproviderreceiver/internal/scraper/githubscraper"
  15. )
  16. // This file implements a factory for the git provider receiver
  17. var (
  18. scraperFactories = map[string]internal.ScraperFactory{
  19. githubscraper.TypeStr: &githubscraper.Factory{},
  20. }
  21. errConfigNotValid = errors.New("configuration is not valid for the git provider receiver")
  22. )
  23. // NewFactory creates a factory for the git provider receiver
  24. func NewFactory() receiver.Factory {
  25. return receiver.NewFactory(
  26. metadata.Type,
  27. createDefaultConfig,
  28. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability),
  29. )
  30. }
  31. // Gets a factory for defined scraper.
  32. func getScraperFactory(key string) (internal.ScraperFactory, bool) {
  33. if factory, ok := scraperFactories[key]; ok {
  34. return factory, true
  35. }
  36. return nil, false
  37. }
  38. // Create the default config based on the const(s) defined above.
  39. func createDefaultConfig() component.Config {
  40. return &Config{
  41. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  42. // TODO: metrics builder configuration may need to be in each sub scraper,
  43. // TODO: for right now setting here because the metrics in this receiver will apply to all
  44. // TODO: scrapers defined as a common set of gitprovider
  45. // TODO: aqp completely remove these comments if the metrics build config
  46. // needs to be defined in each scraper
  47. // MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  48. }
  49. }
  50. // Create the metrics receiver according to the OTEL conventions taking in the
  51. // context, receiver params, configuration from the component, and consumer (process or exporter)
  52. func createMetricsReceiver(
  53. ctx context.Context,
  54. params receiver.CreateSettings,
  55. cfg component.Config,
  56. consumer consumer.Metrics,
  57. ) (receiver.Metrics, error) {
  58. // check that the configuration is valid
  59. conf, ok := cfg.(*Config)
  60. if !ok {
  61. return nil, errConfigNotValid
  62. }
  63. addScraperOpts, err := createAddScraperOpts(ctx, params, conf, scraperFactories)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return scraperhelper.NewScraperControllerReceiver(
  68. &conf.ScraperControllerSettings,
  69. params,
  70. consumer,
  71. addScraperOpts...,
  72. )
  73. }
  74. func createAddScraperOpts(
  75. ctx context.Context,
  76. params receiver.CreateSettings,
  77. cfg *Config,
  78. factories map[string]internal.ScraperFactory,
  79. ) ([]scraperhelper.ScraperControllerOption, error) {
  80. scraperControllerOptions := make([]scraperhelper.ScraperControllerOption, 0, len(cfg.Scrapers))
  81. for key, cfg := range cfg.Scrapers {
  82. gitProviderScraper, err := createGitProviderScraper(ctx, params, key, cfg, factories)
  83. if err != nil {
  84. return nil, fmt.Errorf("failed to create scraper %q: %w", key, err)
  85. }
  86. scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddScraper(gitProviderScraper))
  87. }
  88. return scraperControllerOptions, nil
  89. }
  90. func createGitProviderScraper(
  91. ctx context.Context,
  92. params receiver.CreateSettings,
  93. key string,
  94. cfg internal.Config,
  95. factories map[string]internal.ScraperFactory,
  96. ) (scraper scraperhelper.Scraper, err error) {
  97. factory := factories[key]
  98. if factory == nil {
  99. return nil, fmt.Errorf("factory not found for scraper %q", key)
  100. }
  101. scraper, err = factory.CreateMetricsScraper(ctx, params, cfg)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return
  106. }