-
Notifications
You must be signed in to change notification settings - Fork 266
/
buffered-event-stream.ts
325 lines (278 loc) · 9.73 KB
/
buffered-event-stream.ts
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
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Bluebird from "bluebird"
import { Events, EventName, EventBus, eventNames } from "../events"
import { LogEntryMetadata, LogEntry } from "../logger/log-entry"
import { chainMessages } from "../logger/renderers"
import { got } from "../util/http"
import { makeAuthHeader } from "./auth"
import { LogLevel } from "../logger/log-node"
import { Garden } from "../garden"
export type StreamEvent = {
name: EventName
payload: Events[EventName]
timestamp: Date
}
export interface LogEntryEvent {
key: string
parentKey: string | null
revision: number
msg: string | string[]
timestamp: Date
level: LogLevel
data?: any
section?: string
metadata?: LogEntryMetadata
}
export function formatLogEntryForEventStream(entry: LogEntry): LogEntryEvent {
const { section, data } = entry.getMessageState()
const { key, revision, level } = entry
const parentKey = entry.parent ? entry.parent.key : null
const metadata = entry.getMetadata()
const msg = chainMessages(entry.getMessageStates() || [])
const timestamp = new Date()
return { key, parentKey, revision, msg, data, metadata, section, timestamp, level }
}
interface StreamTarget {
host: string
clientAuthToken: string
}
export interface ConnectBufferedEventStreamParams {
targets?: StreamTarget[]
garden: Garden
}
interface ApiBatchBase {
workflowRunUid?: string
sessionId: string | null
projectUid?: string
}
export interface ApiEventBatch extends ApiBatchBase {
events: StreamEvent[]
environment: string
namespace: string
}
export interface ApiLogBatch extends ApiBatchBase {
logEntries: LogEntryEvent[]
}
export const controlEventNames: Set<EventName> = new Set(["_workflowRunRegistered"])
/**
* Buffers events and log entries and periodically POSTs them to Garden Enterprise or another Garden service.
*
* Subscribes to logger events once, in the constructor.
*
* Subscribes to Garden events via the connect method, since we need to subscribe to the event bus of
* new Garden instances (and unsubscribe from events from the previously connected Garden instance, if
* any) e.g. when config changes during a watch-mode command.
*/
export class BufferedEventStream {
protected log: LogEntry
public sessionId: string
protected targets: StreamTarget[]
protected garden: Garden
private workflowRunUid: string | undefined
/**
* We maintain this map to facilitate unsubscribing from a previously connected event bus
* when a new event bus is connected.
*/
private gardenEventListeners: { [eventName: string]: (payload: any) => void }
private intervalId: NodeJS.Timer | null
private bufferedEvents: StreamEvent[]
private bufferedLogEntries: LogEntryEvent[]
protected intervalMsec = 1000
/**
* We use 600 kilobytes as the maximum combined size of the events / log entries in a given batch. This number
* was chosen to fit comfortably below e.g. nginx' default max request size, while still being able to carry a decent
* number of records.
*/
private maxBatchBytes = 600 * 1024 // 600 kilobytes
constructor(log: LogEntry, sessionId: string) {
this.sessionId = sessionId
this.log = log
this.log.root.events.onAny((_name: string, payload: LogEntryEvent) => {
this.streamLogEntry(payload)
})
this.bufferedEvents = []
this.bufferedLogEntries = []
this.targets = []
}
connect({ garden, targets }: ConnectBufferedEventStreamParams) {
if (this.intervalId) {
clearInterval(this.intervalId)
}
if (targets) {
this.targets = targets
}
if (this.garden) {
// We unsubscribe from the old event bus' events.
this.unsubscribeFromGardenEvents(this.garden.events)
}
this.garden = garden
this.subscribeToGardenEvents(this.garden.events)
this.log.silly("BufferedEventStream: Connected")
this.startInterval()
}
subscribeToGardenEvents(eventBus: EventBus) {
// We maintain this map to facilitate unsubscribing from events when the Garden instance is closed.
const gardenEventListeners = {}
for (const gardenEventName of eventNames) {
const listener = (payload: LogEntryEvent) => this.streamEvent(gardenEventName, payload)
gardenEventListeners[gardenEventName] = listener
eventBus.on(gardenEventName, listener)
}
this.gardenEventListeners = gardenEventListeners
}
unsubscribeFromGardenEvents(eventBus: EventBus) {
for (const [gardenEventName, listener] of Object.entries(this.gardenEventListeners)) {
eventBus.removeListener(gardenEventName, listener)
}
}
startInterval() {
this.intervalId = setInterval(() => {
this.flushBuffered().catch((err) => {
this.log.error(err)
})
}, this.intervalMsec)
}
async close() {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = null
}
try {
await this.flushAll()
} catch (err) {
/**
* We don't throw an exception here, since a failure to stream events and log entries doesn't mean that the
* command failed.
*/
this.log.error(`Error while flushing events and log entries: ${err.message}`)
}
}
streamEvent<T extends EventName>(name: T, payload: Events[T]) {
if (controlEventNames.has(name)) {
this.handleControlEvent(name, payload)
return
}
this.bufferedEvents.push({
name,
payload,
timestamp: new Date(),
})
}
streamLogEntry(logEntry: LogEntryEvent) {
this.bufferedLogEntries.push(logEntry)
}
private getHeaders(target: StreamTarget) {
return makeAuthHeader(target.clientAuthToken)
}
async flushEvents(events: StreamEvent[]) {
if (events.length === 0) {
return
}
const data: ApiEventBatch = {
events,
workflowRunUid: this.workflowRunUid,
sessionId: this.sessionId,
projectUid: this.garden.projectId || undefined,
environment: this.garden.environmentName,
namespace: this.garden.namespace,
}
await this.postToTargets(`${events.length} events`, "events", data)
}
async flushLogEntries(logEntries: LogEntryEvent[]) {
if (logEntries.length === 0 || !this.garden) {
return
}
const data: ApiLogBatch = {
logEntries,
workflowRunUid: this.workflowRunUid,
sessionId: this.sessionId,
projectUid: this.garden.projectId || undefined,
}
await this.postToTargets(`${logEntries.length} log entries`, "log-entries", data)
}
private async postToTargets(description: string, path: string, data: ApiEventBatch | ApiLogBatch) {
if (this.targets.length === 0) {
this.log.silly("No targets to send events to. Dropping them.")
}
const targetUrls = this.targets.map((target) => `${target.host}/${path}`)
this.log.silly(`Flushing ${description} to ${targetUrls.join(", ")}`)
this.log.silly(`--------`)
this.log.silly(`data: ${JSON.stringify(data)}`)
this.log.silly(`--------`)
try {
await Bluebird.map(this.targets, (target) => {
const headers = this.getHeaders(target)
return got.post(`${target.host}/${path}`, { json: data, headers })
})
} catch (err) {
/**
* We don't throw an exception here, since a failure to stream events and log entries doesn't mean that the
* command failed.
*/
this.log.debug(`Error while flushing events and log entries: ${err.message}`)
}
}
/**
* Flushes all events and log entries until none remain, and returns a promise that resolves when all of them
* have been posted to their targets.
*/
async flushAll() {
if (!this.garden || this.targets.length === 0) {
return
}
this.log.silly(`Flushing all remaining events and log entries`)
const flushPromises: Promise<any>[] = []
try {
while (this.bufferedEvents.length > 0 || this.bufferedLogEntries.length > 0) {
this.log.silly(`remaining: ${this.bufferedEvents.length} events, ${this.bufferedLogEntries.length} log entries`)
// while (this.bufferedEvents.length > 0 || this.bufferedLogEntries.length > 0) {
flushPromises.push(this.flushBuffered())
}
} catch (err) {
throw err
}
return Bluebird.all(flushPromises)
}
async flushBuffered() {
if (!this.garden || this.targets.length === 0) {
return
}
const eventsToFlush = this.makeBatch(this.bufferedEvents)
const logEntriesToFlush = this.makeBatch(this.bufferedLogEntries)
return Bluebird.all([this.flushEvents(eventsToFlush), this.flushLogEntries(logEntriesToFlush)])
}
/**
* Adds buffered records (events or log entries) to a batch until none remain or until their combined size
* exceeds `MAX_MATCH_BYTES`, and returns the batch.
*/
makeBatch<B>(buffered: B[]): B[] {
const batch: B[] = []
let batchBytes = 0
while (batchBytes < this.maxBatchBytes && buffered.length > 0) {
let nextRecordBytes = Buffer.from(JSON.stringify(buffered[0])).length
if (nextRecordBytes > this.maxBatchBytes) {
this.log.error(`Event or log entry too large to flush, dropping it.`)
this.log.debug(JSON.stringify(buffered[0]))
buffered.shift() // Drop first record.
nextRecordBytes = Buffer.from(JSON.stringify(buffered[0])).length
}
if (batchBytes + nextRecordBytes > this.maxBatchBytes) {
break
}
batch.push(buffered.shift() as B)
batchBytes += nextRecordBytes
}
return batch
}
handleControlEvent<T extends EventName>(name: T, payload: Events[T]) {
if (name === "_workflowRunRegistered") {
this.workflowRunUid = payload.workflowRunUid
}
}
}