Skip to content

Commit c1d5251

Browse files
committed
proxy fetch with withSentry
1 parent 320aa98 commit c1d5251

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import handler, { createServerEntry } from '@tanstack/react-start/server-entry'
2-
import type { ServerEntry } from '@tanstack/react-start/server-entry'
1+
import { withSentry } from '@sentry/tanstackstart-react';
32

4-
const requestHandler: ServerEntry = {
3+
import handler, { createServerEntry } from '@tanstack/react-start/server-entry';
4+
import type { ServerEntry } from '@tanstack/react-start/server-entry';
5+
6+
const requestHandler: ServerEntry = withSentry({
57
fetch(request: Request) {
68
console.log('requestHandler fetch')
79
console.log(request)
810
return handler.fetch(request)
911
},
10-
}
12+
});
1113

1214
export default createServerEntry(requestHandler);

packages/tanstackstart-react/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export * from '@sentry/node';
55

66
export { init } from './sdk';
7+
export { withSentry } from './withSentry';
78

89
/**
910
* A passthrough error boundary for the server that doesn't depend on any react. Error boundaries don't catch SSR errors
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/node';
2+
3+
export type ServerEntry = { fetch?: (request: Request) => Promise<unknown> }
4+
5+
/**
6+
*
7+
* @param serverEntry - server entry function to wrap
8+
* @returns - wrapped server entry function
9+
*/
10+
export function withSentry(serverEntry: ServerEntry): ServerEntry {
11+
if (serverEntry.fetch) {
12+
serverEntry.fetch = new Proxy<typeof serverEntry.fetch>(serverEntry.fetch, {
13+
apply: async (target, thisArg, args) => {
14+
console.log(args[0].url?.toString());
15+
console.log('serverFn: ',args[0].url?.includes('_serverFn') || args[0].url?.includes('createServerFn'))
16+
if (args[0].url?.includes('_serverFn') || args[0].url?.includes('createServerFn')) {
17+
const op = 'function.tanstackstart';
18+
console.log('fetch with startSpan')
19+
return await startSpan(
20+
{
21+
op: op,
22+
name: 'server.fetch', // TODO: use the actual server function name
23+
attributes: {
24+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.tanstackstart.serverFn',
25+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op
26+
},
27+
},
28+
async () => { return await target.apply(thisArg, args) }
29+
);
30+
}
31+
32+
return await target.apply(thisArg, args)
33+
},
34+
});
35+
}
36+
return serverEntry;
37+
}

0 commit comments

Comments
 (0)