Skip to content

Commit 78d6b0c

Browse files
authored
hash error messages with sha1 (#1067)
1 parent dcad220 commit 78d6b0c

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

src/daemon/serverLog/logWatcher.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,21 @@ export class LogWatcher {
5353
if (Date.now() - this.logProcessedTimestamp < 1000) { return; } // reduce frequency of log file I/O.
5454
const logs = await logsForLatestSession(e.fsPath);
5555
const errors = collectErrorsSince(logs, this.logProcessedTimestamp);
56-
const consentToCollectLogs = vscode.workspace.getConfiguration("java").get<boolean>("help.collectErrorLog");
56+
const consentToCollectLogs = vscode.workspace.getConfiguration("java").get<boolean>("help.collectErrorLog") ?? false;
5757
if (errors) {
5858
errors.forEach(e => {
59-
if (consentToCollectLogs) {
60-
sendInfo("", {
61-
name: "jdtls-error",
62-
error: e.message,
63-
stack: e.stack!,
64-
timestamp: e.timestamp!.toString()
65-
});
66-
} else {
67-
const {message, tags} = redact(e.message);
68-
sendInfo("", {
69-
name: "jdtls-error",
70-
error: message,
71-
tags: tags.join(","),
72-
timestamp: e.timestamp!.toString()
73-
})
59+
const {message, tags, hash} = redact(e.message, consentToCollectLogs);
60+
const infoBody: {[key: string]: any} = {
61+
name: "jdtls-error",
62+
error: message,
63+
tags: tags.join(","),
64+
hash: hash,
65+
timestamp: e.timestamp!.toString()
66+
};
67+
if (consentToCollectLogs && e.stack) {
68+
infoBody.stack = e.stack;
7469
}
70+
sendInfo("", infoBody);
7571
})
7672
}
7773
this.logProcessedTimestamp = Date.now();
@@ -111,26 +107,21 @@ export class LogWatcher {
111107
if (this.serverLogUri) {
112108
const logs = await logsForLatestSession(path.join(this.serverLogUri?.fsPath, ".log"));
113109
const errors = collectErrors(logs);
114-
const consentToCollectLogs = vscode.workspace.getConfiguration("java").get<boolean>("help.collectErrorLog");
110+
const consentToCollectLogs = vscode.workspace.getConfiguration("java").get<boolean>("help.collectErrorLog") ?? false;
115111
if (errors) {
116112
errors.forEach(e => {
117-
if (consentToCollectLogs) {
118-
sendInfo("", {
119-
name: "jdtls-error-in-crashed-session",
120-
error: e.message,
121-
stack: e.stack!,
122-
timestamp: e.timestamp!.toString()
123-
})
124-
} else {
125-
const { message, tags } = redact(e.message);
126-
sendInfo("", {
127-
name: "jdtls-error-in-crashed-session",
128-
error: message,
129-
tags: tags.join(","),
130-
timestamp: e.timestamp!.toString()
131-
})
113+
const {message, tags, hash} = redact(e.message, consentToCollectLogs);
114+
const infoBody: {[key: string]: any} = {
115+
name: "jdtls-error-in-crashed-session",
116+
error: message,
117+
tags: tags.join(","),
118+
hash: hash,
119+
timestamp: e.timestamp!.toString()
120+
};
121+
if (consentToCollectLogs && e.stack) {
122+
infoBody.stack = e.stack;
132123
}
133-
124+
sendInfo("", infoBody);
134125
})
135126
}
136127
}

src/daemon/serverLog/whitelist.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
3+
import { createHash } from "crypto";
34

45
const TAGS: string[] = [
56
"buildship",
@@ -42,15 +43,25 @@ const MESSAGE_WHITELIST: string[] = [
4243
"Workspace restored, but some problems occurred.",
4344
];
4445

45-
export function redact(rawMessage: string): {
46+
export function redact(rawMessage: string, consentToCollectLogs: boolean): {
4647
message: string;
4748
tags: string[];
49+
hash: string;
4850
} {
49-
const message = MESSAGE_WHITELIST.find(msg => rawMessage.includes(msg)) ?? "";
51+
const matchedMessage = MESSAGE_WHITELIST.find(msg => rawMessage.includes(msg));
52+
const message = matchedMessage ?? (consentToCollectLogs ? rawMessage : "");
53+
const hash = sha1(matchedMessage ?? rawMessage);
5054
const tags = TAGS.filter(tag => rawMessage.toLocaleLowerCase().includes(tag));
5155

5256
return {
5357
message,
54-
tags
58+
tags,
59+
hash
5560
}
61+
}
62+
63+
function sha1(content: string): string {
64+
const hash = createHash("sha1");
65+
hash.update(content);
66+
return hash.digest('hex');
5667
}

0 commit comments

Comments
 (0)