UNPKG

strong-trace

Version:

StrongTrace Node.js Tracer

86 lines (75 loc) 2.21 kB
"use strict"; var zlib = require("zlib") var fs = require("fs") var http = require("http") var HOSTNAME = require("os").hostname() module.exports = UploadClient var DEFAULT_HOST = "localhost" var DEFAULT_PORT = 8103 function UploadClient(config) { if (!(this instanceof UploadClient)) return new UploadClient(config) this.doUpload = !(config.skip_upload) this.version = config.version this.accountKey = config.accountKey this.apiHost = config.api_host || DEFAULT_HOST this.apiPort = config.api_port || DEFAULT_PORT this.apiPath = "/results/" this.logPath = config.log_path this.logfile = null if (this.logPath) { // NOTE: this is only intended for internal debugging, not customer use. this.logfile = fs.createWriteStream(this.logPath) } this.uploadCount = 0 } UploadClient.prototype.send = function send(tracerData, callback) { if (!this.doUpload && !this.logfile) { return } var self = this // compress var serialized = JSON.stringify(tracerData) if (this.doUpload) { zlib.gzip(serialized, function (err, compressed) { if (err) { console.log("Error trying to gzip concurix tracer data!") return } self._upload(compressed, callback) }) } if (this.logfile) { this.logfile.write(serialized) this.logfile.write("\n") } } UploadClient.prototype._upload = function _upload(compressed, callback) { if (!this.doUpload) { return } var options = { agent: false, host: this.apiHost, port: this.apiPort, path: this.apiPath + this.version, method: "POST", headers: { "content-type": "application/json", "content-length": compressed.length, "content-encoding": "gzip", "Concurix-API-Key": this.accountKey, "Concurix-Host": HOSTNAME, "Concurix-Pid": process.pid } } var req = http.request(options, function (res) { if (res.statusCode != 202) { console.log("Failed to send archive to Concurix: %s", res.statusCode) } callback({statusCode: res.statusCode, length: compressed.length}) }) req.end(compressed) req.on("error", function (err) { console.log("Error attempting to send trace file: %s", err) }) }