aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/resetting_timer.go
blob: e5327d3bd3391cc1138153be4e2f852d7d164f35 (plain) (blame)
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
package metrics

import (
    "math"
    "sort"
    "sync"
    "time"
)

// Initial slice capacity for the values stored in a ResettingTimer
const InitialResettingTimerSliceCap = 10

// ResettingTimer is used for storing aggregated values for timers, which are reset on every flush interval.
type ResettingTimer interface {
    Values() []int64
    Snapshot() ResettingTimer
    Percentiles([]float64) []int64
    Mean() float64
    Time(func())
    Update(time.Duration)
    UpdateSince(time.Time)
}

// GetOrRegisterResettingTimer returns an existing ResettingTimer or constructs and registers a
// new StandardResettingTimer.
func GetOrRegisterResettingTimer(name string, r Registry) ResettingTimer {
    if nil == r {
        r = DefaultRegistry
    }
    return r.GetOrRegister(name, NewResettingTimer).(ResettingTimer)
}

// NewRegisteredResettingTimer constructs and registers a new StandardResettingTimer.
func NewRegisteredResettingTimer(name string, r Registry) ResettingTimer {
    c := NewResettingTimer()
    if nil == r {
        r = DefaultRegistry
    }
    r.Register(name, c)
    return c
}

// NewResettingTimer constructs a new StandardResettingTimer
func NewResettingTimer() ResettingTimer {
    if !Enabled {
        return NilResettingTimer{}
    }
    return &StandardResettingTimer{
        values: make([]int64, 0, InitialResettingTimerSliceCap),
    }
}

// NilResettingTimer is a no-op ResettingTimer.
type NilResettingTimer struct {
}

// Values is a no-op.
func (NilResettingTimer) Values() []int64 { return nil }

// Snapshot is a no-op.
func (NilResettingTimer) Snapshot() ResettingTimer {
    return &ResettingTimerSnapshot{
        values: []int64{},
    }
}

// Time is a no-op.
func (NilResettingTimer) Time(func()) {}

// Update is a no-op.
func (NilResettingTimer) Update(time.Duration) {}

// Percentiles panics.
func (NilResettingTimer) Percentiles([]float64) []int64 {
    panic("Percentiles called on a NilResettingTimer")
}

// Mean panics.
func (NilResettingTimer) Mean() float64 {
    panic("Mean called on a NilResettingTimer")
}

// UpdateSince is a no-op.
func (NilResettingTimer) UpdateSince(time.Time) {}

// StandardResettingTimer is the standard implementation of a ResettingTimer.
// and Meter.
type StandardResettingTimer struct {
    values []int64
    mutex  sync.Mutex
}

// Values returns a slice with all measurements.
func (t *StandardResettingTimer) Values() []int64 {
    return t.values
}

// Snapshot resets the timer and returns a read-only copy of its contents.
func (t *StandardResettingTimer) Snapshot() ResettingTimer {
    t.mutex.Lock()
    defer t.mutex.Unlock()
    currentValues := t.values
    t.values = make([]int64, 0, InitialResettingTimerSliceCap)

    return &ResettingTimerSnapshot{
        values: currentValues,
    }
}

// Percentiles panics.
func (t *StandardResettingTimer) Percentiles([]float64) []int64 {
    panic("Percentiles called on a StandardResettingTimer")
}

// Mean panics.
func (t *StandardResettingTimer) Mean() float64 {
    panic("Mean called on a StandardResettingTimer")
}

// Record the duration of the execution of the given function.
func (t *StandardResettingTimer) Time(f func()) {
    ts := time.Now()
    f()
    t.Update(time.Since(ts))
}

// Record the duration of an event.
func (t *StandardResettingTimer) Update(d time.Duration) {
    t.mutex.Lock()
    defer t.mutex.Unlock()
    t.values = append(t.values, int64(d))
}

// Record the duration of an event that started at a time and ends now.
func (t *StandardResettingTimer) UpdateSince(ts time.Time) {
    t.mutex.Lock()
    defer t.mutex.Unlock()
    t.values = append(t.values, int64(time.Since(ts)))
}

// ResettingTimerSnapshot is a point-in-time copy of another ResettingTimer.
type ResettingTimerSnapshot struct {
    values              []int64
    mean                float64
    thresholdBoundaries []int64
    calculated          bool
}

// Snapshot returns the snapshot.
func (t *ResettingTimerSnapshot) Snapshot() ResettingTimer { return t }

// Time panics.
func (*ResettingTimerSnapshot) Time(func()) {
    panic("Time called on a ResettingTimerSnapshot")
}

// Update panics.
func (*ResettingTimerSnapshot) Update(time.Duration) {
    panic("Update called on a ResettingTimerSnapshot")
}

// UpdateSince panics.
func (*ResettingTimerSnapshot) UpdateSince(time.Time) {
    panic("UpdateSince called on a ResettingTimerSnapshot")
}

// Values returns all values from snapshot.
func (t *ResettingTimerSnapshot) Values() []int64 {
    return t.values
}

// Percentiles returns the boundaries for the input percentiles.
func (t *ResettingTimerSnapshot) Percentiles(percentiles []float64) []int64 {
    t.calc(percentiles)

    return t.thresholdBoundaries
}

// Mean returns the mean of the snapshotted values
func (t *ResettingTimerSnapshot) Mean() float64 {
    if !t.calculated {
        t.calc([]float64{})
    }

    return t.mean
}

func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
    sort.Sort(Int64Slice(t.values))

    count := len(t.values)
    if count > 0 {
        min := t.values[0]
        max := t.values[count-1]

        cumulativeValues := make([]int64, count)
        cumulativeValues[0] = min
        for i := 1; i < count; i++ {
            cumulativeValues[i] = t.values[i] + cumulativeValues[i-1]
        }

        t.thresholdBoundaries = make([]int64, len(percentiles))

        thresholdBoundary := max

        for i, pct := range percentiles {
            if count > 1 {
                var abs float64
                if pct >= 0 {
                    abs = pct
                } else {
                    abs = 100 + pct
                }
                // poor man's math.Round(x):
                // math.Floor(x + 0.5)
                indexOfPerc := int(math.Floor(((abs / 100.0) * float64(count)) + 0.5))
                if pct >= 0 && indexOfPerc > 0 {
                    indexOfPerc -= 1 // index offset=0
                }
                thresholdBoundary = t.values[indexOfPerc]
            }

            t.thresholdBoundaries[i] = thresholdBoundary
        }

        sum := cumulativeValues[count-1]
        t.mean = float64(sum) / float64(count)
    } else {
        t.thresholdBoundaries = make([]int64, len(percentiles))
        t.mean = 0
    }

    t.calculated = true
}

// Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order.
type Int64Slice []int64

func (s Int64Slice) Len() int           { return len(s) }
func (s Int64Slice) Less(i, j int) bool { return s[i] < s[j] }
func (s Int64Slice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }