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
|
package kafka
import (
"sync/atomic"
"time"
)
// SummaryStats is a data structure that carries a summary of observed values.
// The average, minimum, and maximum are reported.
type SummaryStats struct {
Avg int64 `metric:"avg" type:"gauge"`
Min int64 `metric:"min" type:"gauge"`
Max int64 `metric:"max" type:"gauge"`
}
// DurationStats is a data structure that carries a summary of observed duration
// values. The average, minimum, and maximum are reported.
type DurationStats struct {
Avg time.Duration `metric:"avg" type:"gauge"`
Min time.Duration `metric:"min" type:"gauge"`
Max time.Duration `metric:"max" type:"gauge"`
}
// counter is an atomic incrementing counter which gets reset on snapshot.
//
// Since atomic is used to mutate the statistic the value must be 64-bit aligned.
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
type counter int64
func (c *counter) ptr() *int64 {
return (*int64)(c)
}
func (c *counter) observe(v int64) {
atomic.AddInt64(c.ptr(), v)
}
func (c *counter) snapshot() int64 {
p := c.ptr()
v := atomic.LoadInt64(p)
atomic.AddInt64(p, -v)
return v
}
// gauge is an atomic integer that may be set to any arbitrary value, the value
// does not change after a snapshot.
//
// Since atomic is used to mutate the statistic the value must be 64-bit aligned.
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
type gauge int64
func (g *gauge) ptr() *int64 {
return (*int64)(g)
}
func (g *gauge) observe(v int64) {
atomic.StoreInt64(g.ptr(), v)
}
func (g *gauge) snapshot() int64 {
return atomic.LoadInt64(g.ptr())
}
// minimum is an atomic integral type that keeps track of the minimum of all
// values that it observed between snapshots.
//
// Since atomic is used to mutate the statistic the value must be 64-bit aligned.
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
type minimum int64
func (m *minimum) ptr() *int64 {
return (*int64)(m)
}
func (m *minimum) observe(v int64) {
for {
ptr := m.ptr()
min := atomic.LoadInt64(ptr)
if min >= 0 && min <= v {
break
}
if atomic.CompareAndSwapInt64(ptr, min, v) {
break
}
}
}
func (m *minimum) snapshot() int64 {
p := m.ptr()
v := atomic.LoadInt64(p)
atomic.CompareAndSwapInt64(p, v, -1)
if v < 0 {
v = 0
}
return v
}
// maximum is an atomic integral type that keeps track of the maximum of all
// values that it observed between snapshots.
//
// Since atomic is used to mutate the statistic the value must be 64-bit aligned.
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
type maximum int64
func (m *maximum) ptr() *int64 {
return (*int64)(m)
}
func (m *maximum) observe(v int64) {
for {
ptr := m.ptr()
max := atomic.LoadInt64(ptr)
if max >= 0 && max >= v {
break
}
if atomic.CompareAndSwapInt64(ptr, max, v) {
break
}
}
}
func (m *maximum) snapshot() int64 {
p := m.ptr()
v := atomic.LoadInt64(p)
atomic.CompareAndSwapInt64(p, v, -1)
if v < 0 {
v = 0
}
return v
}
type summary struct {
min minimum
max maximum
sum counter
count counter
}
func makeSummary() summary {
return summary{
min: -1,
max: -1,
}
}
func (s *summary) observe(v int64) {
s.min.observe(v)
s.max.observe(v)
s.sum.observe(v)
s.count.observe(1)
}
func (s *summary) observeDuration(v time.Duration) {
s.observe(int64(v))
}
func (s *summary) snapshot() SummaryStats {
avg := int64(0)
min := s.min.snapshot()
max := s.max.snapshot()
sum := s.sum.snapshot()
count := s.count.snapshot()
if count != 0 {
avg = int64(float64(sum) / float64(count))
}
return SummaryStats{
Avg: avg,
Min: min,
Max: max,
}
}
func (s *summary) snapshotDuration() DurationStats {
summary := s.snapshot()
return DurationStats{
Avg: time.Duration(summary.Avg),
Min: time.Duration(summary.Min),
Max: time.Duration(summary.Max),
}
}
|