Skip to content

Commit 84ef980

Browse files
Samuronpichlermarc
andauthored
perf(instrumentation-pg): reduce temp objects allocations (#2019)
* perf(instrumentation-pg): reduce temp objects allocations * fix: review comments * fix: do not export span names from module --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 6cd67c0 commit 84ef980

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// Contains span names produced by instrumentation
17+
export enum SpanNames {
18+
QUERY_PREFIX = 'pg.query',
19+
CONNECT = 'pg.connect',
20+
POOL_CONNECT = 'pg-pool.connect',
21+
}

plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,11 @@ import {
3838
} from './internal-types';
3939
import { PgInstrumentationConfig } from './types';
4040
import * as utils from './utils';
41-
import { AttributeNames } from './enums/AttributeNames';
42-
import {
43-
SemanticAttributes,
44-
DbSystemValues,
45-
} from '@opentelemetry/semantic-conventions';
4641
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
4742
import { VERSION } from './version';
48-
49-
const PG_POOL_COMPONENT = 'pg-pool';
43+
import { SpanNames } from './enums/SpanNames';
5044

5145
export class PgInstrumentation extends InstrumentationBase {
52-
static readonly COMPONENT = 'pg';
53-
54-
static readonly BASE_SPAN_NAME = PgInstrumentation.COMPONENT + '.query';
55-
5646
constructor(config: PgInstrumentationConfig = {}) {
5747
super(
5848
'@opentelemetry/instrumentation-pg',
@@ -149,16 +139,10 @@ export class PgInstrumentation extends InstrumentationBase {
149139
return original.call(this, callback);
150140
}
151141

152-
const span = plugin.tracer.startSpan(
153-
`${PgInstrumentation.COMPONENT}.connect`,
154-
{
155-
kind: SpanKind.CLIENT,
156-
attributes: {
157-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
158-
...utils.getSemanticAttributesFromConnection(this),
159-
},
160-
}
161-
);
142+
const span = plugin.tracer.startSpan(SpanNames.CONNECT, {
143+
kind: SpanKind.CLIENT,
144+
attributes: utils.getSemanticAttributesFromConnection(this),
145+
});
162146

163147
if (callback) {
164148
const parentSpan = trace.getSpan(context.active());
@@ -183,9 +167,7 @@ export class PgInstrumentation extends InstrumentationBase {
183167
private _getClientQueryPatch() {
184168
const plugin = this;
185169
return (original: typeof pgTypes.Client.prototype.query) => {
186-
this._diag.debug(
187-
`Patching ${PgInstrumentation.COMPONENT}.Client.prototype.query`
188-
);
170+
this._diag.debug('Patching pg.Client.prototype.query');
189171
return function query(this: PgClientExtended, ...args: unknown[]) {
190172
if (utils.shouldSkipInstrumentation(plugin.getConfig())) {
191173
return original.apply(this, args as never);
@@ -367,15 +349,9 @@ export class PgInstrumentation extends InstrumentationBase {
367349
}
368350

369351
// setup span
370-
const span = plugin.tracer.startSpan(`${PG_POOL_COMPONENT}.connect`, {
352+
const span = plugin.tracer.startSpan(SpanNames.POOL_CONNECT, {
371353
kind: SpanKind.CLIENT,
372-
attributes: {
373-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
374-
...utils.getSemanticAttributesFromConnection(this.options),
375-
[AttributeNames.IDLE_TIMEOUT_MILLIS]:
376-
this.options.idleTimeoutMillis,
377-
[AttributeNames.MAX_CLIENT]: this.options.maxClient,
378-
},
354+
attributes: utils.getSemanticAttributesFromPool(this.options),
379355
});
380356

381357
if (callback) {

plugins/node/opentelemetry-instrumentation-pg/src/utils.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ import {
3434
PgPoolCallback,
3535
PgPoolExtended,
3636
PgParsedConnectionParams,
37+
PgPoolOptionsParams,
3738
} from './internal-types';
3839
import { PgInstrumentationConfig } from './types';
3940
import type * as pgTypes from 'pg';
40-
import { PgInstrumentation } from './';
4141
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
42+
import { SpanNames } from './enums/SpanNames';
4243

4344
/**
4445
* Helper function to get a low cardinality span name from whatever info we have
@@ -66,7 +67,7 @@ export function getQuerySpanName(
6667
// NB: when the query config is invalid, we omit the dbName too, so that
6768
// someone (or some tool) reading the span name doesn't misinterpret the
6869
// dbName as being a prepared statement or sql commit name.
69-
if (!queryConfig) return PgInstrumentation.BASE_SPAN_NAME;
70+
if (!queryConfig) return SpanNames.QUERY_PREFIX;
7071

7172
// Either the name of a prepared statement; or an attempted parse
7273
// of the SQL command, normalized to uppercase; or unknown.
@@ -75,9 +76,7 @@ export function getQuerySpanName(
7576
? queryConfig.name
7677
: parseNormalizedOperationName(queryConfig.text);
7778

78-
return `${PgInstrumentation.BASE_SPAN_NAME}:${command}${
79-
dbName ? ` ${dbName}` : ''
80-
}`;
79+
return `${SpanNames.QUERY_PREFIX}:${command}${dbName ? ` ${dbName}` : ''}`;
8180
}
8281

8382
function parseNormalizedOperationName(queryText: string) {
@@ -109,6 +108,7 @@ export function getSemanticAttributesFromConnection(
109108
params: PgParsedConnectionParams
110109
) {
111110
return {
111+
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
112112
[SemanticAttributes.DB_NAME]: params.database, // required
113113
[SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required
114114
[SemanticAttributes.NET_PEER_NAME]: params.host, // required
@@ -117,6 +117,19 @@ export function getSemanticAttributesFromConnection(
117117
};
118118
}
119119

120+
export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
121+
return {
122+
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
123+
[SemanticAttributes.DB_NAME]: params.database, // required
124+
[SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required
125+
[SemanticAttributes.NET_PEER_NAME]: params.host, // required
126+
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
127+
[SemanticAttributes.DB_USER]: params.user,
128+
[AttributeNames.IDLE_TIMEOUT_MILLIS]: params.idleTimeoutMillis,
129+
[AttributeNames.MAX_CLIENT]: params.maxClient,
130+
};
131+
}
132+
120133
export function shouldSkipInstrumentation(
121134
instrumentationConfig: PgInstrumentationConfig
122135
) {
@@ -141,10 +154,7 @@ export function handleConfigQuery(
141154
const spanName = getQuerySpanName(dbName, queryConfig);
142155
const span = tracer.startSpan(spanName, {
143156
kind: SpanKind.CLIENT,
144-
attributes: {
145-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, // required
146-
...getSemanticAttributesFromConnection(connectionParameters),
147-
},
157+
attributes: getSemanticAttributesFromConnection(connectionParameters),
148158
});
149159

150160
if (!queryConfig) {

0 commit comments

Comments
 (0)