-
Notifications
You must be signed in to change notification settings - Fork 3
/
oms_nozzle.go
334 lines (303 loc) · 10.2 KB
/
oms_nozzle.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
package omsnozzle
import (
"encoding/json"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"code.cloudfoundry.org/lager"
"github.com/Azure/oms-log-analytics-firehose-nozzle/caching"
"github.com/Azure/oms-log-analytics-firehose-nozzle/client"
"github.com/Azure/oms-log-analytics-firehose-nozzle/firehose"
"github.com/Azure/oms-log-analytics-firehose-nozzle/messages"
"github.com/cloudfoundry/sonde-go/events"
)
type OmsNozzle struct {
logger lager.Logger
errChan <-chan error
msgChan <-chan *events.Envelope
signalChan chan os.Signal
omsClient client.Client
firehoseClient firehose.Client
nozzleConfig *NozzleConfig
goroutineSem chan int // to control the number of active post goroutines
cachingClient caching.CachingClient
totalEventsReceived uint64
totalEventsSent uint64
totalEventsLost uint64
totalDataSent uint64
mutex *sync.Mutex
}
type NozzleConfig struct {
OmsTypePrefix string
OmsBatchTime time.Duration
OmsMaxMsgNumPerBatch int
ExcludeMetricEvents bool
ExcludeLogEvents bool
ExcludeHttpEvents bool
LogEventCount bool
LogEventCountInterval time.Duration
}
func NewOmsNozzle(logger lager.Logger, firehoseClient firehose.Client, omsClient client.Client, nozzleConfig *NozzleConfig, caching caching.CachingClient) *OmsNozzle {
maxPostGoroutines := int(100000 / nozzleConfig.OmsMaxMsgNumPerBatch)
return &OmsNozzle{
logger: logger,
errChan: make(<-chan error),
msgChan: make(<-chan *events.Envelope),
signalChan: make(chan os.Signal, 2),
omsClient: omsClient,
firehoseClient: firehoseClient,
nozzleConfig: nozzleConfig,
goroutineSem: make(chan int, maxPostGoroutines),
cachingClient: caching,
totalEventsReceived: uint64(0),
totalEventsSent: uint64(0),
totalEventsLost: uint64(0),
totalDataSent: uint64(0),
mutex: &sync.Mutex{},
}
}
func (o *OmsNozzle) Start() error {
o.cachingClient.Initialize(true)
// setup for termination signal from CF
signal.Notify(o.signalChan, syscall.SIGTERM, syscall.SIGINT)
o.msgChan, o.errChan = o.firehoseClient.Connect()
if o.nozzleConfig.LogEventCount {
o.logTotalEvents(o.nozzleConfig.LogEventCountInterval)
}
err := o.routeEvents()
return err
}
func (o *OmsNozzle) logTotalEvents(interval time.Duration) {
logEventCountTicker := time.NewTicker(interval)
lastReceivedCount := uint64(0)
lastSentCount := uint64(0)
lastLostCount := uint64(0)
go func() {
for range logEventCountTicker.C {
timeStamp := time.Now().UnixNano()
totalReceivedCount := o.totalEventsReceived
totalSentCount := o.totalEventsSent
totalLostCount := o.totalEventsLost
currentEvents := make(map[string][]interface{})
// Generate CounterEvent
o.addEventCountEvent("eventsReceived", totalReceivedCount-lastReceivedCount, totalReceivedCount, &timeStamp, ¤tEvents)
o.addEventCountEvent("eventsSent", totalSentCount-lastSentCount, totalSentCount, &timeStamp, ¤tEvents)
o.addEventCountEvent("eventsLost", totalLostCount-lastLostCount, totalLostCount, &timeStamp, ¤tEvents)
o.goroutineSem <- 1
o.postData(¤tEvents, false)
lastReceivedCount = totalReceivedCount
lastSentCount = totalSentCount
lastLostCount = totalLostCount
}
}()
}
func (o *OmsNozzle) addEventCountEvent(name string, deltaCount uint64, count uint64, timeStamp *int64, currentEvents *map[string][]interface{}) {
counterEvent := &events.CounterEvent{
Name: &name,
Delta: &deltaCount,
Total: &count,
}
eventType := events.Envelope_CounterEvent
job := "nozzle"
origin := "stats"
envelope := &events.Envelope{
EventType: &eventType,
Timestamp: timeStamp,
Job: &job,
Origin: &origin,
CounterEvent: counterEvent,
}
var omsMsg OMSMessage
eventTypeString := eventType.String()
omsMsg = messages.NewCounterEvent(envelope, o.cachingClient)
(*currentEvents)[eventTypeString] = append((*currentEvents)[eventTypeString], omsMsg)
}
func (o *OmsNozzle) postData(events *map[string][]interface{}, addCount bool) {
for k, v := range *events {
if len(v) > 0 {
if msgAsJson, err := json.Marshal(&v); err != nil {
o.logger.Error("error marshalling message to JSON", err,
lager.Data{"event type": k},
lager.Data{"event count": len(v)})
} else {
o.logger.Debug("Posting to OMS",
lager.Data{"event type": k},
lager.Data{"event count": len(v)},
lager.Data{"total size": len(msgAsJson)})
if len(o.nozzleConfig.OmsTypePrefix) > 0 {
k = o.nozzleConfig.OmsTypePrefix + k
}
nRetries := 4
for nRetries > 0 {
requestStartTime := time.Now()
if err = o.omsClient.PostData(&msgAsJson, k); err != nil {
nRetries--
elapsedTime := time.Since(requestStartTime)
o.logger.Error("error posting message to OMS", err,
lager.Data{"event type": k},
lager.Data{"elapse time": elapsedTime.String()},
lager.Data{"event count": len(v)},
lager.Data{"total size": len(msgAsJson)},
lager.Data{"remaining attempts": nRetries})
time.Sleep(time.Second * 1)
} else {
if addCount {
o.mutex.Lock()
o.totalEventsSent += uint64(len(v))
o.totalDataSent += uint64(len(msgAsJson))
o.mutex.Unlock()
}
break
}
}
if nRetries == 0 && addCount {
o.mutex.Lock()
o.totalEventsLost += uint64(len(v))
o.mutex.Unlock()
}
}
}
}
<-o.goroutineSem
}
func (o *OmsNozzle) routeEvents() error {
pendingEvents := make(map[string][]interface{})
// Firehose message processing loop
ticker := time.NewTicker(o.nozzleConfig.OmsBatchTime)
for {
// loop over message and signal channel
select {
case s := <-o.signalChan:
o.logger.Info("exiting", lager.Data{"signal caught": s.String()})
err := o.firehoseClient.CloseConsumer()
if err != nil {
o.logger.Error("error closing consumer", err)
}
os.Exit(1)
case <-ticker.C:
// get the pending as current
currentEvents := pendingEvents
// reset the pending events
pendingEvents = make(map[string][]interface{})
o.goroutineSem <- 1
go o.postData(¤tEvents, true)
case msg := <-o.msgChan:
o.totalEventsReceived++
// process message
var omsMessage OMSMessage
var omsMessageType = msg.GetEventType().String()
switch msg.GetEventType() {
// Metrics
case events.Envelope_ValueMetric:
if !o.nozzleConfig.ExcludeMetricEvents {
omsMessage = messages.NewValueMetric(msg, o.cachingClient)
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
case events.Envelope_CounterEvent:
m := messages.NewCounterEvent(msg, o.cachingClient)
if strings.Contains(m.Name, "TruncatingBuffer.DroppedMessage") {
o.logger.Error("received TruncatingBuffer alert", nil)
o.logSlowConsumerAlert()
}
if strings.Contains(m.Name, "doppler_proxy.slow_consumer") && m.Delta > 0 {
o.logger.Error("received slow_consumer alert", nil)
o.logSlowConsumerAlert()
}
if !o.nozzleConfig.ExcludeMetricEvents {
omsMessage = m
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
case events.Envelope_ContainerMetric:
if !o.nozzleConfig.ExcludeMetricEvents {
omsMessage = messages.NewContainerMetric(msg, o.cachingClient)
if omsMessage != nil {
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
}
// Logs Errors
case events.Envelope_LogMessage:
if !o.nozzleConfig.ExcludeLogEvents {
omsMessage = messages.NewLogMessage(msg, o.cachingClient)
if omsMessage != nil {
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
}
case events.Envelope_Error:
if !o.nozzleConfig.ExcludeLogEvents {
omsMessage = messages.NewError(msg, o.cachingClient)
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
// HTTP Start/Stop
case events.Envelope_HttpStartStop:
if !o.nozzleConfig.ExcludeHttpEvents {
omsMessage = messages.NewHTTPStartStop(msg, o.cachingClient)
if omsMessage != nil {
pendingEvents[omsMessageType] = append(pendingEvents[omsMessageType], omsMessage)
}
}
default:
o.logger.Info("uncategorized message", lager.Data{"message": msg.String()})
continue
}
// When the number of one type of events reaches the max per batch, trigger the post immediately
doPost := false
for _, v := range pendingEvents {
if len(v) >= o.nozzleConfig.OmsMaxMsgNumPerBatch {
doPost = true
break
}
}
if doPost {
currentEvents := pendingEvents
pendingEvents = make(map[string][]interface{})
o.goroutineSem <- 1
go o.postData(¤tEvents, true)
}
case err := <-o.errChan:
o.logger.Error("Error while reading from the firehose", err)
if strings.Contains(err.Error(), "close 1008 (policy violation)") {
o.logger.Error("Disconnected because nozzle couldn't keep up. Please try scaling up the nozzle.", nil)
o.logSlowConsumerAlert()
}
// post the buffered messages
o.goroutineSem <- 1
o.postData(&pendingEvents, true)
o.logger.Error("Closing connection with traffic controller", nil)
o.firehoseClient.CloseConsumer()
return err
}
}
}
// Log slowConsumerAlert as a ValueMetric event to OMS
func (o *OmsNozzle) logSlowConsumerAlert() {
name := "slowConsumerAlert"
value := float64(1)
unit := "b"
valueMetric := &events.ValueMetric{
Name: &name,
Value: &value,
Unit: &unit,
}
timeStamp := time.Now().UnixNano()
eventType := events.Envelope_ValueMetric
job := "nozzle"
origin := "alert"
envelope := &events.Envelope{
EventType: &eventType,
Timestamp: &timeStamp,
Job: &job,
Origin: &origin,
ValueMetric: valueMetric,
}
var omsMsg OMSMessage
omsMsg = messages.NewValueMetric(envelope, o.cachingClient)
currentEvents := make(map[string][]interface{})
currentEvents[eventType.String()] = append(currentEvents[eventType.String()], omsMsg)
o.goroutineSem <- 1
o.postData(¤tEvents, false)
}
// OMSMessage is a marker inteface for JSON formatted messages published to OMS
type OMSMessage interface{}