-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
460 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const { Writable } = require('stream') | ||
|
||
const INITIAL_SIZE = 64 * 1024 | ||
|
||
class FlareFile extends Writable { | ||
constructor () { | ||
super() | ||
|
||
this.length = 0 | ||
|
||
this._buffer = Buffer.alloc(INITIAL_SIZE) | ||
} | ||
|
||
get data () { | ||
return this._buffer.subarray(0, this.length) | ||
} | ||
|
||
_write (chunk, encoding, callback) { | ||
const length = Buffer.byteLength(chunk) | ||
|
||
this._reserve(length) | ||
|
||
if (Buffer.isBuffer(chunk)) { | ||
this.length += chunk.copy(this._buffer, this.length) | ||
} else { | ||
this.length += this._buffer.write(chunk, encoding) | ||
} | ||
|
||
callback() | ||
} | ||
|
||
_reserve (length) { | ||
while (this.length + length > this._buffer.length) { | ||
const buffer = Buffer.alloc(this.length * 2) | ||
|
||
this._buffer.copy(buffer) | ||
this._buffer = buffer | ||
} | ||
} | ||
} | ||
|
||
module.exports = FlareFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
|
||
const log = require('../log') | ||
const startupLog = require('../startup-log') | ||
const FlareFile = require('./file') | ||
const { LogChannel } = require('../log/channels') | ||
const request = require('../exporters/common/request') | ||
const FormData = require('../exporters/common/form-data') | ||
|
||
const MAX_LOG_SIZE = 12 * 1024 * 1024 // 12MB soft limit | ||
const TIMEOUT = 20 * 1000 * 60 | ||
|
||
let logChannel = null | ||
let tracerLogs = null | ||
let timer | ||
let tracerConfig = null | ||
|
||
const logger = { | ||
debug: (msg) => recordLog(msg), | ||
info: (msg) => recordLog(msg), | ||
warn: (msg) => recordLog(msg), | ||
error: (err) => recordLog(err.stack) | ||
} | ||
|
||
const flare = { | ||
enable (tracerConfig_) { | ||
tracerConfig = tracerConfig_ | ||
}, | ||
|
||
disable () { | ||
tracerConfig = null | ||
|
||
flare.cleanup() | ||
}, | ||
|
||
prepare (logLevel) { | ||
if (!tracerConfig) return | ||
|
||
logChannel?.unsubscribe(logger) | ||
logChannel = new LogChannel(logLevel) | ||
logChannel.subscribe(logger) | ||
tracerLogs = tracerLogs || new FlareFile() | ||
timer = timer || setTimeout(flare.cleanup, TIMEOUT) | ||
}, | ||
|
||
send (task) { | ||
if (!tracerConfig) return | ||
|
||
const tracerInfo = new FlareFile() | ||
|
||
tracerInfo.write(JSON.stringify(startupLog.tracerInfo(), null, 2)) | ||
|
||
flare._sendFile(task, tracerInfo, 'tracer_info.txt') | ||
flare._sendFile(task, tracerLogs, 'tracer_logs.txt') | ||
|
||
flare.cleanup() | ||
}, | ||
|
||
cleanup () { | ||
logChannel?.unsubscribe(logger) | ||
timer = clearTimeout(timer) | ||
logChannel = null | ||
tracerLogs = null | ||
}, | ||
|
||
_sendFile (task, file, filename) { | ||
if (!file) return | ||
|
||
const form = new FormData() | ||
|
||
form.append('case_id', task.case_id) | ||
form.append('hostname', task.hostname) | ||
form.append('email', task.user_handle) | ||
form.append('source', 'tracer_nodejs') | ||
form.append('flare_file', file.data, { filename }) | ||
|
||
request(form, { | ||
url: tracerConfig.url, | ||
hostname: tracerConfig.hostname, | ||
port: tracerConfig.port, | ||
method: 'POST', | ||
path: '/tracer_flare/v1', | ||
headers: form.getHeaders() | ||
}, (err) => { | ||
if (err) { | ||
log.error(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
function recordLog (msg) { | ||
if (tracerLogs.length > MAX_LOG_SIZE) return | ||
|
||
tracerLogs.write(`${msg}\n`) // TODO: gzip | ||
} | ||
|
||
module.exports = flare |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.