Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up CallstackOrException telemetry in typescript-language-features #96108

Merged
merged 1 commit into from Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 27 additions & 25 deletions extensions/typescript-language-features/src/tsServer/serverError.ts
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import type * as Proto from '../protocol';
import { escapeRegExp } from '../utils/regexp';
import { TypeScriptVersion } from '../utils/versionProvider';


Expand All @@ -14,16 +13,17 @@ export class TypeScriptServerError extends Error {
version: TypeScriptVersion,
response: Proto.Response
): TypeScriptServerError {
const parsedResult = TypeScriptServerError.parseErrorText(version, response);
return new TypeScriptServerError(serverId, version, response, parsedResult?.message, parsedResult?.stack);
const parsedResult = TypeScriptServerError.parseErrorText(response);
return new TypeScriptServerError(serverId, version, response, parsedResult?.message, parsedResult?.stack, parsedResult?.sanitizedStack);
}

private constructor(
serverId: string,
public readonly version: TypeScriptVersion,
private readonly response: Proto.Response,
public readonly serverMessage: string | undefined,
public readonly serverStack: string | undefined
public readonly serverStack: string | undefined,
private readonly sanitizedStack: string | undefined
) {
super(`<${serverId}> TypeScript Server Error (${version.displayName})\n${serverMessage}\n${serverStack}`);
}
Expand All @@ -33,47 +33,38 @@ export class TypeScriptServerError extends Error {
public get serverCommand() { return this.response.command; }

public get telemetry() {
// The "sanitizedstack" has been purged of error messages, paths, and file names (other than tsserver)
// and, thus, can be classified as SystemMetaData, rather than CallstackOrException.
/* __GDPR__FRAGMENT__
"TypeScriptRequestErrorProperties" : {
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"message" : { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe dropping these causes them to be on the server side?

"stack" : { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
"errortext" : { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" }
"sanitizedstack" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" },
}
*/
return {
command: this.serverCommand,
message: this.serverMessage || '',
stack: this.serverStack || '',
errortext: this.serverErrorText || '',
sanitizedstack: this.sanitizedStack || '',
} as const;
}

/**
* Given a `errorText` from a tsserver request indicating failure in handling a request,
* prepares a payload for telemetry-logging.
*/
private static parseErrorText(version: TypeScriptVersion, response: Proto.Response) {
private static parseErrorText(response: Proto.Response) {
const errorText = response.message;
if (errorText) {
const errorPrefix = 'Error processing request. ';
if (errorText.startsWith(errorPrefix)) {
let prefixFreeErrorText = errorText.substr(errorPrefix.length);

// Prior to https://github.com/microsoft/TypeScript/pull/32785, this error
// returned and excessively long and detailed list of paths. Since server-side
// filtering doesn't have sufficient granularity to drop these specific
// messages, we sanitize them here.
if (prefixFreeErrorText.indexOf('Could not find sourceFile') >= 0) {
prefixFreeErrorText = prefixFreeErrorText.replace(/ in \[[^\]]*\]/g, '');
}

const prefixFreeErrorText = errorText.substr(errorPrefix.length);
const newlineIndex = prefixFreeErrorText.indexOf('\n');
if (newlineIndex >= 0) {
// Newline expected between message and stack.
const stack = prefixFreeErrorText.substring(newlineIndex + 1);
return {
message: prefixFreeErrorText.substring(0, newlineIndex),
stack: TypeScriptServerError.normalizeMessageStack(version, prefixFreeErrorText.substring(newlineIndex + 1))
stack,
sanitizedStack: TypeScriptServerError.sanitizeStack(stack)
};
}
}
Expand All @@ -82,12 +73,23 @@ export class TypeScriptServerError extends Error {
}

/**
* Try to replace full TS Server paths with 'tsserver.js' so that we don't have to post process the data as much
* Drop everything but ".js" and line/column numbers (though retain "tsserver" if that's the filename).
*/
private static normalizeMessageStack(version: TypeScriptVersion, message: string | undefined) {
private static sanitizeStack(message: string | undefined) {
if (!message) {
return '';
}
return message.replace(new RegExp(`${escapeRegExp(version.path)}[/\\\\]tsserver.js:`, 'gi'), 'tsserver.js:');
const regex = /(tsserver)?(\.(?:ts|tsx|js|jsx)(?::\d+(?::\d+))?)\)?$/igm;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This moves us from an exclusion list to an inclusion list, so it should be impossible for customer data to slip through.

let serverStack = '';
while (true) {
const match = regex.exec(message);
if (!match) {
break;
}
// [1] is 'tsserver' or undefined
// [2] is '.js:{line_number}:{column_number}'
serverStack += `${match[1] || 'suppressed'}${match[2]}\n`;
}
return serverStack;
}
}
Expand Up @@ -391,10 +391,12 @@ export default class TypeScriptServiceClient extends Disposable implements IType
if (code === null || typeof code === 'undefined') {
this.info('TSServer exited');
} else {
// In practice, the exit code is an integer with no ties to any identity,
// so it can be classified as SystemMetaData, rather than CallstackOrException.
this.error(`TSServer exited with code: ${code}`);
/* __GDPR__
"tsserver.exitWithCode" : {
"code" : { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
"code" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" },
"${include}": [
"${TypeScriptCommonProperties}"
]
Expand Down