UNPKG

ca-apm-probe

Version:

CA APM Node.js Agent monitors real-time health and performance of Node.js applications

111 lines (92 loc) 3.19 kB
/** * Copyright (c) 2015 CA. All rights reserved. * * This software and all information contained therein is confidential and proprietary and * shall not be duplicated, used, disclosed or disseminated in any way except as authorized * by the applicable license agreement, without the express written permission of CA. All * authorized reproductions must be marked with this language. * * EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT * PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT WARRANTY * OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL CA BE * LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR * INDIRECT, FROM THE USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST * PROFITS, BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF CA IS * EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE. */ // module for generating guid // algorithm used over here is almost similar to the one used in core Java Agent to generate guid 'use strict'; var os = require('os'); var dns = require('dns'); var logger = require("../logger.js"); var CHARS_HEX_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; var DEFAULT_IP = '127.0.0.1'; var defaultSeeder = new SimpleRandomSeeder(0, 99999); // random number generator function SimpleRandomSeeder(low, high) { this.low = low; this.high = high; this.nextInt = function() { return Math.floor(Math.random() * (high - low) + low); } }; // utility functions var toHex = function(value, length) { var buffer = ''; var shift = (length - 1) << 2; var i = -1; while (++i < length) { buffer = buffer.concat(CHARS_HEX_DIGITS[(value >> shift) & 0x0000000F]); value <<= 4; } return buffer; } var convertIpToBytes = function(ipAddress) { var arr = ipAddress.split('.'); var bytes = []; var i = -1; while (++i < arr.length) { bytes[i] = parseInt(arr[i]); } return bytes; }; var toInt = function(bytes) { var value = 0; var b = 0; var i = -1; while (++i < bytes.length) { value <<= 8; b = bytes[i] & 0xff; value |= b; } return value; }; var makeGuidGenerator = function() { var hostAndSystemId; var findHostAndSystemId = function() { var processId = toHex(process.pid, 8); var host = os.hostname(); hostAndSystemId = toHex(toInt(convertIpToBytes(DEFAULT_IP)), 8).concat(processId); dns.lookup(host, function onLookup(err, address, family) { if(err) { logger.debug('Error while looking up ipaddress for host: ' + host); return; } hostAndSystemId = toHex(toInt(convertIpToBytes(address)), 8).concat(processId); }); }; //invoke function to find host and system id findHostAndSystemId(); // generates guid key return function() { var guid = toHex((Date.now() & 0xFFFFFFFF), 8); guid = guid.concat(hostAndSystemId).concat(toHex(defaultSeeder.nextInt(), 8)); return guid; }; }; function GuidGenerator() { this.generateGuid = makeGuidGenerator(); } module.exports = new GuidGenerator();