Skip to content

Commit 9396528

Browse files
authored
feat: Implement timed events & remove transaction.measurements (#11398)
Now, we interpret timed events with special attributes as timed events. For now, we ignore all other timed events.
1 parent e6b4efa commit 9396528

File tree

18 files changed

+146
-56
lines changed

18 files changed

+146
-56
lines changed

.github/workflows/flaky-test-detector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
workflow_dispatch:
44
pull_request:
55
paths:
6-
- 'dev-packages/browser-integration-tests/suites/**'
6+
- 'dev-packages/browser-integration-tests/suites/**/test.ts'
77
branches-ignore:
88
- master
99

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
const transaction = Sentry.startInactiveSpan({
2-
name: 'some_transaction',
3-
forceTransaction: true,
1+
Sentry.startSpan({ name: 'some_transaction' }, () => {
2+
Sentry.setMeasurement('metric.foo', 42, 'ms');
3+
Sentry.setMeasurement('metric.bar', 1337, 'nanoseconds');
4+
Sentry.setMeasurement('metric.baz', 99, 's');
5+
Sentry.setMeasurement('metric.baz', 1, '');
46
});
5-
6-
transaction.setMeasurement('metric.foo', 42, 'ms');
7-
transaction.setMeasurement('metric.bar', 1337, 'nanoseconds');
8-
transaction.setMeasurement('metric.baz', 99, 's');
9-
transaction.setMeasurement('metric.baz', 1);
10-
11-
transaction.end();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1,
8+
transport: loggingTransport,
9+
});
10+
11+
Sentry.startSpan({ name: 'some_transaction' }, () => {
12+
Sentry.setMeasurement('metric.foo', 42, 'ms');
13+
Sentry.setMeasurement('metric.bar', 1337, 'nanoseconds');
14+
Sentry.setMeasurement('metric.baz', 99, 's');
15+
Sentry.setMeasurement('metric.baz', 1, '');
16+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
test('should attach measurement to transaction', done => {
8+
createRunner(__dirname, 'scenario.ts')
9+
.expect({
10+
transaction: {
11+
transaction: 'some_transaction',
12+
measurements: {
13+
'metric.foo': { value: 42, unit: 'ms' },
14+
'metric.bar': { value: 1337, unit: 'nanoseconds' },
15+
'metric.baz': { value: 1, unit: '' },
16+
},
17+
},
18+
})
19+
.start(done);
20+
});

packages/browser/src/index.bundle.tracing.replay.feedback.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
startSpanManual,
1919
withActiveSpan,
2020
getSpanDescendants,
21+
setMeasurement,
2122
} from '@sentry/core';
2223

2324
export {

packages/browser/src/index.bundle.tracing.replay.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
startSpanManual,
1919
withActiveSpan,
2020
getSpanDescendants,
21+
setMeasurement,
2122
} from '@sentry/core';
2223

2324
export {

packages/browser/src/index.bundle.tracing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
startSpanManual,
1919
withActiveSpan,
2020
getSpanDescendants,
21+
setMeasurement,
2122
} from '@sentry/core';
2223

2324
export {

packages/core/src/semanticAttributes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ export const SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'sentry.origin';
2222

2323
/** The reason why an idle span finished. */
2424
export const SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON = 'sentry.idle_span_finish_reason';
25+
26+
/** The unit of a measurement, which may be stored as a TimedEvent. */
27+
export const SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT = 'sentry.measurement_unit';
28+
29+
/** The value of a measurement, which may be stored as a TimedEvent. */
30+
export const SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE = 'sentry.measurement_value';

packages/core/src/tracing/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export {
1616
withActiveSpan,
1717
} from './trace';
1818
export { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
19-
export { setMeasurement } from './measurement';
19+
export { setMeasurement, timedEventsToMeasurements } from './measurement';
2020
export { sampleSpan } from './sampling';
2121
export { logSpanEnd, logSpanStart } from './logSpans';
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { MeasurementUnit, Span, Transaction } from '@sentry/types';
1+
import type { MeasurementUnit, Measurements, TimedEvent } from '@sentry/types';
2+
import {
3+
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT,
4+
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE,
5+
} from '../semanticAttributes';
26
import { getActiveSpan, getRootSpan } from '../utils/spanUtils';
37

48
/**
@@ -8,13 +12,28 @@ export function setMeasurement(name: string, value: number, unit: MeasurementUni
812
const activeSpan = getActiveSpan();
913
const rootSpan = activeSpan && getRootSpan(activeSpan);
1014

11-
if (rootSpan && rootSpanIsTransaction(rootSpan)) {
12-
// eslint-disable-next-line deprecation/deprecation
13-
rootSpan.setMeasurement(name, value, unit);
15+
if (rootSpan) {
16+
rootSpan.addEvent(name, {
17+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: value,
18+
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: unit as string,
19+
});
1420
}
1521
}
1622

17-
function rootSpanIsTransaction(rootSpan: Span): rootSpan is Transaction {
18-
// eslint-disable-next-line deprecation/deprecation
19-
return typeof (rootSpan as Transaction).setMeasurement === 'function';
23+
/**
24+
* Convert timed events to measurements.
25+
*/
26+
export function timedEventsToMeasurements(events: TimedEvent[]): Measurements {
27+
const measurements: Measurements = {};
28+
events.forEach(event => {
29+
const attributes = event.attributes || {};
30+
const unit = attributes[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT] as MeasurementUnit | undefined;
31+
const value = attributes[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE] as number | undefined;
32+
33+
if (typeof unit === 'string' && typeof value === 'number') {
34+
measurements[event.name] = { value, unit };
35+
}
36+
});
37+
38+
return measurements;
2039
}

0 commit comments

Comments
 (0)