diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 1fa8f58d3d..512a13f53c 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -39,19 +39,13 @@ import { import { PgInstrumentationConfig } from './types'; import * as utils from './utils'; import { AttributeNames } from './enums/AttributeNames'; -import { - SemanticAttributes, - DbSystemValues, -} from '@opentelemetry/semantic-conventions'; import { addSqlCommenterComment } from '@opentelemetry/sql-common'; import { VERSION } from './version'; -const PG_POOL_COMPONENT = 'pg-pool'; +const clientKind = { kind: SpanKind.CLIENT }; export class PgInstrumentation extends InstrumentationBase { - static readonly COMPONENT = 'pg'; - - static readonly BASE_SPAN_NAME = PgInstrumentation.COMPONENT + '.query'; + static readonly BASE_SPAN_NAME = 'pg.query'; constructor(config: PgInstrumentationConfig = {}) { super( @@ -149,16 +143,9 @@ export class PgInstrumentation extends InstrumentationBase { return original.call(this, callback); } - const span = plugin.tracer.startSpan( - `${PgInstrumentation.COMPONENT}.connect`, - { - kind: SpanKind.CLIENT, - attributes: { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - ...utils.getSemanticAttributesFromConnection(this), - }, - } - ); + const span = plugin.tracer.startSpan('pg.connect', clientKind); + + span.setAttributes(utils.getSemanticAttributesFromConnection(this)); if (callback) { const parentSpan = trace.getSpan(context.active()); @@ -183,9 +170,7 @@ export class PgInstrumentation extends InstrumentationBase { private _getClientQueryPatch() { const plugin = this; return (original: typeof pgTypes.Client.prototype.query) => { - this._diag.debug( - `Patching ${PgInstrumentation.COMPONENT}.Client.prototype.query` - ); + this._diag.debug('Patching pg.Client.prototype.query'); return function query(this: PgClientExtended, ...args: unknown[]) { if (utils.shouldSkipInstrumentation(plugin.getConfig())) { return original.apply(this, args as never); @@ -208,12 +193,12 @@ export class PgInstrumentation extends InstrumentationBase { // to properly narrow arg0, but TS 4.3.5 does not. const queryConfig = firstArgIsString ? { - text: arg0 as string, - values: Array.isArray(args[1]) ? args[1] : undefined, - } + text: arg0 as string, + values: Array.isArray(args[1]) ? args[1] : undefined, + } : firstArgIsQueryObjectWithText - ? (arg0 as utils.ObjectWithText) - : undefined; + ? (arg0 as utils.ObjectWithText) + : undefined; const instrumentationConfig = plugin.getConfig(); @@ -230,11 +215,11 @@ export class PgInstrumentation extends InstrumentationBase { args[0] = firstArgIsString ? addSqlCommenterComment(span, arg0) : firstArgIsQueryObjectWithText - ? { + ? { ...arg0, text: addSqlCommenterComment(span, arg0.text), } - : args[0]; + : args[0]; } // Bind callback (if any) to parent span (if any) @@ -367,16 +352,16 @@ export class PgInstrumentation extends InstrumentationBase { } // setup span - const span = plugin.tracer.startSpan(`${PG_POOL_COMPONENT}.connect`, { - kind: SpanKind.CLIENT, - attributes: { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, - ...utils.getSemanticAttributesFromConnection(this.options), - [AttributeNames.IDLE_TIMEOUT_MILLIS]: - this.options.idleTimeoutMillis, - [AttributeNames.MAX_CLIENT]: this.options.maxClient, - }, - }); + const span = plugin.tracer.startSpan('pg-pool.connect', clientKind); + + span.setAttributes( + utils.getSemanticAttributesFromConnection(this.options) + ); + span.setAttribute( + AttributeNames.IDLE_TIMEOUT_MILLIS, + this.options.idleTimeoutMillis + ); + span.setAttribute(AttributeNames.MAX_CLIENT, this.options.maxClient); if (callback) { const parentSpan = trace.getSpan(context.active()); diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index bcf8e4efb7..2bdcefed37 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -109,6 +109,7 @@ export function getSemanticAttributesFromConnection( params: PgParsedConnectionParams ) { return { + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, [SemanticAttributes.DB_NAME]: params.database, // required [SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required [SemanticAttributes.NET_PEER_NAME]: params.host, // required @@ -141,12 +142,10 @@ export function handleConfigQuery( const spanName = getQuerySpanName(dbName, queryConfig); const span = tracer.startSpan(spanName, { kind: SpanKind.CLIENT, - attributes: { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, // required - ...getSemanticAttributesFromConnection(connectionParameters), - }, }); + span.setAttributes(getSemanticAttributesFromConnection(connectionParameters)); + if (!queryConfig) { return span; }