forked from draganm/missing-container-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_handler.go
146 lines (124 loc) · 3.11 KB
/
event_handler.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
// "fmt"
"strconv"
"sync"
"time"
"strings"
"github.com/docker/docker/api/types/events"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
type container struct {
// id string
name string
// imageID string
pod string
namespace string
}
func (c *container) labels() prometheus.Labels {
return prometheus.Labels{
// "docker_container_id": c.id,
// "container_short_id": c.id[:12],
// "container_id": fmt.Sprintf("docker://%s", c.id),
"name": strings.Split(c.name,"_")[1],
// "image_id": fmt.Sprintf("docker-pullable://%s", c.imageID),
"pod": c.pod,
"namespace": c.namespace,
}
}
func (c *container) create() {
containerRestarts.GetMetricWith(c.labels())
containerOOMs.GetMetricWith(c.labels())
containerLastExitCode.GetMetricWith(c.labels())
}
func (c *container) die(exitCode int) {
containerLastExitCode.With(c.labels()).Set(float64(exitCode))
}
func (c *container) start() {
containerRestarts.With(c.labels()).Inc()
}
func (c *container) oom() {
containerOOMs.With(c.labels()).Inc()
}
func (c *container) destroy() {
containerRestarts.Delete(c.labels())
containerOOMs.Delete(c.labels())
containerLastExitCode.Delete(c.labels())
}
type eventHandler struct {
containers map[string]*container
mu *sync.Mutex
getContainerPodAndNamespace func(string) (string, string)
}
func newEventHandler(getContainerPodAndNamespace func(string) (string, string)) *eventHandler {
return &eventHandler{
containers: map[string]*container{},
mu: &sync.Mutex{},
getContainerPodAndNamespace: getContainerPodAndNamespace,
}
}
func (eh *eventHandler) hasContainer(id string) (*container, bool) {
c, ex := eh.containers[id]
return c, ex
}
func (eh *eventHandler) addContainer(id, name, imageID string) *container {
cnt, ex := eh.hasContainer(id)
if ex {
return cnt
}
pod, namespace := eh.getContainerPodAndNamespace(id)
c := &container{
// id: id,
name: name,
// imageID: imageID,
pod: pod,
namespace: namespace,
}
c.create()
eh.containers[id] = c
return c
}
func (eh *eventHandler) handle(e events.Message) error {
eh.mu.Lock()
defer eh.mu.Unlock()
if e.Type != "container" {
return nil
}
c := eh.addContainer(e.Actor.ID, e.Actor.Attributes["name"], e.Actor.Attributes["image"])
switch e.Action {
case "create":
// just ignore
case "destroy":
if c != nil {
go func() {
// wait 5 minutes to receive pending
// events and for scraping by Prometheus
time.Sleep(5 * time.Minute)
eh.mu.Lock()
defer eh.mu.Unlock()
c.destroy()
delete(eh.containers, e.Actor.ID)
}()
}
case "die":
if c != nil {
exitCodeString := e.Actor.Attributes["exitCode"]
ec, err := strconv.Atoi(exitCodeString)
if err != nil {
return errors.Wrapf(err, "while parsing exit code %q", exitCodeString)
}
c.die(ec)
}
case "start":
if c != nil {
c.start()
}
// case "exec_create":
case "oom":
if c != nil {
c.oom()
}
}
return nil
}