Skip to content

Commit ff9248e

Browse files
committed
Clean up
1 parent 3ba19f8 commit ff9248e

File tree

3 files changed

+38
-74
lines changed

3 files changed

+38
-74
lines changed

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ describe('postgresjs auto instrumentation', () => {
99
expect.objectContaining({
1010
data: expect.objectContaining({
1111
'db.namespace': 'test_db',
12-
'db.operation.name': 'CREATE TABLE',
13-
'db.query.text':
14-
'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"))',
1512
'db.system.name': 'postgres',
1613
'sentry.op': 'db',
1714
'sentry.origin': 'manual',
1815
'server.address': 'localhost',
1916
'server.port': 5444,
2017
}),
21-
description: 'CREATE TABLE db:test_db',
18+
description:
19+
'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"))',
2220
op: 'db',
2321
status: 'ok',
2422
origin: 'manual',
@@ -31,16 +29,14 @@ describe('postgresjs auto instrumentation', () => {
3129
expect.objectContaining({
3230
data: expect.objectContaining({
3331
'db.namespace': 'test_db',
34-
'db.operation.name': 'SELECT',
35-
'db.query.text':
36-
"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",
3732
'db.system.name': 'postgres',
3833
'sentry.op': 'db',
3934
'sentry.origin': 'manual',
4035
'server.address': 'localhost',
4136
'server.port': 5444,
4237
}),
43-
description: 'SELECT db:test_db',
38+
description:
39+
"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",
4440
op: 'db',
4541
status: 'ok',
4642
origin: 'manual',
@@ -53,15 +49,13 @@ describe('postgresjs auto instrumentation', () => {
5349
expect.objectContaining({
5450
data: expect.objectContaining({
5551
'db.namespace': 'test_db',
56-
'db.operation.name': 'INSERT',
57-
'db.query.text': 'INSERT INTO "User" ("email", "name") VALUES (\'Foo\', \'bar@baz.com\')',
5852
'db.system.name': 'postgres',
5953
'sentry.origin': 'manual',
6054
'sentry.op': 'db',
6155
'server.address': 'localhost',
6256
'server.port': 5444,
6357
}),
64-
description: 'INSERT db:test_db',
58+
description: 'INSERT INTO "User" ("email", "name") VALUES (\'Foo\', \'bar@baz.com\')',
6559
op: 'db',
6660
status: 'ok',
6761
origin: 'manual',
@@ -74,15 +68,13 @@ describe('postgresjs auto instrumentation', () => {
7468
expect.objectContaining({
7569
data: expect.objectContaining({
7670
'db.namespace': 'test_db',
77-
'db.operation.name': 'UPDATE',
78-
'db.query.text': 'UPDATE "User" SET "name" = \'Foo\' WHERE "email" = \'bar@baz.com\'',
7971
'db.system.name': 'postgres',
8072
'sentry.op': 'db',
8173
'sentry.origin': 'manual',
8274
'server.address': 'localhost',
8375
'server.port': 5444,
8476
}),
85-
description: 'UPDATE db:test_db',
77+
description: 'UPDATE "User" SET "name" = \'Foo\' WHERE "email" = \'bar@baz.com\'',
8678
op: 'db',
8779
status: 'ok',
8880
origin: 'manual',
@@ -95,15 +87,13 @@ describe('postgresjs auto instrumentation', () => {
9587
expect.objectContaining({
9688
data: expect.objectContaining({
9789
'db.namespace': 'test_db',
98-
'db.operation.name': 'SELECT',
99-
'db.query.text': 'SELECT * FROM "User" WHERE "email" = \'bar@baz.com\'',
10090
'db.system.name': 'postgres',
10191
'sentry.op': 'db',
10292
'sentry.origin': 'manual',
10393
'server.address': 'localhost',
10494
'server.port': 5444,
10595
}),
106-
description: 'SELECT db:test_db',
96+
description: 'SELECT * FROM "User" WHERE "email" = \'bar@baz.com\'',
10797
op: 'db',
10898
status: 'ok',
10999
origin: 'manual',
@@ -116,15 +106,13 @@ describe('postgresjs auto instrumentation', () => {
116106
expect.objectContaining({
117107
data: expect.objectContaining({
118108
'db.namespace': 'test_db',
119-
'db.operation.name': 'SELECT',
120-
'db.query.text': 'SELECT * from generate_series(?,?) as x',
121109
'db.system.name': 'postgres',
122110
'sentry.op': 'db',
123111
'sentry.origin': 'manual',
124112
'server.address': 'localhost',
125113
'server.port': 5444,
126114
}),
127-
description: 'SELECT db:test_db',
115+
description: 'SELECT * from generate_series(?,?) as x',
128116
op: 'db',
129117
status: 'ok',
130118
origin: 'manual',
@@ -137,15 +125,13 @@ describe('postgresjs auto instrumentation', () => {
137125
expect.objectContaining({
138126
data: expect.objectContaining({
139127
'db.namespace': 'test_db',
140-
'db.operation.name': 'DROP TABLE',
141-
'db.query.text': 'DROP TABLE "User"',
142128
'db.system.name': 'postgres',
143129
'sentry.op': 'db',
144130
'sentry.origin': 'manual',
145131
'server.address': 'localhost',
146132
'server.port': 5444,
147133
}),
148-
description: 'DROP TABLE db:test_db',
134+
description: 'DROP TABLE "User"',
149135
op: 'db',
150136
status: 'ok',
151137
origin: 'manual',
@@ -158,16 +144,13 @@ describe('postgresjs auto instrumentation', () => {
158144
expect.objectContaining({
159145
data: expect.objectContaining({
160146
'db.namespace': 'test_db',
161-
'db.query.text': 'SELECT * FROM "User" WHERE "email" = \'foo@baz.com\'',
162147
'db.system.name': 'postgres',
163148
'sentry.op': 'db',
164149
'sentry.origin': 'manual',
165150
'server.address': 'localhost',
166151
'server.port': 5444,
167152
}),
168-
// This span is an error span and the `command` is not available when a does not resolve
169-
// That's why we can't update the span description when the query fails
170-
description: 'postgresjs.query',
153+
description: 'SELECT * FROM "User" WHERE "email" = \'foo@baz.com\'',
171154
op: 'db',
172155
status: 'unknown_error',
173156
origin: 'manual',

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Instrumentation for https://github.com/porsager/postgres
2-
import { SpanStatusCode } from '@opentelemetry/api';
32
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
43
import {
54
InstrumentationBase,
@@ -8,15 +7,12 @@ import {
87
} from '@opentelemetry/instrumentation';
98
import {
109
ATTR_DB_NAMESPACE,
11-
ATTR_DB_OPERATION_NAME,
12-
ATTR_DB_QUERY_TEXT,
1310
ATTR_DB_SYSTEM_NAME,
1411
ATTR_SERVER_ADDRESS,
1512
ATTR_SERVER_PORT,
1613
} from '@opentelemetry/semantic-conventions';
1714
import type { IntegrationFn, Span } from '@sentry/core';
18-
import { defineIntegration, getCurrentScope, startSpanManual } from '@sentry/core';
19-
import { SDK_VERSION } from '@sentry/core';
15+
import { defineIntegration, getCurrentScope, SDK_VERSION, SPAN_STATUS_ERROR, startSpanManual } from '@sentry/core';
2016
import { generateInstrumentOnce } from '../../otel/instrument';
2117

2218
const INTEGRATION_NAME = 'PostgresJs';
@@ -63,7 +59,7 @@ export class PostgresJsInstrumentation extends InstrumentationBase {
6359
}
6460

6561
/**
66-
* Patches the reject method of the Query class to set the span status
62+
* Patches the reject method of the Query class to set the span status and end it
6763
*/
6864
private _patchReject(rejectTarget: any, span: Span): any {
6965
return new Proxy(rejectTarget, {
@@ -75,8 +71,7 @@ export class PostgresJsInstrumentation extends InstrumentationBase {
7571
}[],
7672
) => {
7773
span.setStatus({
78-
code: SpanStatusCode.ERROR,
79-
message: rejectArgs[0]?.message || 'PostgresJs Query Error',
74+
code: SPAN_STATUS_ERROR,
8075
});
8176

8277
const result = Reflect.apply(rejectTarget, rejectThisArg, rejectArgs);
@@ -87,23 +82,11 @@ export class PostgresJsInstrumentation extends InstrumentationBase {
8782
}
8883

8984
/**
90-
* Patches the resolve method of the Query class to start a span
91-
* and set the relevant attributes.
85+
* Patches the resolve method of the Query class to end the span when the query is resolved.
9286
*/
93-
private _patchResolve(resolveTarget: any, span: Span, databaseName: string): any {
87+
private _patchResolve(resolveTarget: any, span: Span): any {
9488
return new Proxy(resolveTarget, {
95-
apply: (resolveTarget, resolveThisArg, resolveArgs: [{ command?: string; statement?: { string: string } }]) => {
96-
const sqlCommand = resolveArgs?.[0]?.command;
97-
98-
const spanDescription = sqlCommand
99-
? `${sqlCommand.replace(/\s+/g, ' ').trim()} db:${databaseName}`
100-
: `Unknown Query db:${databaseName}`;
101-
102-
span.updateName(spanDescription);
103-
104-
// Set command and database attributes on the span
105-
span.setAttribute(ATTR_DB_OPERATION_NAME, sqlCommand);
106-
89+
apply: (resolveTarget, resolveThisArg, resolveArgs) => {
10790
const result = Reflect.apply(resolveTarget, resolveThisArg, resolveArgs);
10891
span.end();
10992
return result;
@@ -127,14 +110,15 @@ export class PostgresJsInstrumentation extends InstrumentationBase {
127110
handleThisArg: {
128111
resolve: any;
129112
reject: any;
130-
resolveArgs: [{ command?: string; statement?: { string: string } }];
131113
strings?: string[];
132114
},
133115
handleArgs,
134116
) => {
117+
const sanitizedSqlQuery = this._sanitizeSqlQuery(handleThisArg.strings?.[0]);
118+
135119
return startSpanManual(
136120
{
137-
name: 'postgresjs.query',
121+
name: sanitizedSqlQuery || 'postgresjs.query',
138122
op: 'db',
139123
},
140124
(span: Span) => {
@@ -152,23 +136,20 @@ export class PostgresJsInstrumentation extends InstrumentationBase {
152136
const databaseName = postgresConnectionContext?.ATTR_DB_NAMESPACE || '<unknown database>';
153137
const databaseHost = postgresConnectionContext?.ATTR_SERVER_ADDRESS || '<unknown host>';
154138
const databasePort = postgresConnectionContext?.ATTR_SERVER_PORT || '<unknown port>';
155-
const sanitizedSqlQuery = this._sanitizeSqlQuery(handleThisArg.strings?.[0]);
156139

157140
span.setAttribute(ATTR_DB_SYSTEM_NAME, 'postgres');
158141
span.setAttribute(ATTR_DB_NAMESPACE, databaseName);
159142
span.setAttribute(ATTR_SERVER_ADDRESS, databaseHost);
160143
span.setAttribute(ATTR_SERVER_PORT, databasePort);
161-
span.setAttribute(ATTR_DB_QUERY_TEXT, sanitizedSqlQuery);
162144

163-
handleThisArg.resolve = this._patchResolve(handleThisArg.resolve, span, databaseName);
145+
handleThisArg.resolve = this._patchResolve(handleThisArg.resolve, span);
164146
handleThisArg.reject = this._patchReject(handleThisArg.reject, span);
165147

166148
try {
167149
return Reflect.apply(handleTarget, handleThisArg, handleArgs);
168150
} catch (error) {
169151
span.setStatus({
170-
code: SpanStatusCode.ERROR,
171-
message: (error as Error).message || 'PostgresJs Query Error',
152+
code: SPAN_STATUS_ERROR,
172153
});
173154
span.end();
174155
throw error; // Re-throw the error to propagate it

yarn.lock

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26925,7 +26925,7 @@ string-template@~0.2.1:
2692526925
is-fullwidth-code-point "^3.0.0"
2692626926
strip-ansi "^6.0.1"
2692726927

26928-
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
26928+
string-width@4.2.3, "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
2692926929
version "4.2.3"
2693026930
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
2693126931
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -27035,6 +27035,13 @@ stringify-object@^3.2.1:
2703527035
dependencies:
2703627036
ansi-regex "^5.0.1"
2703727037

27038+
strip-ansi@6.0.1, strip-ansi@^6.0.0, strip-ansi@^6.0.1:
27039+
version "6.0.1"
27040+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
27041+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
27042+
dependencies:
27043+
ansi-regex "^5.0.1"
27044+
2703827045
strip-ansi@^3.0.0:
2703927046
version "3.0.1"
2704027047
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
@@ -27056,13 +27063,6 @@ strip-ansi@^5.1.0, strip-ansi@^5.2.0:
2705627063
dependencies:
2705727064
ansi-regex "^4.1.0"
2705827065

27059-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
27060-
version "6.0.1"
27061-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
27062-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
27063-
dependencies:
27064-
ansi-regex "^5.0.1"
27065-
2706627066
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
2706727067
version "7.1.0"
2706827068
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -27198,7 +27198,7 @@ stylus@0.59.0, stylus@^0.59.0:
2719827198
sax "~1.2.4"
2719927199
source-map "^0.7.3"
2720027200

27201-
sucrase@^3.27.0, sucrase@^3.35.0:
27201+
sucrase@^3.27.0, sucrase@^3.35.0, sucrase@getsentry/sucrase#es2020-polyfills:
2720227202
version "3.36.0"
2720327203
resolved "https://codeload.github.com/getsentry/sucrase/tar.gz/fd682f6129e507c00bb4e6319cc5d6b767e36061"
2720427204
dependencies:
@@ -29833,19 +29833,19 @@ wrangler@^3.67.1:
2983329833
string-width "^4.1.0"
2983429834
strip-ansi "^6.0.0"
2983529835

29836-
wrap-ansi@^6.0.1:
29837-
version "6.2.0"
29838-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
29839-
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
29836+
wrap-ansi@7.0.0, wrap-ansi@^7.0.0:
29837+
version "7.0.0"
29838+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
29839+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
2984029840
dependencies:
2984129841
ansi-styles "^4.0.0"
2984229842
string-width "^4.1.0"
2984329843
strip-ansi "^6.0.0"
2984429844

29845-
wrap-ansi@^7.0.0:
29846-
version "7.0.0"
29847-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
29848-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
29845+
wrap-ansi@^6.0.1:
29846+
version "6.2.0"
29847+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
29848+
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
2984929849
dependencies:
2985029850
ansi-styles "^4.0.0"
2985129851
string-width "^4.1.0"

0 commit comments

Comments
 (0)