/
message_router.go
83 lines (68 loc) · 2.04 KB
/
message_router.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
package sinks
import (
"encoding/binary"
"fmt"
"log"
"code.cloudfoundry.org/loggregator-release/src/diodes"
"github.com/cloudfoundry/sonde-go/events"
)
// MessageRouter consumes from a diode and sends envelopes to all recipients
// passed to the NewMessageRouter constructor.
type MessageRouter struct {
senders []EnvelopeSender
done chan struct{}
}
// EnvelopeSender is the interface the MessageRouter uses to send envelopes
// read from the diode.
type EnvelopeSender interface {
SendTo(string, *events.Envelope)
}
// NewMessageRouter is the preferred means of constructing a MessageRouter.
func NewMessageRouter(e ...EnvelopeSender) *MessageRouter {
return &MessageRouter{
senders: e,
done: make(chan struct{}),
}
}
// Start begins an infinite loop which reads from the diode and sends any
// received envelopes to the MessageRouter's senders.
func (r *MessageRouter) Start(incomingLog *diodes.ManyToOneEnvelope) {
log.Print("MessageRouter:Starting")
for {
envelope := incomingLog.Next()
appId := getAppId(envelope)
for _, sm := range r.senders {
sm.SendTo(appId, envelope)
}
}
}
const systemAppId = "system"
func getAppId(envelope *events.Envelope) string {
if envelope.GetEventType() == events.Envelope_LogMessage {
return envelope.GetLogMessage().GetAppId()
}
if envelope.GetEventType() == events.Envelope_ContainerMetric {
return envelope.GetContainerMetric().GetApplicationId()
}
var event hasAppId
switch envelope.GetEventType() {
case events.Envelope_HttpStartStop:
event = envelope.GetHttpStartStop()
default:
return systemAppId
}
uuid := event.GetApplicationId()
if uuid != nil {
return formatUUID(uuid)
}
return systemAppId
}
type hasAppId interface {
GetApplicationId() *events.UUID
}
func formatUUID(uuid *events.UUID) string {
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:])
}