Skip to content

Commit 7664ee9

Browse files
committed
create domain for datafunction
1 parent 4b6f920 commit 7664ee9

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,41 @@ function makeWrappedDocumentRequestFunction(
128128

129129
function makeWrappedDataFunction(origFn: DataFunction, id: string, name: 'action' | 'loader'): DataFunction {
130130
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
131-
let res: Response | AppData;
132-
const activeTransaction = getActiveTransaction();
133-
const currentScope = getCurrentHub().getScope();
134-
135-
if (!activeTransaction || !currentScope) {
136-
return origFn.call(this, args);
137-
}
138-
139-
try {
140-
const span = activeTransaction.startChild({
141-
op: `remix.server.${name}`,
142-
description: id,
143-
tags: {
144-
name,
145-
},
146-
});
131+
const local = domain.create();
132+
return local.bind(async () => {
133+
let res: Response | AppData;
134+
const activeTransaction = getActiveTransaction();
135+
const currentScope = getCurrentHub().getScope();
147136

148-
if (span) {
149-
// Assign data function to hub to be able to see `db` transactions (if any) as children.
150-
currentScope.setSpan(span);
137+
if (!activeTransaction || !currentScope) {
138+
return origFn.call(this, args);
151139
}
152140

153-
res = await origFn.call(this, args);
154-
155-
currentScope.setSpan(activeTransaction);
156-
span.finish();
157-
} catch (err) {
158-
captureRemixServerException(err, name);
159-
throw err;
160-
}
141+
try {
142+
const span = activeTransaction.startChild({
143+
op: `remix.server.${name}`,
144+
description: id,
145+
tags: {
146+
name,
147+
},
148+
});
149+
150+
if (span) {
151+
// Assign data function to hub to be able to see `db` transactions (if any) as children.
152+
currentScope.setSpan(span);
153+
}
154+
155+
res = await origFn.call(this, args);
156+
157+
currentScope.setSpan(activeTransaction);
158+
span.finish();
159+
} catch (err) {
160+
captureRemixServerException(err, name);
161+
throw err;
162+
}
161163

162-
return res;
164+
return res;
165+
});
163166
};
164167
}
165168

packages/remix/test/integration/app/routes/scope-bleed/$id.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as Sentry from '@sentry/remix';
55
export const loader: LoaderFunction = async ({ params: { id } }) => {
66
// Set delay to simulate requests at the same time
77
const randomNum = Math.floor(Math.random() * 15) + 1;
8-
await new Promise(resolve => setTimeout(resolve, 4000 - (parseInt(id || '', 10) * 1000 - randomNum)));
8+
await new Promise(resolve => setTimeout(resolve, 3000 - (parseInt(id || '', 10) * 1000 - randomNum)));
99
Sentry.setTag(`tag${id}`, id);
1010
return json({ test: 'test' });
1111
};

0 commit comments

Comments
 (0)