/
metrics_uploader.go
315 lines (273 loc) · 7.88 KB
/
metrics_uploader.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cidataloader
import (
"bytes"
"cloud.google.com/go/storage"
"context"
"encoding/json"
"fmt"
"io"
"log"
"strconv"
"time"
)
type metricsLoader struct {
instanceTime *time.Time
}
func generateMetricsUploader(client *storage.Client, ctx context.Context, event *JobRunDataEvent, dataLoader DataLoader) (SimpleUploader, error) {
m := metricsLoader{}
if !event.GCSEvent.TimeCreated.IsZero() {
m.instanceTime = &event.GCSEvent.TimeCreated
}
rows, err := m.parseRows(client, ctx, event.GCSEvent.Bucket, event.GCSEvent.Name)
if err != nil {
return nil, err
}
dataInstance := DataInstance{CreationTime: *m.instanceTime, JobRunName: event.BuildID, Source: event.Filename}
dataFile := DataFile{
TableName: "e2e_metrics",
Schema: map[string]DataType{"value": DataTypeFloat64, "timestamp": DataTypeInteger, "key": DataTypeString},
ChunkSize: 1000,
Rows: rows,
}
dataInstance.DataFile = &dataFile
loader, err := generateDataUploader(dataInstance, dataLoader)
if err != nil {
return nil, err
}
return loader, nil
}
func (m *metricsLoader) parseRows(client *storage.Client, ctx context.Context, bucket, name string) ([]map[string]string, error) {
output, err := m.getJSONMetrics(client, ctx, bucket, name)
if err != nil {
return nil, err
}
rows := make([]map[string]string, 0)
for k, v := range output {
if v.Value == "NaN" {
continue
}
sv := strconv.FormatInt(v.Timestamp, 10)
if sv == "NaN" {
continue
}
row := map[string]string{"key": k, "value": v.Value, "timestamp": sv}
rows = append(rows, row)
}
return rows, nil
}
type PrometheusData struct {
ResultType string `json:"resultType"`
Result []PrometheusMetric `json:"result"`
}
type PrometheusMetric struct {
Metric PrometheusLabels `json:"metric"`
Value PrometheusValue `json:"value"`
}
type PrometheusValue struct {
Timestamp int64
Value string
}
type OutputMetric struct {
Timestamp int64 `json:"timestamp"`
Value string `json:"value"`
}
type PrometheusResult struct {
Status string `json:"status"`
Data PrometheusData `json:"data"`
}
func (m *metricsLoader) getJSONMetrics(client *storage.Client, ctx context.Context, bucket, name string) (map[string]OutputMetric, error) {
o := client.Bucket(bucket).Object(name)
if m.instanceTime == nil {
attrs, err := o.Attrs(ctx)
if err != nil {
return nil, err
}
if attrs.Created.IsZero() {
return nil, fmt.Errorf("invalid creation time")
}
m.instanceTime = &attrs.Created
}
r, err := o.NewReader(ctx)
if err != nil {
return nil, err
}
defer r.Close()
// straight up lift from https://github.com/openshift/ci-search-functions/blob/master/functions.go#L191
metrics := make(map[string]PrometheusResult)
d := json.NewDecoder(r)
var rows int
for err = d.Decode(&metrics); err == nil; err = d.Decode(&metrics) {
rows++
}
if err != nil && err != io.EOF {
return nil, fmt.Errorf("failed to decode metric on line %d: %v", rows+1, err)
}
outputMetrics := make(map[string]OutputMetric, len(metrics))
for name, v := range metrics {
if v.Status != "success" {
continue
}
if v.Data.ResultType != "vector" {
continue
}
if len(v.Data.Result) == 0 {
continue
}
if len(v.Data.Result) == 1 && len(v.Data.Result[0].Metric) == 0 {
outputMetrics[name] = OutputMetric{
Value: v.Data.Result[0].Value.Value,
Timestamp: v.Data.Result[0].Value.Timestamp,
}
// log.Printf("%s %s @ %d", name, v.Data.Result[0].Value.Value, v.Data.Result[0].Value.Timestamp)
continue
}
var labels []string
for i, result := range v.Data.Result {
if len(labels) == 0 {
labels = make([]string, len(result.Metric))
j := 0
for k := range result.Metric {
labels[j] = k
j++
}
if len(labels) == 0 {
continue
}
}
var metricSelector string
for _, label := range labels {
value, ok := result.Metric[label]
if !ok {
log.Printf("warn: Dropped result %d from %s because no value for metric %s", i, name, label)
continue
}
if metricSelector != "" {
metricSelector += ","
}
metricSelector += fmt.Sprintf("%s=%q", label, value)
}
outputMetrics[fmt.Sprintf("%s{%s}", name, metricSelector)] = OutputMetric{
Value: result.Value.Value,
Timestamp: result.Value.Timestamp,
}
// log.Printf("%s{%s} %s @ %d", name, metricSelector, result.Value.Value, result.Value.Timestamp)
}
}
_, ok := outputMetrics["job:duration:total:seconds"]
if !ok {
return nil, fmt.Errorf("job not indexed, does not have metric %q", "job:duration:total:seconds")
}
return outputMetrics, nil
}
// PrometheusLabels avoids deserialization allocations
type PrometheusLabels map[string]string
var _ json.Marshaler = PrometheusLabels(nil)
var _ json.Unmarshaler = &PrometheusLabels{}
func (l PrometheusLabels) MarshalJSON() ([]byte, error) {
if len(l) == 0 {
return []byte(`{}`), nil
}
return json.Marshal(map[string]string(l))
}
func (l *PrometheusLabels) UnmarshalJSON(data []byte) error {
switch {
case len(data) == 4 && bytes.Equal(data, []byte("null")):
return nil
case len(data) == 2 && bytes.Equal(data, []byte("{}")):
if l == nil {
return nil
}
for k := range *l {
delete(*l, k)
}
return nil
}
if l == nil {
*l = make(map[string]string)
}
var m *map[string]string = (*map[string]string)(l)
return json.Unmarshal(data, m)
}
type parseState int
const (
startState parseState = iota
timestampState
stringNumberState
closeState
doneState
)
func (l *PrometheusValue) UnmarshalJSON(data []byte) error {
switch {
case len(data) == 4 && bytes.Equal(data, []byte("null")):
return nil
case len(data) == 2 && bytes.Equal(data, []byte("[]")):
return fmt.Errorf("unexpected value")
}
var state parseState = startState
data = bytes.TrimSpace(data)
for len(data) > 0 {
switch data[0] {
case '[':
switch state {
case startState:
if l == nil {
*l = PrometheusValue{}
}
data = bytes.TrimSpace(data[1:])
state = timestampState
default:
return fmt.Errorf("unexpected character %c in state %d", data[0], state)
}
case ']':
switch state {
case closeState:
data = bytes.TrimSpace(data[1:])
state = doneState
default:
return fmt.Errorf("unexpected character %c in state %d", data[0], state)
}
default:
switch state {
case timestampState:
pos := bytes.Index(data, []byte(","))
if pos == -1 {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], could not find comma")
}
timestampBytes := bytes.TrimSpace(data[:pos])
var err error
l.Timestamp, err = strconv.ParseInt(string(timestampBytes), 10, 64)
if err != nil {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], timestamp was not an int64: %v", err)
}
data = data[pos+1:]
state = stringNumberState
case stringNumberState:
pos := bytes.Index(data, []byte("]"))
if pos == -1 {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], could not find ending bracket in %q", string(data))
}
numberBytes := bytes.TrimSpace(data[:pos])
if len(numberBytes) < 2 || numberBytes[0] != '"' || numberBytes[len(numberBytes)-1] != '"' {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], could not find number string")
}
b := numberBytes[1 : len(numberBytes)-1]
if len(b) != len(bytes.TrimSpace(b)) {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], number was not a valid float64: whitespace in string")
}
s := string(b)
if _, err := strconv.ParseFloat(s, 64); err != nil {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"], number was not a valid float64: %v", err)
}
l.Value = s
data = data[pos:]
state = closeState
default:
return fmt.Errorf("unexpected character %c in state %d", data[0], state)
}
}
}
if state != doneState {
return fmt.Errorf("expected [<timestamp int>, \"<number string>\"]")
}
return nil
}