Skip to content

Commit

Permalink
Enable autocollection of unhandled exceptions (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
millicentachieng authored May 24, 2021
1 parent 81a57ae commit 81b0bab
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/telemetry/error-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const NETWORK_ERROR = 'NETWORK_ERROR';
export const LINK_ERROR = 'LINK_ERROR';
export const COMPONENT_ERROR = 'COMPONENT_ERROR';
export const OPERATIONAL_ERROR = 'OPERATIONAL_ERROR';
export const UNHANDLED_ERROR = 'UNHANDLED_ERROR';
67 changes: 49 additions & 18 deletions src/telemetry/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ITelemetryItem } from '@microsoft/applicationinsights-web';
import { exception } from 'console';
import { errorTypes } from '.';
import {
DEVX_API_URL,
GRAPH_API_SANDBOX_URL,
Expand All @@ -23,26 +25,25 @@ export function filterTelemetryTypes(envelope: ITelemetryItem) {
}

export function filterRemoteDependencyData(envelope: ITelemetryItem): boolean {
const baseData = envelope.baseData || {};
if (envelope.baseType !== 'RemoteDependencyData') {
return true;
}
if (envelope.baseType === 'RemoteDependencyData') {
const baseData = envelope.baseData || {};
const urlObject = new URL(baseData.target || '');

const targetsToInclude = [GRAPH_URL, DEVX_API_URL, GRAPH_API_SANDBOX_URL];
const urlObject = new URL(baseData.target || '');
if (!targetsToInclude.includes(urlObject.origin)) {
return false;
}
const targetsToInclude = [GRAPH_URL, DEVX_API_URL, GRAPH_API_SANDBOX_URL];
if (!targetsToInclude.includes(urlObject.origin)) {
return false;
}

const target = baseData.target || '';
switch (urlObject.origin) {
case GRAPH_URL:
baseData.name = sanitizeQueryUrl(target);
break;
case GRAPH_API_SANDBOX_URL:
baseData.name = sanitizeGraphAPISandboxUrl(target);
default:
break;
const target = baseData.target || '';
switch (urlObject.origin) {
case GRAPH_URL:
baseData.name = sanitizeQueryUrl(target);
break;
case GRAPH_API_SANDBOX_URL:
baseData.name = sanitizeGraphAPISandboxUrl(target);
default:
break;
}
}
return true;
}
Expand Down Expand Up @@ -73,3 +74,33 @@ export function sanitizeTelemetryItemUriProperty(envelope: ITelemetryItem) {
}
return true;
}

export function sanitizeStackTrace(envelope: ITelemetryItem) {
if (envelope.baseType === 'ExceptionData') {
const telemetryItem = envelope.baseData || {};
telemetryItem.properties = telemetryItem.properties || {};

if (telemetryItem.exceptions && telemetryItem.exceptions.length > 0) {
const exception = telemetryItem.exceptions[0];
const parsedStack = exception.parsedStack[0];

// Only capture errors coming from our source code and not dependencies, to reduce noise
if (!parsedStack.fileName.startsWith('webpack-internal')) {
return false;
}

// Add properties for unhandled exceptions only
if (!telemetryItem.properties.ComponentName) {
telemetryItem.properties.ComponentName = parsedStack.assembly;
telemetryItem.properties.Message = exception.stack.split('\n')[0]; // Read first line only
exception.message = errorTypes.UNHANDLED_ERROR;
}

exception.hasFullStack = false;
exception.stack = null;
exception.parsedStack = [parsedStack];
telemetryItem.exceptions = [exception];
}
}
return true;
}
4 changes: 3 additions & 1 deletion src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
addCommonTelemetryItemProperties,
filterRemoteDependencyData,
filterTelemetryTypes,
sanitizeStackTrace,
sanitizeTelemetryItemUriProperty,
} from './filters';
import ITelemetry from './ITelemetry';
Expand All @@ -32,7 +33,7 @@ class Telemetry implements ITelemetry {

this.config = {
instrumentationKey: this.getInstrumentationKey(),
disableExceptionTracking: true,
disableExceptionTracking: false, // Enables autocollection of uncaught exceptions. Used with `sanitizeStackTrace` telemetry initializer to remove any data that might be PII.
disableAjaxTracking: true,
disableFetchTracking: false, // Enables capturing of telemetry data for outgoing requests. Used with `filterRemoteDependencyData` telemetry initializer to sanitize captured data to prevent inadvertent capture of PII.
disableTelemetry: this.getInstrumentationKey() ? false : true,
Expand All @@ -49,6 +50,7 @@ class Telemetry implements ITelemetry {
this.appInsights.trackPageView();
this.appInsights.addTelemetryInitializer(filterTelemetryTypes);
this.appInsights.addTelemetryInitializer(filterRemoteDependencyData);
this.appInsights.addTelemetryInitializer(sanitizeStackTrace);
this.appInsights.addTelemetryInitializer(sanitizeTelemetryItemUriProperty);
this.appInsights.addTelemetryInitializer(addCommonTelemetryItemProperties);
this.appInsights.context.application.ver = version;
Expand Down

0 comments on commit 81b0bab

Please sign in to comment.