-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nestjs): Add function-level span decorator to nestjs (#12721)
- Loading branch information
1 parent
f8f3c98
commit 41d946e
Showing
6 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dev-packages/e2e-tests/test-applications/nestjs/tests/span-decorator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
||
test('Transaction includes span and correct value for decorated async function', async ({ baseURL }) => { | ||
const transactionEventPromise = waitForTransaction('nestjs', transactionEvent => { | ||
return ( | ||
transactionEvent?.contexts?.trace?.op === 'http.server' && | ||
transactionEvent?.transaction === 'GET /test-span-decorator-async' | ||
); | ||
}); | ||
|
||
const response = await fetch(`${baseURL}/test-span-decorator-async`); | ||
const body = await response.json(); | ||
|
||
expect(body.result).toEqual('test'); | ||
|
||
const transactionEvent = await transactionEventPromise; | ||
|
||
expect(transactionEvent.spans).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
data: { | ||
'sentry.origin': 'manual', | ||
'sentry.op': 'wait and return a string', | ||
'otel.kind': 'INTERNAL', | ||
}, | ||
description: 'wait', | ||
parent_span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
status: 'ok', | ||
op: 'wait and return a string', | ||
origin: 'manual', | ||
}), | ||
]), | ||
); | ||
}); | ||
|
||
test('Transaction includes span and correct value for decorated sync function', async ({ baseURL }) => { | ||
const transactionEventPromise = waitForTransaction('nestjs', transactionEvent => { | ||
return ( | ||
transactionEvent?.contexts?.trace?.op === 'http.server' && | ||
transactionEvent?.transaction === 'GET /test-span-decorator-sync' | ||
); | ||
}); | ||
|
||
const response = await fetch(`${baseURL}/test-span-decorator-sync`); | ||
const body = await response.json(); | ||
|
||
expect(body.result).toEqual('test'); | ||
|
||
const transactionEvent = await transactionEventPromise; | ||
|
||
expect(transactionEvent.spans).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
data: { | ||
'sentry.origin': 'manual', | ||
'sentry.op': 'return a string', | ||
'otel.kind': 'INTERNAL', | ||
}, | ||
description: 'getString', | ||
parent_span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
status: 'ok', | ||
op: 'return a string', | ||
origin: 'manual', | ||
}), | ||
]), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from '@sentry/node'; | ||
|
||
export { init } from './sdk'; | ||
|
||
export { SentryTraced } from './span-decorator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { startSpan } from '@sentry/node'; | ||
|
||
/** | ||
* A decorator usable to wrap arbitrary functions with spans. | ||
*/ | ||
export function SentryTraced(op: string = 'function') { | ||
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
descriptor.value = function (...args: any[]) { | ||
return startSpan( | ||
{ | ||
op: op, | ||
name: propertyKey, | ||
}, | ||
async () => { | ||
return originalMethod.apply(this, args); | ||
}, | ||
); | ||
}; | ||
return descriptor; | ||
}; | ||
} |