This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
88 lines (72 loc) · 2.73 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package state_container
import (
"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"
)
const (
// Nanocores conversion 10^9
nanocores = 1000000000
)
func eventMapping(families []*dto.MetricFamily) ([]common.MapStr, error) {
eventsMap := map[string]common.MapStr{}
for _, family := range families {
for _, metric := range family.GetMetric() {
container := util.GetLabel(metric, "container")
if container == "" {
continue
}
event, ok := eventsMap[container]
if !ok {
event = common.MapStr{}
eventsMap[container] = event
}
switch family.GetName() {
case "kube_pod_container_info":
event.Put(mb.ModuleDataKey+".pod.name", util.GetLabel(metric, "pod"))
event.Put(mb.ModuleDataKey+".namespace", util.GetLabel(metric, "namespace"))
event.Put(mb.NamespaceKey, "container")
event.Put("name", util.GetLabel(metric, "container"))
event.Put("id", util.GetLabel(metric, "container_id"))
event.Put("image", util.GetLabel(metric, "image"))
case "kube_pod_container_resource_limits_cpu_cores":
event.Put(mb.ModuleDataKey+".node.name", util.GetLabel(metric, "node"))
event.Put("cpu.limit.nanocores", metric.GetGauge().GetValue()*nanocores)
case "kube_pod_container_resource_requests_cpu_cores":
event.Put(mb.ModuleDataKey+".node.name", util.GetLabel(metric, "node"))
event.Put("cpu.request.nanocores", metric.GetGauge().GetValue()*nanocores)
case "kube_pod_container_resource_limits_memory_bytes":
event.Put(mb.ModuleDataKey+".node.name", util.GetLabel(metric, "node"))
event.Put("memory.limit.bytes", metric.GetGauge().GetValue())
case "kube_pod_container_resource_requests_memory_bytes":
event.Put(mb.ModuleDataKey+".node.name", util.GetLabel(metric, "node"))
event.Put("memory.request.bytes", metric.GetGauge().GetValue())
case "kube_pod_container_status_ready":
event.Put("status.ready", metric.GetGauge().GetValue() == 1)
case "kube_pod_container_status_restarts":
event.Put("status.restarts", metric.GetCounter().GetValue())
case "kube_pod_container_status_running":
if metric.GetGauge().GetValue() == 1 {
event.Put("status.phase", "running")
}
case "kube_pod_container_status_terminated":
if metric.GetGauge().GetValue() == 1 {
event.Put("status.phase", "terminate")
}
case "kube_pod_container_status_waiting":
if metric.GetGauge().GetValue() == 1 {
event.Put("status.phase", "waiting")
}
default:
// Ignore unknown metric
continue
}
}
}
var events []common.MapStr
for _, event := range eventsMap {
events = append(events, event)
}
return events, nil
}