Skip to content

Commit 84f7a5c

Browse files
committed
chore(opentelemetry-instrumentation-pg): use exported strings for attributes
Use exported strings for Semantic Attributes Signed-off-by: maryliag <[email protected]>
1 parent 85cbc8d commit 84f7a5c

File tree

7 files changed

+94
-66
lines changed

7 files changed

+94
-66
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-pg/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
* devDependencies
77
* @opentelemetry/contrib-test-utils bumped from ^0.34.3 to ^0.35.0
88

9+
### Dependencies
10+
11+
* The following workspace dependencies were updated
12+
* dependencies
13+
* @opentelemetry/semantic-conventions bumped from ^1.0.0 to ^1.22.0
14+
15+
### Enhancement
16+
17+
* refactor: use exported strings for Semantic Attributes
18+
919
## [0.39.1](https://github.com/open-telemetry/opentelemetry-js-contrib/compare/instrumentation-pg-v0.39.0...instrumentation-pg-v0.39.1) (2024-03-11)
1020

1121

plugins/node/opentelemetry-instrumentation-pg/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
},
7373
"dependencies": {
7474
"@opentelemetry/instrumentation": "^0.49.1",
75-
"@opentelemetry/semantic-conventions": "^1.0.0",
75+
"@opentelemetry/semantic-conventions": "^1.22.0",
7676
"@opentelemetry/sql-common": "^0.40.0",
7777
"@types/pg": "8.6.1",
7878
"@types/pg-pool": "2.0.4"

plugins/node/opentelemetry-instrumentation-pg/src/utils.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ import {
2525
} from '@opentelemetry/api';
2626
import { AttributeNames } from './enums/AttributeNames';
2727
import {
28-
SemanticAttributes,
29-
DbSystemValues,
28+
SEMATTRS_DB_SYSTEM,
29+
SEMATTRS_DB_NAME,
30+
SEMATTRS_DB_CONNECTION_STRING,
31+
SEMATTRS_NET_PEER_NAME,
32+
SEMATTRS_NET_PEER_PORT,
33+
SEMATTRS_DB_USER,
34+
SEMATTRS_DB_STATEMENT,
35+
DBSYSTEMVALUES_POSTGRESQL,
3036
} from '@opentelemetry/semantic-conventions';
3137
import {
3238
PgClientExtended,
@@ -108,23 +114,23 @@ export function getSemanticAttributesFromConnection(
108114
params: PgParsedConnectionParams
109115
) {
110116
return {
111-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
112-
[SemanticAttributes.DB_NAME]: params.database, // required
113-
[SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required
114-
[SemanticAttributes.NET_PEER_NAME]: params.host, // required
115-
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
116-
[SemanticAttributes.DB_USER]: params.user,
117+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
118+
[SEMATTRS_DB_NAME]: params.database, // required
119+
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
120+
[SEMATTRS_NET_PEER_NAME]: params.host, // required
121+
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
122+
[SEMATTRS_DB_USER]: params.user,
117123
};
118124
}
119125

120126
export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
121127
return {
122-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
123-
[SemanticAttributes.DB_NAME]: params.database, // required
124-
[SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required
125-
[SemanticAttributes.NET_PEER_NAME]: params.host, // required
126-
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
127-
[SemanticAttributes.DB_USER]: params.user,
128+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
129+
[SEMATTRS_DB_NAME]: params.database, // required
130+
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
131+
[SEMATTRS_NET_PEER_NAME]: params.host, // required
132+
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
133+
[SEMATTRS_DB_USER]: params.user,
128134
[AttributeNames.IDLE_TIMEOUT_MILLIS]: params.idleTimeoutMillis,
129135
[AttributeNames.MAX_CLIENT]: params.maxClient,
130136
};
@@ -163,7 +169,7 @@ export function handleConfigQuery(
163169

164170
// Set attributes
165171
if (queryConfig.text) {
166-
span.setAttribute(SemanticAttributes.DB_STATEMENT, queryConfig.text);
172+
span.setAttribute(SEMATTRS_DB_STATEMENT, queryConfig.text);
167173
}
168174

169175
if (

plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ import * as pgPool from 'pg-pool';
4141
import { AttributeNames } from '../src/enums/AttributeNames';
4242
import { TimedEvent } from './types';
4343
import {
44-
SemanticAttributes,
45-
DbSystemValues,
44+
DBSYSTEMVALUES_POSTGRESQL,
45+
SEMATTRS_DB_SYSTEM,
46+
SEMATTRS_DB_NAME,
47+
SEMATTRS_NET_PEER_NAME,
48+
SEMATTRS_DB_CONNECTION_STRING,
49+
SEMATTRS_NET_PEER_PORT,
50+
SEMATTRS_DB_USER,
51+
SEMATTRS_DB_STATEMENT,
4652
} from '@opentelemetry/semantic-conventions';
4753

4854
const memoryExporter = new InMemorySpanExporter();
@@ -60,23 +66,23 @@ const CONFIG = {
6066
};
6167

6268
const DEFAULT_PGPOOL_ATTRIBUTES = {
63-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
64-
[SemanticAttributes.DB_NAME]: CONFIG.database,
65-
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
66-
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
67-
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
68-
[SemanticAttributes.DB_USER]: CONFIG.user,
69+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
70+
[SEMATTRS_DB_NAME]: CONFIG.database,
71+
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
72+
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
73+
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
74+
[SEMATTRS_DB_USER]: CONFIG.user,
6975
[AttributeNames.MAX_CLIENT]: CONFIG.maxClient,
7076
[AttributeNames.IDLE_TIMEOUT_MILLIS]: CONFIG.idleTimeoutMillis,
7177
};
7278

7379
const DEFAULT_PG_ATTRIBUTES = {
74-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
75-
[SemanticAttributes.DB_NAME]: CONFIG.database,
76-
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
77-
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
78-
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
79-
[SemanticAttributes.DB_USER]: CONFIG.user,
80+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
81+
[SEMATTRS_DB_NAME]: CONFIG.database,
82+
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
83+
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
84+
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
85+
[SEMATTRS_DB_USER]: CONFIG.user,
8086
};
8187

8288
const unsetStatus: SpanStatus = {
@@ -179,7 +185,7 @@ describe('pg-pool', () => {
179185
};
180186
const pgAttributes = {
181187
...DEFAULT_PG_ATTRIBUTES,
182-
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
188+
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
183189
};
184190
const events: TimedEvent[] = [];
185191
const span = provider.getTracer('test-pg-pool').startSpan('test span');
@@ -211,7 +217,7 @@ describe('pg-pool', () => {
211217
};
212218
const pgAttributes = {
213219
...DEFAULT_PG_ATTRIBUTES,
214-
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
220+
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
215221
};
216222
const events: TimedEvent[] = [];
217223
const parentSpan = provider
@@ -284,7 +290,7 @@ describe('pg-pool', () => {
284290
};
285291
const pgAttributes = {
286292
...DEFAULT_PG_ATTRIBUTES,
287-
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
293+
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
288294
};
289295
const events: TimedEvent[] = [];
290296
const span = provider.getTracer('test-pg-pool').startSpan('test span');
@@ -303,7 +309,7 @@ describe('pg-pool', () => {
303309
};
304310
const pgAttributes = {
305311
...DEFAULT_PG_ATTRIBUTES,
306-
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
312+
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
307313
};
308314
const events: TimedEvent[] = [];
309315
const parentSpan = provider
@@ -340,7 +346,7 @@ describe('pg-pool', () => {
340346
};
341347
const pgAttributes = {
342348
...DEFAULT_PG_ATTRIBUTES,
343-
[SemanticAttributes.DB_STATEMENT]: query,
349+
[SEMATTRS_DB_STATEMENT]: query,
344350
[dataAttributeName]: '{"rowCount":1}',
345351
};
346352

@@ -422,7 +428,7 @@ describe('pg-pool', () => {
422428
};
423429
const pgAttributes = {
424430
...DEFAULT_PG_ATTRIBUTES,
425-
[SemanticAttributes.DB_STATEMENT]: query,
431+
[SEMATTRS_DB_STATEMENT]: query,
426432
};
427433

428434
beforeEach(async () => {

plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ import {
4242
import { AttributeNames } from '../src/enums/AttributeNames';
4343
import { TimedEvent } from './types';
4444
import {
45-
SemanticAttributes,
46-
DbSystemValues,
45+
SEMATTRS_DB_STATEMENT,
46+
SEMATTRS_DB_SYSTEM,
47+
SEMATTRS_DB_NAME,
48+
SEMATTRS_NET_PEER_NAME,
49+
SEMATTRS_DB_CONNECTION_STRING,
50+
SEMATTRS_NET_PEER_PORT,
51+
SEMATTRS_DB_USER,
52+
DBSYSTEMVALUES_POSTGRESQL,
4753
} from '@opentelemetry/semantic-conventions';
4854
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
4955

@@ -60,12 +66,12 @@ const CONFIG = {
6066
};
6167

6268
const DEFAULT_ATTRIBUTES = {
63-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
64-
[SemanticAttributes.DB_NAME]: CONFIG.database,
65-
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
66-
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
67-
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
68-
[SemanticAttributes.DB_USER]: CONFIG.user,
69+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
70+
[SEMATTRS_DB_NAME]: CONFIG.database,
71+
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
72+
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
73+
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
74+
[SEMATTRS_DB_USER]: CONFIG.user,
6975
};
7076

7177
const unsetStatus: SpanStatus = {
@@ -348,7 +354,7 @@ describe('pg', () => {
348354
it('should intercept client.query(text, callback)', done => {
349355
const attributes = {
350356
...DEFAULT_ATTRIBUTES,
351-
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
357+
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
352358
};
353359
const events: TimedEvent[] = [];
354360
const span = tracer.startSpan('test span');
@@ -368,7 +374,7 @@ describe('pg', () => {
368374
const values = ['0'];
369375
const attributes = {
370376
...DEFAULT_ATTRIBUTES,
371-
[SemanticAttributes.DB_STATEMENT]: query,
377+
[SEMATTRS_DB_STATEMENT]: query,
372378
};
373379
const events: TimedEvent[] = [];
374380
const span = tracer.startSpan('test span');
@@ -387,7 +393,7 @@ describe('pg', () => {
387393
const query = 'SELECT NOW()';
388394
const attributes = {
389395
...DEFAULT_ATTRIBUTES,
390-
[SemanticAttributes.DB_STATEMENT]: query,
396+
[SEMATTRS_DB_STATEMENT]: query,
391397
};
392398
const events: TimedEvent[] = [];
393399
const span = tracer.startSpan('test span');
@@ -409,7 +415,7 @@ describe('pg', () => {
409415
const query = 'SELECT NOW()';
410416
const attributes = {
411417
...DEFAULT_ATTRIBUTES,
412-
[SemanticAttributes.DB_STATEMENT]: query,
418+
[SEMATTRS_DB_STATEMENT]: query,
413419
};
414420
const events: TimedEvent[] = [];
415421
const span = tracer.startSpan('test span');
@@ -429,7 +435,7 @@ describe('pg', () => {
429435
const values = ['0'];
430436
const attributes = {
431437
...DEFAULT_ATTRIBUTES,
432-
[SemanticAttributes.DB_STATEMENT]: query,
438+
[SEMATTRS_DB_STATEMENT]: query,
433439
};
434440
const events: TimedEvent[] = [];
435441
const span = tracer.startSpan('test span');
@@ -449,7 +455,7 @@ describe('pg', () => {
449455
const values = ['0'];
450456
const attributes = {
451457
...DEFAULT_ATTRIBUTES,
452-
[SemanticAttributes.DB_STATEMENT]: query,
458+
[SEMATTRS_DB_STATEMENT]: query,
453459
};
454460
const events: TimedEvent[] = [];
455461
const span = tracer.startSpan('test span');
@@ -474,7 +480,7 @@ describe('pg', () => {
474480
const attributes = {
475481
...DEFAULT_ATTRIBUTES,
476482
[AttributeNames.PG_PLAN]: name,
477-
[SemanticAttributes.DB_STATEMENT]: query,
483+
[SEMATTRS_DB_STATEMENT]: query,
478484
};
479485
const events: TimedEvent[] = [];
480486
const span = tracer.startSpan('test span');
@@ -498,7 +504,7 @@ describe('pg', () => {
498504
const query = 'SELECT NOW()';
499505
const attributes = {
500506
...DEFAULT_ATTRIBUTES,
501-
[SemanticAttributes.DB_STATEMENT]: query,
507+
[SEMATTRS_DB_STATEMENT]: query,
502508
};
503509
const events: TimedEvent[] = [];
504510
const span = tracer.startSpan('test span');
@@ -537,7 +543,7 @@ describe('pg', () => {
537543

538544
const attributes = {
539545
...DEFAULT_ATTRIBUTES,
540-
[SemanticAttributes.DB_STATEMENT]: query,
546+
[SEMATTRS_DB_STATEMENT]: query,
541547
[AttributeNames.PG_VALUES]: [
542548
'Hello,World',
543549
'abc',
@@ -576,7 +582,7 @@ describe('pg', () => {
576582
// span if there is no requestHook.
577583
const attributes = {
578584
...DEFAULT_ATTRIBUTES,
579-
[SemanticAttributes.DB_STATEMENT]: query,
585+
[SEMATTRS_DB_STATEMENT]: query,
580586
};
581587

582588
// These are the attributes we expect on the span after the requestHook
@@ -669,7 +675,7 @@ describe('pg', () => {
669675
describe('AND valid responseHook', () => {
670676
const attributes = {
671677
...DEFAULT_ATTRIBUTES,
672-
[SemanticAttributes.DB_STATEMENT]: query,
678+
[SEMATTRS_DB_STATEMENT]: query,
673679
[dataAttributeName]: '{"rowCount":1}',
674680
};
675681
beforeEach(async () => {
@@ -702,7 +708,7 @@ describe('pg', () => {
702708
it('should attach response hook data to resulting spans for query returning a Promise', async () => {
703709
const attributes = {
704710
...DEFAULT_ATTRIBUTES,
705-
[SemanticAttributes.DB_STATEMENT]: query,
711+
[SEMATTRS_DB_STATEMENT]: query,
706712
[dataAttributeName]: '{"rowCount":1}',
707713
};
708714

@@ -727,7 +733,7 @@ describe('pg', () => {
727733
describe('AND invalid responseHook', () => {
728734
const attributes = {
729735
...DEFAULT_ATTRIBUTES,
730-
[SemanticAttributes.DB_STATEMENT]: query,
736+
[SEMATTRS_DB_STATEMENT]: query,
731737
};
732738

733739
beforeEach(async () => {

0 commit comments

Comments
 (0)