Skip to content

Commit 6346e53

Browse files
authored
feat(tracing): Add setName method on spans (#8725)
In preparation for adding the new span creation methods, `startSpan` and `startActiveSpan`, this PR adds `name` to `spanContext`. Over time we are going to be moving away from `span.description`, and this is the first step to get there. For now, `name` is just an alias for `span.description`, and is only added so that users can do something like so relatively easily: ```js const span = span.startChild({ name: 'NAME HERE' }); ```
1 parent f30da60 commit 6346e53

File tree

6 files changed

+32
-2
lines changed

6 files changed

+32
-2
lines changed

packages/core/src/tracing/span.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export class Span implements SpanInterface {
152152
if (spanContext.description) {
153153
this.description = spanContext.description;
154154
}
155+
if (spanContext.name) {
156+
this.description = spanContext.name;
157+
}
155158
if (spanContext.data) {
156159
this.data = spanContext.data;
157160
}
@@ -243,6 +246,13 @@ export class Span implements SpanInterface {
243246
return this;
244247
}
245248

249+
/**
250+
* @inheritDoc
251+
*/
252+
public setName(name: string): void {
253+
this.description = name;
254+
}
255+
246256
/**
247257
* @inheritDoc
248258
*/

packages/core/src/tracing/transaction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class Transaction extends SpanClass implements TransactionInterface {
4444
*/
4545
public constructor(transactionContext: TransactionContext, hub?: Hub) {
4646
super(transactionContext);
47+
// We need to delete description since it's set by the Span class constructor
48+
// but not needed for transactions.
49+
delete this.description;
4750

4851
this._measurements = {};
4952
this._contexts = {};

packages/sveltekit/test/server/handle.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe('handleSentry', () => {
201201
expect(ref.spanRecorder.spans).toHaveLength(2);
202202
expect(ref.spanRecorder.spans).toEqual(
203203
expect.arrayContaining([
204-
expect.objectContaining({ op: 'http.server', description: 'GET /users/[id]' }),
204+
expect.objectContaining({ op: 'http.server', name: 'GET /users/[id]' }),
205205
expect.objectContaining({ op: 'http.server', description: 'GET api/users/details/[id]' }),
206206
]),
207207
);

packages/tracing/test/span.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ describe('Span', () => {
8282
span.setData('foo', true);
8383
expect(span.data.foo).toBe(true);
8484
});
85+
86+
test('setName', () => {
87+
const span = new Span({});
88+
expect(span.description).toBeUndefined();
89+
span.setName('foo');
90+
expect(span.description).toBe('foo');
91+
});
8592
});
8693

8794
describe('status', () => {

packages/types/src/span.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export interface SpanContext {
99
*/
1010
description?: string;
1111

12+
/**
13+
* Human-readable identifier for the span. Alias for span.description.
14+
*/
15+
name?: string;
16+
1217
/**
1318
* Operation of the Span.
1419
*/
@@ -139,6 +144,11 @@ export interface Span extends SpanContext {
139144
*/
140145
setHttpStatus(httpStatus: number): this;
141146

147+
/**
148+
* Set the name of the span.
149+
*/
150+
setName(name: string): void;
151+
142152
/**
143153
* Creates a new `Span` while setting the current `Span.id` as `parentSpanId`.
144154
* Also the `sampled` decision will be inherited.

packages/types/src/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type TraceparentData = Pick<TransactionContext, 'traceId' | 'parentSpanId
4242
/**
4343
* Transaction "Class", inherits Span only has `setName`
4444
*/
45-
export interface Transaction extends TransactionContext, Span {
45+
export interface Transaction extends TransactionContext, Omit<Span, 'setName' | 'name'> {
4646
/**
4747
* @inheritDoc
4848
*/

0 commit comments

Comments
 (0)