forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
112 lines (91 loc) · 2.49 KB
/
collector.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
102
103
104
105
106
107
108
109
110
111
112
package collector
import (
"bufio"
"fmt"
"net/http"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
const (
defaultScheme = "http"
defaultPath = "/metrics"
)
var (
debugf = logp.MakeDebug("prometheus-collector")
hostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
DefaultPath: defaultPath,
PathConfigKey: "metrics_path",
}.Build()
)
func init() {
if err := mb.Registry.AddMetricSet("prometheus", "collector", New, hostParser); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
client *http.Client
namespace string
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("EXPERIMENTAL: The prometheus collector metricset is experimental")
config := struct {
Namespace string `config:"namespace" validate:"required"`
}{}
err := base.Module().UnpackConfig(&config)
if err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
client: &http.Client{Timeout: base.Module().Config().Timeout},
namespace: config.Namespace,
}, nil
}
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
req, err := http.NewRequest("GET", m.HostData().SanitizedURI, nil)
if m.HostData().User != "" || m.HostData().Password != "" {
req.SetBasicAuth(m.HostData().User, m.HostData().Password)
}
resp, err := m.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error %d: %s", resp.StatusCode, resp.Status)
}
eventList := map[string]common.MapStr{}
scanner := bufio.NewScanner(resp.Body)
// Iterate through all events to gather data
for scanner.Scan() {
line := scanner.Text()
// Skip comment lines
if line[0] == '#' {
continue
}
promEvent := NewPromEvent(line)
if promEvent.value == nil {
continue
}
// If MapString for this label group does not exist yet, it is created
if _, ok := eventList[promEvent.labelHash]; !ok {
eventList[promEvent.labelHash] = common.MapStr{}
// Add labels
if len(promEvent.labels) > 0 {
eventList[promEvent.labelHash]["label"] = promEvent.labels
}
}
eventList[promEvent.labelHash][promEvent.key] = promEvent.value
}
// Converts hash list to slice
events := []common.MapStr{}
for _, e := range eventList {
e["_namespace"] = m.namespace
events = append(events, e)
}
return events, err
}