-
Notifications
You must be signed in to change notification settings - Fork 9
/
prometheus.go
93 lines (83 loc) · 2.64 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
// Copyright 2023 The go-commons Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"
api "github.com/prometheus/client_golang/api"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
// Used to intercept and passed custom auth headers to prometheus client request
func (bat authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if bat.username != "" {
req.SetBasicAuth(bat.username, bat.password)
}
if bat.token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bat.token))
}
return bat.Transport.RoundTrip(req)
}
// NewClient creates a prometheus struct instance with the given parameters
func NewClient(url, token, username, password string, tlsSkipVerify bool) (*Prometheus, error) {
prometheus := Prometheus{
Endpoint: url,
}
cfg := api.Config{
Address: url,
RoundTripper: authTransport{
Transport: &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: tlsSkipVerify}},
token: token,
username: username,
password: password,
},
}
c, err := api.NewClient(cfg)
if err != nil {
return &prometheus, err
}
prometheus.api = apiv1.NewAPI(c)
// Verify Prometheus connection prior returning
if err := prometheus.verifyConnection(); err != nil {
return &prometheus, err
}
return &prometheus, nil
}
// Query prometheus query wrapper
func (p *Prometheus) Query(query string, time time.Time) (model.Value, error) {
var v model.Value
v, _, err := p.api.Query(context.TODO(), query, time)
if err != nil {
return v, err
}
return v, nil
}
// QueryRange prometheus queryRange wrapper
func (p *Prometheus) QueryRange(query string, start, end time.Time, step time.Duration) (model.Value, error) {
var v model.Value
r := apiv1.Range{Start: start, End: end, Step: step}
v, _, err := p.api.QueryRange(context.TODO(), query, r)
if err != nil {
return v, err
}
return v, nil
}
// Verifies prometheus connection
func (p *Prometheus) verifyConnection() error {
_, err := p.Query("up{}", time.Now().UTC().UTC())
return err
}