Skip to content

ref(tracing): Record transaction name changes #5723

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
Sep 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/node-integration-tests/suites/express/tracing/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ test('should set a correct transaction name for routes specified in RegEx', asyn
transaction: 'GET /\\/test\\/regex/',
transaction_info: {
source: 'route',
changes: [],
changes: [
{
propagations: 0,
source: 'route',
timestamp: expect.any(Number),
},
],
propagations: 0,
},
contexts: {
Expand Down Expand Up @@ -68,7 +74,13 @@ test.each([['array1'], ['array5']])(
transaction: 'GET /test/array1,/\\/test\\/array[2-9]',
transaction_info: {
source: 'route',
changes: [],
changes: [
{
propagations: 0,
source: 'route',
timestamp: expect.any(Number),
},
],
propagations: 0,
},
contexts: {
Expand Down Expand Up @@ -106,7 +118,13 @@ test.each([
transaction: 'GET /test/arr/:id,/\\/test\\/arr[0-9]*\\/required(path)?(\\/optionalPath)?\\/(lastParam)?',
transaction_info: {
source: 'route',
changes: [],
changes: [
{
propagations: 0,
source: 'route',
timestamp: expect.any(Number),
},
],
propagations: 0,
},
contexts: {
Expand Down
21 changes: 16 additions & 5 deletions packages/tracing/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import {
TransactionContext,
TransactionMetadata,
} from '@sentry/types';
import { createBaggage, dropUndefinedKeys, getSentryBaggageItems, isBaggageMutable, logger } from '@sentry/utils';
import {
createBaggage,
dropUndefinedKeys,
getSentryBaggageItems,
isBaggageMutable,
logger,
timestampInSeconds,
} from '@sentry/utils';

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

Expand Down Expand Up @@ -60,18 +67,22 @@ export class Transaction extends SpanClass implements TransactionInterface {
return this._name;
}

/** Setter for `name` property, which also sets `source` */
/** Setter for `name` property, which also sets `source` as custom */
public set name(newName: string) {
this._name = newName;
this.metadata.source = 'custom';
this.setName(newName);
}
Comment on lines +70 to 73
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: Is there a reason we're maintaining this setter and setName? Always seemed a bit redundant to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

to be backwards compatible - we should maybe fix this for v8, will add a todo


/**
* JSDoc
*/
public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void {
this.name = name;
this._name = name;
this.metadata.source = source;
this.metadata.changes.push({
source,
timestamp: timestampInSeconds(),
propagations: this.metadata.propagations,
});
}

/**
Expand Down
80 changes: 80 additions & 0 deletions packages/tracing/test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ describe('`Transaction` class', () => {
expect(transaction.metadata.source).toEqual('custom');
});

it('updates transaction metadata with correct variables needed', () => {
const transaction = new Transaction({ name: 'dogpark' });
expect(transaction.metadata.changes).toEqual([]);

transaction.name = 'ballpit';

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
]);

transaction.metadata.propagations += 3;

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
]);

transaction.name = 'playground';

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 3,
},
]);
});

describe('`setName` method', () => {
it("sets source to `'custom'` if no source provided", () => {
const transaction = new Transaction({ name: 'dogpark' });
Expand All @@ -40,6 +80,46 @@ describe('`Transaction` class', () => {
expect(transaction.name).toEqual('ballpit');
expect(transaction.metadata.source).toEqual('route');
});

it('updates transaction metadata with correct variables needed', () => {
const transaction = new Transaction({ name: 'dogpark' });
expect(transaction.metadata.changes).toEqual([]);

transaction.name = 'ballpit';

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
]);

transaction.metadata.propagations += 3;

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
]);

transaction.setName('playground', 'task');

expect(transaction.metadata.changes).toEqual([
{
source: 'custom',
timestamp: expect.any(Number),
propagations: 0,
},
{
source: 'task',
timestamp: expect.any(Number),
propagations: 3,
},
]);
});
});
});
});