Skip to content

Commit 1bf5cdd

Browse files
authored
fix(tracing): Avoid browser tracing initialization on node environment. (#3548)
1 parent e7f5255 commit 1bf5cdd

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import { Measurements, SpanContext } from '@sentry/types';
4-
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, logger } from '@sentry/utils';
4+
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';
55

66
import { Span } from '../span';
77
import { Transaction } from '../transaction';
@@ -22,7 +22,7 @@ export class MetricsInstrumentation {
2222
private _lcpEntry: LargestContentfulPaint | undefined;
2323

2424
public constructor() {
25-
if (global && global.performance) {
25+
if (!isNodeEnv() && global?.performance) {
2626
if (global.performance.mark) {
2727
global.performance.mark('sentry-tracing-init');
2828
}

packages/tracing/test/browser/metrics.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Span, Transaction } from '../../src';
2-
import { _startChild, addResourceSpans, ResourceEntry } from '../../src/browser/metrics';
2+
import { _startChild, addResourceSpans, MetricsInstrumentation, ResourceEntry } from '../../src/browser/metrics';
3+
import { addDOMPropertiesToGlobal } from '../testutils';
4+
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
6+
declare var global: any;
37

48
describe('_startChild()', () => {
59
it('creates a span with given properties', () => {
@@ -167,3 +171,29 @@ describe('addResourceSpans', () => {
167171
);
168172
});
169173
});
174+
175+
describe('MetricsInstrumentation', () => {
176+
it('does not initialize trackers when on node', () => {
177+
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
178+
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
179+
);
180+
181+
new MetricsInstrumentation();
182+
183+
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
184+
});
185+
186+
it('initializes trackers when not on node and `global.performance` is available.', () => {
187+
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
188+
const backup = global.process;
189+
global.process = undefined;
190+
191+
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
192+
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
193+
);
194+
new MetricsInstrumentation();
195+
global.process = backup;
196+
197+
trackers.forEach(tracker => expect(tracker).toBeCalled());
198+
});
199+
});

0 commit comments

Comments
 (0)