From abd35fbca249929db1191e580b903ce6ee11f26b Mon Sep 17 00:00:00 2001 From: Ethan Resnick Date: Fri, 25 Nov 2022 19:37:13 -0800 Subject: [PATCH] refactor(pg): dry up attribute generation --- .../src/instrumentation.ts | 14 ++----------- .../src/utils.ts | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 9a946cc5f7..f70e39f671 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -136,12 +136,7 @@ export class PgInstrumentation extends InstrumentationBase { `${PgInstrumentation.COMPONENT}.connect`, { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - [SemanticAttributes.DB_NAME]: this.database, - [SemanticAttributes.NET_PEER_NAME]: this.host, - [SemanticAttributes.DB_CONNECTION_STRING]: - utils.getConnectionString(this), - [SemanticAttributes.NET_PEER_PORT]: this.port, - [SemanticAttributes.DB_USER]: this.user, + ...utils.getSemanticAttributesFromConnection(this), } ); @@ -308,7 +303,6 @@ export class PgInstrumentation extends InstrumentationBase { const plugin = this; return (originalConnect: typeof pgPoolTypes.prototype.connect) => { return function connect(this: PgPoolExtended, callback?: PgPoolCallback) { - const connString = utils.getConnectionString(this.options); // setup span const span = startSpan( plugin.tracer, @@ -316,11 +310,7 @@ export class PgInstrumentation extends InstrumentationBase { `${PG_POOL_COMPONENT}.connect`, { [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - [SemanticAttributes.DB_NAME]: this.options.database, // required - [SemanticAttributes.NET_PEER_NAME]: this.options.host, // required - [SemanticAttributes.DB_CONNECTION_STRING]: connString, // required - [SemanticAttributes.NET_PEER_PORT]: this.options.port, - [SemanticAttributes.DB_USER]: this.options.user, + ...utils.getSemanticAttributesFromConnection(this.options), [AttributeNames.IDLE_TIMEOUT_MILLIS]: this.options.idleTimeoutMillis, [AttributeNames.MAX_CLIENT]: this.options.maxClient, diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index 970aedaf73..4148870173 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -57,13 +57,25 @@ function getCommandFromText(text?: string): string { return words[0].length > 0 ? words[0] : 'unknown'; } -export function getConnectionString(params: PgClientConnectionParams) { +function getConnectionString(params: PgClientConnectionParams) { const host = params.host || 'localhost'; const port = params.port || 5432; const database = params.database || ''; return `postgresql://${host}:${port}/${database}`; } +export function getSemanticAttributesFromConnection( + params: PgClientConnectionParams +) { + return { + [SemanticAttributes.DB_NAME]: params.database, // required + [SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required + [SemanticAttributes.NET_PEER_NAME]: params.host, // required + [SemanticAttributes.NET_PEER_PORT]: params.port, + [SemanticAttributes.DB_USER]: params.user, + }; +} + export function startSpan( tracer: Tracer, instrumentationConfig: PgInstrumentationConfig, @@ -91,14 +103,9 @@ function startQuerySpan( instrumentationConfig: PgInstrumentationConfig, name: string ) { - const jdbcString = getConnectionString(client.connectionParameters); return startSpan(tracer, instrumentationConfig, name, { - [SemanticAttributes.DB_NAME]: client.connectionParameters.database, // required [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, // required - [SemanticAttributes.DB_CONNECTION_STRING]: jdbcString, // required - [SemanticAttributes.NET_PEER_NAME]: client.connectionParameters.host, // required - [SemanticAttributes.NET_PEER_PORT]: client.connectionParameters.port, - [SemanticAttributes.DB_USER]: client.connectionParameters.user, + ...getSemanticAttributesFromConnection(client.connectionParameters), }); }