Skip to content

Commit 9786d50

Browse files
committed
smaller errors + backgroundtab
1 parent e565b86 commit 9786d50

File tree

6 files changed

+77
-29
lines changed

6 files changed

+77
-29
lines changed

packages/apm/src/integrations/errors.ts

Whitespace-only changes.

packages/tracing/src/integrations/browsertracing.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { IdleTransaction } from '../idletransaction';
66
import { Span as SpanClass } from '../span';
77
import { SpanStatus } from '../spanstatus';
88

9+
import { registerBackgroundTabDetection } from './tracing/backgroundtab';
10+
import { registerErrorHandlers } from './tracing/errors';
911
import {
1012
defaultRequestInstrumentionOptions,
1113
RequestInstrumentationClass,
@@ -20,12 +22,6 @@ import {
2022
} from './tracing/router';
2123

2224
/**
23-
* TODO: _setupErrorHandling
24-
* - This should be a integration that runs automatically
25-
* TODO: _setupBackgroundTabDetection
26-
* - This something that works automatically too
27-
* - This just cancels active pageload/navigation on scope
28-
* - Provide option to to extend to all transactions??
2925
* TODO: Tracing._addPerformanceEntries
3026
* - This is a beforeFinish() hook here
3127
*/
@@ -43,6 +39,15 @@ export type BrowserTracingOptions = {
4339
*/
4440
maxTransactionDuration: number;
4541

42+
/**
43+
* Flag Transactions where tabs moved to background with "cancelled". Browser background tab timing is
44+
* not suited towards doing precise measurements of operations. Background transaction can mess up your
45+
* statistics in non deterministic ways that's why we by default recommend leaving this opition enabled.
46+
*
47+
* Default: true
48+
*/
49+
markBackgroundTransactions: boolean;
50+
4651
/**
4752
* This is only if you want to debug in prod.
4853
* writeAsBreadcrumbs: Instead of having console.log statements we log messages to breadcrumbs
@@ -120,6 +125,10 @@ export class BrowserTracing implements Integration {
120125

121126
BrowserTracing._initRoutingInstrumentation(hub);
122127
BrowserTracing._initRequestInstrumentation();
128+
registerErrorHandlers();
129+
if (BrowserTracing.options.markBackgroundTransactions) {
130+
registerBackgroundTabDetection();
131+
}
123132

124133
// This EventProcessor makes sure that the transaction is not longer than maxTransactionDuration
125134
addGlobalEventProcessor((event: Event) => {
@@ -139,7 +148,7 @@ export class BrowserTracing implements Integration {
139148
event.type === 'transaction' &&
140149
isOutdatedTransaction
141150
) {
142-
BrowserTracing._log(`[Tracing] Transaction: ${SpanStatus.Cancelled} since it maxed out maxTransactionDuration`);
151+
BrowserTracing.log(`[Tracing] Transaction: ${SpanStatus.Cancelled} since it maxed out maxTransactionDuration`);
143152
if (event.contexts && event.contexts.trace) {
144153
event.contexts.trace = {
145154
...event.contexts.trace,
@@ -205,7 +214,7 @@ export class BrowserTracing implements Integration {
205214
if (header) {
206215
const span = SpanClass.fromTraceparent(header);
207216
if (span) {
208-
BrowserTracing._log(
217+
BrowserTracing.log(
209218
`[Tracing] found 'sentry-meta' '<meta />' continuing trace with: trace_id: ${span.traceId} span_id: ${
210219
span.parentSpanId
211220
}`,
@@ -225,7 +234,7 @@ export class BrowserTracing implements Integration {
225234
/**
226235
* Uses logger.log to log things in the SDK or as breadcrumbs if defined in options
227236
*/
228-
private static _log(...args: any[]): void {
237+
public static log(...args: any[]): void {
229238
if (BrowserTracing.options && BrowserTracing.options.debug && BrowserTracing.options.debug.writeAsBreadcrumbs) {
230239
const _getCurrentHub = BrowserTracing._getCurrentHub;
231240
if (_getCurrentHub) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { getGlobalObject } from '@sentry/utils';
2+
3+
import { SpanStatus } from '../../spanstatus';
4+
5+
import { getActiveTransaction } from './utils';
6+
7+
const global = getGlobalObject<Window>();
8+
9+
/**
10+
* Add a listener that cancels and finishes a transaction when the global
11+
* document is hidden.
12+
*/
13+
export function registerBackgroundTabDetection(): void {
14+
if (global && global.document) {
15+
document.addEventListener('visibilitychange', () => {
16+
// TODO: THis will cancel ALL Transactions now, is this desired behaviour?
17+
const activeTransaction = getActiveTransaction();
18+
if (document.hidden && activeTransaction) {
19+
activeTransaction.setStatus(SpanStatus.Cancelled);
20+
activeTransaction.setTag('visibilitychange', 'document.hidden');
21+
activeTransaction.finish();
22+
}
23+
});
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { addInstrumentationHandler } from '@sentry/utils';
2+
3+
import { SpanStatus } from '../../spanstatus';
4+
5+
import { getActiveTransaction } from './utils';
6+
7+
/**
8+
* Configures global error listeners
9+
*/
10+
export function registerErrorHandlers(): void {
11+
addInstrumentationHandler({
12+
callback: errorCallback,
13+
type: 'error',
14+
});
15+
addInstrumentationHandler({
16+
callback: errorCallback,
17+
type: 'unhandledrejection',
18+
});
19+
}
20+
21+
/**
22+
* If an error or unhandled promise occurs, we mark the active transaction as failed
23+
*/
24+
function errorCallback(): void {
25+
const activeTransaction = getActiveTransaction();
26+
if (activeTransaction) {
27+
activeTransaction.setStatus(SpanStatus.InternalError);
28+
}
29+
}

packages/tracing/src/integrations/tracing/request.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { getCurrentHub } from '@sentry/hub';
21
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';
32

4-
import { IdleTransaction } from '../../idletransaction';
53
import { Span } from '../../span';
6-
import { Transaction } from '../../transaction';
4+
5+
import { getActiveTransaction } from './utils';
76

87
const defaultTracingOrigins = ['localhost', /^\//];
98

@@ -254,18 +253,3 @@ function xhrCallback(handlerData: XHRData, shouldCreateSpan: (url: string) => bo
254253
}
255254
}
256255
}
257-
258-
/**
259-
* Get active transaction on scope if possible
260-
*/
261-
function getActiveTransaction(): Transaction | IdleTransaction | undefined {
262-
const hub = getCurrentHub();
263-
if (hub) {
264-
const scope = hub.getScope();
265-
if (scope) {
266-
return scope.getTransaction() as Transaction | IdleTransaction | undefined;
267-
}
268-
}
269-
270-
return undefined;
271-
}

packages/tracing/src/integrations/tracing/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { getCurrentHub } from '@sentry/hub';
22

33
import { IdleTransaction } from '../../idletransaction';
4+
import { Transaction } from '../../transaction';
45

56
/**
67
* Grabs active transaction off scope
78
*/
8-
export function getActiveTransaction(): IdleTransaction | undefined {
9+
export function getActiveTransaction(): Transaction | IdleTransaction | undefined {
910
const hub = getCurrentHub();
1011
const scope = hub.getScope();
1112
if (scope) {
12-
return scope.getTransaction() as IdleTransaction;
13+
return scope.getTransaction() as Transaction | IdleTransaction | undefined;
1314
}
1415

1516
return undefined;

0 commit comments

Comments
 (0)