/
client.go
54 lines (43 loc) · 1.48 KB
/
client.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
package prom
import (
"context"
"time"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
// Client queries the prometheus metrics
type Client struct {
api v1.API
}
// NewClient creates the prometheus client.
func NewClient(addr string) (*Client, error) {
c, err := api.NewClient(api.Config{Address: addr})
if err != nil {
return nil, err
}
api := v1.NewAPI(c)
return &Client{api: api}, nil
}
// Query queries the metric from start to end time with step.
func (c *Client) Query(ctx context.Context, query string, startTime time.Time, endTime time.Time, step time.Duration) (model.Value, error) {
return c.api.QueryRange(ctx, query, v1.Range{Start: startTime, End: endTime, Step: step})
}
// Series finds series by label matchers.
func (c *Client) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
return c.api.Series(ctx, matches, startTime, endTime)
}
// LabelValues returns all the values for the label by the matchers
func (c *Client) LabelValues(ctx context.Context, matcher string, label string, startTime time.Time, endTime time.Time) (model.LabelValues, error) {
set, err := c.Series(ctx, []string{matcher}, startTime, endTime)
if err != nil {
return nil, err
}
lvs := make(model.LabelValues, 0, len(set))
for _, s := range set {
if v, ok := s[model.LabelName(label)]; ok {
lvs = append(lvs, v)
}
}
return lvs, nil
}