Skip to content

Commit 481be92

Browse files
committed
update pg to stable semantic conventions
1 parent 11a94b0 commit 481be92

File tree

5 files changed

+97
-34
lines changed

5 files changed

+97
-34
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-pg/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"dependencies": {
7272
"@opentelemetry/core": "^2.0.0",
7373
"@opentelemetry/instrumentation": "^0.202.0",
74-
"@opentelemetry/semantic-conventions": "^1.27.0",
74+
"@opentelemetry/semantic-conventions": "^1.34.0",
7575
"@opentelemetry/sql-common": "^0.41.0",
7676
"@types/pg": "8.15.1",
7777
"@types/pg-pool": "2.0.6"

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
InstrumentationNodeModuleDefinition,
2020
safeExecuteInTheMiddle,
2121
InstrumentationNodeModuleFile,
22+
SemconvStability,
23+
semconvStabilityFromStr,
2224
} from '@opentelemetry/instrumentation';
2325
import {
2426
context,
@@ -59,6 +61,7 @@ import {
5961
ATTR_ERROR_TYPE,
6062
ATTR_SERVER_PORT,
6163
ATTR_SERVER_ADDRESS,
64+
ATTR_DB_SYSTEM_NAME,
6265
} from '@opentelemetry/semantic-conventions';
6366
import {
6467
METRIC_DB_CLIENT_CONNECTION_COUNT,
@@ -88,9 +91,14 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
8891
idle: 0,
8992
pending: 0,
9093
};
94+
private _semconvStability: SemconvStability = SemconvStability.OLD;
9195

9296
constructor(config: PgInstrumentationConfig = {}) {
9397
super(PACKAGE_NAME, PACKAGE_VERSION, config);
98+
this._semconvStability = semconvStabilityFromStr(
99+
'databases',
100+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
101+
);
94102
}
95103

96104
override _updateMetricInstruments() {
@@ -247,7 +255,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
247255

248256
const span = plugin.tracer.startSpan(SpanNames.CONNECT, {
249257
kind: SpanKind.CLIENT,
250-
attributes: utils.getSemanticAttributesFromConnection(this),
258+
attributes: utils.getSemanticAttributesFromConnection(this, plugin._semconvStability),
251259
});
252260

253261
if (callback) {
@@ -272,14 +280,19 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
272280

273281
private recordOperationDuration(attributes: Attributes, startTime: HrTime) {
274282
const metricsAttributes: Attributes = {};
275-
const keysToCopy = [
276-
SEMATTRS_DB_SYSTEM,
283+
let keysToCopy = [
277284
ATTR_DB_NAMESPACE,
278285
ATTR_ERROR_TYPE,
279286
ATTR_SERVER_PORT,
280287
ATTR_SERVER_ADDRESS,
281288
ATTR_DB_OPERATION_NAME,
282289
];
290+
if (this._semconvStability & SemconvStability.OLD) {
291+
keysToCopy.push(SEMATTRS_DB_SYSTEM);
292+
}
293+
if (this._semconvStability & SemconvStability.STABLE) {
294+
keysToCopy.push(ATTR_DB_SYSTEM_NAME);
295+
}
283296

284297
keysToCopy.forEach(key => {
285298
if (key in attributes) {
@@ -348,6 +361,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
348361
this,
349362
plugin.tracer,
350363
instrumentationConfig,
364+
plugin._semconvStability,
351365
queryConfig
352366
);
353367

@@ -548,7 +562,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
548562
// setup span
549563
const span = plugin.tracer.startSpan(SpanNames.POOL_CONNECT, {
550564
kind: SpanKind.CLIENT,
551-
attributes: utils.getSemanticAttributesFromPool(this.options),
565+
attributes: utils.getSemanticAttributesFromPool(this.options, plugin._semconvStability),
552566
});
553567

554568
plugin._setPoolConnectEventListeners(this);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type PostgresCallback = (err: Error, res: object) => unknown;
3232
export interface PgParsedConnectionParams {
3333
database?: string;
3434
host?: string;
35+
namespace?: string;
3536
port?: number;
3637
user?: string;
3738
}
@@ -47,17 +48,18 @@ export type PgPoolCallback = (
4748
) => void;
4849

4950
export interface PgPoolOptionsParams {
51+
allowExitOnIdle: boolean;
52+
connectionString?: string; // connection string if provided directly
5053
database: string;
5154
host: string;
52-
port: number;
53-
user: string;
5455
idleTimeoutMillis: number; // the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time
55-
maxClient: number; // maximum size of the pool
56-
connectionString?: string; // connection string if provided directly
5756
max: number;
58-
maxUses: number;
59-
allowExitOnIdle: boolean;
57+
maxClient: number; // maximum size of the pool
6058
maxLifetimeSeconds: number;
59+
maxUses: number;
60+
namespace: string;
61+
port: number;
62+
user: string;
6163
}
6264

6365
export const EVENT_LISTENERS_SET = Symbol(

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

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
import { AttributeNames } from './enums/AttributeNames';
2929
import {
3030
ATTR_ERROR_TYPE,
31+
ATTR_DB_SYSTEM_NAME,
32+
ATTR_DB_NAMESPACE,
3133
SEMATTRS_DB_SYSTEM,
3234
SEMATTRS_DB_NAME,
3335
SEMATTRS_DB_CONNECTION_STRING,
@@ -36,6 +38,9 @@ import {
3638
SEMATTRS_DB_USER,
3739
SEMATTRS_DB_STATEMENT,
3840
DBSYSTEMVALUES_POSTGRESQL,
41+
ATTR_NETWORK_PEER_ADDRESS,
42+
ATTR_NETWORK_PEER_PORT,
43+
ATTR_DB_QUERY_TEXT,
3944
} from '@opentelemetry/semantic-conventions';
4045
import {
4146
ATTR_DB_CLIENT_CONNECTION_POOL_NAME,
@@ -53,7 +58,7 @@ import {
5358
} from './internal-types';
5459
import { PgInstrumentationConfig } from './types';
5560
import type * as pgTypes from 'pg';
56-
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
61+
import { safeExecuteInTheMiddle, SemconvStability, semconvStabilityFromStr } from '@opentelemetry/instrumentation';
5762
import { SpanNames } from './enums/SpanNames';
5863

5964
/**
@@ -146,19 +151,39 @@ function getPort(port: number | undefined): number | undefined {
146151
}
147152

148153
export function getSemanticAttributesFromConnection(
149-
params: PgParsedConnectionParams
154+
params: PgParsedConnectionParams,
155+
semconvStability: SemconvStability,
150156
) {
151-
return {
152-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
153-
[SEMATTRS_DB_NAME]: params.database, // required
154-
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
155-
[SEMATTRS_NET_PEER_NAME]: params.host, // required
156-
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
157-
[SEMATTRS_DB_USER]: params.user,
158-
};
157+
let attributes: Attributes = {};
158+
159+
if (semconvStability & SemconvStability.OLD) {
160+
attributes = {
161+
...attributes,
162+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
163+
[SEMATTRS_DB_NAME]: params.database,
164+
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params),
165+
[SEMATTRS_DB_USER]: params.user,
166+
[SEMATTRS_NET_PEER_NAME]: params.host, // required
167+
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
168+
}
169+
}
170+
if (semconvStability & SemconvStability.STABLE) {
171+
attributes = {
172+
...attributes,
173+
[ATTR_DB_SYSTEM_NAME]: DBSYSTEMVALUES_POSTGRESQL,
174+
[ATTR_DB_NAMESPACE]: params.namespace,
175+
[ATTR_NETWORK_PEER_ADDRESS]: params.host,
176+
[ATTR_NETWORK_PEER_PORT]: getPort(params.port),
177+
}
178+
}
179+
180+
return attributes;
159181
}
160182

161-
export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
183+
export function getSemanticAttributesFromPool(
184+
params: PgPoolOptionsParams,
185+
semconvStability: SemconvStability,
186+
) {
162187
let url: URL | undefined;
163188
try {
164189
url = params.connectionString
@@ -167,17 +192,33 @@ export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
167192
} catch (e) {
168193
url = undefined;
169194
}
170-
171-
return {
172-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
173-
[SEMATTRS_DB_NAME]: url?.pathname.slice(1) ?? params.database, // required
174-
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
175-
[SEMATTRS_NET_PEER_NAME]: url?.hostname ?? params.host, // required
176-
[SEMATTRS_NET_PEER_PORT]: Number(url?.port) || getPort(params.port),
177-
[SEMATTRS_DB_USER]: url?.username ?? params.user,
195+
let attributes: Attributes = {
178196
[AttributeNames.IDLE_TIMEOUT_MILLIS]: params.idleTimeoutMillis,
179197
[AttributeNames.MAX_CLIENT]: params.maxClient,
180198
};
199+
200+
if (semconvStability & SemconvStability.OLD) {
201+
attributes = {
202+
...attributes,
203+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
204+
[SEMATTRS_DB_NAME]: url?.pathname.slice(1) ?? params.database,
205+
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params),
206+
[SEMATTRS_NET_PEER_NAME]: url?.hostname ?? params.host,
207+
[SEMATTRS_NET_PEER_PORT]: Number(url?.port) || getPort(params.port),
208+
[SEMATTRS_DB_USER]: url?.username ?? params.user,
209+
}
210+
}
211+
if (semconvStability & SemconvStability.STABLE) {
212+
attributes = {
213+
...attributes,
214+
[ATTR_DB_SYSTEM_NAME]: DBSYSTEMVALUES_POSTGRESQL,
215+
[ATTR_DB_NAMESPACE]: params.namespace,
216+
[ATTR_NETWORK_PEER_ADDRESS]: url?.hostname ?? params.host,
217+
[ATTR_NETWORK_PEER_PORT]: Number(url?.port) || getPort(params.port),
218+
}
219+
}
220+
221+
return attributes;
181222
}
182223

183224
export function shouldSkipInstrumentation(
@@ -195,6 +236,7 @@ export function handleConfigQuery(
195236
this: PgClientExtended,
196237
tracer: Tracer,
197238
instrumentationConfig: PgInstrumentationConfig,
239+
semconvStability: SemconvStability,
198240
queryConfig?: { text: string; values?: unknown; name?: unknown }
199241
) {
200242
// Create child span.
@@ -204,7 +246,7 @@ export function handleConfigQuery(
204246
const spanName = getQuerySpanName(dbName, queryConfig);
205247
const span = tracer.startSpan(spanName, {
206248
kind: SpanKind.CLIENT,
207-
attributes: getSemanticAttributesFromConnection(connectionParameters),
249+
attributes: getSemanticAttributesFromConnection(connectionParameters, semconvStability),
208250
});
209251

210252
if (!queryConfig) {
@@ -213,7 +255,12 @@ export function handleConfigQuery(
213255

214256
// Set attributes
215257
if (queryConfig.text) {
216-
span.setAttribute(SEMATTRS_DB_STATEMENT, queryConfig.text);
258+
if (semconvStability & SemconvStability.OLD) {
259+
span.setAttribute(SEMATTRS_DB_STATEMENT, queryConfig.text);
260+
}
261+
if (semconvStability & SemconvStability.STABLE) {
262+
span.setAttribute(ATTR_DB_QUERY_TEXT, queryConfig.text);
263+
}
217264
}
218265

219266
if (

0 commit comments

Comments
 (0)