@reportfy/apm
Version:
Pacote para utilização de apm do reportfy.com.br
116 lines • 7 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogHttpRequest = void 0;
var http_1 = __importDefault(require("http"));
var util_1 = require("util");
var config_1 = require("../config");
var helper_1 = require("../helper");
var report_1 = require("./report");
var listNotBlock = ['dev.apm.reportfy.com.br', 'apm.reportfy.com.br'];
var emitClientRequest = http_1.default.ClientRequest.prototype.emit;
// @ts-ignore
var emitHttpServer = http_1.default.ServerResponse.prototype._send;
var emitHttpServerEmit = http_1.default.ServerResponse.prototype.emit;
function validateFilterRules(config, statusCode, method) {
if (!config.methods.length || !config.statusCodes.length)
return true;
return config.statusCodes.some(function (val) { return Number(val) === Number(statusCode); }) &&
config.methods.some(function (val) { return val.toLowerCase() === method.toLowerCase(); });
}
var filterIntercept = function (host) { return listNotBlock.some(function (val) { return val.toLowerCase().trim() === String(host).trim().toLowerCase(); }); };
function LogHttpRequest(_a) {
var key = _a.key, environment = _a.environment, accessKey = _a.accessKey, secretKey = _a.secretKey, _b = _a.tracing, tracing = _b === void 0 ? false : _b, provider = _a.provider;
var startIntercept = 0;
var startRequest = 0;
if (http_1.default === null || http_1.default === void 0 ? void 0 : http_1.default.ClientRequest) {
// @ts-ignore
http_1.default.ClientRequest.prototype.emit = function (type, req, res) {
var _this = this;
if (type === 'socket')
startIntercept = new Date().getTime();
if (type === 'response' && !filterIntercept(this.host)) {
// @ts-ignore
var chunks_1 = [];
var senderBody_1 = {};
var headers_1 = {};
var url_1 = '';
if (req.client._httpMessage._redirectable &&
req.client._httpMessage._redirectable._requestBodyBuffers &&
Array.isArray(req.client._httpMessage._redirectable._requestBodyBuffers)) {
if (req.client._httpMessage._redirectable._requestBodyBuffers.length)
senderBody_1 = new util_1.TextDecoder("utf-8").decode(req.client._httpMessage._redirectable._requestBodyBuffers[0].data);
headers_1 = req.client._httpMessage.getHeaders();
// @ts-ignore
url_1 = this._redirectable._currentUrl;
}
req.on('data', function (chunk) {
// @ts-ignore
chunks_1.push(new Buffer.from(chunk));
});
req.on('end', function () {
var config = (0, config_1.getConfigFile)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey });
var diffTime = Math.abs(new Date().getTime() - startIntercept);
// @ts-ignore
var body = Buffer.concat(chunks_1).toString('utf8');
var method = _this.method;
var statusCode = Number(req.statusCode);
var path = _this.path;
var host = _this.host;
headers_1 = __assign(__assign({}, headers_1), { host: host });
if (config && config.intercept && validateFilterRules(config.intercept, statusCode, method)) {
(0, report_1.handlerErrorNotTreatment)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey, publicRSA: config.publicRSA, provider: provider }, (0, helper_1.mountedErrorHttpRequest)({ diffTime: diffTime, path: path, url: url_1, host: host, method: method, body: body, senderBody: senderBody_1, headers: headers_1, statusCode: statusCode }, config.intercept));
}
});
}
// @ts-ignore
return emitClientRequest.apply(this, arguments);
};
}
if (http_1.default === null || http_1.default === void 0 ? void 0 : http_1.default.ServerResponse) {
// @ts-ignore
http_1.default.ServerResponse.prototype.emit = function (type, data) {
if (type === 'socket')
startRequest = new Date().getTime();
// @ts-ignore
return emitHttpServerEmit.apply(this, arguments);
};
}
if (http_1.default === null || http_1.default === void 0 ? void 0 : http_1.default.ServerResponse) {
// @ts-ignore
http_1.default.ServerResponse.prototype._send = function (data) {
// TODO: verificar porque o prototype está chamado mais de uma vez
if (!data)
return emitHttpServer.apply(this, arguments);
var config = (0, config_1.getConfigFile)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey });
var responseBody = data.length > 1 ? data.toString() : '';
var req = this.req;
var diffTime = Math.abs(new Date().getTime() - startRequest);
if (config && config.apm) {
(0, report_1.handlerRequestApplication)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey, publicRSA: config.publicRSA, provider: provider }, (0, helper_1.mountedApplicationResponse)(req, { statusCode: Number(this.statusCode) }, diffTime, config.apm));
if (Boolean(tracing)) {
(0, report_1.handlerErrorNotTreatment)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey, publicRSA: config.publicRSA, provider: provider }, (0, helper_1.mountedTracingRequest)(req, { statusCode: Number(this.statusCode) }, responseBody, diffTime, config.apm));
}
if (validateFilterRules(config.apm, Number(this.statusCode), String(req.method))) {
(0, report_1.handlerErrorNotTreatment)({ key: key, environment: environment, accessKey: accessKey, secretKey: secretKey, publicRSA: config.publicRSA, provider: provider }, (0, helper_1.mountedErrorResponse)(req, { statusCode: Number(this.statusCode) }, responseBody, diffTime, config.apm));
}
}
return emitHttpServer.apply(this, arguments);
};
}
}
exports.LogHttpRequest = LogHttpRequest;
//# sourceMappingURL=index.js.map