Skip to content

Commit f0a2d4c

Browse files
committed
ref: Remove static methods
1 parent 9418bf0 commit f0a2d4c

File tree

2 files changed

+67
-58
lines changed

2 files changed

+67
-58
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ export interface BrowserTracingOptions {
8585
// TODO: Should this be an option, or a static class variable and passed
8686
// in and we use something like `BrowserTracing.addRoutingProcessor()`
8787
routingInstrumentationProcessors: routingInstrumentationProcessor[];
88-
89-
/**
90-
* Flag to enable default routing instrumentation.
91-
*
92-
* Default: true
93-
*/
94-
defaultRoutingInstrumentation: boolean;
9588
}
9689

9790
/**
@@ -108,33 +101,31 @@ export class BrowserTracing implements Integration {
108101
public static id: string = 'BrowserTracing';
109102

110103
/** Browser Tracing integration options */
111-
public static options: BrowserTracingOptions;
104+
public options: BrowserTracingOptions = {
105+
beforeNavigate(location: LocationType): string | undefined {
106+
return location.pathname;
107+
},
108+
debug: {
109+
writeAsBreadcrumbs: false,
110+
},
111+
idleTimeout: DEFAULT_IDLE_TIMEOUT,
112+
routingInstrumentationProcessors: [],
113+
startTransactionOnLocationChange: true,
114+
startTransactionOnPageLoad: true,
115+
};
112116

113117
/**
114118
* @inheritDoc
115119
*/
116120
public name: string = BrowserTracing.id;
117121

118-
private static _activeTransaction?: IdleTransaction;
122+
private _activeTransaction?: IdleTransaction;
119123

120-
private static _getCurrentHub?: () => Hub;
124+
private _getCurrentHub?: () => Hub;
121125

