Skip to content

Commit 8b6ecb0

Browse files
committed
reorg structure
1 parent 5bc8215 commit 8b6ecb0

File tree

5 files changed

+103
-148
lines changed

5 files changed

+103
-148
lines changed

packages/tracing/src/browser/metrics.ts renamed to packages/tracing/src/browser/metrics/index.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/* eslint-disable max-lines */
2-
import { Measurements, Span, SpanContext } from '@sentry/types';
2+
import { Measurements } from '@sentry/types';
33
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';
44

5-
import { IS_DEBUG_BUILD } from '../flags';
6-
import { Transaction } from '../transaction';
7-
import { msToSec } from '../utils';
8-
import { getCLS, LayoutShift } from './web-vitals/getCLS';
9-
import { getFID } from './web-vitals/getFID';
10-
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
11-
import { getVisibilityWatcher } from './web-vitals/lib/getVisibilityWatcher';
12-
import { NavigatorDeviceMemory, NavigatorNetworkInformation } from './web-vitals/types';
5+
import { IS_DEBUG_BUILD } from '../../flags';
6+
import { Transaction } from '../../transaction';
7+
import { msToSec } from '../../utils';
8+
import { getCLS, LayoutShift } from '../web-vitals/getCLS';
9+
import { getFID } from '../web-vitals/getFID';
10+
import { getLCP, LargestContentfulPaint } from '../web-vitals/getLCP';
11+
import { getVisibilityWatcher } from '../web-vitals/lib/getVisibilityWatcher';
12+
import { NavigatorDeviceMemory, NavigatorNetworkInformation } from '../web-vitals/types';
13+
import { _startChild, isMeasurementValue } from './utils';
1314

1415
const global = getGlobalObject<Window>();
1516

@@ -123,7 +124,7 @@ export function addPerformanceEntries(transaction: Transaction): void {
123124
case 'mark':
124125
case 'paint':
125126
case 'measure': {
126-
const startTimestamp = addMeasureSpans(transaction, entry, startTime, duration, timeOrigin);
127+
const startTimestamp = _addMeasureSpans(transaction, entry, startTime, duration, timeOrigin);
127128

128129
// capture web vitals
129130
const firstHidden = getVisibilityWatcher();
@@ -229,7 +230,7 @@ export function addPerformanceEntries(transaction: Transaction): void {
229230
}
230231

231232
/** Create measure related spans */
232-
function addMeasureSpans(
233+
export function _addMeasureSpans(
233234
transaction: Transaction,
234235
// eslint-disable-next-line @typescript-eslint/no-explicit-any
235236
entry: Record<string, any>,
@@ -387,29 +388,6 @@ function _trackNavigator(transaction: Transaction): void {
387388
}
388389
}
389390

390-
/**
391-
* Checks if a given value is a valid measurement value.
392-
*/
393-
function isMeasurementValue(value: unknown): value is number {
394-
return typeof value === 'number' && isFinite(value);
395-
}
396-
397-
/**
398-
* Helper function to start child on transactions. This function will make sure that the transaction will
399-
* use the start timestamp of the created child span if it is earlier than the transactions actual
400-
* start timestamp.
401-
*/
402-
export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }: SpanContext): Span {
403-
if (startTimestamp && transaction.startTimestamp > startTimestamp) {
404-
transaction.startTimestamp = startTimestamp;
405-
}
406-
407-
return transaction.startChild({
408-
startTimestamp,
409-
...ctx,
410-
});
411-
}
412-
413391
/** Add LCP / CLS data to transaction to allow debugging */
414392
function _tagMetricInfo(transaction: Transaction): void {
415393
if (_lcpEntry) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Span, SpanContext } from '@sentry/types';
2+
3+
import { Transaction } from '../../transaction';
4+
5+
/**
6+
* Checks if a given value is a valid measurement value.
7+
*/
8+
export function isMeasurementValue(value: unknown): value is number {
9+
return typeof value === 'number' && isFinite(value);
10+
}
11+
12+
/**
13+
* Helper function to start child on transactions. This function will make sure that the transaction will
14+
* use the start timestamp of the created child span if it is earlier than the transactions actual
15+
* start timestamp.
16+
*/
17+
export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }: SpanContext): Span {
18+
if (startTimestamp && transaction.startTimestamp > startTimestamp) {
19+
transaction.startTimestamp = startTimestamp;
20+
}
21+
22+
return transaction.startChild({
23+
startTimestamp,
24+
...ctx,
25+
});
26+
}

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
getHeaderContext,
1010
getMetaContent,
1111
} from '../../src/browser/browsertracing';
12-
import { MetricsInstrumentation } from '../../src/browser/metrics';
1312
import { defaultRequestInstrumentationOptions } from '../../src/browser/request';
1413
import { instrumentRoutingWithDefaults } from '../../src/browser/router';
1514
import * as hubExtensions from '../../src/hubextensions';
@@ -466,29 +465,4 @@ describe('BrowserTracing', () => {
466465
);
467466
});
468467
});
469-
470-
describe('metrics', () => {
471-
beforeEach(() => {
472-
// @ts-ignore mock clear
473-
MetricsInstrumentation.mockClear();
474-
});
475-
476-
it('creates metrics instrumentation', () => {
477-
createBrowserTracing(true, {});
478-
479-
expect(MetricsInstrumentation).toHaveBeenCalledTimes(1);
480-
expect(MetricsInstrumentation).toHaveBeenLastCalledWith(undefined);
481-
});
482-
483-
it('creates metrics instrumentation with custom options', () => {
484-
createBrowserTracing(true, {
485-
_metricOptions: {
486-
_reportAllChanges: true,
487-
},
488-
});
489-
490-
expect(MetricsInstrumentation).toHaveBeenCalledTimes(1);
491-
expect(MetricsInstrumentation).toHaveBeenLastCalledWith(true);
492-
});
493-
});
494468
});

