-
Notifications
You must be signed in to change notification settings - Fork 12
/
store.go
52 lines (44 loc) · 886 Bytes
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2018-2019, Square, Inc.
package metrics
import (
"fmt"
"sync"
)
type Store interface {
Add(m Metrics, name string) error
Get(name string) Metrics
Names() []string
}
type memoryStore struct {
metrics map[string]Metrics
*sync.RWMutex
}
func NewMemoryStore() Store {
return &memoryStore{
metrics: map[string]Metrics{},
RWMutex: &sync.RWMutex{},
}
}
func (s *memoryStore) Add(m Metrics, name string) error {
s.Lock()
defer s.Unlock()
if _, ok := s.metrics[name]; ok {
return fmt.Errorf("metrics for %s already stored", name)
}
s.metrics[name] = m
return nil
}
func (s *memoryStore) Get(name string) Metrics {
s.RLock()
defer s.RUnlock()
return s.metrics[name]
}
func (s *memoryStore) Names() []string {
s.RLock()
names := make([]string, 0, len(s.metrics))
for k := range s.metrics {
names = append(names, k)
}
s.RUnlock()
return names
}