Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction op default #2691

Merged
merged 7 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

### Fix

- `Sentry.startTransaction` doesn't require `op` ([#2691](https://github.com/getsentry/sentry-react-native/pull/2691))

### Dependencies

- Bump Cocoa SDK from v7.31.2 to v7.31.3 ([#2647](https://github.com/getsentry/sentry-react-native/pull/2647))
Expand Down
21 changes: 19 additions & 2 deletions src/js/measurements.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getCurrentHub, getMainCarrier, Hub } from '@sentry/core';
import { Transaction } from '@sentry/tracing';
import { CustomSamplingContext, TransactionContext } from '@sentry/types';
import { CustomSamplingContext, Span, SpanContext, TransactionContext } from '@sentry/types';

import { ReactNativeTracing } from './tracing';

const SPAN_OP_DEFAULT = 'default';

/**
* Adds React Native's extensions. Needs to be called after @sentry/tracing's extension methods are added
*/
Expand All @@ -28,7 +30,7 @@ export function _addTracingExtensions(): void {
}
}

type StartTransactionFunction = (
export type StartTransactionFunction = (
this: Hub,
transactionContext: TransactionContext,
customSamplingContext?: CustomSamplingContext
Expand All @@ -48,10 +50,25 @@ const _patchStartTransaction = (
transactionContext: TransactionContext,
customSamplingContext?: CustomSamplingContext
): Transaction {
// Native SDKs require op to be set - for JS Relay sets `default`
if (!transactionContext.op) {
transactionContext.op = SPAN_OP_DEFAULT;
}

const transaction: Transaction = originalStartTransaction.apply(this, [
transactionContext,
customSamplingContext,
]);
const originalStartChild: Transaction['startChild'] = transaction.startChild.bind(transaction);
transaction.startChild = (
spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'sampled' | 'traceId' | 'parentSpanId'>>,
): Span => {
return originalStartChild({
...spanContext,
// Native SDKs require op to be set
op: spanContext?.op || SPAN_OP_DEFAULT,
});
};

const reactNativeTracing = getCurrentHub().getIntegration(
ReactNativeTracing
Expand Down
56 changes: 56 additions & 0 deletions test/measurements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Carrier, getCurrentHub, getMainCarrier } from '@sentry/core';
import { Transaction } from '@sentry/tracing';
import { Hub } from '@sentry/types';

import { _addTracingExtensions, StartTransactionFunction } from '../src/js/measurements';

describe('Tracing extensions', () => {
let hub: Hub;
let carrier: Carrier;
let startTransaction: StartTransactionFunction | undefined;

beforeEach(() => {
_addTracingExtensions();
hub = getCurrentHub();
carrier = getMainCarrier();
startTransaction = carrier.__SENTRY__?.extensions?.startTransaction as StartTransactionFunction | undefined;
});

test('transaction has default op', async () => {
const transaction: Transaction = startTransaction?.apply(hub, [{}]);

expect(transaction).toEqual(expect.objectContaining({ op: 'default' }));
});

test('transaction does not overwrite custom op', async () => {
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);

expect(transaction).toEqual(expect.objectContaining({ op: 'custom' }));
});

test('transaction start span creates default op', async () => {
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
const span = transaction?.startChild();

expect(span).toEqual(expect.objectContaining({ op: 'default' }));
});

test('transaction start span keeps custom op', async () => {
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
const span = transaction?.startChild({ op: 'custom' });

expect(span).toEqual(expect.objectContaining({ op: 'custom' }));
});

test('transaction start span passes correct values to the child', async () => {
const transaction: Transaction = startTransaction?.apply(hub, [{ op: 'custom' }]);
const span = transaction?.startChild({ op: 'custom' });

expect(span).toEqual(expect.objectContaining({
transaction,
parentSpanId: transaction.spanId,
sampled: transaction.sampled,
traceId: transaction.traceId,
}));
});
});