UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

122 lines 4.12 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.Util = void 0; const api_1 = require("@opentelemetry/api"); class Util { constructor() { this._listenerAttached = false; this.isNodeExit = false; this._addCloseHandler(); } static getInstance() { if (!Util._instance) { Util._instance = new Util(); } return Util._instance; } /** * helper method to trim strings (IE8 does not implement String.prototype.trim) */ trim(str) { if (typeof str === "string") { return str.replace(/^\s+|\s+$/g, ""); } return ""; } /** * Check if an object is of type Array */ isArray(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; } /** * Check if an object is of type Error */ isError(obj) { return obj instanceof Error; } isPrimitive(input) { const propType = typeof input; return propType === "string" || propType === "number" || propType === "boolean"; } /** * Check if an object is of type Date */ isDate(obj) { return Object.prototype.toString.call(obj) === "[object Date]"; } /** * Convert milliseconds to Breeze expected time. * @internal */ msToTimeSpan(ms) { let totalms = ms; if (Number.isNaN(totalms) || totalms < 0 || !Number.isFinite(ms)) { totalms = 0; } let sec = ((totalms / 1000) % 60).toFixed(7).replace(/0{0,4}$/, ""); let min = `${Math.floor(totalms / (1000 * 60)) % 60}`; let hour = `${Math.floor(totalms / (1000 * 60 * 60)) % 24}`; const days = Math.floor(totalms / (1000 * 60 * 60 * 24)); sec = sec.indexOf(".") < 2 ? `0${sec}` : sec; min = min.length < 2 ? `0${min}` : min; hour = hour.length < 2 ? `0${hour}` : hour; const daysText = days > 0 ? `${days}.` : ""; return `${daysText + hour}:${min}:${sec}`; } /** * Using JSON.stringify, by default Errors do not serialize to something useful: * Simplify a generic Node Error into a simpler map for customDimensions * Custom errors can still implement toJSON to override this functionality */ extractError(err) { // Error is often subclassed so may have code OR id properties: // https://nodejs.org/api/errors.html#errors_error_code const looseError = err; return { message: err.message, code: looseError.code || looseError.id || "", }; } isDbDependency(dependencyType) { return (dependencyType.indexOf("SQL") > -1 || dependencyType === "mysql" || dependencyType === "postgresql" || dependencyType === "mongodb" || dependencyType === "redis"); } /** * Returns string representation of an object suitable for diagnostics diag. */ dumpObj(object) { const objectTypeDump = Object["prototype"].toString.call(object); let propertyValueDump = ""; if (objectTypeDump === "[object Error]") { propertyValueDump = `{ stack: '${object.stack}', message: '${object.message}', name: '${object.name}'`; } else { propertyValueDump = JSON.stringify(object); } return objectTypeDump + propertyValueDump; } stringify(payload) { try { return JSON.stringify(payload); } catch (error) { api_1.diag.warn("Failed to serialize payload", error, payload); } } _addCloseHandler() { if (!this._listenerAttached) { process.on("exit", () => { this.isNodeExit = true; }); this._listenerAttached = true; } } } exports.Util = Util; //# sourceMappingURL=util.js.map