-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
processor.go
251 lines (211 loc) · 7.18 KB
/
processor.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package containerlifecycle
import (
"context"
"strings"
"time"
"github.com/DataDog/agent-payload/v5/contlcycle"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
types "github.com/DataDog/datadog-agent/pkg/containerlifecycle"
"github.com/DataDog/datadog-agent/pkg/util/log"
"google.golang.org/protobuf/proto"
)
type processor struct {
sender sender.Sender
podsQueue *queue
containersQueue *queue
tasksQueue *queue
store workloadmeta.Component
}
func newProcessor(sender sender.Sender, chunkSize int, store workloadmeta.Component) *processor {
return &processor{
sender: sender,
podsQueue: newQueue(chunkSize),
containersQueue: newQueue(chunkSize),
tasksQueue: newQueue(chunkSize),
store: store,
}
}
// start spawns a go routine to consume event queues
func (p *processor) start(ctx context.Context, pollInterval time.Duration) {
go p.processQueues(ctx, pollInterval)
}
// processEvents handles workloadmeta events, supports pods and container unset events.
func (p *processor) processEvents(evBundle workloadmeta.EventBundle) {
evBundle.Acknowledge()
log.Tracef("Processing %d events", len(evBundle.Events))
for _, event := range evBundle.Events {
entityID := event.Entity.GetID()
log.Debugf("Received deletion event for kind %q - ID %q", entityID.Kind, entityID.ID)
switch entityID.Kind {
case workloadmeta.KindContainer:
container, ok := event.Entity.(*workloadmeta.Container)
if !ok {
log.Debugf("Expected workloadmeta.Container got %T, skipping", event.Entity)
continue
}
err := p.processContainer(container, []workloadmeta.Source{workloadmeta.SourceRuntime})
if err != nil {
log.Debugf("Couldn't process container %q: %v", container.ID, err)
}
case workloadmeta.KindKubernetesPod:
pod, ok := event.Entity.(*workloadmeta.KubernetesPod)
if !ok {
log.Debugf("Expected workloadmeta.KubernetesPod got %T, skipping", event.Entity)
continue
}
err := p.processPod(pod)
if err != nil {
log.Debugf("Couldn't process pod %q: %v", event.Entity.GetID().ID, err)
}
case workloadmeta.KindECSTask:
task, ok := event.Entity.(*workloadmeta.ECSTask)
if !ok {
log.Debugf("Expected workloadmeta.ECSTask got %T, skipping", event.Entity)
continue
}
err := p.processTask(task)
if err != nil {
log.Debugf("Couldn't process task %q: %v", event.Entity.GetID().ID, err)
}
default:
log.Tracef("Cannot handle event for entity %q with kind %q", entityID.ID, entityID.Kind)
}
}
}
// processContainer enqueue container events
func (p *processor) processContainer(container *workloadmeta.Container, sources []workloadmeta.Source) error {
event := newEvent()
event.withObjectKind(types.ObjectKindContainer)
event.withEventType(types.EventNameDelete)
event.withObjectID(container.ID)
if len(sources) > 0 {
event.withSource(string(sources[0]))
}
if !container.State.FinishedAt.IsZero() {
ts := container.State.FinishedAt.Unix()
event.withContainerExitTimestamp(&ts)
}
if container.State.ExitCode != nil {
code := int32(*container.State.ExitCode)
event.withContainerExitCode(&code)
}
// Because the container processor is triggered off of runtime events, and the
// container runtime would have no knowledge surrounding what owns the container,
// we need to query the workloadmeta store to get this information.
if c, err := p.store.GetContainer(container.ID); err == nil {
if c.Owner != nil {
event.withOwnerID(c.Owner.ID)
switch c.Owner.Kind {
case workloadmeta.KindKubernetesPod:
event.withOwnerType(types.ObjectKindPod)
case workloadmeta.KindECSTask:
event.withOwnerType(types.ObjectKindTask)
default:
log.Tracef("Cannot handle owner for container %q with type %q", container.ID, c.Owner.Kind)
}
}
}
return p.containersQueue.add(event)
}
// processPod enqueue pod events
func (p *processor) processPod(pod *workloadmeta.KubernetesPod) error {
event := newEvent()
event.withObjectKind(types.ObjectKindPod)
event.withEventType(types.EventNameDelete)
event.withObjectID(pod.GetID().ID)
event.withSource(string(workloadmeta.SourceNodeOrchestrator))
if !pod.FinishedAt.IsZero() {
ts := pod.FinishedAt.Unix()
event.withPodExitTimestamp(&ts)
}
return p.podsQueue.add(event)
}
func (p *processor) processTask(task *workloadmeta.ECSTask) error {
event := newEvent()
event.withObjectKind(types.ObjectKindTask)
event.withEventType(types.EventNameDelete)
event.withObjectID(task.GetID().ID)
event.withSource(string(workloadmeta.SourceNodeOrchestrator))
if task.LaunchType == workloadmeta.ECSLaunchTypeFargate {
event.withSource(string(workloadmeta.SourceRuntime))
}
// we don't have exit timestamp for tasks in the response of metadata v1 api, so we use the current timestamp
ts := time.Now().Unix()
event.withTaskExitTimestamp(&ts)
return p.tasksQueue.add(event)
}
// processQueues consumes the data available in the queues
func (p *processor) processQueues(ctx context.Context, pollInterval time.Duration) {
ticker := time.NewTicker(pollInterval)
for {
select {
case <-ticker.C:
p.flush()
case <-ctx.Done():
p.flush()
return
}
}
}
// flush forwards all queued events to the aggregator
func (p *processor) flush() {
p.flushContainers()
p.flushPods()
p.flushTasks()
}
// flushContainers forwards queued container events to the aggregator
func (p *processor) flushContainers() {
msgs := p.containersQueue.flush()
if len(msgs) > 0 {
p.containerLifecycleEvent(msgs)
for eventType, eventCount := range eventCountByType(msgs) {
emittedEvents.Add(float64(eventCount), eventType, types.ObjectKindContainer)
}
}
}
// flushPods forwards queued pod events to the aggregator
func (p *processor) flushPods() {
msgs := p.podsQueue.flush()
if len(msgs) > 0 {
p.containerLifecycleEvent(msgs)
for eventType, eventCount := range eventCountByType(msgs) {
emittedEvents.Add(float64(eventCount), eventType, types.ObjectKindPod)
}
}
}
// flushTasks forwards queued task events to the aggregator
func (p *processor) flushTasks() {
msgs := p.tasksQueue.flush()
if len(msgs) > 0 {
p.containerLifecycleEvent(msgs)
for eventType, eventCount := range eventCountByType(msgs) {
emittedEvents.Add(float64(eventCount), eventType, types.ObjectKindTask)
}
}
}
func (p *processor) containerLifecycleEvent(msgs []*contlcycle.EventsPayload) {
for _, msg := range msgs {
encoded, err := proto.Marshal(msg)
if err != nil {
log.Errorf("Unable to encode message: %+v", err)
continue
}
p.sender.EventPlatformEvent(encoded, eventplatform.EventTypeContainerLifecycle)
}
}
func eventCountByType(eventPayloads []*contlcycle.EventsPayload) map[string]int {
res := make(map[string]int)
for _, payload := range eventPayloads {
for _, ev := range payload.Events {
eventType := strings.ToLower(ev.GetEventType().String())
res[eventType]++
}
}
return res
}