-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
executable file
·51 lines (40 loc) · 882 Bytes
/
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
package main
import (
"encoding/json"
"net/http"
"net/url"
)
type promResult struct {
Metric map[string]string
Value []interface{}
}
type promResults struct {
Status string
Data struct {
ResultType string
Result []promResult
}
}
func getRunningImages(promURL *url.URL) ([]string, error) {
query := "max by (image) (container_start_time_seconds)"
req, err := http.NewRequest("GET", promURL.String(), nil)
if err != nil {
return []string{}, err
}
q := req.URL.Query()
q.Add("query", query)
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return []string{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var res promResults
decoder.Decode(&res)
var result []string
for _, res := range res.Data.Result {
result = append(result, res.Metric["image"])
}
return result, nil
}