-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwithSentry.ts
More file actions
69 lines (62 loc) · 2.45 KB
/
withSentry.ts
File metadata and controls
69 lines (62 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/node';
import { extractServerFunctionSha256 } from './utils';
export type ServerEntry = {
fetch: (request: Request, opts?: unknown) => Promise<Response> | Response;
};
/**
* This function can be used to wrap the server entry request handler to add tracing to server-side functionality.
* You must explicitly define a server entry point in your application for this to work. This is done by passing the request handler to the `createServerEntry` function.
* For more information about the server entry point, see the [TanStack Start documentation](https://tanstack.com/start/docs/server-entry).
*
* @example
* ```ts
* import { withSentry } from '@sentry/tanstackstart-react';
*
* import handler, { createServerEntry } from '@tanstack/react-start/server-entry';
* import type { ServerEntry } from '@tanstack/react-start/server-entry';
*
* const requestHandler: ServerEntry = withSentry({
* fetch(request: Request) {
* return handler.fetch(request);
* },
* });
*
* export default serverEntry = createServerEntry(requestHandler);
* ```
*
* @param serverEntry - request handler to wrap
* @returns - wrapped request handler
*/
export function withSentry(serverEntry: ServerEntry): ServerEntry {
if (serverEntry.fetch) {
serverEntry.fetch = new Proxy<typeof serverEntry.fetch>(serverEntry.fetch, {
apply: (target, thisArg, args) => {
const request: Request = args[0];
const url = new URL(request.url);
const method = request.method || 'GET';
// instrument server functions
if (url.pathname.includes('_serverFn') || url.pathname.includes('createServerFn')) {
const functionSha256 = extractServerFunctionSha256(url.pathname);
const op = 'function.tanstackstart';
const serverFunctionSpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.tanstackstart.server',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
'tanstackstart.function.hash.sha256': functionSha256,
};
return startSpan(
{
op: op,
name: `${method} ${url.pathname}`,
attributes: serverFunctionSpanAttributes,
},
() => {
return target.apply(thisArg, args);
},
);
}
return target.apply(thisArg, args);
},
});
}
return serverEntry;
}