Skip to content

Commit 00dcc7d

Browse files
committed
feat: Add tracingSampleRate
1 parent eb64802 commit 00dcc7d

File tree

1 file changed

+67
-28
lines changed

1 file changed

+67
-28
lines changed

packages/integrations/src/transactionactivity.ts

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface TransactionActivityOptions {
1010
*/
1111
onLocationChange(state: any): string;
1212
startTransactionOnLocationChange: boolean;
13+
tracesSampleRate: number;
1314
}
1415

1516
/** JSDoc */
@@ -32,6 +33,11 @@ export class TransactionActivity implements Integration {
3233
*/
3334
public static id: string = 'TransactionActivity';
3435

36+
/**
37+
* Is Tracing enabled, this will be determined once per pageload.
38+
*/
39+
private static _enabled?: boolean;
40+
3541
/** JSDoc */
3642
private static _options: TransactionActivityOptions;
3743

@@ -51,55 +57,80 @@ export class TransactionActivity implements Integration {
5157
/**
5258
* @inheritDoc
5359
*/
54-
public constructor(
55-
public readonly _options: TransactionActivityOptions = {
60+
public constructor(public readonly _options?: Partial<TransactionActivityOptions>) {
61+
const defaults = {
5662
idleTimeout: 500,
5763
onLocationChange: () => global.location.href,
5864
patchHistory: true,
5965
startTransactionOnLocationChange: true,
60-
},
61-
) {
62-
TransactionActivity._options = _options;
66+
tracesSampleRate: 1,
67+
};
68+
TransactionActivity._options = {
69+
...defaults,
70+
..._options,
71+
};
6372
}
6473

6574
/**
6675
* @inheritDoc
6776
*/
6877
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
6978
TransactionActivity._getCurrentHub = getCurrentHub;
70-
if (this._options.patchHistory) {
71-
// tslint:disable: no-unsafe-any
72-
if (global.history) {
79+
if (!TransactionActivity._isEnabled()) {
80+
return;
81+
}
82+
// `${window.location.href}` will be used a temp transaction name
83+
TransactionActivity.startIdleTransaction(`${window.location.href}`, {
84+
op: 'pageload',
85+
sampled: true,
86+
});
87+
88+
// tslint:disable: no-unsafe-any
89+
if (global.history && this._options && this._options.patchHistory) {
90+
// tslint:disable-next-line: typedef only-arrow-functions
91+
(function(history: any) {
92+
const pushState = history.pushState;
7393
// tslint:disable-next-line: typedef only-arrow-functions
74-
(function(history: any) {
75-
const pushState = history.pushState;
76-
// tslint:disable-next-line: typedef only-arrow-functions
77-
history.pushState = function(state: any) {
78-
if (typeof history.onpushstate === 'function') {
79-
history.onpushstate({ state });
80-
}
81-
// ... whatever else you want to do
82-
// maybe call onhashchange e.handler
83-
return pushState.apply(history, arguments);
84-
};
85-
})(global.history);
86-
global.onpopstate = (history as any).onpushstate = (_state: any) => {
87-
if (this._options.startTransactionOnLocationChange) {
88-
TransactionActivity.startIdleTransaction(`${global.location.href}`, {
89-
op: 'navigation',
90-
sampled: true,
91-
});
94+
history.pushState = function(state: any) {
95+
if (typeof history.onpushstate === 'function') {
96+
history.onpushstate({ state });
9297
}
98+
// ... whatever else you want to do
99+
// maybe call onhashchange e.handler
100+
return pushState.apply(history, arguments);
93101
};
94-
}
95-
// tslint:enable: no-unsafe-any
102+
})(global.history);
103+
global.onpopstate = (history as any).onpushstate = (_state: any) => {
104+
if (this._options && this._options.startTransactionOnLocationChange) {
105+
TransactionActivity.startIdleTransaction(`${global.location.href}`, {
106+
op: 'navigation',
107+
sampled: true,
108+
});
109+
}
110+
};
96111
}
112+
// tslint:enable: no-unsafe-any
113+
}
114+
115+
/**
116+
* Is tracing enabled
117+
*/
118+
private static _isEnabled(): boolean {
119+
if (TransactionActivity._enabled !== undefined) {
120+
return TransactionActivity._enabled;
121+
}
122+
TransactionActivity._enabled = Math.random() > TransactionActivity._options.tracesSampleRate ? false : true;
123+
return TransactionActivity._enabled;
97124
}
98125

99126
/**
100127
* Starts a Transaction waiting for activity idle to finish
101128
*/
102129
public static startIdleTransaction(name: string, spanContext?: SpanContext): Span | undefined {
130+
if (!TransactionActivity._isEnabled()) {
131+
// Tracing is not enabled
132+
return undefined;
133+
}
103134
const activeTransaction = TransactionActivity._activeTransaction;
104135

105136
if (activeTransaction) {
@@ -170,6 +201,10 @@ export class TransactionActivity implements Integration {
170201
* Starts tracking for a specifc activity
171202
*/
172203
public static pushActivity(name: string, spanContext?: SpanContext): number {
204+
if (!TransactionActivity._isEnabled()) {
205+
// Tracing is not enabled
206+
return 0;
207+
}
173208
const _getCurrentHub = TransactionActivity._getCurrentHub;
174209
if (spanContext && _getCurrentHub) {
175210
const hub = _getCurrentHub();
@@ -192,6 +227,10 @@ export class TransactionActivity implements Integration {
192227
* Removes activity and finishes the span in case there is one
193228
*/
194229
public static popActivity(id: number): void {
230+
if (!TransactionActivity._isEnabled()) {
231+
// Tracing is not enabled
232+
return;
233+
}
195234
const activity = TransactionActivity._activities[id];
196235
if (activity) {
197236
if (activity.span) {

0 commit comments

Comments
 (0)