Skip to content

Commit 9798064

Browse files
committed
Add more attributes
1 parent edcc016 commit 9798064

File tree

2 files changed

+44
-1
lines changed
  • dev-packages/node-integration-tests/suites/tracing/postgresjs
  • packages/node/src/integrations/tracing

2 files changed

+44
-1
lines changed

dev-packages/node-integration-tests/suites/tracing/postgresjs/test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ describe('postgresjs auto instrumentation', () => {
1010
data: expect.objectContaining({
1111
'db.namespace': 'test_db',
1212
'db.system.name': 'postgres',
13+
'db.operation.name': 'CREATE TABLE',
14+
'db.query.text':
15+
'CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(?) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"))',
1316
'sentry.op': 'db',
1417
'sentry.origin': 'auto.db.otel.postgres',
1518
'server.address': 'localhost',
@@ -30,6 +33,9 @@ describe('postgresjs auto instrumentation', () => {
3033
data: expect.objectContaining({
3134
'db.namespace': 'test_db',
3235
'db.system.name': 'postgres',
36+
'db.operation.name': 'SELECT',
37+
'db.query.text':
38+
"select b.oid, b.typarray from pg_catalog.pg_type a left join pg_catalog.pg_type b on b.oid = a.typelem where a.typcategory = 'A' group by b.oid, b.typarray order by b.oid",
3339
'sentry.op': 'db',
3440
'sentry.origin': 'auto.db.otel.postgres',
3541
'server.address': 'localhost',
@@ -50,6 +56,8 @@ describe('postgresjs auto instrumentation', () => {
5056
data: expect.objectContaining({
5157
'db.namespace': 'test_db',
5258
'db.system.name': 'postgres',
59+
'db.operation.name': 'INSERT',
60+
'db.query.text': 'INSERT INTO "User" ("email", "name") VALUES (\'Foo\', \'[email protected]\')',
5361
'sentry.origin': 'auto.db.otel.postgres',
5462
'sentry.op': 'db',
5563
'server.address': 'localhost',
@@ -69,6 +77,8 @@ describe('postgresjs auto instrumentation', () => {
6977
data: expect.objectContaining({
7078
'db.namespace': 'test_db',
7179
'db.system.name': 'postgres',
80+
'db.operation.name': 'UPDATE',
81+
'db.query.text': 'UPDATE "User" SET "name" = \'Foo\' WHERE "email" = \'[email protected]\'',
7282
'sentry.op': 'db',
7383
'sentry.origin': 'auto.db.otel.postgres',
7484
'server.address': 'localhost',
@@ -88,6 +98,8 @@ describe('postgresjs auto instrumentation', () => {
8898
data: expect.objectContaining({
8999
'db.namespace': 'test_db',
90100
'db.system.name': 'postgres',
101+
'db.operation.name': 'SELECT',
102+
'db.query.text': 'SELECT * FROM "User" WHERE "email" = \'[email protected]\'',
91103
'sentry.op': 'db',
92104
'sentry.origin': 'auto.db.otel.postgres',
93105
'server.address': 'localhost',
@@ -107,6 +119,8 @@ describe('postgresjs auto instrumentation', () => {
107119
data: expect.objectContaining({
108120
'db.namespace': 'test_db',
109121
'db.system.name': 'postgres',
122+
'db.operation.name': 'SELECT',
123+
'db.query.text': 'SELECT * from generate_series(?,?) as x',
110124
'sentry.op': 'db',
111125
'sentry.origin': 'auto.db.otel.postgres',
112126
'server.address': 'localhost',
@@ -126,6 +140,8 @@ describe('postgresjs auto instrumentation', () => {
126140
data: expect.objectContaining({
127141
'db.namespace': 'test_db',
128142
'db.system.name': 'postgres',
143+
'db.operation.name': 'DROP TABLE',
144+
'db.query.text': 'DROP TABLE "User"',
129145
'sentry.op': 'db',
130146
'sentry.origin': 'auto.db.otel.postgres',
131147
'server.address': 'localhost',
@@ -145,6 +161,10 @@ describe('postgresjs auto instrumentation', () => {
145161
data: expect.objectContaining({
146162
'db.namespace': 'test_db',
147163
'db.system.name': 'postgres',
164+
// No db.operation.name here, as this is an errored span
165+
'db.response.status_code': '42P01',
166+
'error.type': 'PostgresError',
167+
'db.query.text': 'SELECT * FROM "User" WHERE "email" = \'[email protected]\'',
148168
'sentry.op': 'db',
149169
'sentry.origin': 'auto.db.otel.postgres',
150170
'server.address': 'localhost',

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
} from '@opentelemetry/instrumentation';
1010
import {
1111
ATTR_DB_NAMESPACE,
12+
ATTR_DB_OPERATION_NAME,
13+
ATTR_DB_QUERY_TEXT,
14+
ATTR_DB_RESPONSE_STATUS_CODE,
1215
ATTR_DB_SYSTEM_NAME,
16+
ATTR_ERROR_TYPE,
1317
ATTR_SERVER_ADDRESS,
1418
ATTR_SERVER_PORT,
1519
} from '@opentelemetry/semantic-conventions';
@@ -117,13 +121,25 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns
117121
rejectThisArg,
118122
rejectArgs: {
119123
message?: string;
124+
code?: string;
125+
name?: string;
120126
}[],
121127
) => {
122128
span.setStatus({
123129
code: SPAN_STATUS_ERROR,
130+
// This message is the error message from the rejectArgs, when available
131+
// e.g "relation 'User' does not exist"
132+
message: rejectArgs?.[0]?.message || 'unknown_error',
124133
});
125134

126135
const result = Reflect.apply(rejectTarget, rejectThisArg, rejectArgs);
136+
137+
// This status code is PG error code, e.g. '42P01' for "relation does not exist"
138+
// https://www.postgresql.org/docs/current/errcodes-appendix.html
139+
span.setAttribute(ATTR_DB_RESPONSE_STATUS_CODE, rejectArgs?.[0]?.code || 'Unknown error');
140+
// This is the error type, e.g. 'PostgresError' for a Postgres error
141+
span.setAttribute(ATTR_ERROR_TYPE, rejectArgs?.[0]?.name || 'Unknown error');
142+
127143
span.end();
128144
return result;
129145
},
@@ -135,8 +151,14 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns
135151
*/
136152
private _patchResolve(resolveTarget: any, span: Span): any {
137153
return new Proxy(resolveTarget, {
138-
apply: (resolveTarget, resolveThisArg, resolveArgs) => {
154+
apply: (resolveTarget, resolveThisArg, resolveArgs: [{ command?: string }]) => {
139155
const result = Reflect.apply(resolveTarget, resolveThisArg, resolveArgs);
156+
const sqlCommand = resolveArgs?.[0]?.command;
157+
158+
if (sqlCommand) {
159+
// SQL command is only available when the query is resolved successfully
160+
span.setAttribute(ATTR_DB_OPERATION_NAME, sqlCommand);
161+
}
140162
span.end();
141163
return result;
142164
},
@@ -206,6 +228,7 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns
206228
span.setAttribute(ATTR_DB_NAMESPACE, databaseName);
207229
span.setAttribute(ATTR_SERVER_ADDRESS, databaseHost);
208230
span.setAttribute(ATTR_SERVER_PORT, databasePort);
231+
span.setAttribute(ATTR_DB_QUERY_TEXT, sanitizedSqlQuery);
209232

210233
handleThisArg.resolve = this._patchResolve(handleThisArg.resolve, span);
211234
handleThisArg.reject = this._patchReject(handleThisArg.reject, span);

0 commit comments

Comments
 (0)