packages/tracing/test/browser/metrics.test.ts renamed to packages/tracing/test/browser/metrics/index.test.ts

Lines changed: 25 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1-
import { Span, Transaction } from '../../src';
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;
7-
8-
describe('_startChild()', () => {
9-
it('creates a span with given properties', () => {
10-
const transaction = new Transaction({ name: 'test' });
11-
const span = _startChild(transaction, {
12-
description: 'evaluation',
13-
op: 'script',
14-
});
1+
import { Transaction } from '../../../src';
2+
import { _addResourceSpans, _addMeasureSpans, ResourceEntry } from '../../../src/browser/metrics';
153

16-
expect(span).toBeInstanceOf(Span);
17-
expect(span.description).toBe('evaluation');
18-
expect(span.op).toBe('script');
4+
describe('_addMeasureSpans', () => {
5+
const transaction = new Transaction({ op: 'pageload', name: '/' });
6+
beforeEach(() => {
7+
transaction.startChild = jest.fn();
198
});
209

21-
it('adjusts the start timestamp if child span starts before transaction', () => {
22-
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
23-
const span = _startChild(transaction, {
24-
description: 'script.js',
25-
op: 'resource',
26-
startTimestamp: 100,
27-
});
10+
it('adds measure spans to a transaction', () => {
11+
const entry: Omit<PerformanceMeasure, 'toJSON'> = {
12+
entryType: 'measure',
13+
name: 'measure-1',
14+
duration: 10,
15+
startTime: 12,
16+
};
2817

29-
expect(transaction.startTimestamp).toEqual(span.startTimestamp);
30-
expect(transaction.startTimestamp).toEqual(100);
31-
});
18+
const timeOrigin = 100;
19+
const startTime = 23;
20+
const duration = 356;
3221

33-
it('does not adjust start timestamp if child span starts after transaction', () => {
34-
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
35-
const span = _startChild(transaction, {
36-
description: 'script.js',
37-
op: 'resource',
38-
startTimestamp: 150,
22+
expect(transaction.startChild).toHaveBeenCalledTimes(0);
23+
_addMeasureSpans(transaction, entry, startTime, duration, timeOrigin);
24+
expect(transaction.startChild).toHaveBeenCalledTimes(1);
25+
expect(transaction.startChild).toHaveBeenLastCalledWith({
26+
description: 'measure-1',
27+
startTimestamp: timeOrigin + startTime,
28+
endTimestamp: timeOrigin + startTime + duration,
29+
op: 'measure',
3930
});
40-
41-
expect(transaction.startTimestamp).not.toEqual(span.startTimestamp);
42-
expect(transaction.startTimestamp).toEqual(123);
4331
});
4432
});
4533

4634
describe('_addResourceSpans', () => {
47-
const transaction = new Transaction({ name: 'hello' });
35+
const transaction = new Transaction({ op: 'pageload', name: '/' });
4836
beforeEach(() => {
4937
transaction.startChild = jest.fn();
5038
});
@@ -169,54 +157,3 @@ describe('_addResourceSpans', () => {
169157
);
170158
});
171159
});
172-
173-
// TODO: Add these tests back
174-
// describe('MetricsInstrumentation', () => {
175-
// afterEach(() => {
176-
// jest.clearAllMocks();
177-
// });
178-
179-
// it('does not initialize trackers when on node', () => {
180-
// const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
181-
// jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
182-
// );
183-
184-
// new MetricsInstrumentation();
185-
186-
// trackers.forEach(tracker => expect(tracker).not.toBeCalled());
187-
// });
188-
189-
// it('initializes trackers when not on node and `global.performance` and `global.document` are available.', () => {
190-
// addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
191-
192-
// const backup = global.process;
193-
// global.process = undefined;
194-
195-
// const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
196-
// jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
197-
// );
198-
// new MetricsInstrumentation();
199-
// global.process = backup;
200-
201-
// trackers.forEach(tracker => expect(tracker).toBeCalled());
202-
// });
203-
204-
// it('does not initialize trackers when not on node but `global.document` is not available (in worker)', () => {
205-
// // window not necessary for this test, but it is here to exercise that it is absence of document that is checked
206-
// addDOMPropertiesToGlobal(['performance', 'addEventListener', 'window']);
207-
208-
// const processBackup = global.process;
209-
// global.process = undefined;
210-
// const documentBackup = global.document;
211-
// global.document = undefined;
212-
213-
// const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
214-
// jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
215-
// );
216-
// new MetricsInstrumentation();
217-
// global.process = processBackup;
218-
// global.document = documentBackup;
219-
220-
// trackers.forEach(tracker => expect(tracker).not.toBeCalled());
221-
// });
222-
// });
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Span, Transaction } from '../../../src';
2+
import { _startChild } from '../../../src/browser/metrics/utils';
3+
4+
describe('_startChild()', () => {
5+
it('creates a span with given properties', () => {
6+
const transaction = new Transaction({ name: 'test' });
7+
const span = _startChild(transaction, {
8+
description: 'evaluation',
9+
op: 'script',
10+
});
11+
12+
expect(span).toBeInstanceOf(Span);
13+
expect(span.description).toBe('evaluation');
14+
expect(span.op).toBe('script');
15+
});
16+
17+
it('adjusts the start timestamp if child span starts before transaction', () => {
18+
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
19+
const span = _startChild(transaction, {
20+
description: 'script.js',
21+
op: 'resource',
22+
startTimestamp: 100,
23+
});
24+
25+
expect(transaction.startTimestamp).toEqual(span.startTimestamp);
26+
expect(transaction.startTimestamp).toEqual(100);
27+
});
28+
29+
it('does not adjust start timestamp if child span starts after transaction', () => {
30+
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
31+
const span = _startChild(transaction, {
32+
description: 'script.js',
33+
op: 'resource',
34+
startTimestamp: 150,
35+
});
36+
37+
expect(transaction.startTimestamp).not.toEqual(span.startTimestamp);
38+
expect(transaction.startTimestamp).toEqual(123);
39+
});
40+
});

0 commit comments

Comments
 (0)