From d3c2ac8729016a9c1e1e0e678dae2639c2b84cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Thu, 19 Nov 2020 16:11:33 +0100 Subject: [PATCH 1/2] feat: node-postgres tracing integration --- packages/tracing/src/integrations/index.ts | 1 + packages/tracing/src/integrations/postgres.ts | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 packages/tracing/src/integrations/postgres.ts diff --git a/packages/tracing/src/integrations/index.ts b/packages/tracing/src/integrations/index.ts index abe1faf43c27..c211cd948588 100644 --- a/packages/tracing/src/integrations/index.ts +++ b/packages/tracing/src/integrations/index.ts @@ -1 +1,2 @@ export { Express } from './express'; +export { Postgres } from './postgres'; diff --git a/packages/tracing/src/integrations/postgres.ts b/packages/tracing/src/integrations/postgres.ts new file mode 100644 index 000000000000..22dec6cd0e71 --- /dev/null +++ b/packages/tracing/src/integrations/postgres.ts @@ -0,0 +1,73 @@ +import { Hub } from '@sentry/hub'; +import { EventProcessor, Integration } from '@sentry/types'; +import { dynamicRequire, fill, logger } from '@sentry/utils'; + +interface PgClient { + prototype: { + query: () => void | Promise; + }; +} + +/** Tracing integration for node-postgres package */ +export class Postgres implements Integration { + /** + * @inheritDoc + */ + public static id: string = 'Postgres'; + + /** + * @inheritDoc + */ + public name: string = Postgres.id; + + /** + * @inheritDoc + */ + public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { + let client: PgClient; + + try { + const pgModule = dynamicRequire(module, 'pg') as { Client: PgClient }; + client = pgModule.Client; + } catch (e) { + logger.error('Postgres Integration was unable to require `pg` package.'); + return; + } + + /** + * function (query, callback) => void + * function (query, params, callback) => void + * function (query) => Promise + * function (query, params) => Promise + */ + fill(client.prototype, 'query', function(orig: () => void | Promise) { + return function(this: unknown, config: unknown, values: unknown, callback: unknown) { + const scope = getCurrentHub().getScope(); + const transaction = scope?.getTransaction(); + const span = transaction?.startChild({ + description: typeof config === 'string' ? config : (config as { text: string }).text, + op: `query`, + }); + + if (typeof callback === 'function') { + return orig.call(this, config, values, function(err: Error, result: unknown) { + span?.finish(); + callback(err, result); + }); + } + + if (typeof values === 'function') { + return orig.call(this, config, function(err: Error, result: unknown) { + span?.finish(); + values(err, result); + }); + } + + return (orig.call(this, config, values) as Promise).then((res: unknown) => { + span?.finish(); + return res; + }); + }; + }); + } +} From ae8373333ba79d7c1a653805c1a6c56fa1f2b1dd Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Fri, 4 Dec 2020 12:42:24 +0100 Subject: [PATCH 2/2] ref: Span content --- packages/tracing/src/integrations/postgres.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/tracing/src/integrations/postgres.ts b/packages/tracing/src/integrations/postgres.ts index 22dec6cd0e71..f78f9d26121b 100644 --- a/packages/tracing/src/integrations/postgres.ts +++ b/packages/tracing/src/integrations/postgres.ts @@ -43,10 +43,10 @@ export class Postgres implements Integration { fill(client.prototype, 'query', function(orig: () => void | Promise) { return function(this: unknown, config: unknown, values: unknown, callback: unknown) { const scope = getCurrentHub().getScope(); - const transaction = scope?.getTransaction(); - const span = transaction?.startChild({ + const parentSpan = scope?.getSpan(); + const span = parentSpan?.startChild({ description: typeof config === 'string' ? config : (config as { text: string }).text, - op: `query`, + op: `db`, }); if (typeof callback === 'function') {