forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
546 lines (497 loc) · 16.7 KB
/
events.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cloudfoundry
import (
"encoding/binary"
"fmt"
"net/url"
"strings"
"time"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/cloudfoundry/sonde-go/events"
)
// EventType defines the different event types that can be raised from RPLClient.
type EventType uint
// EventTypes from loggregator documented here: https://github.com/cloudfoundry/loggregator-api
const (
// EventTypeHttpAccess is a http access event.
EventTypeHttpAccess EventType = iota
// EventTypeLog is a log event.
EventTypeLog
// EventTypeCounter is a counter event.
EventTypeCounter
// EventTypeValueMetric is a value metric event.
EventTypeValueMetric
// EventTypeContainerMetric is a container metric event.
EventTypeContainerMetric
// EventTypeError is an error event.
EventTypeError
)
// String returns string representation of the event type.
func (t EventType) String() string {
switch t {
case EventTypeHttpAccess:
return "access"
case EventTypeLog:
return "log"
case EventTypeCounter:
return "counter"
case EventTypeValueMetric:
return "value"
case EventTypeContainerMetric:
return "container"
case EventTypeError:
return "error"
default:
return "unknown"
}
}
// EventMessageType defines the different log message types.
type EventLogMessageType uint
const (
// EventLogMessageTypeStdout is a message that was received from stdout.
EventLogMessageTypeStdout EventLogMessageType = iota + 1
// EventLogMessageTypeStderr is a message that was received from stderr.
EventLogMessageTypeStderr
)
// String returns string representation of the event log message type.
func (t EventLogMessageType) String() string {
switch t {
case EventLogMessageTypeStdout:
return "stdout"
case EventLogMessageTypeStderr:
return "stderr"
default:
return "unknown"
}
}
// Event is the interface all events implements.
type Event interface {
fmt.Stringer
Origin() string
EventType() EventType
Timestamp() time.Time
Deployment() string
Job() string
Index() string
IP() string
Tags() map[string]string
ToFields() common.MapStr
}
// EventWithAppID is the interface all events implement that provide an application ID for the event.
type EventWithAppID interface {
Event
AppGuid() string
}
type eventBase struct {
origin string
timestamp time.Time
deployment string
job string
index string
ip string
tags map[string]string
}
type eventAppBase struct {
eventBase
appGuid string
}
// EventHttpAccess represents a http access event.
type EventHttpAccess struct {
eventAppBase
startTimestamp time.Time
stopTimestamp time.Time
requestID string
peerType string
method string
uri string
remoteAddress string
userAgent string
statusCode int32
contentLength int64
instanceIndex int32
forwarded []string
}
func (*EventHttpAccess) EventType() EventType { return EventTypeHttpAccess }
func (e *EventHttpAccess) String() string { return e.EventType().String() }
func (e *EventHttpAccess) Origin() string { return e.origin }
func (e *EventHttpAccess) Timestamp() time.Time { return e.timestamp }
func (e *EventHttpAccess) Deployment() string { return e.deployment }
func (e *EventHttpAccess) Job() string { return e.job }
func (e *EventHttpAccess) Index() string { return e.index }
func (e *EventHttpAccess) IP() string { return e.ip }
func (e *EventHttpAccess) Tags() map[string]string { return e.tags }
func (e *EventHttpAccess) AppGuid() string { return e.appGuid }
func (e *EventHttpAccess) StartTimestamp() time.Time { return e.startTimestamp }
func (e *EventHttpAccess) StopTimestamp() time.Time { return e.stopTimestamp }
func (e *EventHttpAccess) RequestID() string { return e.requestID }
func (e *EventHttpAccess) PeerType() string { return e.peerType }
func (e *EventHttpAccess) Method() string { return e.method }
func (e *EventHttpAccess) URI() string { return e.uri }
func (e *EventHttpAccess) RemoteAddress() string { return e.remoteAddress }
func (e *EventHttpAccess) UserAgent() string { return e.userAgent }
func (e *EventHttpAccess) StatusCode() int32 { return e.statusCode }
func (e *EventHttpAccess) ContentLength() int64 { return e.contentLength }
func (e *EventHttpAccess) InstanceIndex() int32 { return e.instanceIndex }
func (e *EventHttpAccess) Forwarded() []string { return e.forwarded }
func (e *EventHttpAccess) ToFields() common.MapStr {
fields := baseMapWithApp(e)
fields.DeepUpdate(common.MapStr{
"http": common.MapStr{
"response": common.MapStr{
"status_code": e.StatusCode(),
"method": e.Method(),
"bytes": e.ContentLength(),
},
},
"user_agent": common.MapStr{
"original": e.UserAgent(),
},
"url": urlMap(e.URI()),
})
return fields
}
// EventLog represents a log message event.
type EventLog struct {
eventAppBase
message string
messageType EventLogMessageType
sourceType string
sourceID string
}
func (*EventLog) EventType() EventType { return EventTypeLog }
func (e *EventLog) String() string { return e.EventType().String() }
func (e *EventLog) Origin() string { return e.origin }
func (e *EventLog) Timestamp() time.Time { return e.timestamp }
func (e *EventLog) Deployment() string { return e.deployment }
func (e *EventLog) Job() string { return e.job }
func (e *EventLog) Index() string { return e.index }
func (e *EventLog) IP() string { return e.ip }
func (e *EventLog) Tags() map[string]string { return e.tags }
func (e *EventLog) AppGuid() string { return e.appGuid }
func (e *EventLog) Message() string { return e.message }
func (e *EventLog) MessageType() EventLogMessageType { return e.messageType }
func (e *EventLog) SourceType() string { return e.sourceType }
func (e *EventLog) SourceID() string { return e.sourceID }
func (e *EventLog) ToFields() common.MapStr {
fields := baseMapWithApp(e)
fields.DeepUpdate(common.MapStr{
"cloudfoundry": common.MapStr{
e.String(): common.MapStr{
"source": common.MapStr{
"instance": e.SourceID(),
"type": e.SourceType(),
},
},
},
"message": e.Message(),
"stream": e.MessageType().String(),
})
return fields
}
// EventCounter represents a counter event.
type EventCounter struct {
eventBase
name string
delta uint64
total uint64
}
func (*EventCounter) EventType() EventType { return EventTypeCounter }
func (e *EventCounter) String() string { return e.EventType().String() }
func (e *EventCounter) Origin() string { return e.origin }
func (e *EventCounter) Timestamp() time.Time { return e.timestamp }
func (e *EventCounter) Deployment() string { return e.deployment }
func (e *EventCounter) Job() string { return e.job }
func (e *EventCounter) Index() string { return e.index }
func (e *EventCounter) IP() string { return e.ip }
func (e *EventCounter) Tags() map[string]string { return e.tags }
func (e *EventCounter) Name() string { return e.name }
func (e *EventCounter) Delta() uint64 { return e.delta }
func (e *EventCounter) Total() uint64 { return e.total }
func (e *EventCounter) ToFields() common.MapStr {
fields := baseMap(e)
fields.DeepUpdate(common.MapStr{
"cloudfoundry": common.MapStr{
e.String(): common.MapStr{
"name": e.Name(),
"delta": e.Delta(),
"total": e.Total(),
},
},
})
return fields
}
// EventValueMetric represents a value metric event.
type EventValueMetric struct {
eventBase
name string
value float64
unit string
}
func (*EventValueMetric) EventType() EventType { return EventTypeValueMetric }
func (e *EventValueMetric) String() string { return e.EventType().String() }
func (e *EventValueMetric) Origin() string { return e.origin }
func (e *EventValueMetric) Timestamp() time.Time { return e.timestamp }
func (e *EventValueMetric) Deployment() string { return e.deployment }
func (e *EventValueMetric) Job() string { return e.job }
func (e *EventValueMetric) Index() string { return e.index }
func (e *EventValueMetric) IP() string { return e.ip }
func (e *EventValueMetric) Tags() map[string]string { return e.tags }
func (e *EventValueMetric) Name() string { return e.name }
func (e *EventValueMetric) Value() float64 { return e.value }
func (e *EventValueMetric) Unit() string { return e.unit }
func (e *EventValueMetric) ToFields() common.MapStr {
fields := baseMap(e)
fields.DeepUpdate(common.MapStr{
"cloudfoundry": common.MapStr{
e.String(): common.MapStr{
"name": e.Name(),
"unit": e.Unit(),
"value": e.Value(),
},
},
})
return fields
}
// EventContainerMetric represents a container metric event.
type EventContainerMetric struct {
eventAppBase
instanceIndex int32
cpuPercentage float64
memoryBytes uint64
diskBytes uint64
memoryBytesQuota uint64
diskBytesQuota uint64
}
func (*EventContainerMetric) EventType() EventType { return EventTypeContainerMetric }
func (e *EventContainerMetric) String() string { return e.EventType().String() }
func (e *EventContainerMetric) Origin() string { return e.origin }
func (e *EventContainerMetric) Timestamp() time.Time { return e.timestamp }
func (e *EventContainerMetric) Deployment() string { return e.deployment }
func (e *EventContainerMetric) Job() string { return e.job }
func (e *EventContainerMetric) Index() string { return e.index }
func (e *EventContainerMetric) IP() string { return e.ip }
func (e *EventContainerMetric) Tags() map[string]string { return e.tags }
func (e *EventContainerMetric) AppGuid() string { return e.appGuid }
func (e *EventContainerMetric) InstanceIndex() int32 { return e.instanceIndex }
func (e *EventContainerMetric) CPUPercentage() float64 { return e.cpuPercentage }
func (e *EventContainerMetric) MemoryBytes() uint64 { return e.memoryBytes }
func (e *EventContainerMetric) DiskBytes() uint64 { return e.diskBytes }
func (e *EventContainerMetric) MemoryBytesQuota() uint64 { return e.memoryBytesQuota }
func (e *EventContainerMetric) DiskBytesQuota() uint64 { return e.diskBytesQuota }
func (e *EventContainerMetric) ToFields() common.MapStr {
fields := baseMapWithApp(e)
fields.DeepUpdate(common.MapStr{
"cloudfoundry": common.MapStr{
e.String(): common.MapStr{
"instance_index": e.InstanceIndex(),
"cpu.pct": e.CPUPercentage(),
"memory.bytes": e.MemoryBytes(),
"memory.quota.bytes": e.MemoryBytesQuota(),
"disk.bytes": e.DiskBytes(),
"disk.quota.bytes": e.DiskBytesQuota(),
},
},
})
return fields
}
// EventError represents an error event.
type EventError struct {
eventBase
message string
code int32
source string
}
func (*EventError) EventType() EventType { return EventTypeError }
func (e *EventError) String() string { return e.EventType().String() }
func (e *EventError) Origin() string { return e.origin }
func (e *EventError) Timestamp() time.Time { return e.timestamp }
func (e *EventError) Deployment() string { return e.deployment }
func (e *EventError) Job() string { return e.job }
func (e *EventError) Index() string { return e.index }
func (e *EventError) IP() string { return e.ip }
func (e *EventError) Tags() map[string]string { return e.tags }
func (e *EventError) Message() string { return e.message }
func (e *EventError) Code() int32 { return e.code }
func (e *EventError) Source() string { return e.source }
func (e *EventError) ToFields() common.MapStr {
fields := baseMap(e)
fields.DeepUpdate(common.MapStr{
"cloudfoundry": common.MapStr{
e.String(): common.MapStr{
"source": e.Source(),
},
},
"message": e.Message(),
"code": e.Code(),
})
return fields
}
func newEventBase(env *events.Envelope) eventBase {
return eventBase{
origin: *env.Origin,
timestamp: time.Unix(0, *env.Timestamp),
deployment: *env.Deployment,
job: *env.Job,
index: *env.Index,
ip: *env.Ip,
tags: env.Tags,
}
}
func newEventHttpAccess(env *events.Envelope) *EventHttpAccess {
msg := env.GetHttpStartStop()
e := EventHttpAccess{
eventAppBase: eventAppBase{
eventBase: newEventBase(env),
appGuid: formatUUID(msg.ApplicationId),
},
startTimestamp: time.Unix(0, *msg.StartTimestamp),
stopTimestamp: time.Unix(0, *msg.StopTimestamp),
requestID: formatUUID(msg.RequestId),
peerType: strings.ToLower(msg.PeerType.String()),
method: msg.Method.String(),
uri: *msg.Uri,
remoteAddress: *msg.RemoteAddress,
userAgent: *msg.UserAgent,
statusCode: *msg.StatusCode,
contentLength: *msg.ContentLength,
forwarded: msg.Forwarded,
}
if msg.InstanceIndex != nil {
e.instanceIndex = *msg.InstanceIndex
}
return &e
}
func newEventLog(env *events.Envelope) *EventLog {
msg := env.GetLogMessage()
return &EventLog{
eventAppBase: eventAppBase{
eventBase: newEventBase(env),
appGuid: *msg.AppId,
},
message: string(msg.Message),
messageType: EventLogMessageType(*msg.MessageType),
sourceType: *msg.SourceType,
sourceID: *msg.SourceInstance,
}
}
func newEventCounter(env *events.Envelope) *EventCounter {
msg := env.GetCounterEvent()
return &EventCounter{
eventBase: newEventBase(env),
name: *msg.Name,
delta: *msg.Delta,
total: *msg.Total,
}
}
func newEventValueMetric(env *events.Envelope) *EventValueMetric {
msg := env.GetValueMetric()
return &EventValueMetric{
eventBase: newEventBase(env),
name: *msg.Name,
value: *msg.Value,
unit: *msg.Unit,
}
}
func newEventContainerMetric(env *events.Envelope) *EventContainerMetric {
msg := env.GetContainerMetric()
return &EventContainerMetric{
eventAppBase: eventAppBase{
eventBase: newEventBase(env),
appGuid: *msg.ApplicationId,
},
instanceIndex: *msg.InstanceIndex,
cpuPercentage: *msg.CpuPercentage,
memoryBytes: *msg.MemoryBytes,
diskBytes: *msg.DiskBytes,
memoryBytesQuota: *msg.MemoryBytesQuota,
diskBytesQuota: *msg.DiskBytesQuota,
}
}
func newEventError(env *events.Envelope) *EventError {
msg := env.GetError()
return &EventError{
eventBase: newEventBase(env),
message: *msg.Message,
code: *msg.Code,
source: *msg.Source,
}
}
func envelopeToEvent(env *events.Envelope) Event {
switch *env.EventType {
case events.Envelope_HttpStartStop:
return newEventHttpAccess(env)
case events.Envelope_LogMessage:
return newEventLog(env)
case events.Envelope_CounterEvent:
return newEventCounter(env)
case events.Envelope_ValueMetric:
return newEventValueMetric(env)
case events.Envelope_ContainerMetric:
return newEventContainerMetric(env)
case events.Envelope_Error:
return newEventError(env)
}
return nil
}
func envelopMap(evt Event) common.MapStr {
return common.MapStr{
"origin": evt.Origin(),
"deployment": evt.Deployment(),
"ip": evt.IP(),
"job": evt.Job(),
"index": evt.Index(),
}
}
func baseMap(evt Event) common.MapStr {
return common.MapStr{
"cloudfoundry": common.MapStr{
"type": evt.String(),
evt.String(): common.MapStr{
"timestamp": evt.Timestamp(),
},
"envelope": envelopMap(evt),
"tags": dedotedTags(evt.Tags()),
},
}
}
func dedotedTags(tags map[string]string) common.MapStr {
result := common.MapStr{}
for name, value := range tags {
result[common.DeDot(name)] = value
}
return result
}
func baseMapWithApp(evt EventWithAppID) common.MapStr {
base := baseMap(evt)
appID := evt.AppGuid()
if appID != "" {
base.Put("cloudfoundry.app.id", appID)
}
return base
}
func urlMap(uri string) common.MapStr {
u, err := url.Parse(uri)
if err != nil {
return common.MapStr{
"original": uri,
}
}
return common.MapStr{
"original": uri,
"scheme": u.Scheme,
"port": u.Port(),
"path": u.Path,
"domain": u.Hostname(),
}
}
func formatUUID(uuid *events.UUID) string {
if uuid == nil {
return ""
}
var uuidBytes [16]byte
binary.LittleEndian.PutUint64(uuidBytes[:8], uuid.GetLow())
binary.LittleEndian.PutUint64(uuidBytes[8:], uuid.GetHigh())
return fmt.Sprintf("%x-%x-%x-%x-%x", uuidBytes[0:4], uuidBytes[4:6], uuidBytes[6:8], uuidBytes[8:10], uuidBytes[10:])
}