forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
68 lines (57 loc) · 1.69 KB
/
data.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
package state_pod
import (
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/kubernetes/util"
dto "github.com/prometheus/client_model/go"
)
func eventMapping(families []*dto.MetricFamily) ([]common.MapStr, error) {
eventsMap := map[string]common.MapStr{}
for _, family := range families {
for _, metric := range family.GetMetric() {
pod := util.GetLabel(metric, "pod")
if pod == "" {
continue
}
event, ok := eventsMap[pod]
if !ok {
event = common.MapStr{}
eventsMap[pod] = event
}
switch family.GetName() {
case "kube_pod_info":
event.Put(mb.ModuleDataKey+".node.name", util.GetLabel(metric, "node"))
event.Put(mb.ModuleDataKey+".namespace", util.GetLabel(metric, "namespace"))
event.Put(mb.NamespaceKey, "pod")
event.Put("name", util.GetLabel(metric, "pod"))
podIP := util.GetLabel(metric, "pod_ip")
hostIP := util.GetLabel(metric, "host_ip")
if podIP != "" {
event.Put("ip", podIP)
}
if hostIP != "" {
event.Put("host_ip", hostIP)
}
case "kube_pod_status_phase":
event.Put("status.phase", strings.ToLower(util.GetLabel(metric, "phase")))
case "kube_pod_status_ready":
if metric.GetGauge().GetValue() == 1 {
event.Put("status.ready", util.GetLabel(metric, "condition"))
}
case "kube_pod_status_scheduled":
if metric.GetGauge().GetValue() == 1 {
event.Put("status.scheduled", util.GetLabel(metric, "condition"))
}
default:
// Ignore unknown metric
continue
}
}
}
var events []common.MapStr
for _, event := range eventsMap {
events = append(events, event)
}
return events, nil
}