Skip to content

Commit f9181d2

Browse files
authored
fix(nestjs): Add missing sentry.origin span attribute to SentryTraced decorator (#17318)
Every automatically started span from our instrumentation should include an origin attribute. Also added the `sentry.op` attribute for completeness
1 parent 7264677 commit f9181d2

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

dev-packages/e2e-tests/test-applications/nestjs-11/tests/span-decorator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ test('Transaction includes span and correct value for decorated async function',
2222
span_id: expect.stringMatching(/[a-f0-9]{16}/),
2323
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
2424
data: {
25-
'sentry.origin': 'manual',
25+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
2626
'sentry.op': 'wait and return a string',
2727
},
2828
description: 'wait',
2929
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
3030
start_timestamp: expect.any(Number),
3131
status: 'ok',
3232
op: 'wait and return a string',
33-
origin: 'manual',
33+
origin: 'auto.function.nestjs.sentry_traced',
3434
}),
3535
]),
3636
);
@@ -57,15 +57,15 @@ test('Transaction includes span and correct value for decorated sync function',
5757
span_id: expect.stringMatching(/[a-f0-9]{16}/),
5858
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
5959
data: {
60-
'sentry.origin': 'manual',
60+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
6161
'sentry.op': 'return a string',
6262
},
6363
description: 'getString',
6464
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
6565
start_timestamp: expect.any(Number),
6666
status: 'ok',
6767
op: 'return a string',
68-
origin: 'manual',
68+
origin: 'auto.function.nestjs.sentry_traced',
6969
}),
7070
]),
7171
);

dev-packages/e2e-tests/test-applications/nestjs-8/tests/span-decorator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ test('Transaction includes span and correct value for decorated async function',
2222
span_id: expect.stringMatching(/[a-f0-9]{16}/),
2323
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
2424
data: {
25-
'sentry.origin': 'manual',
25+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
2626
'sentry.op': 'wait and return a string',
2727
},
2828
description: 'wait',
2929
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
3030
start_timestamp: expect.any(Number),
3131
status: 'ok',
3232
op: 'wait and return a string',
33-
origin: 'manual',
33+
origin: 'auto.function.nestjs.sentry_traced',
3434
}),
3535
]),
3636
);
@@ -57,15 +57,15 @@ test('Transaction includes span and correct value for decorated sync function',
5757
span_id: expect.stringMatching(/[a-f0-9]{16}/),
5858
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
5959
data: {
60-
'sentry.origin': 'manual',
60+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
6161
'sentry.op': 'return a string',
6262
},
6363
description: 'getString',
6464
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
6565
start_timestamp: expect.any(Number),
6666
status: 'ok',
6767
op: 'return a string',
68-
origin: 'manual',
68+
origin: 'auto.function.nestjs.sentry_traced',
6969
}),
7070
]),
7171
);

dev-packages/e2e-tests/test-applications/nestjs-basic/tests/span-decorator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ test('Transaction includes span and correct value for decorated async function',
2222
span_id: expect.stringMatching(/[a-f0-9]{16}/),
2323
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
2424
data: {
25-
'sentry.origin': 'manual',
25+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
2626
'sentry.op': 'wait and return a string',
2727
},
2828
description: 'wait',
2929
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
3030
start_timestamp: expect.any(Number),
3131
status: 'ok',
3232
op: 'wait and return a string',
33-
origin: 'manual',
33+
origin: 'auto.function.nestjs.sentry_traced',
3434
}),
3535
]),
3636
);
@@ -57,15 +57,15 @@ test('Transaction includes span and correct value for decorated sync function',
5757
span_id: expect.stringMatching(/[a-f0-9]{16}/),
5858
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
5959
data: {
60-
'sentry.origin': 'manual',
60+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
6161
'sentry.op': 'return a string',
6262
},
6363
description: 'getString',
6464
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
6565
start_timestamp: expect.any(Number),
6666
status: 'ok',
6767
op: 'return a string',
68-
origin: 'manual',
68+
origin: 'auto.function.nestjs.sentry_traced',
6969
}),
7070
]),
7171
);

dev-packages/e2e-tests/test-applications/nestjs-fastify/tests/span-decorator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ test('Transaction includes span and correct value for decorated async function',
2222
span_id: expect.stringMatching(/[a-f0-9]{16}/),
2323
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
2424
data: {
25-
'sentry.origin': 'manual',
25+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
2626
'sentry.op': 'wait and return a string',
2727
},
2828
description: 'wait',
2929
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
3030
start_timestamp: expect.any(Number),
3131
status: 'ok',
3232
op: 'wait and return a string',
33-
origin: 'manual',
33+
origin: 'auto.function.nestjs.sentry_traced',
3434
}),
3535
]),
3636
);
@@ -57,15 +57,15 @@ test('Transaction includes span and correct value for decorated sync function',
5757
span_id: expect.stringMatching(/[a-f0-9]{16}/),
5858
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
5959
data: {
60-
'sentry.origin': 'manual',
60+
'sentry.origin': 'auto.function.nestjs.sentry_traced',
6161
'sentry.op': 'return a string',
6262
},
6363
description: 'getString',
6464
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
6565
start_timestamp: expect.any(Number),
6666
status: 'ok',
6767
op: 'return a string',
68-
origin: 'manual',
68+
origin: 'auto.function.nestjs.sentry_traced',
6969
}),
7070
]),
7171
);

packages/nestjs/src/decorators.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { MonitorConfig } from '@sentry/core';
2-
import { captureException, isThenable } from '@sentry/core';
2+
import {
3+
captureException,
4+
isThenable,
5+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
6+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
7+
} from '@sentry/core';
38
import * as Sentry from '@sentry/node';
49
import { startSpan } from '@sentry/node';
510
import { isExpectedError } from './helpers';
@@ -52,6 +57,10 @@ export function SentryTraced(op: string = 'function') {
5257
{
5358
op: op,
5459
name: propertyKey,
60+
attributes: {
61+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nestjs.sentry_traced',
62+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
63+
},
5564
},
5665
() => {
5766
return originalMethod.apply(this, args);

packages/nestjs/test/decorators.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'reflect-metadata';
22
import * as core from '@sentry/core';
3+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
34
import { beforeEach, describe, expect, it, vi } from 'vitest';
45
import { SentryCron, SentryExceptionCaptured, SentryTraced } from '../src/decorators';
56
import * as helpers from '../src/helpers';
@@ -38,6 +39,10 @@ describe('SentryTraced decorator', () => {
3839
{
3940
op: 'test-operation',
4041
name: 'testMethod',
42+
attributes: {
43+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nestjs.sentry_traced',
44+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'test-operation',
45+
},
4146
},
4247
expect.any(Function),
4348
);
@@ -67,6 +72,10 @@ describe('SentryTraced decorator', () => {
6772
{
6873
op: 'function', // default value
6974
name: 'testDefaultOp',
75+
attributes: {
76+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nestjs.sentry_traced',
77+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'function',
78+
},
7079
},
7180
expect.any(Function),
7281
);
@@ -96,6 +105,10 @@ describe('SentryTraced decorator', () => {
96105
{
97106
op: 'sync-operation',
98107
name: 'syncMethod',
108+
attributes: {
109+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nestjs.sentry_traced',
110+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'sync-operation',
111+
},
99112
},
100113
expect.any(Function),
101114
);

0 commit comments

Comments
 (0)