Skip to content

Commit

Permalink
perf(instrumentation-pg): reduce temp objects allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuron authored and Ievgen Makukh committed Mar 18, 2024
1 parent 358345f commit b6c0bce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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)
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 3 additions & 4 deletions plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b6c0bce

Please sign in to comment.