-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedCounters.go
287 lines (246 loc) · 7.88 KB
/
CachedCounters.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package count
import (
"context"
"sync"
"time"
"github.com/pip-services3-gox/pip-services3-commons-gox/config"
)
// CachedCounters abstract implementation of performance counters that measures and
// stores counters in memory. Child classes implement saving of the counters
// into various destinations.
// Configuration parameters:
// - options:
// - interval: interval in milliseconds to save current counters measurements (default: 5 mins)
// - reset_timeout: timeout in milliseconds to reset the counters. 0 disables the reset (default: 0)
type CachedCounters struct {
cache map[string]*AtomicCounter
updated bool
lastDumpTime time.Time
lastResetTime time.Time
mux sync.RWMutex
interval int64
resetTimeout int64
Overrides ICachedCountersOverrides
}
type ICachedCountersOverrides interface {
Save(ctx context.Context, counters []Counter) error
}
const (
DefaultInterval int64 = 300000
DefaultResetTimeout int64 = 300000
ConfigParameterInterval = "interval"
ConfigParameterResetTimeout = "reset_timeout"
)
// InheritCacheCounters inherit cache counters from saver
// Parameters:
// - save ICountersSaver
// Returns: *CachedCounters
func InheritCacheCounters(overrides ICachedCountersOverrides) *CachedCounters {
return &CachedCounters{
cache: make(map[string]*AtomicCounter),
updated: false,
lastDumpTime: time.Now(),
lastResetTime: time.Now(),
interval: DefaultInterval,
resetTimeout: DefaultResetTimeout,
Overrides: overrides,
}
}
// Configure configures component by passing configuration parameters.
// Parameters:
// - ctx context.Context
// - config *config.ConfigParams configuration parameters to be set.
func (c *CachedCounters) Configure(ctx context.Context, config *config.ConfigParams) {
c.interval = config.GetAsLongWithDefault(ConfigParameterInterval, c.interval)
c.resetTimeout = config.GetAsLongWithDefault(ConfigParameterResetTimeout, c.resetTimeout)
}
// Clear clears (resets) a counter specified by its name.
// Parameters:
// - ctx context.Context
// - name string a counter name to clear.
func (c *CachedCounters) Clear(ctx context.Context, name string) {
c.mux.Lock()
defer c.mux.Unlock()
delete(c.cache, name)
}
// ClearAll clears (resets) all counters.
// Parameters:
// - ctx context.Context
func (c *CachedCounters) ClearAll(ctx context.Context) {
c.mux.Lock()
defer c.mux.Unlock()
c.cache = make(map[string]*AtomicCounter)
}
func (c *CachedCounters) isUpdated() bool {
c.mux.RLock()
defer c.mux.RUnlock()
return c.updated
}
// Dump (saves) the current values of counters.
// Parameters:
// - ctx context.Context
func (c *CachedCounters) Dump(ctx context.Context) error {
if !c.isUpdated() {
return nil
}
counters := c.GetAllCountersStats()
err := c.Overrides.Save(ctx, counters)
if err != nil {
return err
}
c.mux.Lock()
c.updated = false
c.lastDumpTime = time.Now()
c.mux.Unlock()
return nil
}
func (c *CachedCounters) update(ctx context.Context) error {
c.mux.Lock()
c.updated = true
newDumpTime := c.lastDumpTime.Add(time.Duration(c.interval) * time.Millisecond)
c.mux.Unlock()
if time.Now().After(newDumpTime) {
return c.Dump(ctx)
}
return nil
}
func (c *CachedCounters) resetIfNeeded(ctx context.Context) {
c.mux.Lock()
defer c.mux.Unlock()
if c.resetTimeout == 0 {
return
}
newResetTime := c.lastResetTime.Add(time.Duration(c.resetTimeout) * time.Millisecond)
if time.Now().After(newResetTime) {
c.cache = make(map[string]*AtomicCounter)
c.updated = false
c.lastDumpTime = time.Now()
}
}
// GetAll gets all captured counters.
// Returns: []*AtomicCounter
func (c *CachedCounters) GetAll() []*AtomicCounter {
c.mux.RLock()
defer c.mux.RUnlock()
result := make([]*AtomicCounter, 0, len(c.cache))
for _, v := range c.cache {
result = append(result, v)
}
return result
}
// GetAllCountersStats gets all captured counters stats.
// Returns: []Counter
func (c *CachedCounters) GetAllCountersStats() []Counter {
c.mux.RLock()
defer c.mux.RUnlock()
return c.getAllCountersStats()
}
func (c *CachedCounters) getAllCountersStats() []Counter {
result := make([]Counter, 0, len(c.cache))
for _, v := range c.cache {
result = append(result, v.GetCounter())
}
return result
}
// Get a counter specified by its name. It counter does not exist or its type doesn't match the
// specified type it creates a new one.
// Parameters:
// - ctx context.Context
// - name string a counter name to retrieve.
// - typ int a counter type.
// Returns: *Counter an existing or newly created counter of the specified type.
func (c *CachedCounters) Get(ctx context.Context, name string, typ CounterType) (*AtomicCounter, bool) {
if name == "" {
return nil, false
}
c.resetIfNeeded(ctx)
c.mux.Lock()
defer c.mux.Unlock()
counter, ok := c.cache[name]
if !ok || counter.Type() != typ {
counter = NewAtomicCounter(name, typ)
c.cache[name] = counter
}
return counter, true
}
// BeginTiming begins measurement of execution time interval.
// It returns Timing object which has to be called at
// Timing.EndTiming to end the measurement and update the counter.
// Parameters
// - ctx context.Context
// - name string a counter name of Interval type.
// Returns: *Timing a Timing callback object to end timing.
func (c *CachedCounters) BeginTiming(ctx context.Context, name string) *CounterTiming {
return NewCounterTiming(name, c)
}
// EndTiming ends measurement of execution elapsed time and updates specified counter.
// see Timing.EndTiming
// Parameters:
// - ctx context.Context
// - name string a counter name
// - elapsed float64 execution elapsed time in milliseconds to update the counter.
func (c *CachedCounters) EndTiming(ctx context.Context, name string, elapsed float64) {
if counter, ok := c.Get(ctx, name, Interval); ok {
counter.CalculateStats(elapsed)
_ = c.update(ctx)
}
}
// Stats calculates min/average/max statistics based on the current and previous values.
// Parameters:
// - ctx context.Context
// - name string a counter name of Statistics type
// - value float32 a value to update statistics
func (c *CachedCounters) Stats(ctx context.Context, name string, value float64) {
if counter, ok := c.Get(ctx, name, Statistics); ok {
counter.CalculateStats(value)
_ = c.update(ctx)
}
}
// Last records the last calculated measurement value.
// Usually this method is used by metrics calculated externally.
// Parameters:
// - ctx context.Context
// - name string a counter name of Last type.
// - value number a last value to record.
func (c *CachedCounters) Last(ctx context.Context, name string, value float64) {
if counter, ok := c.Get(ctx, name, LastValue); ok {
counter.SetLast(value)
_ = c.update(ctx)
}
}
// TimestampNow records the current time as a timestamp.
// Parameters:
// - ctx context.Context
// - name string a counter name of Timestamp type.
func (c *CachedCounters) TimestampNow(ctx context.Context, name string) {
c.Timestamp(ctx, name, time.Now())
}
// Timestamp records the given timestamp.
// Parameters:
// - ctx context.Context
// - name string a counter name of Timestamp type.
// - value time.Time a timestamp to record.
func (c *CachedCounters) Timestamp(ctx context.Context, name string, value time.Time) {
if counter, ok := c.Get(ctx, name, Timestamp); ok {
counter.SetTime(value)
_ = c.update(ctx)
}
}
// IncrementOne increments counter by 1.
// Parameters:
// - ctx context.Context
// - name string a counter name of Increment type.
func (c *CachedCounters) IncrementOne(ctx context.Context, name string) {
c.Increment(ctx, name, 1)
}
// Increment increments counter by given value.
// Parameters:
// - ctx context.Context
// - name string a counter name of Increment type.
// - value int a value to add to the counter.
func (c *CachedCounters) Increment(ctx context.Context, name string, value int64) {
if counter, ok := c.Get(ctx, name, Increment); ok {
counter.Inc(value)
_ = c.update(ctx)
}
}