diff --git a/src/appInsights.ts b/src/appInsights.ts index 3443b8c..0d1ba92 100644 --- a/src/appInsights.ts +++ b/src/appInsights.ts @@ -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`; } @@ -136,9 +149,10 @@ export class AppInsights extends AsyncCreatable { * @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 }); } /**