From c018ab8458b19731e9d55e63f35d2425ae376978 Mon Sep 17 00:00:00 2001 From: Hector Date: Tue, 26 Sep 2023 16:10:34 -0700 Subject: [PATCH] Fix issue with live metrics initialization --- Tests/applicationInsights.tests.ts | 5 ++++- applicationinsights.ts | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Tests/applicationInsights.tests.ts b/Tests/applicationInsights.tests.ts index 7cd5cdd8c..be0a69b4c 100644 --- a/Tests/applicationInsights.tests.ts +++ b/Tests/applicationInsights.tests.ts @@ -77,7 +77,10 @@ describe("ApplicationInsights", () => { WebSnippet.INSTANCE= undefined; }); - afterEach(() => AppInsights.defaultClient = undefined); + afterEach(() => { + AppInsights.defaultClient = undefined; + AppInsights.liveMetricsClient = undefined; + }); it("should warn if start is called before setup", () => { var warnStub = sandbox.stub(console, "warn"); diff --git a/applicationinsights.ts b/applicationinsights.ts index 2aa133cfe..cec074c88 100644 --- a/applicationinsights.ts +++ b/applicationinsights.ts @@ -120,7 +120,7 @@ function _setDefaultConfig() { defaultClient.config.enableAutoDependencyCorrelation = true; } if (defaultClient.config.enableSendLiveMetrics == undefined) { - defaultClient.config.enableSendLiveMetrics = true; + defaultClient.config.enableSendLiveMetrics = false; } if (defaultClient.config.enableAutoCollectExtendedMetrics == undefined) { defaultClient.config.enableAutoCollectExtendedMetrics = true; @@ -154,7 +154,14 @@ export function start() { _serverRequests.enable(defaultClient.config.enableAutoCollectRequests); _clientRequests.enable(defaultClient.config.enableAutoCollectDependencies); _webSnippet.enable(defaultClient.config.enableWebInstrumentation, defaultClient.config.webInstrumentationConnectionString); - if (liveMetricsClient && defaultClient.config.enableSendLiveMetrics) { + if (defaultClient.config.enableSendLiveMetrics) { + if (!liveMetricsClient) { + // No qps client exists. Create one and prepare it to be enabled at .start() + liveMetricsClient = new QuickPulseClient(defaultClient.config, defaultClient.context, defaultClient.getAuthorizationHandler); + _performanceLiveMetrics = new AutoCollectPerformance(liveMetricsClient as any, 1000, true); + liveMetricsClient.addCollector(_performanceLiveMetrics); + defaultClient.quickPulseClient = liveMetricsClient; // Need this so we can forward all manual tracks to live metrics via PerformanceMetricsTelemetryProcessor + } liveMetricsClient.enable(defaultClient.config.enableSendLiveMetrics); } _azureFunctions.enable(defaultClient.config.enableAutoCollectIncomingRequestAzureFunctions); @@ -505,7 +512,6 @@ export function dispose() { } if (liveMetricsClient) { liveMetricsClient.enable(false); - defaultClient.config.enableSendLiveMetrics = false; liveMetricsClient = undefined; } if (_azureFunctions) {