-
Notifications
You must be signed in to change notification settings - Fork 12
/
metrics.go
101 lines (90 loc) · 2.5 KB
/
metrics.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
package gohalt
import (
"context"
"fmt"
"sync"
"time"
client "github.com/prometheus/client_golang/api"
prometheus "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
// Metric defines single metric querier interface that returns the metric query result.
type Metric interface {
// Query returns the result of the query or internal error if any happened.
Query(context.Context) (bool, error)
}
// mtcv defines inner runnable type that returns metric query result and possible error.
type mtcv func(context.Context) (bool, error)
type mtcprometheus struct {
mtcv mtcv
value bool
}
// NewMetricPrometheus creates prometheus metric querier instance
// with cache interval defined by the provided duration
// which executes provided prometheus boolean metric query and cache it.
// Only successful metric results are cached.
func NewMetricPrometheus(url string, query string, cache time.Duration) Metric {
mtc := &mtcprometheus{}
var api prometheus.API
mempull, _ := cached(cache, func(ctx context.Context) (err error) {
if api == nil {
api, err = mtc.connect(ctx, url)
if err != nil {
return err
}
}
return mtc.pull(ctx, api, query)
})
var lock sync.Mutex
mtc.mtcv = func(ctx context.Context) (bool, error) {
lock.Lock()
defer lock.Unlock()
if err := mempull(ctx); err != nil {
return mtc.value, err
}
return mtc.value, nil
}
return mtc
}
func (mtc *mtcprometheus) Query(ctx context.Context) (bool, error) {
return mtc.mtcv(ctx)
}
func (mtc *mtcprometheus) connect(_ context.Context, url string) (prometheus.API, error) {
client, err := client.NewClient(
client.Config{
Address: url,
RoundTripper: client.DefaultRoundTripper,
},
)
if err != nil {
return nil, err
}
return prometheus.NewAPI(client), nil
}
func (mtc *mtcprometheus) pull(ctx context.Context, api prometheus.API, query string) error {
ts := time.Now().UTC()
val, warns, err := api.Query(ctx, query, ts)
if err != nil {
return err
}
for _, warn := range warns {
log("prometheus warning happened: %s", warn)
}
vec, ok := val.(model.Vector)
if !ok || vec.Len() != 1 {
return fmt.Errorf("vector metric with single sample expected instead of %v", val)
}
value := vec[0].Value
if value != 0 && value != 1 {
return fmt.Errorf("boolean metric value expected instead of %v", value)
}
mtc.value = value == 1
return nil
}
type mtcmock struct {
metric bool
err error
}
func (mtc mtcmock) Query(context.Context) (bool, error) {
return mtc.metric, mtc.err
}