UNPKG

@harvve/tslog-influxdb-transport

Version:

Send logs to influxdb2 via telegraf without writing them to files.

116 lines (115 loc) 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Transporter = void 0; const util_1 = require("util"); const dgram_1 = require("dgram"); class Transporter { constructor(options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; this.options = options; this.MATCH_SPECIAL_CHARS = /[\n|\'|\{|\}|/|\[|\]|,]/gm; this.MATCH_TWO_OR_MORE_SPACES = /\s{2,}/gm; this.SKIP_SPACES_PATTERN = /\s/gm; this.SKIP_SPACES_REPLACEMENT = '\\ '; this.DEFAULT_FIELD_KEYS = ['columnNumber', 'lineNumber', 'logLevelId']; this.DEFAULT_TAG_KEYS = [ 'fileName', 'isConstructor', 'filePath', 'fullFilePath', 'functionName', 'hostname', 'instanceName', 'logLevel', 'loggerName', 'methodName', 'requestId', 'typeName', 'argumentsArray' ]; if (!this.options.measurementName) { throw new Error('You need to provide measurementName !'); } if (((_a = this.options.tagKeys) === null || _a === void 0 ? void 0 : _a.length) && !((_c = (_b = this.options) === null || _b === void 0 ? void 0 : _b.fieldKeys) === null || _c === void 0 ? void 0 : _c.length)) { throw this.errorIfOnlyOneListProvided('tagKeys', 'fieldKeys'); } if (((_d = this.options.fieldKeys) === null || _d === void 0 ? void 0 : _d.length) && !((_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.tagKeys) === null || _f === void 0 ? void 0 : _f.length)) { throw this.errorIfOnlyOneListProvided('fieldKeys', 'tagKeys'); } if (((_g = this.options.fieldKeys) === null || _g === void 0 ? void 0 : _g.length) && ((_j = (_h = this.options) === null || _h === void 0 ? void 0 : _h.tagKeys) === null || _j === void 0 ? void 0 : _j.length)) { for (const itr of this.options.fieldKeys) { if (this.options.tagKeys.includes(itr)) { throw new Error(`Keys cannot be duplicated! (duplicated: ${itr})`); } } } if (!((_k = this.options.tagKeys) === null || _k === void 0 ? void 0 : _k.length)) { this.options.tagKeys = [...this.DEFAULT_TAG_KEYS]; } if (!((_l = this.options.fieldKeys) === null || _l === void 0 ? void 0 : _l.length)) { this.options.fieldKeys = [...this.DEFAULT_FIELD_KEYS]; } this.socket = (0, dgram_1.createSocket)(this.options.socketType); } /** * @returns Transport provider */ getTransportProvider() { var _a; return { minLevel: ((_a = this.options) === null || _a === void 0 ? void 0 : _a.minLevel) || 'debug', transportLogger: { debug: this.transport.bind(this), error: this.transport.bind(this), fatal: this.transport.bind(this), info: this.transport.bind(this), silly: this.transport.bind(this), trace: this.transport.bind(this), warn: this.transport.bind(this) } }; } transport(message) { const preparedMsg = this.prepareMessage(message); const msg = Buffer.from(preparedMsg); this.socket.send(msg, 0, msg.length, this.options.port, this.options.address || 'localhost'); } prepareMessage(message) { const tags = this.prepareEntries(message, this.options.tagKeys || []); const fields = this.prepareEntries(message, this.options.fieldKeys || []); return `${this.options.measurementName},${tags} ${fields}`; } prepareEntries(message, entries) { const keyValuePairs = Object.entries(message) .filter(([key]) => entries.includes(key)) .map(([key, value]) => { if (typeof value === 'undefined') { value = 'unknown'; } if (typeof value === 'object' && !Array.isArray(value)) { value = [value]; } if (typeof value === 'string') { value = value .trim() .replace(this.MATCH_TWO_OR_MORE_SPACES, ' ') .replace(this.SKIP_SPACES_PATTERN, this.SKIP_SPACES_REPLACEMENT); } if (Array.isArray(value)) { return (`${key}=` + (0, util_1.format)(...value) .replace(this.MATCH_SPECIAL_CHARS, '') .replace(this.MATCH_TWO_OR_MORE_SPACES, ' ') .trim() .replace(this.SKIP_SPACES_PATTERN, this.SKIP_SPACES_REPLACEMENT)); } return `${key}=${value}`; }) .join(','); return keyValuePairs; } errorIfOnlyOneListProvided(exist, missing) { return new Error(`If you provided ${exist} you must provide ${missing} as well`); } } exports.Transporter = Transporter;