forked from bookingcom/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
235 lines (226 loc) · 8.06 KB
/
function.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
package below
import (
"github.com/bookingcom/carbonapi/expr/helper"
"github.com/bookingcom/carbonapi/expr/interfaces"
"github.com/bookingcom/carbonapi/expr/types"
"github.com/bookingcom/carbonapi/pkg/parser"
"strings"
)
type below struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &below{}
functions := []string{"averageAbove", "averageBelow", "currentAbove", "currentBelow", "maximumAbove", "maximumBelow", "minimumAbove", "minimumBelow"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// averageAbove(seriesList, n), averageBelow(seriesList, n), currentAbove(seriesList, n), currentBelow(seriesList, n), maximumAbove(seriesList, n), maximumBelow(seriesList, n), minimumAbove(seriesList, n), minimumBelow
func (f *below) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
args, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
n, err := e.GetFloatArg(1)
if err != nil {
return nil, err
}
isAbove := strings.HasSuffix(e.Target(), "Above")
isInclusive := true
var compute func([]float64, []bool) float64
switch {
case strings.HasPrefix(e.Target(), "average"):
compute = helper.AvgValue
case strings.HasPrefix(e.Target(), "current"):
compute = helper.CurrentValue
case strings.HasPrefix(e.Target(), "maximum"):
compute = helper.MaxValue
isInclusive = false
case strings.HasPrefix(e.Target(), "minimum"):
compute = helper.MinValue
isInclusive = false
}
var results []*types.MetricData
for _, a := range args {
value := compute(a.Values, a.IsAbsent)
if isAbove {
if isInclusive {
if value >= n {
results = append(results, a)
}
} else {
if value > n {
results = append(results, a)
}
}
} else {
if value <= n {
results = append(results, a)
}
}
}
return results, err
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *below) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"averageAbove": {
Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics with an average value\nabove N for the time period specified.\n\nExample:\n\n.. code-block:: none\n\n &target=averageAbove(server*.instance*.threads.busy,25)\n\nDraws the servers with average values above 25.",
Function: "averageAbove(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "averageAbove",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"averageBelow": {
Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics with an average value\nbelow N for the time period specified.\n\nExample:\n\n.. code-block:: none\n\n &target=averageBelow(server*.instance*.threads.busy,25)\n\nDraws the servers with average values below 25.",
Function: "averageBelow(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "averageBelow",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"currentAbove": {
Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics whose value is above N\nat the end of the time period specified.\n\nExample:\n\n.. code-block:: none\n\n &target=currentAbove(server*.instance*.threads.busy,50)\n\nDraws the servers with more than 50 busy threads.",
Function: "currentAbove(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "currentAbove",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"currentBelow": {
Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics whose value is below N\nat the end of the time period specified.\n\nExample:\n\n.. code-block:: none\n\n &target=currentBelow(server*.instance*.threads.busy,3)\n\nDraws the servers with less than 3 busy threads.",
Function: "currentBelow(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "currentBelow",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"maximumAbove": {
Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a maximum value above n.\n\nExample:\n\n.. code-block:: none\n\n &target=maximumAbove(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent more than 1000 packets/min.",
Function: "maximumAbove(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "maximumAbove",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"maximumBelow": {
Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a maximum value below n.\n\nExample:\n\n.. code-block:: none\n\n &target=maximumBelow(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent less than 1000 packets/min.",
Function: "maximumBelow(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "maximumBelow",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"minimumAbove": {
Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a minimum value above n.\n\nExample:\n\n.. code-block:: none\n\n &target=minimumAbove(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent more than 1000 packets/min.",
Function: "minimumAbove(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "minimumAbove",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
"minimumBelow": {
Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a minimum value below n.\n\nExample:\n\n.. code-block:: none\n\n &target=minimumBelow(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which at one point sent less than 1000 packets/min.",
Function: "minimumBelow(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "minimumBelow",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
}
}