Skip to content

Commit bc60bc6

Browse files
Add transaction op default (#2691)
1 parent e027a90 commit bc60bc6

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Add `lastEventId` method to the API ([#2675](https://github.com/getsentry/sentry-react-native/pull/2675))
88

9+
### Fix
10+
11+
- `Sentry.startTransaction` doesn't require `op` ([#2691](https://github.com/getsentry/sentry-react-native/pull/2691))
12+
913
### Dependencies
1014

1115
- Bump Cocoa SDK from v7.31.2 to v7.31.3 ([#2647](https://github.com/getsentry/sentry-react-native/pull/2647))

src/js/measurements.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { getCurrentHub, getMainCarrier, Hub } from '@sentry/core';
22
import { Transaction } from '@sentry/tracing';
3-
import { CustomSamplingContext, TransactionContext } from '@sentry/types';
3+
import { CustomSamplingContext, Span, SpanContext, TransactionContext } from '@sentry/types';
44

55
import { ReactNativeTracing } from './tracing';
66

7+
const SPAN_OP_DEFAULT = 'default';
8+
79
/**
810
* Adds React Native's extensions. Needs to be called after @sentry/tracing's extension methods are added
911
*/
@@ -28,7 +30,7 @@ export function _addTracingExtensions(): void {
2830
}
2931
}
3032

31-
type StartTransactionFunction = (
33+
export type StartTransactionFunction = (
3234
this: Hub,
3335
transactionContext: TransactionContext,
3436
customSamplingContext?: CustomSamplingContext
@@ -48,10 +50,25 @@ const _patchStartTransaction = (
4850
transactionContext: TransactionContext,
4951
customSamplingContext?: CustomSamplingContext
5052
): Transaction {
53+
// Native SDKs require op to be set - for JS Relay sets `default`
54+
if (!transactionContext.op) {
55+
transactionContext.op = SPAN_OP_DEFAULT;
56+
}
57+
5158
const transaction: Transaction = originalStartTransaction.apply(this, [
5259
transactionContext,
5360
customSamplingContext,
5461
]);
62+
const originalStartChild: Transaction['startChild'] = transaction.startChild.bind(transaction);
63+
transaction.startChild = (
64+
spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'sampled' | 'traceId' | 'parentSpanId'>>,
65+
): Span => {
66+
return originalStartChild({
67+
...spanContext,
68+
// Native SDKs require op to be set
69+
op: spanContext?.op || SPAN_OP_DEFAULT,
70+
});
71+
};
5572

5673
const reactNativeTracing = getCurrentHub().getIntegration(
5774
ReactNativeTracing

test/measurements.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Carrier, getCurrentHub, getMainCarrier } from '@sentry/core';
2+
import { Transaction } from '@sentry/tracing';
3+
import { Hub } from '@sentry/types';
4+
5+
import { _addTracingExtensions, StartTransactionFunction } from '../src/js/measurements';
6+
7+
describe('Tracing extensions', () => {
8+
let hub: Hub;
9+
let carrier: Carrier;
10+
let startTransaction: StartTransactionFunction | undefined;
11+
12+
beforeEach(() => {
13+
_addTracingExtensions();
14+
hub = getCurrentHub();
15+
carrier = getMainCarrier();
16+
startTransaction = carrier.__SENTRY__?.extensions?.startTransaction as StartTransactionFunction | undefined;
17+
});
18+
19+
test('transaction has default op', async () => {
20+
const transaction: Transaction = startTransaction?.apply(hub, [{}]);
21+
22+
expect(transaction).toEqual(expect.objectContaining({ op: 'default' }));
23+
});
24+
25+
test('transaction does not overwrite custom op', async () => {
26+
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
27+
28+
expect(transaction).toEqual(expect.objectContaining({ op: 'custom' }));
29+
});
30+
31+
test('transaction start span creates default op', async () => {
32+
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
33+
const span = transaction?.startChild();
34+
35+
expect(span).toEqual(expect.objectContaining({ op: 'default' }));
36+
});
37+
38+
test('transaction start span keeps custom op', async () => {
39+
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
40+
const span = transaction?.startChild({ op: 'custom' });
41+
42+
expect(span).toEqual(expect.objectContaining({ op: 'custom' }));
43+
});
44+
45+
test('transaction start span passes correct values to the child', async () => {
46+
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
47+
const span = transaction?.startChild({ op: 'custom' });
48+
49+
expect(span).toEqual(expect.objectContaining({
50+
transaction,
51+
parentSpanId: transaction.spanId,
52+
sampled: transaction.sampled,
53+
traceId: transaction.traceId,
54+
}));
55+
});
56+
});

0 commit comments

Comments
 (0)