Skip to content

Commit f057fd3

Browse files
committed
Improve requestHook error handling
1 parent 8eeb8b1 commit f057fd3

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/node/src/integrations/tracing/postgresjs.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Instrumentation for https://github.com/porsager/postgres
33
import { context, trace } from '@opentelemetry/api';
44
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
5-
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
5+
import {
6+
InstrumentationBase,
7+
InstrumentationNodeModuleDefinition,
8+
safeExecuteInTheMiddle,
9+
} from '@opentelemetry/instrumentation';
610
import {
711
ATTR_DB_NAMESPACE,
812
ATTR_DB_OPERATION_NAME,
@@ -385,14 +389,18 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns
385389
self._setConnectionAttributes(span, connectionContext);
386390

387391
const config = self.getConfig();
388-
if (config.requestHook) {
389-
try {
390-
config.requestHook(span, sanitizedSqlQuery, connectionContext);
391-
} catch (e) {
392-
// Set attribute to indicate hook failure, making it visible in spans
393-
span.setAttribute('sentry.hook.error', 'requestHook failed');
394-
DEBUG_BUILD && debug.error(`Error in requestHook for ${INTEGRATION_NAME} integration:`, e);
395-
}
392+
const { requestHook } = config;
393+
if (requestHook) {
394+
safeExecuteInTheMiddle(
395+
() => requestHook(span, sanitizedSqlQuery, connectionContext),
396+
e => {
397+
if (e) {
398+
span.setAttribute('sentry.hook.error', 'requestHook failed');
399+
DEBUG_BUILD && debug.error(`Error in requestHook for ${INTEGRATION_NAME} integration:`, e);
400+
}
401+
},
402+
true,
403+
);
396404
}
397405

398406
const queryWithCallbacks = this as {

0 commit comments

Comments
 (0)