Skip to content

feat(tracing): Add setName method on spans #8725

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 5 commits into from
Aug 10, 2023
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
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 {
Copy link
Member

Choose a reason for hiding this comment

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

No action required: Should we think about adding span name source field? I was just thinking that in a world of #onlyspans we probably still need a way to annotate the name source, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

For now the answer is no - but we'll come back to this.

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