Skip to content

Commit c5c67a3

Browse files
committed
set source to 'custom' when name is changed directly on transaction
1 parent 0e2898b commit c5c67a3

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

packages/tracing/src/transaction.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import { Span as SpanClass, SpanRecorder } from './span';
1515

1616
/** JSDoc */
1717
export class Transaction extends SpanClass implements TransactionInterface {
18-
public name: string;
19-
2018
public metadata: TransactionMetadata;
2119

2220
/**
2321
* The reference to the current hub.
2422
*/
2523
public readonly _hub: Hub;
2624

25+
private _name: string;
26+
2727
private _measurements: Measurements = {};
2828

2929
private _trimEnd?: boolean;
@@ -40,7 +40,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
4040

4141
this._hub = hub || getCurrentHub();
4242

43-
this.name = transactionContext.name || '';
43+
this._name = transactionContext.name || '';
4444

4545
this.metadata = transactionContext.metadata || {};
4646
this._trimEnd = transactionContext.trimEnd;
@@ -49,11 +49,23 @@ export class Transaction extends SpanClass implements TransactionInterface {
4949
this.transaction = this;
5050
}
5151

52+
/** Getter for `name` property */
53+
public get name(): string {
54+
return this._name;
55+
}
56+
57+
/** Setter for `name` property, which also sets `source` */
58+
public set name(newName: string) {
59+
this._name = newName;
60+
this.metadata.source = 'custom';
61+
}
62+
5263
/**
5364
* JSDoc
5465
*/
55-
public setName(name: string): void {
66+
public setName(name: string, source: TransactionMetadata['source'] = 'custom'): void {
5667
this.name = name;
68+
this.metadata.source = source;
5769
}
5870

5971
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Transaction } from '../src/transaction';
2+
3+
describe('`Transaction` class', () => {
4+
describe('transaction name source', () => {
5+
it('sets source in constructor if provided', () => {
6+
const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'route' } });
7+
8+
expect(transaction.name).toEqual('dogpark');
9+
expect(transaction.metadata.source).toEqual('route');
10+
});
11+
12+
it("doesn't set source in constructor if not provided", () => {
13+
const transaction = new Transaction({ name: 'dogpark' });
14+
15+
expect(transaction.name).toEqual('dogpark');
16+
expect(transaction.metadata.source).toBeUndefined();
17+
});
18+
19+
it("sets source to `'custom'` when assigning to `name` property", () => {
20+
const transaction = new Transaction({ name: 'dogpark' });
21+
transaction.name = 'ballpit';
22+
23+
expect(transaction.name).toEqual('ballpit');
24+
expect(transaction.metadata.source).toEqual('custom');
25+
});
26+
27+
describe('`setName` method', () => {
28+
it("sets source to `'custom'` if no source provided", () => {
29+
const transaction = new Transaction({ name: 'dogpark' });
30+
transaction.setName('ballpit');
31+
32+
expect(transaction.name).toEqual('ballpit');
33+
expect(transaction.metadata.source).toEqual('custom');
34+
});
35+
36+
it('uses given `source` value', () => {
37+
const transaction = new Transaction({ name: 'dogpark' });
38+
transaction.setName('ballpit', 'route');
39+
40+
expect(transaction.name).toEqual('ballpit');
41+
expect(transaction.metadata.source).toEqual('route');
42+
});
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)