Skip to content

Commit

Permalink
refactor(pg): dry up attribute generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanresnick committed Nov 29, 2022
1 parent 07d6c4d commit abd35fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
);

Expand Down Expand Up @@ -308,19 +303,14 @@ 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,
plugin.getConfig(),
`${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,
Expand Down
21 changes: 14 additions & 7 deletions plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
});
}

Expand Down

0 comments on commit abd35fb

Please sign in to comment.