Skip to content

Commit

Permalink
Fix issue with live metrics initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg committed Sep 26, 2023
1 parent 86a575f commit c018ab8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Tests/applicationInsights.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 9 additions & 3 deletions applicationinsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -505,7 +512,6 @@ export function dispose() {
}
if (liveMetricsClient) {
liveMetricsClient.enable(false);
defaultClient.config.enableSendLiveMetrics = false;
liveMetricsClient = undefined;
}
if (_azureFunctions) {
Expand Down

0 comments on commit c018ab8

Please sign in to comment.