-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
218 lines (194 loc) · 4.74 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package stats
import (
"context"
"strings"
"sync"
"sync/atomic"
"time"
)
// Store is a storage for all known counters, gauges and histograms.
type Store struct {
mu sync.RWMutex
flushInterval time.Duration
tp *TagProducer
sinks atomic.Value // []Sink
scopes map[string]*Scope
errors chan error
}
// NewStore returns a stats storage.
func NewStore(o *StoreOption) *Store {
if o == nil {
o = NewStoreOption()
}
store := &Store{
flushInterval: o.FlushInterval,
scopes: make(map[string]*Scope),
errors: make(chan error),
}
store.sinks.Store(o.Sinks)
return store
}
// Errors returns chan receiving errors occurred during flushing.
func (store *Store) Errors() <-chan error {
return store.errors
}
// FlushingLoop flushes stats to remote destinations(defined by Sinks) at an interval, it blocks untils ctx canceled.
// NOTE: this function must be called or metrics won't be sent out.
func (store *Store) FlushingLoop(ctx context.Context) {
ticker := time.NewTicker(store.flushInterval)
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
// make metrics snashot
guages := store.Gauges()
counters := store.Counters()
histograms := store.Histograms()
snapshot := newMetricsSnapshot(guages, counters, histograms)
// flush metrics to the registerd sinks
sinks := store.Sinks()
for _, sink := range sinks {
err := sink.Flush(snapshot)
store.sendError(err)
}
}
}
}
func (store *Store) sendError(err error) {
if err == nil {
return
}
select {
case store.errors <- err:
default:
}
}
// SetTagOption sets tagging option to given one.
func (store *Store) SetTagOption(o *TagOption) error {
var tp *TagProducer
if len(o.DefaultTags) > 0 || len(o.TagExtractStrategies) > 0 {
tp = NewTagProducer(o.defaultTags()...)
for _, strategy := range o.TagExtractStrategies {
te, err := NewTagExtractor(strategy.Name, strategy.Regex, strategy.SubStr)
if err != nil {
return err
}
tp.AddExtractor(te)
}
}
store.tp = tp
return nil
}
func (store *Store) setTagProducer(tp *TagProducer) {
store.tp = tp
}
// AddSink adds a sink
func (store *Store) AddSink(sink Sink) {
store.mu.Lock()
defer store.mu.Unlock()
sinks := store.Sinks()
tmpSinks := make([]Sink, len(sinks), len(sinks)+1)
copy(tmpSinks, sinks)
tmpSinks = append(tmpSinks, sink)
store.sinks.Store(tmpSinks)
}
// Sinks return all known sinks.
func (store *Store) Sinks() []Sink {
return store.sinks.Load().([]Sink)
}
// CreateScope creates the named Scope.
func (store *Store) CreateScope(name string) *Scope {
store.mu.Lock()
defer store.mu.Unlock()
// fix suffix
if len(name) > 0 && !strings.HasSuffix(name, ".") {
name += "."
}
scope, ok := store.scopes[name]
if !ok {
scope = newScope(name, store)
store.scopes[name] = scope
}
scope.refCount++
return scope
}
// DeleteScope deletes the named Scope.
func (store *Store) DeleteScope(scope *Scope) {
store.mu.Lock()
defer store.mu.Unlock()
store.deleteScopeWithAllChilds(scope)
}
func (store *Store) deleteScopeWithAllChilds(scope *Scope) {
name := scope.Name()
scope, ok := store.scopes[name]
if !ok {
return
}
children := scope.loadChildren()
for _, child := range children {
store.deleteScopeWithAllChilds(child)
}
if scope.refCount == 1 {
delete(store.scopes, name)
return
}
scope.refCount--
}
// Scopes returns all known scopes.
func (store *Store) Scopes() []*Scope {
store.mu.RLock()
scopes := make([]*Scope, 0, len(store.scopes))
for _, scope := range store.scopes {
scopes = append(scopes, scope)
}
store.mu.RUnlock()
return scopes
}
// Counters returns all known counters.
func (store *Store) Counters() []*Counter {
scopes := store.Scopes()
cs := make([]*Counter, 0, len(scopes)*2)
for _, scope := range scopes {
cs = append(cs, scope.Counters()...)
}
return cs
}
// Gauges returns all known gauges.
func (store *Store) Gauges() []*Gauge {
scopes := store.Scopes()
gs := make([]*Gauge, 0, len(scopes)*2)
for _, scope := range scopes {
gs = append(gs, scope.Gauges()...)
}
return gs
}
// Histograms returns all known histograms.
func (store *Store) Histograms() []*Histogram {
scopes := store.Scopes()
hs := make([]*Histogram, 0, len(scopes)*2)
for _, scope := range scopes {
hs = append(hs, scope.Histograms()...)
}
return hs
}
func (store *Store) deliverHistogramSampleToSinks(h *Histogram, val uint64) {
sinks := store.Sinks()
for _, sink := range sinks {
sink.WriteHistogramSample(h, val)
}
}
func (store *Store) clearAllScopes() {
store.mu.Lock()
for name := range store.scopes {
delete(store.scopes, name)
}
store.mu.Unlock()
}
func (store *Store) getTagsForName(name string) (string, []*Tag) {
if store.tp == nil {
return name, nil
}
return store.tp.Produce(name)
}