forked from Netflix/spectator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist_summary.go
69 lines (60 loc) · 2.05 KB
/
dist_summary.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
package spectator
import (
"sync/atomic"
)
// DistributionSummary is used to track the distribution of events. This is safe
// for concurrent use.
//
// You can find more about this type by viewing the relevant Java Spectator
// documentation here:
//
// https://netflix.github.io/spectator/en/latest/intro/dist-summary/
type DistributionSummary struct {
count int64
totalAmount int64
totalSqBits uint64
max int64
// Pointers need to be after counters to ensure 64-bit alignment. See
// note in atomicnum.go
id *Id
}
// NewDistributionSummary generates a new distribution summary, using the
// provided meter identifier.
func NewDistributionSummary(id *Id) *DistributionSummary {
return &DistributionSummary{0, 0, 0, 0, id}
}
// MeterId returns the meter identifier.
func (d *DistributionSummary) MeterId() *Id {
return d.id
}
// Record records a new value to track within the distribution.
func (d *DistributionSummary) Record(amount int64) {
if amount >= 0 {
atomic.AddInt64(&d.count, 1)
atomic.AddInt64(&d.totalAmount, amount)
addFloat64(&d.totalSqBits, float64(amount)*float64(amount))
updateMax(&d.max, amount)
}
}
// Count returns the number of unique events that have been tracked.
func (d *DistributionSummary) Count() int64 {
return atomic.LoadInt64(&d.count)
}
// TotalAmount is the total of all records summed together.
func (d *DistributionSummary) TotalAmount() int64 {
return atomic.LoadInt64(&d.totalAmount)
}
// Measure returns the list of measurements known by the counter. This should
// return 4 measurements in the slice:
//
// count
// totalAmount
// totalOfSquares
// max
func (d *DistributionSummary) Measure() []Measurement {
cnt := Measurement{d.id.WithStat("count"), float64(atomic.SwapInt64(&d.count, 0))}
tTime := Measurement{d.id.WithStat("totalAmount"), float64(atomic.SwapInt64(&d.totalAmount, 0))}
tSq := Measurement{d.id.WithStat("totalOfSquares"), swapFloat64(&d.totalSqBits, 0.0)}
mx := Measurement{d.id.WithStat("max"), float64(atomic.SwapInt64(&d.max, 0))}
return []Measurement{cnt, tTime, tSq, mx}
}