forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.go
263 lines (234 loc) · 5.94 KB
/
stats.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package stats
import (
"errors"
"strconv"
"time"
"strings"
"github.com/dustin/go-humanize"
"gopkg.in/guregu/null.v3"
)
const (
counterString = `"counter"`
gaugeString = `"gauge"`
trendString = `"trend"`
rateString = `"rate"`
defaultString = `"default"`
timeString = `"time"`
dataString = `"data"`
)
// Possible values for MetricType.
const (
Counter = MetricType(iota) // A counter that sums its data points
Gauge // A gauge that displays the latest value
Trend // A trend, min/max/avg/med are interesting
Rate // A rate, displays % of values that aren't 0
)
// Possible values for ValueType.
const (
Default = ValueType(iota) // Values are presented as-is
Time // Values are timestamps (nanoseconds)
Data // Values are data amounts (bytes)
)
// The serialized metric type is invalid.
var ErrInvalidMetricType = errors.New("Invalid metric type")
// The serialized value type is invalid.
var ErrInvalidValueType = errors.New("Invalid value type")
// A MetricType specifies the type of a metric.
type MetricType int
// MarshalJSON serializes a MetricType as a human readable string.
func (t MetricType) MarshalJSON() ([]byte, error) {
switch t {
case Counter:
return []byte(counterString), nil
case Gauge:
return []byte(gaugeString), nil
case Trend:
return []byte(trendString), nil
case Rate:
return []byte(rateString), nil
default:
return nil, ErrInvalidMetricType
}
}
// UnmarshalJSON deserializes a MetricType from a string representation.
func (t *MetricType) UnmarshalJSON(data []byte) error {
switch string(data) {
case counterString:
*t = Counter
case gaugeString:
*t = Gauge
case trendString:
*t = Trend
case rateString:
*t = Rate
default:
return ErrInvalidMetricType
}
return nil
}
func (t MetricType) String() string {
switch t {
case Counter:
return counterString
case Gauge:
return gaugeString
case Trend:
return trendString
case Rate:
return rateString
default:
return "[INVALID]"
}
}
// The type of values a metric contains.
type ValueType int
// MarshalJSON serializes a ValueType as a human readable string.
func (t ValueType) MarshalJSON() ([]byte, error) {
switch t {
case Default:
return []byte(defaultString), nil
case Time:
return []byte(timeString), nil
case Data:
return []byte(dataString), nil
default:
return nil, ErrInvalidValueType
}
}
// UnmarshalJSON deserializes a ValueType from a string representation.
func (t *ValueType) UnmarshalJSON(data []byte) error {
switch string(data) {
case defaultString:
*t = Default
case timeString:
*t = Time
case dataString:
*t = Data
default:
return ErrInvalidValueType
}
return nil
}
func (t ValueType) String() string {
switch t {
case Default:
return defaultString
case Time:
return timeString
case Data:
return dataString
default:
return "[INVALID]"
}
}
// A Sample is a single measurement.
type Sample struct {
Metric *Metric
Time time.Time
Tags map[string]string
Value float64
}
// A Metric defines the shape of a set of data.
type Metric struct {
Name string `json:"-"`
Type MetricType `json:"type"`
Contains ValueType `json:"contains"`
Tainted null.Bool `json:"tainted"`
Sink Sink `json:"sink"`
Thresholds Thresholds `json:"thresholds"`
Submetrics []Submetric `json:"submetrics"`
}
func New(name string, typ MetricType, t ...ValueType) *Metric {
vt := Default
if len(t) > 0 {
vt = t[0]
}
var sink Sink
switch typ {
case Counter:
sink = &CounterSink{}
case Gauge:
sink = &GaugeSink{}
case Trend:
sink = &TrendSink{}
case Rate:
sink = &RateSink{}
default:
return nil
}
return &Metric{Name: name, Type: typ, Contains: vt, Sink: sink}
}
func (m Metric) HumanizeValue(v float64) string {
switch m.Type {
case Rate:
return strconv.FormatFloat(100*v, 'f', 2, 64) + "%"
default:
switch m.Contains {
case Time:
d := ToD(v)
switch {
case d > time.Minute:
d -= d % (1 * time.Second)
case d > time.Second:
d -= d % (10 * time.Millisecond)
case d > time.Millisecond:
d -= d % (10 * time.Microsecond)
case d > time.Microsecond:
d -= d % (10 * time.Nanosecond)
}
return d.String()
case Data:
return humanize.Bytes(uint64(v))
default:
return strconv.FormatFloat(v, 'f', -1, 64)
}
}
}
// A Submetric represents a filtered dataset based on a parent metric.
type Submetric struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Metric *Metric `json:"metric"`
}
// Creates a submetric from a name.
func NewSubmetric(name string) (parentName string, sm Submetric) {
parts := strings.SplitN(strings.TrimSuffix(name, "}"), "{", 2)
if len(parts) == 1 {
return parts[0], Submetric{Name: name}
}
kvs := strings.Split(parts[1], ",")
tags := make(map[string]string, len(kvs))
for _, kv := range kvs {
if kv == "" {
continue
}
parts := strings.SplitN(kv, ":", 2)
key := strings.TrimSpace(strings.Trim(parts[0], `"'`))
if len(parts) != 2 {
tags[key] = ""
continue
}
value := strings.TrimSpace(strings.Trim(parts[1], `"'`))
tags[key] = value
}
return parts[0], Submetric{Name: name, Tags: tags}
}