Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(instrumentation-pg): reduce temp objects allocations #2019

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@ 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';

export class PgInstrumentation extends InstrumentationBase {
static readonly COMPONENT = 'pg';

static readonly BASE_SPAN_NAME = PgInstrumentation.COMPONENT + '.query';
static readonly BASE_SPAN_NAME = 'pg.query';
blumamir marked this conversation as resolved.
Show resolved Hide resolved

constructor(config: PgInstrumentationConfig = {}) {
super(
Expand Down Expand Up @@ -149,16 +141,10 @@ 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', {
kind: SpanKind.CLIENT,
attributes: utils.getSemanticAttributesFromConnection(this),
});

if (callback) {
const parentSpan = trace.getSpan(context.active());
Expand All @@ -183,9 +169,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 Down Expand Up @@ -367,17 +351,17 @@ export class PgInstrumentation extends InstrumentationBase {
}

// setup span
const span = plugin.tracer.startSpan(`${PG_POOL_COMPONENT}.connect`, {
const span = plugin.tracer.startSpan('pg-pool.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,
},
attributes: utils.getSemanticAttributesFromConnection(this.options),
});

span.setAttribute(
AttributeNames.IDLE_TIMEOUT_MILLIS,
this.options.idleTimeoutMillis
);
span.setAttribute(AttributeNames.MAX_CLIENT, this.options.maxClient);
blumamir marked this conversation as resolved.
Show resolved Hide resolved

if (callback) {
const parentSpan = trace.getSpan(context.active());
callback = utils.patchCallbackPGPool(
Expand Down
6 changes: 2 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,10 +142,7 @@ 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),
},
attributes: getSemanticAttributesFromConnection(connectionParameters),
});

if (!queryConfig) {
Expand Down
Loading