forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
51 lines (41 loc) · 1.12 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
package helper
import (
"fmt"
"github.com/elastic/beats/metricbeat/mb"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
// Prometheus helper retrieves prometheus formatted metrics
type Prometheus struct {
HTTP
}
// NewPrometheusClient creates new prometheus helper
func NewPrometheusClient(base mb.BaseMetricSet) *Prometheus {
http := NewHTTP(base)
return &Prometheus{*http}
}
// GetFamilies requests metric families from prometheus endpoint and returns them
func (p *Prometheus) GetFamilies() ([]*dto.MetricFamily, error) {
resp, err := p.FetchResponse()
if err != nil {
return nil, err
}
defer resp.Body.Close()
format := expfmt.ResponseFormat(resp.Header)
if format == "" {
return nil, fmt.Errorf("Invalid format for response of response")
}
decoder := expfmt.NewDecoder(resp.Body, format)
if decoder == nil {
return nil, fmt.Errorf("Unable to create decoder to decode response")
}
families := []*dto.MetricFamily{}
for err == nil {
mf := &dto.MetricFamily{}
err = decoder.Decode(mf)
if err == nil {
families = append(families, mf)
}
}
return families, nil
}