forked from subscan-explorer/prometheus-statuspage-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
144 lines (116 loc) · 3.77 KB
/
prometheus.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
package main
import (
"context"
"encoding/json"
"fmt"
"math"
"time"
log "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/api"
prometheus "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
func queryPrometheus(backfill *time.Duration, decimal uint) statuspageMetrics {
client, err := api.NewClient(api.Config{Address: *prometheusURL})
if err != nil {
log.Fatalf("Couldn't create Prometheus client: %s", err)
}
api := prometheus.NewAPI(client)
metrics := make(statuspageMetrics)
for metricID, query := range queryConfig {
ctxlog := log.WithFields(log.Fields{
"metric_id": metricID,
"backfill": backfill,
})
var (
err error
warnings prometheus.Warnings
metricPoints []statuspageMetricPoint
)
if backfill == nil {
metricPoints, warnings, err = queryInstant(api, query, decimal, ctxlog)
} else {
metricPoints, warnings, err = queryRange(api, query, decimal, backfill, ctxlog)
}
for _, w := range warnings {
ctxlog.Warnf("Prometheus query warning: %s", w)
}
if err != nil {
ctxlog.Error(err)
continue
}
metrics[metricID] = metricPoints
}
return metrics
}
func queryInstant(api prometheus.API, query string, decimal uint, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
now := time.Now()
response, warnings, err := api.Query(context.Background(), query, now)
if err != nil {
return nil, warnings, fmt.Errorf("Couldn't query Prometheus: %w", err)
}
if response.Type() != model.ValVector {
return nil, warnings, fmt.Errorf("Expected result type %s, got %s", model.ValVector, response.Type())
}
vec := response.(model.Vector)
if l := vec.Len(); l != 1 {
return nil, warnings, fmt.Errorf("Expected single time serial, got %d", l)
}
value := vec[0].Value
logger.Infof("Query result: %s", value)
if math.IsNaN(float64(value)) {
return nil, warnings, fmt.Errorf("Invalid metric value NaN")
}
return []statuspageMetricPoint{
{
Timestamp: int64(vec[0].Timestamp / 1000),
Value: json.Number(fmt.Sprintf("%.*f", decimal, value)),
},
}, warnings, nil
}
func queryRange(api prometheus.API, query string, decimal uint, backfill *time.Duration, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
now := time.Now()
start := now.Add(-*backfill)
var (
end time.Time
promWarnings prometheus.Warnings
metricPoints []statuspageMetricPoint
)
for start.Before(now) {
end = start.Add(24 * time.Hour) // 24h as a step
if end.After(now) {
end = now
}
logger.Infof("Querying metrics from %s to %s with step %s", start.Format(time.RFC3339), end.Format(time.RFC3339), *metricInterval)
response, warnings, err := api.QueryRange(context.Background(), query, prometheus.Range{
Start: start,
End: end,
Step: *metricInterval,
})
promWarnings = append(promWarnings, warnings...)
if err != nil {
return nil, promWarnings, fmt.Errorf("Couldn't query Prometheus: %w", err)
}
if response.Type() != model.ValMatrix {
return nil, promWarnings, fmt.Errorf("Expected result type %s, got %s", model.ValMatrix, response.Type())
}
mtx := response.(model.Matrix)
if l := mtx.Len(); l != 1 {
return nil, promWarnings, fmt.Errorf("Expected single time serial, got %d", l)
}
logger.Infof("Got %d samples", len(mtx[0].Values))
logger.Debugf("Query result: %v", mtx[0].Values)
for _, v := range mtx[0].Values {
if math.IsNaN(float64(v.Value)) {
logger.Warn("Invalid metric value NaN")
continue
}
metricPoints = append(metricPoints, statuspageMetricPoint{
Timestamp: int64(v.Timestamp / 1000),
Value: json.Number(fmt.Sprintf("%.*f", decimal, v.Value)),
})
}
start = end.Add(1 * time.Millisecond)
}
return metricPoints, promWarnings, nil
}