Skip to content

Commit 31fd2b9

Browse files
committed
add tests for telemetry
1 parent 6572e6b commit 31fd2b9

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

packages/core/src/shared/telemetry/vscodeTelemetry.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"types": [
3+
{
4+
"name": "size",
5+
"type": "int",
6+
"description": "A generic size for when units are clear"
7+
},
38
{
49
"name": "amazonGenerateApproachLatency",
510
"type": "double",
@@ -372,6 +377,16 @@
372377
}
373378
]
374379
},
380+
{
381+
"name": "ide_logActiveProcesses",
382+
"description": "Emitted when user invokes logActiveProcesses command",
383+
"metadata": [
384+
{
385+
"type": "size",
386+
"required": true
387+
}
388+
]
389+
},
375390
{
376391
"name": "vscode_executeCommand",
377392
"description": "Emitted whenever a registered Toolkit command is executed",

packages/core/src/shared/utilities/processUtils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as logger from '../logger'
88
import { Timeout, CancellationError, waitUntil } from './timeoutUtils'
99
import { PollingSet } from './pollingSet'
1010
import { getLogger } from '../logger/logger'
11+
import { telemetry } from '../telemetry'
1112

1213
export interface RunParameterContext {
1314
/** Reports an error parsed from the stdin/stdout streams. */
@@ -161,15 +162,18 @@ export class ChildProcessTracker {
161162
}
162163

163164
public async logAllUsage(): Promise<void> {
164-
if (this.size === 0) {
165-
this.logger.info('No Active Subprocesses')
166-
return
167-
}
168-
const usage = await this.getAllUsage()
169-
const logMsg = Array.from(usage.entries()).reduce((acc, [pid, stats]) => {
170-
return acc + `Process ${pid}: ${stats.cpu}% cpu, ${stats.memory} MB of memory\n`
171-
}, '')
172-
this.logger.info(`Active Subprocesses (${this.size} active)\n${logMsg}`)
165+
telemetry.ide_logActiveProcesses.run(async (span) => {
166+
span.record({ size: this.size })
167+
if (this.size === 0) {
168+
this.logger.info('No Active Subprocesses')
169+
return
170+
}
171+
const usage = await this.getAllUsage()
172+
const logMsg = Array.from(usage.entries()).reduce((acc, [pid, stats]) => {
173+
return acc + `Process ${pid}: ${stats.cpu}% cpu, ${stats.memory} MB of memory\n`
174+
}, '')
175+
this.logger.info(`Active Subprocesses (${this.size} active)\n${logMsg}`)
176+
})
173177
}
174178

175179
public async getUsage(pid: pid): Promise<ProcessStats> {

packages/core/src/test/shared/utilities/processUtils.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { sleep } from '../../../shared/utilities/timeoutUtils'
1919
import { Timeout, waitUntil } from '../../../shared/utilities/timeoutUtils'
2020
import { fs } from '../../../shared'
2121
import * as FakeTimers from '@sinonjs/fake-timers'
22-
import { installFakeClock } from '../../testUtil'
22+
import { assertTelemetry, installFakeClock } from '../../testUtil'
2323
import { isWin } from '../../../shared/vscode/env'
2424
import { assertLogsContain } from '../../globalSetup.test'
2525

@@ -537,4 +537,20 @@ describe('ChildProcessTracker', function () {
537537
await tracker.logAllUsage()
538538
assertLogsContain('No Active Subprocesses', false, 'info')
539539
})
540+
541+
it('logAllUsage emits telemetry with size equal to number of processes (empty)', async function () {
542+
await tracker.logAllUsage()
543+
assertTelemetry('ide_logActiveProcesses', { size: 0 })
544+
})
545+
546+
it('logsAllUsage emits telemetry to number of processes (nonempty)', async function () {
547+
const size = 10
548+
for (const _ of Array.from({ length: size })) {
549+
const runningProcess = startSleepProcess()
550+
tracker.add(runningProcess.childProcess)
551+
}
552+
553+
await tracker.logAllUsage()
554+
assertTelemetry('ide_logActiveProcesses', { size: size })
555+
})
540556
})

0 commit comments

Comments
 (0)