Skip to content

Commit

Permalink
feat(tracing): Add setName method on spans (#8725)
Browse files Browse the repository at this point in the history
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' });
```
  • Loading branch information
AbhiPrasad committed Aug 10, 2023
1 parent f30da60 commit 6346e53
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/tracing/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export class Span implements SpanInterface {
if (spanContext.description) {
this.description = spanContext.description;
}
if (spanContext.name) {
this.description = spanContext.name;
}
if (spanContext.data) {
this.data = spanContext.data;
}
Expand Down Expand Up @@ -243,6 +246,13 @@ export class Span implements SpanInterface {
return this;
}

/**
* @inheritDoc
*/
public setName(name: string): void {
this.description = name;
}

/**
* @inheritDoc
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class Transaction extends SpanClass implements TransactionInterface {
*/
public constructor(transactionContext: TransactionContext, hub?: Hub) {
super(transactionContext);
// We need to delete description since it's set by the Span class constructor
// but not needed for transactions.
delete this.description;

this._measurements = {};
this._contexts = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/sveltekit/test/server/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('handleSentry', () => {
expect(ref.spanRecorder.spans).toHaveLength(2);
expect(ref.spanRecorder.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({ op: 'http.server', description: 'GET /users/[id]' }),
expect.objectContaining({ op: 'http.server', name: 'GET /users/[id]' }),
expect.objectContaining({ op: 'http.server', description: 'GET api/users/details/[id]' }),
]),
);
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/test/span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ describe('Span', () => {
span.setData('foo', true);
expect(span.data.foo).toBe(true);
});

test('setName', () => {
const span = new Span({});
expect(span.description).toBeUndefined();
span.setName('foo');
expect(span.description).toBe('foo');
});
});

describe('status', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface SpanContext {
*/
description?: string;

/**
* Human-readable identifier for the span. Alias for span.description.
*/
name?: string;

/**
* Operation of the Span.
*/
Expand Down Expand Up @@ -139,6 +144,11 @@ export interface Span extends SpanContext {
*/
setHttpStatus(httpStatus: number): this;

/**
* Set the name of the span.
*/
setName(name: string): void;

/**
* Creates a new `Span` while setting the current `Span.id` as `parentSpanId`.
* Also the `sampled` decision will be inherited.
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type TraceparentData = Pick<TransactionContext, 'traceId' | 'parentSpanId
/**
* Transaction "Class", inherits Span only has `setName`
*/
export interface Transaction extends TransactionContext, Span {
export interface Transaction extends TransactionContext, Omit<Span, 'setName' | 'name'> {
/**
* @inheritDoc
*/
Expand Down

0 comments on commit 6346e53

Please sign in to comment.