forked from openfaas/faas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_query.go
76 lines (62 loc) · 1.64 KB
/
prometheus_query.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
package metrics
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// PrometheusQuery represents parameters for querying Prometheus
type PrometheusQuery struct {
Port int
Host string
Client *http.Client
}
type PrometheusQueryFetcher interface {
Fetch(query string) (*VectorQueryResponse, error)
}
// NewPrometheusQuery create a NewPrometheusQuery
func NewPrometheusQuery(host string, port int, client *http.Client) PrometheusQuery {
return PrometheusQuery{
Client: client,
Host: host,
Port: port,
}
}
// Fetch queries aggregated stats
func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
req, reqErr := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/api/v1/query?query=%s", q.Host, q.Port, query), nil)
if reqErr != nil {
return nil, reqErr
}
res, getErr := q.Client.Do(req)
if getErr != nil {
return nil, getErr
}
if res.Body != nil {
defer res.Body.Close()
}
bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected status code from Prometheus want: %d, got: %d, body: %s", http.StatusOK, res.StatusCode, string(bytesOut))
}
var values VectorQueryResponse
unmarshalErr := json.Unmarshal(bytesOut, &values)
if unmarshalErr != nil {
return nil, fmt.Errorf("Error unmarshaling result: %s, '%s'", unmarshalErr, string(bytesOut))
}
return &values, nil
}
type VectorQueryResponse struct {
Data struct {
Result []struct {
Metric struct {
Code string `json:"code"`
FunctionName string `json:"function_name"`
}
Value []interface{} `json:"value"`
}
}
}