scraper.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "time"
  8. "go.opentelemetry.io/collector/component"
  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.uber.org/zap"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/metadata"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/model"
  16. )
  17. var errClientNotInit = errors.New("client not initialized")
  18. // riakScraper handles scraping of Riak metrics
  19. type riakScraper struct {
  20. logger *zap.Logger
  21. cfg *Config
  22. settings component.TelemetrySettings
  23. client client
  24. mb *metadata.MetricsBuilder
  25. }
  26. // newScraper creates a new scraper
  27. func newScraper(logger *zap.Logger, cfg *Config, settings receiver.CreateSettings) *riakScraper {
  28. return &riakScraper{
  29. logger: logger,
  30. cfg: cfg,
  31. settings: settings.TelemetrySettings,
  32. mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
  33. }
  34. }
  35. // start starts the scraper by creating a new HTTP Client on the scraper
  36. func (r *riakScraper) start(_ context.Context, host component.Host) (err error) {
  37. r.client, err = newClient(r.cfg, host, r.settings, r.logger)
  38. return
  39. }
  40. // scrape collects metrics from the Riak API
  41. func (r *riakScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
  42. // Validate we don't attempt to scrape without initializing the client
  43. if r.client == nil {
  44. return pmetric.NewMetrics(), errors.New("client not initialized")
  45. }
  46. // Get stats for processing
  47. stats, err := r.client.GetStats(ctx)
  48. if err != nil {
  49. return pmetric.NewMetrics(), err
  50. }
  51. return r.collectStats(stats)
  52. }
  53. // collectStats collects metrics
  54. func (r *riakScraper) collectStats(stat *model.Stats) (pmetric.Metrics, error) {
  55. now := pcommon.NewTimestampFromTime(time.Now())
  56. var errors scrapererror.ScrapeErrors
  57. // scrape node.operation.count metric
  58. r.mb.RecordRiakNodeOperationCountDataPoint(now, stat.NodeGets, metadata.AttributeRequestGet)
  59. r.mb.RecordRiakNodeOperationCountDataPoint(now, stat.NodePuts, metadata.AttributeRequestPut)
  60. // scrape node.operation.time.mean metric
  61. r.mb.RecordRiakNodeOperationTimeMeanDataPoint(now, stat.NodeGetFsmTimeMean, metadata.AttributeRequestGet)
  62. r.mb.RecordRiakNodeOperationTimeMeanDataPoint(now, stat.NodePutFsmTimeMean, metadata.AttributeRequestPut)
  63. // scrape node.read_repair.count metric
  64. r.mb.RecordRiakNodeReadRepairCountDataPoint(now, stat.ReadRepairs)
  65. // scrape node.memory.limit metric
  66. r.mb.RecordRiakMemoryLimitDataPoint(now, stat.MemAllocated)
  67. // scrape vnode.operation.count metric
  68. r.mb.RecordRiakVnodeOperationCountDataPoint(now, stat.VnodeGets, metadata.AttributeRequestGet)
  69. r.mb.RecordRiakVnodeOperationCountDataPoint(now, stat.VnodePuts, metadata.AttributeRequestPut)
  70. // scrape vnode.index.operation.count metric
  71. r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexReads, metadata.AttributeOperationRead)
  72. r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexWrites, metadata.AttributeOperationWrite)
  73. r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexDeletes, metadata.AttributeOperationDelete)
  74. rb := r.mb.NewResourceBuilder()
  75. rb.SetRiakNodeName(stat.Node)
  76. return r.mb.Emit(metadata.WithResource(rb.Emit())), errors.Combine()
  77. }