ApplicationInsights-node.js/Tests/AutoCollection/NativePerformance.tests.ts

129 строки
6.8 KiB
TypeScript
Исходник Обычный вид История

add node.js native metrics (#508) * release 1.3.0 (#502) * add live metrics (#494) * add QPS files; enablement not included * add tests; add qps ping retry walkback * add off by default config option for QPS * add qps test to ensure off by default * only construct qps when enabled * add qps Sender constants * add QuickPulseEnvelopeFactory * fix ts timer compile * add qps exception and trace error handling * refactor default live metrics test * make qps hostname configurable * fix node0 setTimeout error * add qps telemetrytype constants * move away from export enums for ts backcompat * add qps EventDocument,fix dependency duration * use envelope type in qps telemetryprocessor * send avg execution duration instead of last dur * add error logging for qps endpoint * add telemetry types, move qps config to qpssender * fix keying type issue in TelemetryType * fix wrong telemetrytype in qps conversion (#500) * bump version to 1.3.0 (#501) * bump version to 1.3.0 * add setSendLiveMetrics to README * add native metrics subscriber * mark native members of NativePerformance as static * remove segfault lib * update package.json * native: add throw tests * add refs to metrics * add config for enabling metrics, add heap metrics * report stdDev stats * rename custom metric names * fix native perf test * only run tests for node4+ * use node-pre-gyp native metrics * optionaldevdep for travis tests * simulate optionalDevDependency in travis * remove duped travis npm install * gate native metrics to node6+ * refactor extended metrics enablement * move extended metrics arg parsing to API * add info logging for when native module is loaded * docs: add extended metrics * add extended metrics build stage * use env var for travis extended metrics tests * test: modify test for non-installed case * tweak travis.yml * readme: comment out docs
2019-05-29 02:49:45 +03:00
import assert = require("assert");
import sinon = require("sinon");
import AppInsights = require("../../applicationinsights");
import TelemetryClient = require("../../Library/TelemetryClient");
import Config = require("../../Library/Config");
import { AutoCollectNativePerformance } from "../../AutoCollection/NativePerformance";
describe("AutoCollection/NativePerformance", () => {
afterEach(() => {
AppInsights.dispose();
});
if (AutoCollectNativePerformance.isNodeVersionCompatible()) {
describe("#init and #dispose()", () => {
it("init should enable and dispose should stop autocollection interval", () => {
var setIntervalSpy = sinon.spy(global, "setInterval");
var clearIntervalSpy = sinon.spy(global, "clearInterval");
AppInsights.setup("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333")
.setAutoCollectHeartbeat(false)
.setAutoCollectPerformance(false, true)
.start();
add node.js native metrics (#508) * release 1.3.0 (#502) * add live metrics (#494) * add QPS files; enablement not included * add tests; add qps ping retry walkback * add off by default config option for QPS * add qps test to ensure off by default * only construct qps when enabled * add qps Sender constants * add QuickPulseEnvelopeFactory * fix ts timer compile * add qps exception and trace error handling * refactor default live metrics test * make qps hostname configurable * fix node0 setTimeout error * add qps telemetrytype constants * move away from export enums for ts backcompat * add qps EventDocument,fix dependency duration * use envelope type in qps telemetryprocessor * send avg execution duration instead of last dur * add error logging for qps endpoint * add telemetry types, move qps config to qpssender * fix keying type issue in TelemetryType * fix wrong telemetrytype in qps conversion (#500) * bump version to 1.3.0 (#501) * bump version to 1.3.0 * add setSendLiveMetrics to README * add native metrics subscriber * mark native members of NativePerformance as static * remove segfault lib * update package.json * native: add throw tests * add refs to metrics * add config for enabling metrics, add heap metrics * report stdDev stats * rename custom metric names * fix native perf test * only run tests for node4+ * use node-pre-gyp native metrics * optionaldevdep for travis tests * simulate optionalDevDependency in travis * remove duped travis npm install * gate native metrics to node6+ * refactor extended metrics enablement * move extended metrics arg parsing to API * add info logging for when native module is loaded * docs: add extended metrics * add extended metrics build stage * use env var for travis extended metrics tests * test: modify test for non-installed case * tweak travis.yml * readme: comment out docs
2019-05-29 02:49:45 +03:00
if (AutoCollectNativePerformance["_metricsAvailable"]) {
assert.equal(setIntervalSpy.callCount, 1, "setInterval should be called once as part of NativePerformance initialization");
AppInsights.dispose();
assert.equal(clearIntervalSpy.callCount, 1, "clearInterval should be called once as part of NativePerformance shutdown");
} else {
assert.equal(setIntervalSpy.callCount, 0, "setInterval should not be called if NativePerformance package is not available");
AppInsights.dispose();
assert.equal(clearIntervalSpy.callCount, 0, "clearInterval should not be called if NativePerformance package is not available");
}
setIntervalSpy.restore();
clearIntervalSpy.restore();
});
it("constructor should be safe to call multiple times", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
add node.js native metrics (#508) * release 1.3.0 (#502) * add live metrics (#494) * add QPS files; enablement not included * add tests; add qps ping retry walkback * add off by default config option for QPS * add qps test to ensure off by default * only construct qps when enabled * add qps Sender constants * add QuickPulseEnvelopeFactory * fix ts timer compile * add qps exception and trace error handling * refactor default live metrics test * make qps hostname configurable * fix node0 setTimeout error * add qps telemetrytype constants * move away from export enums for ts backcompat * add qps EventDocument,fix dependency duration * use envelope type in qps telemetryprocessor * send avg execution duration instead of last dur * add error logging for qps endpoint * add telemetry types, move qps config to qpssender * fix keying type issue in TelemetryType * fix wrong telemetrytype in qps conversion (#500) * bump version to 1.3.0 (#501) * bump version to 1.3.0 * add setSendLiveMetrics to README * add native metrics subscriber * mark native members of NativePerformance as static * remove segfault lib * update package.json * native: add throw tests * add refs to metrics * add config for enabling metrics, add heap metrics * report stdDev stats * rename custom metric names * fix native perf test * only run tests for node4+ * use node-pre-gyp native metrics * optionaldevdep for travis tests * simulate optionalDevDependency in travis * remove duped travis npm install * gate native metrics to node6+ * refactor extended metrics enablement * move extended metrics arg parsing to API * add info logging for when native module is loaded * docs: add extended metrics * add extended metrics build stage * use env var for travis extended metrics tests * test: modify test for non-installed case * tweak travis.yml * readme: comment out docs
2019-05-29 02:49:45 +03:00
var native = new AutoCollectNativePerformance(client);
var disposeSpy = sinon.spy(AutoCollectNativePerformance.INSTANCE, "dispose");
assert.ok(native);
assert.ok(disposeSpy.notCalled);
assert.doesNotThrow(() => {native = new AutoCollectNativePerformance(client)}, "NativePerformance can be constructed more than once");
assert.ok(disposeSpy.calledOnce, "dispose is called when second instance is constructed");
});
it("Calling enable multiple times shoud not create multiple timers", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var native = new AutoCollectNativePerformance(client);
var setIntervalSpy = sinon.spy(global, "setInterval");
assert.ok(native);
assert.doesNotThrow(() => native.enable(true), "Does not throw when tryinig to enable");
assert.doesNotThrow(() => native.enable(true), "Does not throw when trying to enable");
assert.equal(setIntervalSpy.callCount, 1, "setInterval should be singleton");
setIntervalSpy.restore();
});
add node.js native metrics (#508) * release 1.3.0 (#502) * add live metrics (#494) * add QPS files; enablement not included * add tests; add qps ping retry walkback * add off by default config option for QPS * add qps test to ensure off by default * only construct qps when enabled * add qps Sender constants * add QuickPulseEnvelopeFactory * fix ts timer compile * add qps exception and trace error handling * refactor default live metrics test * make qps hostname configurable * fix node0 setTimeout error * add qps telemetrytype constants * move away from export enums for ts backcompat * add qps EventDocument,fix dependency duration * use envelope type in qps telemetryprocessor * send avg execution duration instead of last dur * add error logging for qps endpoint * add telemetry types, move qps config to qpssender * fix keying type issue in TelemetryType * fix wrong telemetrytype in qps conversion (#500) * bump version to 1.3.0 (#501) * bump version to 1.3.0 * add setSendLiveMetrics to README * add native metrics subscriber * mark native members of NativePerformance as static * remove segfault lib * update package.json * native: add throw tests * add refs to metrics * add config for enabling metrics, add heap metrics * report stdDev stats * rename custom metric names * fix native perf test * only run tests for node4+ * use node-pre-gyp native metrics * optionaldevdep for travis tests * simulate optionalDevDependency in travis * remove duped travis npm install * gate native metrics to node6+ * refactor extended metrics enablement * move extended metrics arg parsing to API * add info logging for when native module is loaded * docs: add extended metrics * add extended metrics build stage * use env var for travis extended metrics tests * test: modify test for non-installed case * tweak travis.yml * readme: comment out docs
2019-05-29 02:49:45 +03:00
it("Calling enable when metrics are not available should fail gracefully", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
add node.js native metrics (#508) * release 1.3.0 (#502) * add live metrics (#494) * add QPS files; enablement not included * add tests; add qps ping retry walkback * add off by default config option for QPS * add qps test to ensure off by default * only construct qps when enabled * add qps Sender constants * add QuickPulseEnvelopeFactory * fix ts timer compile * add qps exception and trace error handling * refactor default live metrics test * make qps hostname configurable * fix node0 setTimeout error * add qps telemetrytype constants * move away from export enums for ts backcompat * add qps EventDocument,fix dependency duration * use envelope type in qps telemetryprocessor * send avg execution duration instead of last dur * add error logging for qps endpoint * add telemetry types, move qps config to qpssender * fix keying type issue in TelemetryType * fix wrong telemetrytype in qps conversion (#500) * bump version to 1.3.0 (#501) * bump version to 1.3.0 * add setSendLiveMetrics to README * add native metrics subscriber * mark native members of NativePerformance as static * remove segfault lib * update package.json * native: add throw tests * add refs to metrics * add config for enabling metrics, add heap metrics * report stdDev stats * rename custom metric names * fix native perf test * only run tests for node4+ * use node-pre-gyp native metrics * optionaldevdep for travis tests * simulate optionalDevDependency in travis * remove duped travis npm install * gate native metrics to node6+ * refactor extended metrics enablement * move extended metrics arg parsing to API * add info logging for when native module is loaded * docs: add extended metrics * add extended metrics build stage * use env var for travis extended metrics tests * test: modify test for non-installed case * tweak travis.yml * readme: comment out docs
2019-05-29 02:49:45 +03:00
var native = new AutoCollectNativePerformance(client);
AutoCollectNativePerformance["_metricsAvailable"] = false;
assert.ok(!(<any>native)["_emitter"]);
assert.doesNotThrow(() => native.enable(true), "Does not throw when native metrics are not available and trying to enable");
assert.doesNotThrow(() => native.enable(false), "Does not throw when native metrics are not available and trying to disable");
});
});
describe("#_parseEnabled", () =>{
it("should return equal input arg if no env vars are set", () => {
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(true), {isEnabled: true, disabledMetrics: {}});
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(false), {isEnabled: false, disabledMetrics: {}});
const config = {gc: true, heap: true};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(config), {isEnabled: true, disabledMetrics: config});
});
it("should overwrite input arg if disable all extended metrics env var is set", () => {
const env = <{[id: string]: string}>{};
const originalEnv = process.env;
env[Config.ENV_nativeMetricsDisableAll] = "set";
process.env = env;
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(true), {isEnabled: false, disabledMetrics: {}});
assert.deepEqual(AutoCollectNativePerformance.parseEnabled({}), {isEnabled: false, disabledMetrics: {}});
assert.deepEqual(AutoCollectNativePerformance.parseEnabled({gc: true}), {isEnabled: false, disabledMetrics: {}});
process.env = originalEnv;
});
it("should overwrite input arg if individual env vars are set", () => {
const expectation = {gc: true, heap: true};
const env = <{[id: string]: string}>{};
const originalEnv = process.env;
env[Config.ENV_nativeMetricsDisablers] = "gc,heap";
process.env = env;
let inConfig;
inConfig = false;
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig), {isEnabled: false, disabledMetrics: expectation});
inConfig = true;
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig), {isEnabled: true, disabledMetrics: expectation});
inConfig = {};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig), {isEnabled: true, disabledMetrics: expectation});
inConfig = {gc: true};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig), {isEnabled: true, disabledMetrics: expectation});
inConfig = {loop: true};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig), {isEnabled: true, disabledMetrics: {...inConfig, ...expectation}});
inConfig = {gc: false, loop: true, heap: 'abc', something: 'else'};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(<any>inConfig), {isEnabled: true, disabledMetrics: {...inConfig, ...expectation}});
process.env = originalEnv;
});
});
}
});