Skip to content

Commit

Permalink
fix(tracing): Avoid browser tracing initialization on node environmen…
Browse files Browse the repository at this point in the history
…t. (#3548)
  • Loading branch information
onurtemizkan authored May 19, 2021
1 parent e7f5255 commit 1bf5cdd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Measurements, SpanContext } from '@sentry/types';
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, logger } from '@sentry/utils';
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';

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

public constructor() {
if (global && global.performance) {
if (!isNodeEnv() && global?.performance) {
if (global.performance.mark) {
global.performance.mark('sentry-tracing-init');
}
Expand Down
32 changes: 31 additions & 1 deletion packages/tracing/test/browser/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Span, Transaction } from '../../src';
import { _startChild, addResourceSpans, ResourceEntry } from '../../src/browser/metrics';
import { _startChild, addResourceSpans, MetricsInstrumentation, ResourceEntry } from '../../src/browser/metrics';
import { addDOMPropertiesToGlobal } from '../testutils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
declare var global: any;

describe('_startChild()', () => {
it('creates a span with given properties', () => {
Expand Down Expand Up @@ -167,3 +171,29 @@ describe('addResourceSpans', () => {
);
});
});

describe('MetricsInstrumentation', () => {
it('does not initialize trackers when on node', () => {
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);

new MetricsInstrumentation();

trackers.forEach(tracker => expect(tracker).not.toBeCalled());
});

it('initializes trackers when not on node and `global.performance` is available.', () => {
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
const backup = global.process;
global.process = undefined;

const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);
new MetricsInstrumentation();
global.process = backup;

trackers.forEach(tracker => expect(tracker).toBeCalled());
});
});

0 comments on commit 1bf5cdd

Please sign in to comment.