factory.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package oracledbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver"
  4. import (
  5. "context"
  6. "database/sql"
  7. "net"
  8. "net/url"
  9. "strconv"
  10. "time"
  11. go_ora "github.com/sijms/go-ora/v2"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/consumer"
  14. "go.opentelemetry.io/collector/receiver"
  15. "go.opentelemetry.io/collector/receiver/scraperhelper"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver/internal/metadata"
  17. )
  18. // NewFactory creates a new Oracle receiver factory.
  19. func NewFactory() receiver.Factory {
  20. return receiver.NewFactory(
  21. metadata.Type,
  22. createDefaultConfig,
  23. receiver.WithMetrics(createReceiverFunc(func(dataSourceName string) (*sql.DB, error) {
  24. return sql.Open("oracle", dataSourceName)
  25. }, newDbClient), metadata.MetricsStability))
  26. }
  27. func createDefaultConfig() component.Config {
  28. cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  29. cfg.CollectionInterval = 10 * time.Second
  30. return &Config{
  31. ScraperControllerSettings: cfg,
  32. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  33. }
  34. }
  35. type sqlOpenerFunc func(dataSourceName string) (*sql.DB, error)
  36. func createReceiverFunc(sqlOpenerFunc sqlOpenerFunc, clientProviderFunc clientProviderFunc) receiver.CreateMetricsFunc {
  37. return func(
  38. ctx context.Context,
  39. settings receiver.CreateSettings,
  40. cfg component.Config,
  41. consumer consumer.Metrics,
  42. ) (receiver.Metrics, error) {
  43. sqlCfg := cfg.(*Config)
  44. metricsBuilder := metadata.NewMetricsBuilder(sqlCfg.MetricsBuilderConfig, settings)
  45. instanceName, err := getInstanceName(getDataSource(*sqlCfg))
  46. if err != nil {
  47. return nil, err
  48. }
  49. mp, err := newScraper(settings.ID, metricsBuilder, sqlCfg.MetricsBuilderConfig, sqlCfg.ScraperControllerSettings, settings.TelemetrySettings.Logger, func() (*sql.DB, error) {
  50. return sqlOpenerFunc(getDataSource(*sqlCfg))
  51. }, clientProviderFunc, instanceName)
  52. if err != nil {
  53. return nil, err
  54. }
  55. opt := scraperhelper.AddScraper(mp)
  56. return scraperhelper.NewScraperControllerReceiver(
  57. &sqlCfg.ScraperControllerSettings,
  58. settings,
  59. consumer,
  60. opt,
  61. )
  62. }
  63. }
  64. func getDataSource(cfg Config) string {
  65. if cfg.DataSource != "" {
  66. return cfg.DataSource
  67. }
  68. // Don't need to worry about errors here as config validation already checked.
  69. host, portStr, _ := net.SplitHostPort(cfg.Endpoint)
  70. port, _ := strconv.ParseInt(portStr, 10, 32)
  71. return go_ora.BuildUrl(host, int(port), cfg.Service, cfg.Username, cfg.Password, nil)
  72. }
  73. func getInstanceName(datasource string) (string, error) {
  74. datasourceURL, err := url.Parse(datasource)
  75. if err != nil {
  76. return "", err
  77. }
  78. instanceName := datasourceURL.Host + datasourceURL.Path
  79. return instanceName, nil
  80. }