"use strict"; var os = require("os"); var Constants = require("../Declarations/Constants"); var Util = require("../Library/Util"); var Context = require("../Library/Context"); var AutoCollectHttpDependencies = require("../AutoCollection/HttpDependencies"); var AIMS_URI = "http://169.254.169.254/metadata/instance/compute"; var AIMS_API_VERSION = "api-version=2017-12-01"; var AIMS_FORMAT = "format=json"; var ConnectionErrorMessage = "ENETUNREACH"; var HeartBeat = (function () { function HeartBeat(client) { this._collectionInterval = 900000; this._vmData = {}; this._azInst_vmId = ""; this._azInst_subscriptionId = ""; this._azInst_osType = ""; if (!HeartBeat.INSTANCE) { HeartBeat.INSTANCE = this; } this._isInitialized = false; this._client = client; } HeartBeat.prototype.enable = function (isEnabled, config) { var _this = this; this._isEnabled = isEnabled; if (this._isEnabled && !this._isInitialized) { this._isInitialized = true; } if (isEnabled) { if (!this._handle) { this._handle = setInterval(function () { return _this.trackHeartBeat(config, function () { }); }, this._collectionInterval); this._handle.unref(); // Allow the app to terminate even while this loop is going on } } else { if (this._handle) { clearInterval(this._handle); this._handle = null; } } }; HeartBeat.prototype.isInitialized = function () { return this._isInitialized; }; HeartBeat.isEnabled = function () { return HeartBeat.INSTANCE && HeartBeat.INSTANCE._isEnabled; }; HeartBeat.prototype.trackHeartBeat = function (config, callback) { var _this = this; var waiting = false; var properties = {}; var sdkVersion = Context.sdkVersion; // "node" or "node-nativeperf" properties["sdk"] = sdkVersion; properties["osType"] = os.type(); if (process.env.WEBSITE_SITE_NAME) { properties["appSrv_SiteName"] = process.env.WEBSITE_SITE_NAME || ""; properties["appSrv_wsStamp"] = process.env.WEBSITE_HOME_STAMPNAME || ""; properties["appSrv_wsHost"] = process.env.WEBSITE_HOSTNAME || ""; } else if (process.env.FUNCTIONS_WORKER_RUNTIME) { properties["azfunction_appId"] = process.env.WEBSITE_HOSTNAME; } else if (config) { if (this._isVM === undefined) { waiting = true; this._getAzureComputeMetadata(config, function () { if (_this._isVM && Object.keys(_this._vmData).length > 0) { properties["azInst_vmId"] = _this._vmData["vmId"] || ""; properties["azInst_subscriptionId"] = _this._vmData["subscriptionId"] || ""; properties["azInst_osType"] = _this._vmData["osType"] || ""; _this._azInst_vmId = _this._vmData["vmId"] || ""; _this._azInst_subscriptionId = _this._vmData["subscriptionId"] || ""; _this._azInst_osType = _this._vmData["osType"] || ""; } _this._client.trackMetric({ name: Constants.HeartBeatMetricName, value: 0, properties: properties }); callback(); }); } else if (this._isVM) { properties["azInst_vmId"] = this._azInst_vmId; properties["azInst_subscriptionId"] = this._azInst_subscriptionId; properties["azInst_osType"] = this._azInst_osType; } } if (!waiting) { this._client.trackMetric({ name: Constants.HeartBeatMetricName, value: 0, properties: properties }); callback(); } }; HeartBeat.prototype.dispose = function () { HeartBeat.INSTANCE = null; this.enable(false); this._isInitialized = false; }; HeartBeat.prototype._getAzureComputeMetadata = function (config, callback) { var _this = this; var metadataRequestUrl = AIMS_URI + "?" + AIMS_API_VERSION + "&" + AIMS_FORMAT; var requestOptions = (_a = { method: 'GET' }, _a[AutoCollectHttpDependencies.disableCollectionRequestOption] = true, _a.headers = { "Metadata": "True", }, _a); var req = Util.makeRequest(config, metadataRequestUrl, requestOptions, function (res) { if (res.statusCode === 200) { // Success; VM _this._isVM = true; var virtualMachineData_1 = ""; res.on('data', function (data) { virtualMachineData_1 += data; }); res.on('end', function () { _this._vmData = _this._isJSON(virtualMachineData_1) ? JSON.parse(virtualMachineData_1) : {}; callback(); }); } else { // else Retry on next heartbeat metrics call callback(); } }); if (req) { req.on('error', function (error) { // Unable to contact endpoint. // Do nothing for now. if (error && error.message && error.message.indexOf(ConnectionErrorMessage) > -1) { _this._isVM = false; // confirm it's not in VM } // errors other than connect ENETUNREACH - retry callback(); }); req.end(); } var _a; }; HeartBeat.prototype._isJSON = function (str) { try { return (JSON.parse(str) && !!str); } catch (e) { return false; } }; return HeartBeat; }()); module.exports = HeartBeat; //# sourceMappingURL=HeartBeat.js.map