-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext.go
231 lines (195 loc) · 5.2 KB
/
text.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
package metrics
import (
"io"
"net/http"
"strconv"
"time"
)
// SkipTimestamp controls time inclusion with sample serialisation.
// When false, then live running values are stamped with the current
// time and Samples provide their own time.
var SkipTimestamp = false
const headerLine = "# Prometheus Samples\n"
// ServeHTTP provides a sample of each metric as an http.HandlerFunc.
func ServeHTTP(resp http.ResponseWriter, req *http.Request) {
std.ServeHTTP(resp, req)
}
// ServeHTTP provides a sample of each metric as an http.Handler.
func (reg *Register) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet && req.Method != http.MethodHead {
resp.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead)
if req.Method != http.MethodOptions {
http.Error(resp, "read-only resource", http.StatusMethodNotAllowed)
}
return
}
resp.Header().Set("Content-Type", "text/plain;version=0.0.4")
reg.WriteTo(resp)
}
// WriteText serialises a sample of each metric in a simple text
// format. All errors returned by Writer are ignored by design.
// Deprecated: Use WriteTo instead.
func WriteText(w io.Writer) {
std.WriteText(w)
}
// WriteText serialises a sample of each metric in a simple text
// format. All errors returned by Writer are ignored by design.
// Deprecated: Use WriteTo instead.
func (reg *Register) WriteText(w io.Writer) {
reg.WriteTo(w)
}
// WriteTo serialises a sample of each metric in a simple text
// format as an io.WriterTo.
func WriteTo(w io.Writer) (n int64, err error) {
return std.WriteTo(w)
}
// WriteTo serialises a sample of each metric in a simple text
// format as an io.WriterTo.
func (reg *Register) WriteTo(w io.Writer) (n int64, err error) {
wn, err := io.WriteString(w, headerLine)
n = int64(wn)
if err != nil {
return n, err
}
buf := make([]byte, 0, 512)
// snapshot
reg.mutex.RLock()
defer reg.mutex.RUnlock()
// serialise samples in order of appearance
for _, m := range reg.metrics {
buf = append(buf, m.comments...)
switch m.typeID {
case counterID:
if m.counter != nil {
buf = append(buf, m.counter.prefix...)
buf = strconv.AppendUint(buf, m.counter.Get(), 10)
buf = appendTimestamp(buf)
}
for _, l := range m.labels {
l.Lock()
view := l.counters
l.Unlock()
for _, v := range view {
buf = append(buf, v.prefix...)
buf = strconv.AppendUint(buf, v.Get(), 10)
buf = appendTimestamp(buf)
}
}
case integerID:
if m.integer != nil {
buf = append(buf, m.integer.prefix...)
buf = strconv.AppendInt(buf, m.integer.Get(), 10)
buf = appendTimestamp(buf)
}
for _, l := range m.labels {
l.Lock()
view := l.integers
l.Unlock()
for _, v := range view {
buf = append(buf, v.prefix...)
buf = strconv.AppendInt(buf, v.Get(), 10)
buf = appendTimestamp(buf)
}
}
case realID:
if m.real != nil {
buf = append(buf, m.real.prefix...)
buf = strconv.AppendFloat(buf, m.real.Get(), 'g', -1, 64)
buf = appendTimestamp(buf)
}
for _, l := range m.labels {
l.Lock()
view := l.reals
l.Unlock()
for _, v := range view {
buf = append(buf, v.prefix...)
buf = strconv.AppendFloat(buf, v.Get(), 'g', -1, 64)
buf = appendTimestamp(buf)
}
}
case counterSampleID, realSampleID:
if m.sample != nil {
buf = m.sample.append(buf)
}
for _, l := range m.labels {
l.Lock()
view := l.samples
l.Unlock()
for _, v := range view {
buf = v.append(buf)
}
}
case histogramID:
if m.histogram != nil {
buf = m.histogram.append(buf)
}
for _, l := range m.labels {
l.Lock()
view := l.histograms
l.Unlock()
for _, v := range view {
buf = v.append(buf)
}
}
}
wn, err = w.Write(buf)
n += int64(wn)
if err != nil {
return n, err
}
buf = buf[:0]
}
return n, nil
}
func (m *Sample) append(buf []byte) []byte {
if value, timestamp := m.Get(); timestamp != 0 {
buf = append(buf, m.prefix...)
buf = strconv.AppendFloat(buf, value, 'g', -1, 64)
if !SkipTimestamp {
buf = append(buf, ' ')
buf = strconv.AppendUint(buf, timestamp, 10)
}
buf = append(buf, '\n')
}
return buf
}
func (h *Histogram) append(buf []byte) []byte {
var stack [7]uint64
buckets, count, sum := h.Get(stack[:0])
buf = append(buf, h.countPrefix...)
offset := len(buf)
buf = strconv.AppendUint(buf, count, 10)
countSerial := buf[offset:]
timeOffset := len(buf)
buf = appendTimestamp(buf)
timestamp := buf[timeOffset:]
// buckets
var cum uint64
for i, prefix := range h.bucketPrefixes {
if i >= len(buckets) {
// (redundant) +Inf bucket
buf = append(buf, prefix...)
buf = append(buf, countSerial...)
buf = append(buf, timestamp...)
break
}
cum += buckets[i]
buf = append(buf, prefix...)
buf = strconv.AppendUint(buf, cum, 10)
buf = append(buf, timestamp...)
}
// sum
buf = append(buf, h.sumPrefix...)
buf = strconv.AppendFloat(buf, sum, 'g', -1, 64)
buf = append(buf, timestamp...)
return buf
}
func appendTimestamp(buf []byte) []byte {
if !SkipTimestamp {
buf = append(buf, ' ')
ms := time.Now().UnixNano() / 1e6
buf = strconv.AppendInt(buf, ms, 10)
}
buf = append(buf, '\n')
return buf
}