TelemetryClient keeps handles open
See original GitHub issueWhen running some code in a test using the TelemetryClient
, Jest detects open handles.
Here’s a basic replication which causes the issue.
describe('telemetry', () => {
// Requires the APPINSIGHTS_INSTRUMENTATIONKEY environment variable to be set
test('replicate', async () => {
const telemetryClient = new TelemetryClient()
telemetryClient.trackMetric({ name: 'some_metric', value: 2 })
// Wait for all logs to flush
await new Promise((resolve) => {
telemetryClient.flush({ callback: resolve })
})
})
})
If run using the --detectOpenHandles
flag, the following error will be thrown.
Jest has detected the following 3 open handles potentially keeping Jest from exiting:
● TCPWRAP
42 | // Requires the APPINSIGHTS_INSTRUMENTATIONKEY environment variable to be set
43 | test('replicate', async () => {
> 44 | const telemetryClient = new TelemetryClient()
| ^
45 |
46 | telemetryClient.trackMetric({ name: 'some_metric', value: 2 })
47 |
at Function.Object.<anonymous>.Util.makeRequest (../../node_modules/applicationinsights/Library/Util.ts:371:25)
at Function.Object.<anonymous>.AzureVirtualMachine.getAzureComputeMetadata (../../node_modules/applicationinsights/Library/AzureVirtualMachine.ts:34:26)
at ../../node_modules/applicationinsights/AutoCollection/Statsbeat.ts:332:44
at Statsbeat.Object.<anonymous>.Statsbeat._getResourceProvider (../../node_modules/applicationinsights/AutoCollection/Statsbeat.ts:313:16)
at Statsbeat.<anonymous> (../../node_modules/applicationinsights/AutoCollection/Statsbeat.ts:183:24)
at step (../../node_modules/applicationinsights/out/AutoCollection/Statsbeat.js:33:23)
at Object.next (../../node_modules/applicationinsights/out/AutoCollection/Statsbeat.js:14:53)
at ../../node_modules/applicationinsights/out/AutoCollection/Statsbeat.js:8:71
at Object.<anonymous>.__awaiter (../../node_modules/applicationinsights/out/AutoCollection/Statsbeat.js:4:12)
at Statsbeat.Object.<anonymous>.Statsbeat.trackLongIntervalStatsbeats (../../node_modules/applicationinsights/out/AutoCollection/Statsbeat.js:190:16)
at Statsbeat.Object.<anonymous>.Statsbeat.enable (../../node_modules/applicationinsights/AutoCollection/Statsbeat.ts:75:22)
at NodeClient.TelemetryClient (../../node_modules/applicationinsights/Library/TelemetryClient.ts:50:29)
at new NodeClient (../../node_modules/applicationinsights/out/Library/NodeClient.js:28:42)
at Object.<anonymous> (tests/logging.test.ts:44:29)
● TLSWRAP
42 | // Requires the APPINSIGHTS_INSTRUMENTATIONKEY environment variable to be set
43 | test('replicate', async () => {
> 44 | const telemetryClient = new TelemetryClient()
| ^
45 |
46 | telemetryClient.trackMetric({ name: 'some_metric', value: 2 })
47 |
at Function.Object.<anonymous>.CorrelationIdManager.queryCorrelationId (../../node_modules/applicationinsights/Library/CorrelationIdManager.ts:99:9)
at Config.set (../../node_modules/applicationinsights/Library/Config.ts:115:30)
at new Config (../../node_modules/applicationinsights/Library/Config.ts:103:34)
at NodeClient.TelemetryClient (../../node_modules/applicationinsights/Library/TelemetryClient.ts:43:22)
at new NodeClient (../../node_modules/applicationinsights/out/Library/NodeClient.js:28:42)
at Object.<anonymous> (tests/logging.test.ts:44:29)
● TLSWRAP
42 | // Requires the APPINSIGHTS_INSTRUMENTATIONKEY environment variable to be set
43 | test('replicate', async () => {
> 44 | const telemetryClient = new TelemetryClient()
| ^
45 |
46 | telemetryClient.trackMetric({ name: 'some_metric', value: 2 })
47 |
at Function.Object.<anonymous>.CorrelationIdManager.queryCorrelationId (../../node_modules/applicationinsights/Library/CorrelationIdManager.ts:99:9)
at Config.set (../../node_modules/applicationinsights/Library/Config.ts:115:30)
at new Config (../../node_modules/applicationinsights/Library/Config.ts:103:34)
at new Statsbeat (../../node_modules/applicationinsights/AutoCollection/Statsbeat.ts:55:33)
at NodeClient.TelemetryClient (../../node_modules/applicationinsights/Library/TelemetryClient.ts:49:31)
at new NodeClient (../../node_modules/applicationinsights/out/Library/NodeClient.js:28:42)
at Object.<anonymous> (tests/logging.test.ts:44:29)
I assumed this had to do with the use of a https.Agent
internally and tried to specify one without any keep alive, but that didn’t help.
const telemetryClient = new TelemetryClient()
telemetryClient.config.httpsAgent = new https.Agent({ keepAlive: false })
I also tried to call the dispose
function from the package, but as suspected that didn’t help. I assume it’s only applicable when using the default client and appInsights.setup()
.
Is there any way to cleanly dispose of a TelemetryClient
?
Issue Analytics
- State:
- Created a year ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Azure cloud service AI TelemetryClient not working
Right click “WebRole1”, select “Properties”, then the SMUI opens. Check the “Send diagnostics data to Application Insights”, and click “Configure Application ...
Read more >Monitor your Node.js services and apps with Application Insights
TelemetryClient () . This practice lets you keep connection strings out of committed source code, and you can specify different connection ...
Read more >Azure Application Insight Custom Telemetry Failures
... to diagnose and handle the failure of dispatching the event: ... and open GitHub issue at How do you correctly get TelemetryClient...
Read more >2 Monitoring non-web apps using Azure Application Insights ...
This is done using the line telemetryClient.TrackTrace($"The http call returned '{result}'");. In case of an exception we are able to handle we ...
Read more >Amazing custom metrics using Azure Application Insights
It was of course inspired by the way StackOverflow handles their ... the web nodes get a copy and it holds on to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We could add a shutdown functionality in the TelemeryClient that actually wait for these requests, allowing you to wait for it on your tests, let me know if this will work for everyone, @alexg-axis, @jonthysell
I was able to mitigate this by wrapping appInsights initialization in an env check, and then exporting the defaultClient from the module as TelemetryClient | undefined.
Setting all the options for automatic dependency collection etc to false didn’t prevent the handle being opened.
I was hoping appInsights.dispose() would have cleaned up sufficiently.