Skip to content

ref(core): Remove startTransaction & finishTransaction hooks #11008

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

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 17 additions & 18 deletions packages/bun/test/integrations/bunserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('Bun Serve Integration', () => {
});

test('generates a transaction around a request', async () => {
client.on('finishTransaction', transaction => {
expect(spanToJSON(transaction).status).toBe('ok');
expect(spanToJSON(transaction).data?.['http.response.status_code']).toEqual(200);
expect(spanToJSON(transaction).op).toEqual('http.server');
expect(spanToJSON(transaction).description).toEqual('GET /');
client.on('spanEnd', span => {
expect(spanToJSON(span).status).toBe('ok');
expect(spanToJSON(span).data?.['http.response.status_code']).toEqual(200);
expect(spanToJSON(span).op).toEqual('http.server');
expect(spanToJSON(span).description).toEqual('GET /');
});

const server = Bun.serve({
Expand All @@ -43,11 +43,11 @@ describe('Bun Serve Integration', () => {
});

test('generates a post transaction', async () => {
client.on('finishTransaction', transaction => {
expect(spanToJSON(transaction).status).toBe('ok');
expect(spanToJSON(transaction).data?.['http.response.status_code']).toEqual(200);
expect(spanToJSON(transaction).op).toEqual('http.server');
expect(spanToJSON(transaction).description).toEqual('POST /');
client.on('spanEnd', span => {
expect(spanToJSON(span).status).toBe('ok');
expect(spanToJSON(span).data?.['http.response.status_code']).toEqual(200);
expect(spanToJSON(span).op).toEqual('http.server');
expect(spanToJSON(span).description).toEqual('POST /');
});

const server = Bun.serve({
Expand All @@ -72,14 +72,13 @@ describe('Bun Serve Integration', () => {
const SENTRY_TRACE_HEADER = `${TRACE_ID}-${PARENT_SPAN_ID}-${PARENT_SAMPLED}`;
const SENTRY_BAGGAGE_HEADER = 'sentry-version=1.0,sentry-environment=production';

client.on('finishTransaction', transaction => {
expect(transaction.spanContext().traceId).toBe(TRACE_ID);
expect(transaction.parentSpanId).toBe(PARENT_SPAN_ID);
expect(spanIsSampled(transaction)).toBe(true);
// span.endTimestamp is already set in `finishTransaction` hook
expect(transaction.isRecording()).toBe(false);
client.on('spanEnd', span => {
expect(span.spanContext().traceId).toBe(TRACE_ID);
expect(spanToJSON(span).parent_span_id).toBe(PARENT_SPAN_ID);
expect(spanIsSampled(span)).toBe(true);
expect(span.isRecording()).toBe(false);

expect(getDynamicSamplingContextFromSpan(transaction)).toStrictEqual({
expect(getDynamicSamplingContextFromSpan(span)).toStrictEqual({
version: '1.0',
environment: 'production',
});
Expand All @@ -100,7 +99,7 @@ describe('Bun Serve Integration', () => {
});

test('does not create transactions for OPTIONS or HEAD requests', async () => {
client.on('finishTransaction', () => {
client.on('spanEnd', () => {
// This will never run, but we want to make sure it doesn't run.
expect(false).toEqual(true);
});
Expand Down
13 changes: 0 additions & 13 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
SeverityLevel,
Span,
StartSpanOptions,
Transaction,
TransactionEvent,
Transport,
TransportMakeRequestResponse,
Expand Down Expand Up @@ -417,12 +416,6 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
// Keep on() & emit() signatures in sync with types' client.ts interface
/* eslint-disable @typescript-eslint/unified-signatures */

/** @inheritdoc */
public on(hook: 'startTransaction', callback: (transaction: Transaction) => void): void;

/** @inheritdoc */
public on(hook: 'finishTransaction', callback: (transaction: Transaction) => void): void;

/** @inheritdoc */
public on(hook: 'spanStart', callback: (span: Span) => void): void;

Expand Down Expand Up @@ -476,12 +469,6 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
this._hooks[hook].push(callback);
}

/** @inheritdoc */
public emit(hook: 'startTransaction', transaction: Transaction): void;

/** @inheritdoc */
public emit(hook: 'finishTransaction', transaction: Transaction): void;

/** @inheritdoc */
public emit(hook: 'spanStart', span: Span): void;

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ function _startTransaction(transactionContext: TransactionContext): Transaction
},
});
if (client) {
client.emit('startTransaction', transaction);
client.emit('spanStart', transaction);
}
return transaction;
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ export class Transaction extends SentrySpan implements TransactionInterface {

// eslint-disable-next-line deprecation/deprecation
const client = this._hub.getClient();
if (client) {
client.emit('finishTransaction', this);
}

if (this._sampled !== true) {
// At this point if `sampled !== true` we want to discard the transaction.
Expand Down
16 changes: 1 addition & 15 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client, Envelope, Event, Transaction } from '@sentry/types';
import type { Client, Envelope, Event } from '@sentry/types';
import { SentryError, SyncPromise, dsnToString, logger } from '@sentry/utils';

import {
Expand Down Expand Up @@ -1935,20 +1935,6 @@ describe('BaseClient', () => {
] as const;

describe.each(scenarios)('with client %s', (_, client) => {
it('should call a startTransaction hook', () => {
expect.assertions(1);

const mockTransaction = {
traceId: '86f39e84263a4de99c326acab3bfe3bd',
} as Transaction;

client.on('startTransaction', transaction => {
expect(transaction).toEqual(mockTransaction);
});

client.emit('startTransaction', mockTransaction);
});

it('should call a beforeEnvelope hook', () => {
expect.assertions(1);

Expand Down
30 changes: 17 additions & 13 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
withActiveSpan,
} from '../../../src/tracing';
import { SentryNonRecordingSpan } from '../../../src/tracing/sentryNonRecordingSpan';
import { getActiveSpan, getSpanDescendants } from '../../../src/utils/spanUtils';
import { getActiveSpan, getRootSpan, getSpanDescendants } from '../../../src/utils/spanUtils';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';

beforeAll(() => {
Expand Down Expand Up @@ -84,8 +84,8 @@ describe('startSpan', () => {

it('creates a transaction', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
_span = span;
});
try {
await startSpan({ name: 'GET users/[id]' }, () => {
Expand All @@ -102,8 +102,8 @@ describe('startSpan', () => {

it('allows traceparent information to be overriden', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
_span = span;
});
try {
await startSpan(
Expand All @@ -129,8 +129,8 @@ describe('startSpan', () => {

it('allows for transaction to be mutated', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
_span = span;
});
try {
await startSpan({ name: 'GET users/[id]' }, span => {
Expand All @@ -146,8 +146,10 @@ describe('startSpan', () => {

it('creates a span with correct description', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
if (span === getRootSpan(span)) {
_span = span;
}
});
try {
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
Expand All @@ -170,8 +172,10 @@ describe('startSpan', () => {

it('allows for span to be mutated', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
if (span === getRootSpan(span)) {
_span = span;
}
});
try {
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
Expand Down Expand Up @@ -200,8 +204,8 @@ describe('startSpan', () => {
{ origin: 'manual', attributes: { 'sentry.origin': 'auto.http.browser' } },
])('correctly sets the span origin', async () => {
let _span: Span | undefined = undefined;
client.on('finishTransaction', transaction => {
_span = transaction;
client.on('spanEnd', span => {
_span = span;
});
try {
await startSpan({ name: 'GET users/[id]', origin: 'auto.http.browser' }, () => {
Expand Down
19 changes: 10 additions & 9 deletions packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { EventType, record } from '@sentry-internal/rrweb';
import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
captureException,
getActiveSpan,
getClient,
getCurrentScope,
getRootSpan,
spanToJSON,
} from '@sentry/core';
import type { ReplayRecordingMode, Transaction } from '@sentry/types';
import type { ReplayRecordingMode, Span } from '@sentry/types';
import { logger } from '@sentry/utils';

import {
Expand Down Expand Up @@ -87,10 +88,10 @@ export class ReplayContainer implements ReplayContainerInterface {
public recordingMode: ReplayRecordingMode;

/**
* The current or last active transcation.
* The current or last active span.
* This is only available when performance is enabled.
*/
public lastTransaction?: Transaction;
public lastActiveSpan?: Span;

/**
* These are here so we can overwrite them in tests etc.
Expand Down Expand Up @@ -720,16 +721,16 @@ export class ReplayContainer implements ReplayContainerInterface {
* This is only available if performance is enabled, and if an instrumented router is used.
*/
public getCurrentRoute(): string | undefined {
// eslint-disable-next-line deprecation/deprecation
const lastTransaction = this.lastTransaction || getCurrentScope().getTransaction();
const lastActiveSpan = this.lastActiveSpan || getActiveSpan();
const lastRootSpan = lastActiveSpan && getRootSpan(lastActiveSpan);

const attributes = (lastTransaction && spanToJSON(lastTransaction).data) || {};
const attributes = (lastRootSpan && spanToJSON(lastRootSpan).data) || {};
const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
if (!lastTransaction || !source || !['route', 'custom'].includes(source)) {
if (!lastRootSpan || !source || !['route', 'custom'].includes(source)) {
return undefined;
}

return spanToJSON(lastTransaction).description;
return spanToJSON(lastRootSpan).description;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/replay/src/types/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ReplayRecordingData,
ReplayRecordingMode,
SentryWrappedXMLHttpRequest,
Transaction,
Span,
XhrBreadcrumbHint,
} from '@sentry/types';

Expand Down Expand Up @@ -478,7 +478,7 @@ export interface ReplayContainer {
session: Session | undefined;
recordingMode: ReplayRecordingMode;
timeouts: Timeouts;
lastTransaction?: Transaction;
lastActiveSpan?: Span;
throttledAddEvent: (
event: RecordingEvent,
isCheckout?: boolean,
Expand Down
10 changes: 5 additions & 5 deletions packages/replay/src/util/addGlobalListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export function addGlobalListeners(replay: ReplayContainer): void {
}
});

client.on('startTransaction', transaction => {
replay.lastTransaction = transaction;
client.on('spanStart', span => {
replay.lastActiveSpan = span;
});

// We may be missing the initial startTransaction due to timing issues,
// We may be missing the initial spanStart due to timing issues,
// so we capture it on finish again.
client.on('finishTransaction', transaction => {
replay.lastTransaction = transaction;
client.on('spanEnd', span => {
replay.lastActiveSpan = span;
});

// We want to flush replay
Expand Down
Loading