forked from TencentBlueKing/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
72 lines (55 loc) · 1.38 KB
/
stats.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
package stats
import (
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
const (
defaultScheme = "http"
defaultPath = "/metrics"
)
var (
hostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
DefaultPath: defaultPath,
}.Build()
)
func init() {
if err := mb.Registry.AddMetricSet("prometheus", "stats", New, hostParser); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
http *helper.HTTP
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("BETA: The prometheus stats metricset is beta")
return &MetricSet{
BaseMetricSet: base,
http: helper.NewHTTP(base),
}, nil
}
func (m *MetricSet) Fetch() (common.MapStr, error) {
scanner, err := m.http.FetchScanner()
if err != nil {
return nil, err
}
entries := map[string]interface{}{}
// Iterate through all events to gather data
for scanner.Scan() {
line := scanner.Text()
// Skip comments and calculated lines
if line[0] == '#' || strings.Contains(line, "quantile=") {
continue
}
splitPos := strings.LastIndex(line, " ")
split := []string{line[:splitPos], line[splitPos+1:]}
entries[split[0]] = split[1]
}
data, err := eventMapping(entries)
return data, err
}