scraper.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package saphanareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "time"
  9. "go.opentelemetry.io/collector/pdata/pcommon"
  10. "go.opentelemetry.io/collector/pdata/pmetric"
  11. "go.opentelemetry.io/collector/receiver"
  12. "go.opentelemetry.io/collector/receiver/scrapererror"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver/internal/metadata"
  15. )
  16. // Runs intermittently, fetching info from SAP HANA, creating metrics/datapoints,
  17. // and feeding them to a metricsConsumer.
  18. type sapHanaScraper struct {
  19. settings receiver.CreateSettings
  20. cfg *Config
  21. mbs map[string]*metadata.MetricsBuilder
  22. factory sapHanaConnectionFactory
  23. }
  24. func newSapHanaScraper(settings receiver.CreateSettings, cfg *Config, factory sapHanaConnectionFactory) (scraperhelper.Scraper, error) {
  25. rs := &sapHanaScraper{
  26. settings: settings,
  27. cfg: cfg,
  28. mbs: make(map[string]*metadata.MetricsBuilder),
  29. factory: factory,
  30. }
  31. return scraperhelper.NewScraper(metadata.Type, rs.scrape)
  32. }
  33. func (s *sapHanaScraper) getMetricsBuilder(resourceAttributes map[string]string) (*metadata.MetricsBuilder, error) {
  34. bytes, err := json.Marshal(resourceAttributes)
  35. if err != nil {
  36. return nil, fmt.Errorf("Error accessing MetricsBuilder for sap hana collection: %w", err)
  37. }
  38. key := string(bytes)
  39. mb, ok := s.mbs[key]
  40. if !ok {
  41. mb = metadata.NewMetricsBuilder(s.cfg.MetricsBuilderConfig, s.settings)
  42. s.mbs[key] = mb
  43. }
  44. return mb, nil
  45. }
  46. // Scrape is called periodically, querying SAP HANA and building Metrics to send to
  47. // the next consumer.
  48. func (s *sapHanaScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
  49. client := newSapHanaClient(s.cfg, s.factory)
  50. if err := client.Connect(ctx); err != nil {
  51. return pmetric.NewMetrics(), err
  52. }
  53. defer client.Close()
  54. errs := &scrapererror.ScrapeErrors{}
  55. now := pcommon.NewTimestampFromTime(time.Now())
  56. for _, query := range queries {
  57. if query.Enabled == nil || query.Enabled(s.cfg) {
  58. query.CollectMetrics(ctx, s, client, now, errs)
  59. }
  60. }
  61. metrics := pmetric.NewMetrics()
  62. for k, mb := range s.mbs {
  63. var resourceAttributes map[string]string
  64. err := json.Unmarshal([]byte(k), &resourceAttributes)
  65. if err != nil {
  66. errs.Add(fmt.Errorf("Error unmarshaling resource attributes for saphana scraper: %w", err))
  67. continue
  68. }
  69. rb := mb.NewResourceBuilder()
  70. rb.SetDbSystem("saphana")
  71. for attribute, value := range resourceAttributes {
  72. if attribute == "host" {
  73. rb.SetSaphanaHost(value)
  74. } else {
  75. errs.Add(fmt.Errorf("Unsupported resource attribute: %s", attribute))
  76. }
  77. }
  78. resourceMetrics := mb.Emit(metadata.WithResource(rb.Emit()))
  79. resourceMetrics.ResourceMetrics().At(0).MoveTo(metrics.ResourceMetrics().AppendEmpty())
  80. }
  81. s.mbs = make(map[string]*metadata.MetricsBuilder)
  82. return metrics, errs.Combine()
  83. }