122126
public constructor(_options?: Partial<BrowserTracingOptions>) {
123-
const defaults: BrowserTracingOptions = {
124-
beforeNavigate(location: LocationType): string | undefined {
125-
return location.pathname;
126-
},
127-
debug: {
128-
writeAsBreadcrumbs: false,
129-
},
130-
defaultRoutingInstrumentation: true,
131-
idleTimeout: DEFAULT_IDLE_TIMEOUT,
132-
routingInstrumentationProcessors: [],
133-
startTransactionOnLocationChange: true,
134-
startTransactionOnPageLoad: true,
135-
};
136-
BrowserTracing.options = {
137-
...defaults,
127+
this.options = {
128+
...this.options,
138129
..._options,
139130
};
140131
}
@@ -143,25 +134,24 @@ export class BrowserTracing implements Integration {
143134
* @inheritDoc
144135
*/
145136
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
146-
BrowserTracing._getCurrentHub = getCurrentHub;
137+
this._getCurrentHub = getCurrentHub;
147138

148139
if (!global || !global.location) {
149140
return;
150141
}
151142

152-
// TODO: is it fine that this is mutable operation? Could also do = [...routingInstr, setHeaderContext]?
153-
BrowserTracing.options.routingInstrumentationProcessors.push(setHeaderContext);
154-
if (BrowserTracing.options.defaultRoutingInstrumentation) {
155-
BrowserTracing._initRoutingInstrumentation();
156-
}
143+
this._initRoutingInstrumentation();
157144
}
158145

159146
/** Start routing instrumentation */
160-
private static _initRoutingInstrumentation(): void {
161-
const { startTransactionOnPageLoad, startTransactionOnLocationChange } = BrowserTracing.options;
147+
private _initRoutingInstrumentation(): void {
148+
const { startTransactionOnPageLoad, startTransactionOnLocationChange } = this.options;
149+
150+
// TODO: is it fine that this is mutable operation? Could also do = [...routingInstr, setHeaderContext]?
151+
this.options.routingInstrumentationProcessors.push(setHeaderContext);
162152

163153
if (startTransactionOnPageLoad) {
164-
BrowserTracing._activeTransaction = BrowserTracing.createRouteTransaction('pageload');
154+
this._activeTransaction = this._createRouteTransaction('pageload');
165155
}
166156

167157
let startingUrl: string | undefined = global.location.href;
@@ -170,8 +160,8 @@ export class BrowserTracing implements Integration {
170160
callback: ({ to, from }: { to: string; from?: string }) => {
171161
/**
172162
* This early return is there to account for some cases where navigation transaction
173-
* starts right after long running pageload. We make sure that if from is undefined
174-
* and that a valid startingURL exists, we don't uncessarily create a navigation transaction.
163+
* starts right after long running pageload. We make sure that if `from` is undefined
164+
* and that a valid `startingURL` exists, we don't uncessarily create a navigation transaction.
175165
*
176166
* This was hard to duplicate, but this behaviour stopped as soon as this fix
177167
* was applied. This issue might also only be caused in certain development environments
@@ -183,55 +173,51 @@ export class BrowserTracing implements Integration {
183173
}
184174
if (startTransactionOnLocationChange && from !== to) {
185175
startingUrl = undefined;
186-
if (BrowserTracing._activeTransaction) {
176+
if (this._activeTransaction) {
187177
// We want to finish all current ongoing idle transactions as we
188178
// are navigating to a new page.
189-
BrowserTracing._activeTransaction.finishIdleTransaction();
179+
this._activeTransaction.finishIdleTransaction();
190180
}
191-
BrowserTracing._activeTransaction = BrowserTracing.createRouteTransaction('navigation');
181+
this._activeTransaction = this._createRouteTransaction('navigation');
192182
}
193183
},
194184
type: 'history',
195185
});
196186
}
197187

198188
/** Create pageload/navigation idle transaction. */
199-
public static createRouteTransaction(
189+
private _createRouteTransaction(
200190
op: 'pageload' | 'navigation',
201-
ctx?: TransactionContext,
191+
context?: TransactionContext,
202192
): IdleTransaction | undefined {
203-
if (!BrowserTracing._getCurrentHub) {
193+
if (!this._getCurrentHub) {
204194
return undefined;
205195
}
206196

207-
const { beforeNavigate, idleTimeout, routingInstrumentationProcessors } = BrowserTracing.options;
197+
const { beforeNavigate, idleTimeout, routingInstrumentationProcessors } = this.options;
208198

209199
// if beforeNavigate returns undefined, we should not start a transaction.
210200
const name = beforeNavigate(global.location);
211201
if (name === undefined) {
202+
this._log(`[Tracing] Cancelling ${op} idleTransaction due to beforeNavigate:`);
212203
return undefined;
213204
}
214205

215-
let context: TransactionContext = { name, op, ...ctx };
216-
if (routingInstrumentationProcessors) {
217-
for (const processor of routingInstrumentationProcessors) {
218-
context = processor(context);
219-
}
220-
}
206+
const ctx = createContextFromProcessors({ name, op, ...context }, routingInstrumentationProcessors);
221207

222-
const hub = BrowserTracing._getCurrentHub();
223-
BrowserTracing._log(`[Tracing] starting ${op} idleTransaction on scope with context:`, context);
224-
const activeTransaction = startIdleTransaction(hub, context, idleTimeout, true);
208+
const hub = this._getCurrentHub();
209+
this._log(`[Tracing] starting ${op} idleTransaction on scope with context:`, ctx);
210+
const activeTransaction = startIdleTransaction(hub, ctx, idleTimeout, true);
225211

226212
return activeTransaction;
227213
}
228214

229215
/**
230216
* Uses logger.log to log things in the SDK or as breadcrumbs if defined in options
231217
*/
232-
private static _log(...args: any[]): void {
233-
if (BrowserTracing.options && BrowserTracing.options.debug && BrowserTracing.options.debug.writeAsBreadcrumbs) {
234-
const _getCurrentHub = BrowserTracing._getCurrentHub;
218+
private _log(...args: any[]): void {
219+
if (this.options && this.options.debug && this.options.debug.writeAsBreadcrumbs) {
220+
const _getCurrentHub = this._getCurrentHub;
235221
if (_getCurrentHub) {
236222
_getCurrentHub().addBreadcrumb({
237223
category: 'tracing',
@@ -245,9 +231,23 @@ export class BrowserTracing implements Integration {
245231
}
246232
}
247233

248-
/**
249-
* Returns the value of a meta tag
250-
*/
234+
/** Creates transaction context from a set of processors */
235+
export function createContextFromProcessors(
236+
context: TransactionContext,
237+
processors: routingInstrumentationProcessor[],
238+
): TransactionContext {
239+
let ctx = context;
240+
for (const processor of processors) {
241+
const newContext = processor(context);
242+
if (newContext && newContext.name && newContext.op) {
243+
ctx = newContext;
244+
}
245+
}
246+
247+
return ctx;
248+
}
249+
250+
/** Returns the value of a meta tag */
251251
export function getMetaContent(metaName: string): string | null {
252252
const el = document.querySelector(`meta[name=${metaName}]`);
253253
return el ? el.getAttribute('content') : null;

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ describe('BrowserTracing', () => {
113113
expect(transaction.sampled).toBe(true);
114114
});
115115

116+
it('uses a custom routing instrumentation processor', () => {
117+
createBrowserTracing(true, { routingInstrumentationProcessors: [] });
118+
const transaction = getActiveTransaction(hub) as IdleTransaction;
119+
120+
expect(transaction.traceId).toBe('126de09502ae4e0fb26c6967190756a4');
121+
expect(transaction.parentSpanId).toBe('b6e54397b12a2a0f');
122+
expect(transaction.sampled).toBe(true);
123+
});
124+
116125
it('is created with a default idleTimeout', () => {
117126
createBrowserTracing(true);
118127
const mockFinish = jest.fn();

0 commit comments

Comments
 (0)