Skip to content

Commit

Permalink
fix: sanitizing exception strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Nov 17, 2021
1 parent 2bcb2ed commit b12c52c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/appInsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export function getCpus(): string {

const homeDir = os.homedir();

const sanitizeError = (err: Error): Error => {
if (err.name) {
err.name = err.name.replace(homeDir, '~');
}
if (err.message) {
err.message = err.message.replace(homeDir, '~');
}
if (err.stack) {
// there might be lots of this one
err.stack = err.stack.replace(new RegExp(`\\b${homeDir}\\b`, 'gi'), '~');
}
return err;
};
function getSystemMemory(): string {
return `${(os.totalmem() / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}
Expand Down Expand Up @@ -136,9 +149,10 @@ export class AppInsights extends AsyncCreatable<TelemetryOptions> {
* @param attributes {Attributes} - map of measurements to publish alongside the exception.
*/
public sendTelemetryException(exception: Error, attributes: Attributes = {}): void {
this.logger.debug(`Sending telemetry exception: ${exception.message}`);
const cleanException = sanitizeError(exception);
this.logger.debug(`Sending telemetry exception: ${cleanException.message}`);
const { properties, measurements } = buildPropertiesAndMeasurements(attributes);
this.appInsightsClient.trackException({ exception, properties, measurements });
this.appInsightsClient.trackException({ exception: cleanException, properties, measurements });
}

/**
Expand Down

0 comments on commit b12c52c

Please sign in to comment.