Skip to content

Commit de45a57

Browse files
committed
fix(instrumentation-pg): improve connection string parsing for pool attributes and fix tests
1 parent 9c1a893 commit de45a57

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,22 @@ export function getSemanticAttributesFromConnection(
157157
}
158158

159159
export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
160+
// const connectionParams = getConnectionParams(params);
161+
162+
let url: URL | undefined;
163+
try {
164+
url = params.connectionString ? new URL(params.connectionString) : undefined;
165+
} catch (e) {
166+
url = undefined;
167+
}
168+
160169
return {
161170
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
162-
[SEMATTRS_DB_NAME]: params.database, // required
171+
[SEMATTRS_DB_NAME]: url?.pathname.slice(1) ?? params.database, // required
163172
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
164-
[SEMATTRS_NET_PEER_NAME]: params.host, // required
165-
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
166-
[SEMATTRS_DB_USER]: params.user,
173+
[SEMATTRS_NET_PEER_NAME]: url?.hostname ?? params.host, // required
174+
[SEMATTRS_NET_PEER_PORT]: Number(url?.port) || getPort(params.port),
175+
[SEMATTRS_DB_USER]: url?.username ?? params.user,
167176
[AttributeNames.IDLE_TIMEOUT_MILLIS]: params.idleTimeoutMillis,
168177
[AttributeNames.MAX_CLIENT]: params.maxClient,
169178
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,16 @@ describe('pg-pool', () => {
226226
const connectionString = `postgresql://${CONFIG.user}:${CONFIG.password}@${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`;
227227
const poolWithConnString = new pgPool({
228228
connectionString,
229+
idleTimeoutMillis: CONFIG.idleTimeoutMillis,
229230
});
230231

231232
const expectedAttributes = {
232233
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
233234
[SEMATTRS_DB_NAME]: CONFIG.database,
234235
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
235-
[SEMATTRS_DB_CONNECTION_STRING]: connectionString,
236+
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
236237
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
237238
[SEMATTRS_DB_USER]: CONFIG.user,
238-
[AttributeNames.MAX_CLIENT]: CONFIG.maxClient,
239239
[AttributeNames.IDLE_TIMEOUT_MILLIS]: CONFIG.idleTimeoutMillis,
240240
};
241241

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe('utils.ts', () => {
292292
const connectionString = 'not-a-valid-url';
293293
assert.strictEqual(
294294
utils.parseAndMaskConnectionString(connectionString),
295-
'postgresql://localhost'
295+
'postgresql://localhost:5432/'
296296
);
297297
});
298298
});

0 commit comments

Comments
 (0)