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

feat(tracing): Add transaction source field #5367

Merged
merged 1 commit into from
Jul 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/tracing/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
}

/**
* Set metadata for this transaction.
* @hidden
* @inheritDoc
*/
public setMetadata(newMetadata: TransactionMetadata): void {
this.metadata = { ...this.metadata, ...newMetadata };
Expand Down Expand Up @@ -122,6 +121,8 @@ export class Transaction extends SpanClass implements TransactionInterface {
}).endTimestamp;
}

const metadata = this.metadata;

Lms24 marked this conversation as resolved.
Show resolved Hide resolved
const transaction: Event = {
contexts: {
trace: this.getTraceContext(),
Expand All @@ -133,9 +134,14 @@ export class Transaction extends SpanClass implements TransactionInterface {
transaction: this.name,
type: 'transaction',
sdkProcessingMetadata: {
...this.metadata,
...metadata,
baggage: this.getBaggage(),
},
...(metadata.source && {
transaction_info: {
source: metadata.source,
},
}),
};

const hasMeasurements = Object.keys(this._measurements).length > 0;
Expand Down
38 changes: 38 additions & 0 deletions packages/tracing/test/span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,42 @@ describe('Span', () => {
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual('');
});
});

describe('Transaction source', () => {
test('is not included by default', () => {
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
const transaction = hub.startTransaction({ name: 'test', sampled: true });
expect(spy).toHaveBeenCalledTimes(0);

transaction.finish();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(
expect.not.objectContaining({
transaction_info: {
source: expect.any(String),
},
}),
);
});

test('is included when transaction metadata is set', () => {
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
const transaction = hub.startTransaction({ name: 'test', sampled: true });
transaction.setMetadata({
source: 'url',
});
expect(spy).toHaveBeenCalledTimes(0);

transaction.finish();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(
expect.objectContaining({
transaction_info: {
source: 'url',
},
}),
);
});
});
});
4 changes: 4 additions & 0 deletions packages/types/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CaptureContext } from './scope';
import { SdkInfo } from './sdkinfo';
import { Severity, SeverityLevel } from './severity';
import { Span } from './span';
import { TransactionSource } from './transaction';
import { User } from './user';

/** JSDoc */
Expand Down Expand Up @@ -46,6 +47,9 @@ export interface Event {
debug_meta?: DebugMeta;
// A place to stash data which is needed at some point in the SDK's event processing pipeline but which shouldn't get sent to Sentry
sdkProcessingMetadata?: { [key: string]: any };
transaction_info?: {
source: TransactionSource;
};
Lms24 marked this conversation as resolved.
Show resolved Hide resolved
}

/** JSDoc */
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 @@ -71,6 +71,7 @@ export type {
TransactionContext,
TransactionMetadata,
TransactionSamplingMethod,
TransactionSource,
} from './transaction';
export type {
DurationUnit,
Expand Down
29 changes: 29 additions & 0 deletions packages/types/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export interface Transaction extends TransactionContext, Span {
/** Updates the current transaction with a new `TransactionContext` */
updateWithContext(transactionContext: TransactionContext): this;

/**
* Set metadata for this transaction.
* @hidden
*/
setMetadata(newMetadata: TransactionMetadata): void;

/** return the baggage for dynamic sampling and trace propagation */
getBaggage(): Baggage;
}
Expand Down Expand Up @@ -138,4 +144,27 @@ export interface TransactionMetadata {

/** For transactions tracing server-side request handling, the path of the request being tracked. */
requestPath?: string;

/** Information on how a transaction name was generated. */
source?: TransactionSource;
}

/**
* Contains information about how the name of the transaction was determined. This will be used by the server to decide
* whether or not to scrub identifiers from the transaction name, or replace the entire name with a placeholder.
*/
export type TransactionSource =
/** User-defined name */
| 'custom'
/** Raw URL, potentially containing identifiers */
| 'url'
/** Parametrized URL / route */
| 'route'
/** Name of the view handling the request */
| 'view'
/** This is the default value set by Relay for legacy SDKs. */
| 'unknown'
/** Named after a software component, such as a function or class name. */
| 'component'
/** Name of a background task (e.g. a Celery task) */
| 'task';
Lms24 marked this conversation as resolved.
Show resolved Hide resolved