-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
35 lines (30 loc) · 977 Bytes
/
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
package main
// Metrics fetches data from Prometheus.
import (
"context"
"fmt"
prometheus "github.com/prometheus/client_golang/api"
prometheusApi "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"time"
)
func Metrics(server, query string, queryTime time.Time, duration, step time.Duration) (model.Matrix, error) {
client, err := prometheus.NewClient(prometheus.Config{Address: server})
if err != nil {
return nil, fmt.Errorf("failed to create Prometheus client: %v", err)
}
api := prometheusApi.NewAPI(client)
value, _, err := api.QueryRange(context.Background(), query, prometheusApi.Range{
Start: queryTime.Add(-duration),
End: queryTime,
Step: duration / step,
})
if err != nil {
return nil, fmt.Errorf("failed to query Prometheus: %v", err)
}
metrics, ok := value.(model.Matrix)
if !ok {
return nil, fmt.Errorf("unsupported result format: %s", value.Type().String())
}
return metrics, nil
}