|
2 | 2 | // Instrumentation for https://github.com/porsager/postgres |
3 | 3 | import { context, trace } from '@opentelemetry/api'; |
4 | 4 | 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'; |
6 | 10 | import { |
7 | 11 | ATTR_DB_NAMESPACE, |
8 | 12 | ATTR_DB_OPERATION_NAME, |
@@ -385,14 +389,18 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns |
385 | 389 | self._setConnectionAttributes(span, connectionContext); |
386 | 390 |
|
387 | 391 | 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 | + ); |
396 | 404 | } |
397 | 405 |
|
398 | 406 | const queryWithCallbacks = this as { |
|
0 commit comments