store.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package metric
  2. import (
  3. "fmt"
  4. "slices"
  5. "sync"
  6. "time"
  7. "github.com/opencost/opencost/modules/collector-source/pkg/metric/aggregator"
  8. )
  9. // MetricStore is an interface that defines an implementation capable of managing a collection
  10. // of metric instances, and exposes helper methods for routing metric updates and queries to the
  11. // proper metric instances.
  12. type MetricStore interface {
  13. // Register accepts a `MetricCollector` instance and registers it for routing updates and querying.
  14. Register(collector *MetricCollector) error
  15. // Unregister accepts a `MetricCollectorID` and unregisters the metric metric instance from receiving metrics
  16. // updates and query availability.
  17. Unregister(collectorID MetricCollectorID) bool
  18. // Query accepts a `MetricCollectorID` and returns a slice of `MetricResult` instances for that metric.
  19. Query(collectorID MetricCollectorID) ([]*aggregator.MetricResult, error)
  20. // Update accepts the name of a metric, the label set and values to update the metric, the updated Value, and a Timestamp.
  21. // This method does not accept a `MetricCollectorID` because it provides updates across many potential MetricCollector instances
  22. // which utilize the same metric.
  23. Update(metricName string, labels map[string]string, value float64, timestamp time.Time, additionalInformation map[string]string)
  24. }
  25. type MetricStoreFactory func() MetricStore
  26. // InMemoryMetricStore is a thread-safe implementation of the MetricStore interface that stores MetricCollector instances
  27. // in memory.
  28. type InMemoryMetricStore struct {
  29. lock sync.Mutex
  30. byMetricName map[string][]*MetricCollector
  31. byCollectorID map[MetricCollectorID]*MetricCollector
  32. }
  33. func NewInMemoryMetricStore() MetricStore {
  34. return &InMemoryMetricStore{
  35. byMetricName: make(map[string][]*MetricCollector),
  36. byCollectorID: make(map[MetricCollectorID]*MetricCollector),
  37. }
  38. }
  39. func (m *InMemoryMetricStore) Register(collector *MetricCollector) error {
  40. m.lock.Lock()
  41. defer m.lock.Unlock()
  42. if _, ok := m.byCollectorID[collector.id]; ok {
  43. return fmt.Errorf("metric with ID: %s already exists", collector.id)
  44. }
  45. m.byCollectorID[collector.id] = collector
  46. m.byMetricName[collector.metricName] = append(m.byMetricName[collector.metricName], collector)
  47. return nil
  48. }
  49. func (m *InMemoryMetricStore) Unregister(collectorID MetricCollectorID) bool {
  50. m.lock.Lock()
  51. defer m.lock.Unlock()
  52. if _, ok := m.byCollectorID[collectorID]; !ok {
  53. return false
  54. }
  55. inst := m.byCollectorID[collectorID]
  56. m.byMetricName[inst.metricName] = slices.DeleteFunc(m.byMetricName[inst.metricName], func(mc *MetricCollector) bool {
  57. return mc == nil || mc.id == collectorID
  58. })
  59. delete(m.byCollectorID, collectorID)
  60. return true
  61. }
  62. func (m *InMemoryMetricStore) Query(collectorID MetricCollectorID) ([]*aggregator.MetricResult, error) {
  63. m.lock.Lock()
  64. defer m.lock.Unlock()
  65. if _, ok := m.byCollectorID[collectorID]; !ok {
  66. return nil, fmt.Errorf("metric with ID: %s does not exist", collectorID)
  67. }
  68. return m.byCollectorID[collectorID].Get(), nil
  69. }
  70. func (m *InMemoryMetricStore) Update(
  71. metricName string,
  72. labels map[string]string,
  73. value float64,
  74. timestamp time.Time,
  75. additionalInformation map[string]string,
  76. ) {
  77. m.lock.Lock()
  78. defer m.lock.Unlock()
  79. for _, collector := range m.byMetricName[metricName] {
  80. collector.Update(labels, value, timestamp, additionalInformation)
  81. }
  82. }