Skip to content

Commit c6c07a9

Browse files
committed
perf: log CodeCatalyst API request times
1 parent 44186f3 commit c6c07a9

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/shared/clients/codecatalystClient.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const localize = nls.loadMessageBundle()
1010

1111
import * as AWS from 'aws-sdk'
1212
import * as logger from '../logger/logger'
13+
import { PerfLog } from '../logger/logger'
1314
import { ServiceConfigurationOptions } from 'aws-sdk/lib/service'
1415
import { CancellationError, Timeout, waitTimeout, waitUntil } from '../utilities/timeoutUtils'
1516
import { isUserCancelledError } from '../../shared/errors'
@@ -28,6 +29,7 @@ import {
2829
ListSourceRepositoriesItem,
2930
ListSourceRepositoriesItems,
3031
} from 'aws-sdk/clients/codecatalyst'
32+
import { truncate } from '../utilities/textUtilities'
3133

3234
interface CodeCatalystConfig {
3335
readonly region: string
@@ -223,10 +225,12 @@ class CodeCatalystClientInternal {
223225
const log = this.log
224226
const bearerToken = (await this.connection.getToken()).accessToken
225227
req.httpRequest.headers['Authorization'] = `Bearer ${bearerToken}`
228+
const perflog = new PerfLog('API request')
226229

227230
return new Promise<T>((resolve, reject) => {
228231
req.send(function (e, data) {
229232
const r = req as any
233+
const timecost = perflog.elapsed().toFixed(1)
230234
if (e) {
231235
if (e.code === 'AccessDeniedException' || e.statusCode === 401) {
232236
CodeCatalystClientInternal.identityCache.delete(bearerToken)
@@ -260,14 +264,15 @@ class CodeCatalystClientInternal {
260264

261265
if (r.operation || r.params) {
262266
log.error(
263-
'API request failed: %s\nparams: %O\nerror: %O\nheaders: %O',
267+
'API request failed (time: %dms): %s\nparams: %O\nerror: %O\nheaders: %O',
268+
timecost,
264269
r.operation,
265270
r.params,
266271
errNoStack,
267272
logHeaders
268273
)
269274
} else {
270-
log.error('API request failed:%O\nheaders: %O', req, logHeaders)
275+
log.error('API request failed (time: %dms):%O\nheaders: %O', timecost, req, logHeaders)
271276
}
272277
if (silent) {
273278
if (defaultVal === undefined) {
@@ -279,7 +284,22 @@ class CodeCatalystClientInternal {
279284
}
280285
return
281286
}
282-
log.verbose('API request (%s):\nparams: %O\nresponse: %O', r.operation ?? '?', r.params ?? '?', data)
287+
if (log.logLevelEnabled('verbose')) {
288+
const truncatedData = {
289+
...data,
290+
nextToken: (data as any)?.nextToken ?? '',
291+
}
292+
if (truncatedData.nextToken && typeof truncatedData.nextToken === 'string') {
293+
truncatedData.nextToken = truncate(truncatedData.nextToken, 20)
294+
}
295+
log.verbose(
296+
'API request (time: %dms): %s\nparams: %O\nresponse: %O',
297+
timecost,
298+
r.operation ?? '?',
299+
r.params ?? '?',
300+
truncatedData
301+
)
302+
}
283303
resolve(data)
284304
})
285305
})

src/shared/logger/logger.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { Uri } from 'vscode'
7+
import globals from '../extensionGlobals'
78

89
const toolkitLoggers: {
910
main: Logger | undefined
@@ -144,22 +145,24 @@ export function setLogger(logger: Logger | undefined, type?: 'channel' | 'debugC
144145
}
145146

146147
export class PerfLog {
147-
private log
148+
private readonly log
148149
public readonly start
149150

150151
public constructor(public readonly topic: string) {
151152
const log = getLogger()
152153
this.log = log
153-
if (log.logLevelEnabled('verbose')) {
154-
this.start = Date.now()
155-
}
154+
this.start = globals.clock.Date.now()
155+
}
156+
157+
public elapsed(): number {
158+
return globals.clock.Date.now() - this.start
156159
}
157160

158161
public done(): void {
159-
if (!this.start) {
162+
if (!this.log.logLevelEnabled('verbose')) {
160163
return
161164
}
162-
const elapsed = Date.now() - this.start
165+
const elapsed = this.elapsed()
163166
this.log.verbose('%s took %dms', this.topic, elapsed.toFixed(1))
164167
}
165168
}

0 commit comments

Comments
 (0)