Skip to content

Commit 12932e0

Browse files
authored
feat(tracing): Add db connection attributes for postgres spans (#8778)
Align postgres spans with https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/sql.md needed for starfish views.
1 parent 8f5922d commit 12932e0

File tree

2 files changed

+48
-4
lines changed
  • packages
    • node-integration-tests/suites/tracing/auto-instrument/pg
    • tracing-internal/src/node/integrations

2 files changed

+48
-4
lines changed

packages/node-integration-tests/suites/tracing/auto-instrument/pg/test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { assertSentryTransaction, TestEnv } from '../../../../utils';
22

33
class PgClient {
4+
database?: string = 'test';
5+
user?: string = 'user';
6+
host?: string = 'localhost';
7+
port?: number = 5432;
8+
49
// https://node-postgres.com/api/client#clientquery
510
public query(_text: unknown, values: unknown, callback?: () => void) {
611
if (typeof callback === 'function') {
@@ -42,20 +47,32 @@ test('should auto-instrument `pg` package.', async () => {
4247
op: 'db',
4348
data: {
4449
'db.system': 'postgresql',
50+
'db.user': 'user',
51+
'db.name': 'test',
52+
'server.address': 'localhost',
53+
'server.port': 5432,
4554
},
4655
},
4756
{
4857
description: 'SELECT * FROM bazz',
4958
op: 'db',
5059
data: {
5160
'db.system': 'postgresql',
61+
'db.user': 'user',
62+
'db.name': 'test',
63+
'server.address': 'localhost',
64+
'server.port': 5432,
5265
},
5366
},
5467
{
5568
description: 'SELECT NOW()',
5669
op: 'db',
5770
data: {
5871
'db.system': 'postgresql',
72+
'db.user': 'user',
73+
'db.name': 'test',
74+
'server.address': 'localhost',
75+
'server.port': 5432,
5976
},
6077
},
6178
],

packages/tracing-internal/src/node/integrations/postgres.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ interface PgClient {
1111
};
1212
}
1313

14+
interface PgClientThis {
15+
database?: string;
16+
host?: string;
17+
port?: number;
18+
user?: string;
19+
}
20+
1421
interface PgOptions {
1522
usePgNative?: boolean;
1623
}
@@ -74,15 +81,35 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
7481
* function (pg.Cursor) => pg.Cursor
7582
*/
7683
fill(Client.prototype, 'query', function (orig: () => void | Promise<unknown>) {
77-
return function (this: unknown, config: unknown, values: unknown, callback: unknown) {
84+
return function (this: PgClientThis, config: unknown, values: unknown, callback: unknown) {
7885
const scope = getCurrentHub().getScope();
7986
const parentSpan = scope?.getSpan();
87+
88+
const data: Record<string, string | number> = {
89+
'db.system': 'postgresql',
90+
};
91+
92+
try {
93+
if (this.database) {
94+
data['db.name'] = this.database;
95+
}
96+
if (this.host) {
97+
data['server.address'] = this.host;
98+
}
99+
if (this.port) {
100+
data['server.port'] = this.port;
101+
}
102+
if (this.user) {
103+
data['db.user'] = this.user;
104+
}
105+
} catch (e) {
106+
// ignore
107+
}
108+
80109
const span = parentSpan?.startChild({
81110
description: typeof config === 'string' ? config : (config as { text: string }).text,
82111
op: 'db',
83-
data: {
84-
'db.system': 'postgresql',
85-
},
112+
data,
86113
});
87114

88115
if (typeof callback === 'function') {

0 commit comments

Comments
 (0)