-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-reader.go
117 lines (107 loc) · 3.78 KB
/
api-reader.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
package main
import (
"context"
"fmt"
"github.com/go-kit/kit/log/level"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"regexp"
"time"
"github.com/prometheus/client_golang/api"
)
func prepareApi(configuration *Configuration) (v1api v1.API, err error) {
client, err := api.NewClient(api.Config{
Address: configuration.serverAddress(),
})
if err != nil {
_ = level.Error(logger).Log("msg", "problem create Prometheus API client", "error", err)
return nil, err
}
v1api = v1.NewAPI(client)
return v1api, nil
}
func collectSeriesList(v1api v1.API, sources Sources, dateRange v1.Range) (labels []model.LabelSet, err error) {
_ = level.Debug(logger).Log("msg", fmt.Sprintf("entry collect series data for instance %s", sources.Instance))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
instances := fmt.Sprintf("{%s=~\"%s\"}", LabelInstance, sources.Instance)
_ = level.Debug(logger).Log("msg", fmt.Sprintf("instance filter: %s", instances))
dataSet, warnings, err := v1api.Series(ctx, []string{instances}, dateRange.Start, dateRange.End)
if err != nil {
_ = level.Error(logger).Log("msg", "problem query Prometheus API", "error", err)
return nil, err
}
if len(warnings) > 0 {
_ = level.Warn(logger).Log("msg", "Prometheus API return warning", "warn", err)
}
labels = []model.LabelSet{}
var re = regexp.MustCompile(`^go_.*`)
for _, set := range dataSet {
if !sources.IncludeGo {
if re.Match([]byte(set[LabelName])) {
continue
}
}
labels = append(labels, set)
}
_ = level.Debug(logger).Log("msg", fmt.Sprintf("collect %d from %d series for instance %s", len(labels), len(dataSet), sources.Instance))
return labels, nil
}
func collectLabelsSeriesList(v1api v1.API, lbl []Labels, dateRange v1.Range) (labels []model.LabelSet, err error) {
_ = level.Debug(logger).Log("msg", fmt.Sprintf("entry collect series data for instance labels"))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var filter []string
var re []*regexp.Regexp
msg := ""
separator := ""
for _, l := range lbl {
instances := fmt.Sprintf("{%s=~\"%s\"}", l.Label, l.Value)
filter = append(filter, instances)
_ = level.Debug(logger).Log("msg", fmt.Sprintf("instance filter: %s", instances))
msg = fmt.Sprintf("%s%s%s=~\"%s\"", msg, separator, l.Label, l.Value)
separator = ", "
if len(l.ExcludeMetrics) > 0 {
r, err := regexp.Compile(l.ExcludeMetrics)
if err == nil {
re = append(re, r)
}
}
}
dataSet, warnings, err := v1api.Series(ctx, filter, dateRange.Start, dateRange.End)
if err != nil {
_ = level.Error(logger).Log("msg", "problem query Prometheus API", "error", err)
return nil, err
}
if len(warnings) > 0 {
_ = level.Warn(logger).Log("msg", "Prometheus API return warning", "warn", err)
}
labels = []model.LabelSet{}
for _, set := range dataSet {
allowed := true
for _, r := range re {
if !r.Match([]byte(set[LabelName])) {
allowed = false
break
}
}
if allowed {
labels = append(labels, set)
}
}
_ = level.Debug(logger).Log("msg", fmt.Sprintf("collect %d from %d series for instance %s", len(labels), len(dataSet), msg))
return labels, nil
}
func readQueryRange(api v1.API, labelSet model.LabelSet, timeRange v1.Range) (data model.Value, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
data, warnings, err := api.QueryRange(ctx, labelSet.String(), timeRange)
if err != nil {
_ = level.Error(logger).Log("msg", "problem query Prometheus API", "error", err, "queryString", labelSet.String())
return nil, err
}
if warnings != nil {
_ = level.Warn(logger).Log("msg", "warning in query Prometheus API", "warn", warnings, "queryString", labelSet.String())
}
return data, nil
}