Skip to content

Commit 30169a2

Browse files
authored
refactor(codewhisperer): recommendations logging #3888
Problem: codewhisperer "recommendations" logging uses multiple logger calls and the formatting is hard to read. Solution: - Use a single log statement. - Indent the message parts so that related parts are visually grouped. Before: 2023-09-30 08:57:15 [DEBUG]: Request ID: ecc997a9-518a-47ae-a28b-ebbcd0aba39a, timestamp(epoch): 1696089435380, timezone: America/Los_Angeles, datetime: 30/09/2023, 08:57:15, vscode version: '1.82.2', extension version: '1.92.0', filename: 'samCliInvokerUtils.test.ts', left context of line: '... SAM_CLI_TELEMETRY', line number: 89, character location: 34, latency: 678.2888330221176 ms. 2023-09-30 08:57:15 [VERBOSE]: Recommendations: 2023-09-30 08:57:15 [VERBOSE]: [0] telemetryEnabled ? '1' : 'x', AWS_REGION: 'us-east-1', ...(telemetryEnabled ? {} : { SAM_CLI_TELEMETRY: 'x' }) } 2023-09-30 08:57:15 [VERBOSE]: [1] telemetryEnabled ? '1' : '0', AWS_REGION: 'us-east-1', ...(telemetryEnabled ? {} : { SAM_CLI_TELEMETRY: 'x' }) } After: 2023-10-11 11:57:30 [DEBUG]: Request ID: 90c8184d-f67d-463a-a690-629dc2ffb8cb, timestamp(epoch): 1697050650338, timezone: America/Los_Angeles, datetime: 11/10/2023, 11:57:30, vscode version: '1.83.0', extension version: 'testPluginVersion', filename: 'Function.cs', left context of line: ' ', line number: 23, character location: 12, latency: 786.2914998531342 ms. Recommendations: 00: client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client"); 01: client.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client"); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
1 parent fffe6c4 commit 30169a2

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/codewhisperer/service/recommendationHandler.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { AuthUtil } from '../util/authUtil'
3838
import { CodeWhispererUserGroupSettings } from '../util/userGroupUtil'
3939
import { CWInlineCompletionItemProvider } from './inlineCompletionItemProvider'
4040
import { application } from '../util/codeWhispererApplication'
41+
import { indent } from '../../shared/utilities/textUtilities'
4142

4243
/**
4344
* This class is for getRecommendation/listRecommendation API calls and its states
@@ -270,8 +271,9 @@ export class RecommendationHandler {
270271
}
271272
} finally {
272273
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
273-
getLogger().debug(
274-
`Request ID: ${requestId},
274+
275+
let msg = indent(
276+
`codewhisperer: request-id: ${requestId},
275277
timestamp(epoch): ${Date.now()},
276278
timezone: ${timezone},
277279
datetime: ${new Date().toLocaleString([], { timeZone: timezone })},
@@ -281,12 +283,16 @@ export class RecommendationHandler {
281283
left context of line: '${session.leftContextOfCurrentLine}',
282284
line number: ${session.startPos.line},
283285
character location: ${session.startPos.character},
284-
latency: ${latency} ms.`
285-
)
286-
getLogger().verbose('Recommendations:')
286+
latency: ${latency} ms.
287+
Recommendations:`,
288+
4,
289+
true
290+
).trimStart()
287291
recommendations.forEach((item, index) => {
288-
getLogger().verbose(`[${index}]\n${item.content.trimRight()}`)
292+
msg += `\n ${index.toString().padStart(2, '0')}: ${indent(item.content, 8, true).trim()}`
289293
})
294+
getLogger().debug(msg)
295+
290296
if (invocationResult === 'Succeeded') {
291297
CodeWhispererCodeCoverageTracker.getTracker(session.language)?.incrementServiceInvocationCount()
292298
}

src/shared/utilities/textUtilities.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ export function truncate(s: string, n: number, suffix?: string): string {
3030
return n < 0 ? suffix + truncated : truncated + suffix
3131
}
3232

33+
/**
34+
* Indents a string.
35+
*
36+
* @param size Indent width (number of space chars).
37+
* @param clear Clear existing whitespace, if any.
38+
* @param s Text to indent.
39+
*/
40+
export function indent(s: string, size: number = 4, clear: boolean = false): string {
41+
const n = Math.abs(size)
42+
const spaces = ''.padEnd(n, ' ')
43+
if (size < 0) {
44+
throw Error() // TODO: implement "dedent" for negative size.
45+
}
46+
if (clear) {
47+
return s.replace(/^[ \t]*([^\n])/, `${spaces}$1`).replace(/(\n+)[ \t]*([^ \t\n])/g, `$1${spaces}$2`)
48+
}
49+
return spaces + s.replace(/(\n+)(.)/g, `$1${spaces}$2`)
50+
}
51+
3352
/**
3453
* Creates a (shallow) clone of `obj` and truncates its top-level string properties.
3554
*

src/test/shared/utilities/textUtilities.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
removeAnsi,
1111
truncate,
1212
truncateProps,
13+
indent,
1314
} from '../../../shared/utilities/textUtilities'
1415

1516
describe('textUtilities', async function () {
@@ -59,6 +60,14 @@ describe('textUtilities', async function () {
5960
assert.deepStrictEqual(truncate('abc 123', 99), 'abc 123')
6061
assert.deepStrictEqual(truncate('abc 123', -99), 'abc 123')
6162
})
63+
64+
it('indent()', async function () {
65+
assert.deepStrictEqual(indent('abc\n123', 2, false), ' abc\n 123')
66+
assert.deepStrictEqual(indent('abc\n 123\n', 2, false), ' abc\n 123\n')
67+
assert.deepStrictEqual(indent('abc\n 123\n', 2, true), ' abc\n 123\n')
68+
assert.deepStrictEqual(indent(' abc\n\n \n123\nfoo\n', 4, false), ' abc\n\n \n 123\n foo\n')
69+
assert.deepStrictEqual(indent(' abc\n\n \n123\nfoo\n', 4, true), ' abc\n\n \n 123\n foo\n')
70+
})
6271
})
6372

6473
describe('removeAnsi', async function () {

0 commit comments

Comments
 (0)