-
Notifications
You must be signed in to change notification settings - Fork 1
/
summary.go
177 lines (147 loc) · 3.55 KB
/
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
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
package openmetrics
import (
"fmt"
"math"
"sync"
"time"
)
// SummaryFamily is a metric family of summaries.
type SummaryFamily interface {
MetricFamily
// With returns a Summary for the given label values.
With(labelValues ...string) Summary
}
type summaryFamily struct {
metricFamily
}
func (f *summaryFamily) With(labelValues ...string) Summary {
met, err := f.with(labelValues...)
if err != nil {
f.onError(err)
return nullSummary{}
}
return met.(Summary)
}
// ----------------------------------------------------------------------------
// SummaryOptions configure Summary instances.
type SummaryOptions struct {
CreatedAt time.Time // defaults to time.Now()
OnError ErrorHandler // defaults to WarnOnError
}
// Summary is an Metric.
type Summary interface {
Metric
// Observe adds an observation. Attempts to pass negative, NaN or infinity
// values will result in an error.
Observe(val float64)
// Sum returns the sum of all observations.
Sum() float64
// Count returns the total number of observations.
Count() int64
// Created returns the created time.
Created() time.Time
// Reset resets the created time to now and the total to 0.
Reset(SummaryOptions)
}
type summary struct {
sum float64
count int64
created time.Time
onError ErrorHandler
mu sync.RWMutex
}
// NewSummary inits a new summary.
func NewSummary(opts SummaryOptions) (Summary, error) {
// errors cannot happen here but we reserve the "right" to return some to
// avoid backwards compatibility issues in the future
m := &summary{}
m.Reset(opts)
return m, nil
}
func (m *summary) AppendPoints(dst []MetricPoint, _ *Desc) ([]MetricPoint, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return append(dst,
MetricPoint{
Suffix: SuffixCount,
Value: float64(m.count),
},
MetricPoint{
Suffix: SuffixSum,
Value: m.sum,
},
MetricPoint{
Suffix: SuffixCreated,
Value: asEpoch(m.created),
},
), nil
}
func (m *summary) Observe(val float64) {
if err := summaryValidateValue(val); err != nil {
m.handleError(err)
return
}
m.mu.Lock()
m.count++
m.sum += val
m.mu.Unlock()
}
func (m *summary) Reset(opts SummaryOptions) {
m.mu.Lock()
defer m.mu.Unlock()
m.sum = 0
m.count = 0
m.created = opts.CreatedAt
m.onError = opts.OnError
if m.created.IsZero() {
m.created = time.Now()
}
if m.onError == nil {
m.onError = WarnOnError
}
}
func (m *summary) Created() time.Time {
m.mu.RLock()
v := m.created
m.mu.RUnlock()
return v
}
func (m *summary) Sum() float64 {
m.mu.RLock()
v := m.sum
m.mu.RUnlock()
return v
}
func (m *summary) Count() int64 {
m.mu.RLock()
v := m.count
m.mu.RUnlock()
return v
}
func (m *summary) handleError(err error) {
m.mu.RLock()
m.onError(err)
m.mu.RUnlock()
}
type nullSummary struct{}
func (nullSummary) AppendPoints(dst []MetricPoint, _ *Desc) ([]MetricPoint, error) { return dst, nil }
func (nullSummary) Observe(_ float64) {}
func (nullSummary) Reset(_ SummaryOptions) {}
func (nullSummary) Created() time.Time { return time.Time{} }
func (nullSummary) Sum() float64 { return 0.0 }
func (nullSummary) Count() int64 { return 0 }
var (
errSummaryNegative = fmt.Errorf("summaries cannot accept negative values")
errSummaryNaN = fmt.Errorf("summaries cannot accept NaN values")
errSummaryInf = fmt.Errorf("summaries cannot accept infinity values")
)
func summaryValidateValue(val float64) error {
if val < 0 {
return errSummaryNegative
} else if math.IsNaN(val) {
return errSummaryNaN
} else if math.IsInf(val, 1) {
return errSummaryInf
}
return nil
}