Skip to content

draft: Add metadata around transaction name changes #5709

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

Closed
wants to merge 8 commits into from
Closed
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
24 changes: 24 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
SyncPromise,
truncate,
uuid4,
timestampInSeconds,
} from '@sentry/utils';

import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
Expand Down Expand Up @@ -653,6 +654,29 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
throw new SentryError('`beforeSend` returned `null`, will not send event.', 'log');
}

const transactionInfo = processedEvent.transaction_info;
if (
processedEvent.type === 'transaction' &&
transactionInfo &&
processedEvent.transaction &&
processedEvent.transaction !== event.transaction
) {
const source = 'custom';
event.transaction_info = {
...transactionInfo,
source,
name_changes: [
...transactionInfo.name_changes,
{
name: processedEvent.transaction,
source,
timestamp: timestampInSeconds(),
propagations: transactionInfo.propagations,
},
],
};
}

const session = scope && scope.getSession();
if (!isTransaction && session) {
this._updateSessionFromEvent(session, processedEvent);
Expand Down
5 changes: 5 additions & 0 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ function _createWrappedRequestMethodFactory(
addRequestBreadcrumb('response', requestUrl, req, res);
}
if (tracingEnabled && span) {
const transaction = span.transaction;
if (transaction) {
transaction.metadata.propagations += 1;
}

if (res.statusCode) {
span.setHttpStatus(res.statusCode);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export function fetchCallback(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const options = (handlerData.args[1] = (handlerData.args[1] as { [key: string]: any }) || {});
options.headers = addTracingHeaders(request, activeTransaction.getBaggage(), span, options);
activeTransaction.metadata.propagations += 1;
}
}

Expand Down Expand Up @@ -304,6 +305,7 @@ export function xhrCallback(
BAGGAGE_HEADER_NAME,
mergeAndSerializeBaggage(activeTransaction.getBaggage(), headerBaggageString),
);
activeTransaction.metadata.propagations += 1;
} catch (_) {
// Error: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export { BrowserTracing, BROWSER_TRACING_INTEGRATION_ID } from './browser';
export { Span, spanStatusfromHttpCode } from './span';
// eslint-disable-next-line deprecation/deprecation
export { SpanStatus } from './spanstatus';
export { Transaction } from './transaction';
export { Transaction, generateTransactionNameChange } from './transaction';
export { instrumentOutgoingRequests, defaultRequestInstrumentationOptions } from './browser';
export { IdleTransaction } from './idletransaction';
export { startIdleTransaction } from './hubextensions';
Expand Down
29 changes: 27 additions & 2 deletions packages/tracing/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ import {
Transaction as TransactionInterface,
TransactionContext,
TransactionMetadata,
TransactionNameChange,
TransactionSource,
} from '@sentry/types';
import { createBaggage, dropUndefinedKeys, getSentryBaggageItems, isBaggageMutable, logger } from '@sentry/utils';
import {
createBaggage,
dropUndefinedKeys,
getSentryBaggageItems,
isBaggageMutable,
logger,
timestampWithMs,
} from '@sentry/utils';

import { Span as SpanClass, SpanRecorder } from './span';

Expand Down Expand Up @@ -45,6 +54,8 @@ export class Transaction extends SpanClass implements TransactionInterface {
this.metadata = {
...transactionContext.metadata,
spanMetadata: {},
nameChanges: [],
propagations: 0,
};

this._trimEnd = transactionContext.trimEnd;
Expand All @@ -61,7 +72,9 @@ export class Transaction extends SpanClass implements TransactionInterface {
/** Setter for `name` property, which also sets `source` */
public set name(newName: string) {
this._name = newName;
this.metadata.source = 'custom';
const source = 'custom';
this.metadata.source = source;
this.metadata.nameChanges.push(generateTransactionNameChange(source, this.metadata.propagations));
}

/**
Expand All @@ -70,6 +83,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void {
this.name = name;
this.metadata.source = source;
this.metadata.nameChanges.push(generateTransactionNameChange(source, this.metadata.propagations));
}

/**
Expand Down Expand Up @@ -156,6 +170,8 @@ export class Transaction extends SpanClass implements TransactionInterface {
...(metadata.source && {
transaction_info: {
source: metadata.source,
name_changes: metadata.nameChanges,
propagations: metadata.propagations,
},
}),
};
Expand Down Expand Up @@ -273,3 +289,12 @@ export class Transaction extends SpanClass implements TransactionInterface {
);
}
}

/** Generate objects representing a transaction name change */
export function generateTransactionNameChange(source: TransactionSource, propagations: number): TransactionNameChange {
return {
source,
timestamp: timestampWithMs(),
propagations,
};
}
5 changes: 4 additions & 1 deletion packages/types/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CaptureContext } from './scope';
import { SdkInfo } from './sdkinfo';
import { Severity, SeverityLevel } from './severity';
import { Span } from './span';
import { TransactionSource } from './transaction';
import { TransactionNameChange, TransactionSource } from './transaction';
import { User } from './user';

/** JSDoc */
Expand Down Expand Up @@ -49,6 +49,9 @@ export interface Event {
sdkProcessingMetadata?: { [key: string]: any };
transaction_info?: {
source: TransactionSource;
name_changes: TransactionNameChange[];
// The total number of propagations that happened
propagations: number;
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type {
TransactionMetadata,
TransactionSamplingMethod,
TransactionSource,
TransactionNameChange,
} from './transaction';
export type {
DurationUnit,
Expand Down
18 changes: 18 additions & 0 deletions packages/types/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ export interface TransactionMetadata {

/** Metadata for the transaction's spans, keyed by spanId */
spanMetadata: { [spanId: string]: { [key: string]: unknown } };

/** Metadata representing information about transaction name changes */
nameChanges: TransactionNameChange[];

/** The total number of propagations that happened */
propagations: number;
}

/**
Expand All @@ -169,3 +175,15 @@ export type TransactionSource =
| 'component'
/** Name of a background task (e.g. a Celery task) */
| 'task';

/**
* Object representing metadata about when a transaction name was changed.
*/
export interface TransactionNameChange {
// unix timestamp when the name was changed
timestamp: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR: The develop spec and Relay protocol definition should use the same type as other timestamps in the protocol, allowing for ISO strings in addition to numbers.

// new source
source: TransactionSource;
// number of propagations since start of transaction.
propagations: number;
}