From 1386d75d6c5713a96d537a7de27b8399e950f127 Mon Sep 17 00:00:00 2001 From: Severin Neumann Date: Sat, 8 May 2021 10:35:33 +0200 Subject: [PATCH] Fix semantic conventions for PG and PG Pool (#467) Co-authored-by: vmarchaud --- examples/postgres/package.json | 4 +- .../package.json | 1 + .../src/enums.ts | 27 +++---------- .../src/pg.ts | 18 +++++---- .../src/utils.ts | 23 ++++++----- .../test/pg-pool.test.ts | 38 ++++++++++--------- .../test/pg.test.ts | 33 ++++++++-------- 7 files changed, 69 insertions(+), 75 deletions(-) diff --git a/examples/postgres/package.json b/examples/postgres/package.json index d4525cd9de3..bd4414b987a 100644 --- a/examples/postgres/package.json +++ b/examples/postgres/package.json @@ -35,11 +35,11 @@ "@opentelemetry/exporter-zipkin": "^0.19.0", "@opentelemetry/instrumentation": "^0.19.0", "@opentelemetry/instrumentation-http": "^0.19.0", - "@opentelemetry/instrumentation-pg": "^0.15.0", + "@opentelemetry/instrumentation-pg": "^0.16.0", "@opentelemetry/node": "^0.19.0", "@opentelemetry/tracing": "^0.19.0", "express": "^4.17.1", - "pg": "^7.12.1" + "pg": "^8.6.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", "devDependencies": { diff --git a/plugins/node/opentelemetry-instrumentation-pg/package.json b/plugins/node/opentelemetry-instrumentation-pg/package.json index 2442b4b92c3..656f59630b4 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/package.json +++ b/plugins/node/opentelemetry-instrumentation-pg/package.json @@ -67,6 +67,7 @@ "dependencies": { "@opentelemetry/api": "^1.0.0-rc.0", "@opentelemetry/instrumentation": "^0.19.0", + "@opentelemetry/semantic-conventions": "^0.19.0", "@types/pg": "7.14.11", "@types/pg-pool": "2.0.2" } diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/enums.ts b/plugins/node/opentelemetry-instrumentation-pg/src/enums.ts index 435a531c8a6..994de4cb4f4 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/enums.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/enums.ts @@ -14,27 +14,10 @@ * limitations under the License. */ +// Postgresql specific attributes not covered by semantic conventions export enum AttributeNames { - // required by https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/data-database.md - COMPONENT = 'component', - DB_TYPE = 'db.type', - DB_INSTANCE = 'db.instance', - DB_STATEMENT = 'db.statement', - PEER_ADDRESS = 'peer.address', - PEER_HOSTNAME = 'peer.host', - - // optional - DB_USER = 'db.user', - PEER_PORT = 'peer.port', - PEER_IPV4 = 'peer.ipv4', - PEER_IPV6 = 'peer.ipv6', - PEER_SERVICE = 'peer.service', - - // PG specific -- not specified by spec - PG_VALUES = 'pg.values', - PG_PLAN = 'pg.plan', - - // PG-POOL specific -- not specified by spec - IDLE_TIMEOUT_MILLIS = 'idle.timeout.millis', - MAX_CLIENT = 'max', + PG_VALUES = 'db.postgresql.values', + PG_PLAN = 'db.postgresql.plan', + IDLE_TIMEOUT_MILLIS = 'db.postgresql.idle.timeout.millis', + MAX_CLIENT = 'db.postgresql.max.client', } diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/pg.ts b/plugins/node/opentelemetry-instrumentation-pg/src/pg.ts index 1e2cca96a34..00496a190ea 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/pg.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/pg.ts @@ -39,6 +39,10 @@ import { } from './types'; import * as utils from './utils'; import { AttributeNames } from './enums'; +import { + SemanticAttributes, + DbSystemValues, +} from '@opentelemetry/semantic-conventions'; import { VERSION } from './version'; export interface PgInstrumentationConfig extends InstrumentationConfig { @@ -54,7 +58,6 @@ const PG_POOL_COMPONENT = 'pg-pool'; export class PgInstrumentation extends InstrumentationBase { static readonly COMPONENT = 'pg'; - static readonly DB_TYPE = 'sql'; static readonly BASE_SPAN_NAME = PgInstrumentation.COMPONENT + '.query'; @@ -227,13 +230,12 @@ export class PgInstrumentation extends InstrumentationBase { const span = plugin.tracer.startSpan(`${PG_POOL_COMPONENT}.connect`, { kind: SpanKind.CLIENT, attributes: { - [AttributeNames.COMPONENT]: PgInstrumentation.COMPONENT, // required - [AttributeNames.DB_TYPE]: PgInstrumentation.DB_TYPE, // required - [AttributeNames.DB_INSTANCE]: this.options.database, // required - [AttributeNames.PEER_HOSTNAME]: this.options.host, // required - [AttributeNames.PEER_ADDRESS]: jdbcString, // required - [AttributeNames.PEER_PORT]: this.options.port, - [AttributeNames.DB_USER]: this.options.user, + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, + [SemanticAttributes.DB_NAME]: this.options.database, // required + [SemanticAttributes.NET_PEER_NAME]: this.options.host, // required + [SemanticAttributes.DB_CONNECTION_STRING]: jdbcString, // required + [SemanticAttributes.NET_PEER_PORT]: this.options.port, + [SemanticAttributes.DB_USER]: this.options.user, [AttributeNames.IDLE_TIMEOUT_MILLIS]: this.options .idleTimeoutMillis, [AttributeNames.MAX_CLIENT]: this.options.maxClient, diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index 950d4e28f70..8d70757cab0 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -15,6 +15,10 @@ */ import { Span, SpanStatusCode, Tracer, SpanKind } from '@opentelemetry/api'; +import { + SemanticAttributes, + DbSystemValues, +} from '@opentelemetry/semantic-conventions'; import { AttributeNames } from './enums'; import { PgClientExtended, @@ -51,13 +55,12 @@ function pgStartSpan(tracer: Tracer, client: PgClientExtended, name: string) { return tracer.startSpan(name, { kind: SpanKind.CLIENT, attributes: { - [AttributeNames.COMPONENT]: PgInstrumentation.COMPONENT, // required - [AttributeNames.DB_INSTANCE]: client.connectionParameters.database, // required - [AttributeNames.DB_TYPE]: PgInstrumentation.DB_TYPE, // required - [AttributeNames.PEER_ADDRESS]: jdbcString, // required - [AttributeNames.PEER_HOSTNAME]: client.connectionParameters.host, // required - [AttributeNames.PEER_PORT]: client.connectionParameters.port, - [AttributeNames.DB_USER]: client.connectionParameters.user, + [SemanticAttributes.DB_NAME]: client.connectionParameters.database, // required + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, // required + [SemanticAttributes.DB_CONNECTION_STRING]: jdbcString, // required + [SemanticAttributes.NET_PEER_NAME]: client.connectionParameters.host, // required + [SemanticAttributes.NET_PEER_PORT]: client.connectionParameters.port, + [SemanticAttributes.DB_USER]: client.connectionParameters.user, }, }); } @@ -76,7 +79,7 @@ export function handleConfigQuery( // Set attributes if (queryConfig.text) { - span.setAttribute(AttributeNames.DB_STATEMENT, queryConfig.text); + span.setAttribute(SemanticAttributes.DB_STATEMENT, queryConfig.text); } if ( instrumentationConfig.enhancedDatabaseReporting && @@ -109,7 +112,7 @@ export function handleParameterizedQuery( const span = pgStartSpan(tracer, this, name); // Set attributes - span.setAttribute(AttributeNames.DB_STATEMENT, query); + span.setAttribute(SemanticAttributes.DB_STATEMENT, query); if (instrumentationConfig.enhancedDatabaseReporting) { span.setAttribute(AttributeNames.PG_VALUES, arrayStringifyHelper(values)); } @@ -129,7 +132,7 @@ export function handleTextQuery( const span = pgStartSpan(tracer, this, name); // Set attributes - span.setAttribute(AttributeNames.DB_STATEMENT, query); + span.setAttribute(SemanticAttributes.DB_STATEMENT, query); return span; } diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts index c28eda899b9..df49d973874 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts @@ -36,6 +36,10 @@ import * as assert from 'assert'; import * as pg from 'pg'; import * as pgPool from 'pg-pool'; import { AttributeNames } from '../src/enums'; +import { + SemanticAttributes, + DbSystemValues, +} from '@opentelemetry/semantic-conventions'; const memoryExporter = new InMemorySpanExporter(); @@ -52,25 +56,23 @@ const CONFIG = { }; const DEFAULT_PGPOOL_ATTRIBUTES = { - [AttributeNames.COMPONENT]: PgInstrumentation.COMPONENT, - [AttributeNames.DB_INSTANCE]: CONFIG.database, - [AttributeNames.DB_TYPE]: PgInstrumentation.DB_TYPE, - [AttributeNames.PEER_HOSTNAME]: CONFIG.host, - [AttributeNames.PEER_ADDRESS]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, - [AttributeNames.PEER_PORT]: CONFIG.port, - [AttributeNames.DB_USER]: CONFIG.user, + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, + [SemanticAttributes.DB_NAME]: CONFIG.database, + [SemanticAttributes.NET_PEER_NAME]: CONFIG.host, + [SemanticAttributes.DB_CONNECTION_STRING]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, + [SemanticAttributes.NET_PEER_PORT]: CONFIG.port, + [SemanticAttributes.DB_USER]: CONFIG.user, [AttributeNames.MAX_CLIENT]: CONFIG.maxClient, [AttributeNames.IDLE_TIMEOUT_MILLIS]: CONFIG.idleTimeoutMillis, }; const DEFAULT_PG_ATTRIBUTES = { - [AttributeNames.COMPONENT]: PgInstrumentation.COMPONENT, - [AttributeNames.DB_INSTANCE]: CONFIG.database, - [AttributeNames.DB_TYPE]: PgInstrumentation.DB_TYPE, - [AttributeNames.PEER_HOSTNAME]: CONFIG.host, - [AttributeNames.PEER_ADDRESS]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, - [AttributeNames.PEER_PORT]: CONFIG.port, - [AttributeNames.DB_USER]: CONFIG.user, + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, + [SemanticAttributes.DB_NAME]: CONFIG.database, + [SemanticAttributes.NET_PEER_NAME]: CONFIG.host, + [SemanticAttributes.DB_CONNECTION_STRING]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, + [SemanticAttributes.NET_PEER_PORT]: CONFIG.port, + [SemanticAttributes.DB_USER]: CONFIG.user, }; const unsetStatus: SpanStatus = { @@ -162,7 +164,7 @@ describe('pg-pool@2.x', () => { }; const pgAttributes = { ...DEFAULT_PG_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: 'SELECT NOW()', + [SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()', }; const events: TimedEvent[] = []; const span = provider.getTracer('test-pg-pool').startSpan('test span'); @@ -186,7 +188,7 @@ describe('pg-pool@2.x', () => { }; const pgAttributes = { ...DEFAULT_PG_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: 'SELECT NOW()', + [SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()', }; const events: TimedEvent[] = []; const parentSpan = provider @@ -242,7 +244,7 @@ describe('pg-pool@2.x', () => { }; const pgAttributes = { ...DEFAULT_PG_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: 'SELECT NOW()', + [SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()', }; const events: TimedEvent[] = []; const span = provider.getTracer('test-pg-pool').startSpan('test span'); @@ -261,7 +263,7 @@ describe('pg-pool@2.x', () => { }; const pgAttributes = { ...DEFAULT_PG_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: 'SELECT NOW()', + [SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()', }; const events: TimedEvent[] = []; const parentSpan = provider diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index 4182cfd015c..4837846bcae 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -36,6 +36,10 @@ import * as assert from 'assert'; import type * as pg from 'pg'; import { PgInstrumentation } from '../src'; import { AttributeNames } from '../src/enums'; +import { + SemanticAttributes, + DbSystemValues, +} from '@opentelemetry/semantic-conventions'; const memoryExporter = new InMemorySpanExporter(); @@ -50,13 +54,12 @@ const CONFIG = { }; const DEFAULT_ATTRIBUTES = { - [AttributeNames.COMPONENT]: PgInstrumentation.COMPONENT, - [AttributeNames.DB_INSTANCE]: CONFIG.database, - [AttributeNames.DB_TYPE]: PgInstrumentation.DB_TYPE, - [AttributeNames.PEER_HOSTNAME]: CONFIG.host, - [AttributeNames.PEER_ADDRESS]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, - [AttributeNames.PEER_PORT]: CONFIG.port, - [AttributeNames.DB_USER]: CONFIG.user, + [SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL, + [SemanticAttributes.DB_NAME]: CONFIG.database, + [SemanticAttributes.NET_PEER_NAME]: CONFIG.host, + [SemanticAttributes.DB_CONNECTION_STRING]: `jdbc:postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`, + [SemanticAttributes.NET_PEER_PORT]: CONFIG.port, + [SemanticAttributes.DB_USER]: CONFIG.user, }; const unsetStatus: SpanStatus = { @@ -200,7 +203,7 @@ describe('pg@7.x', () => { it('should intercept client.query(text, callback)', done => { const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: 'SELECT NOW()', + [SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()', }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -220,7 +223,7 @@ describe('pg@7.x', () => { const values = ['0']; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -239,7 +242,7 @@ describe('pg@7.x', () => { const query = 'SELECT NOW()'; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -261,7 +264,7 @@ describe('pg@7.x', () => { const query = 'SELECT NOW()'; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -281,7 +284,7 @@ describe('pg@7.x', () => { const values = ['0']; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -301,7 +304,7 @@ describe('pg@7.x', () => { const values = ['0']; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -326,7 +329,7 @@ describe('pg@7.x', () => { const attributes = { ...DEFAULT_ATTRIBUTES, [AttributeNames.PG_PLAN]: name, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); @@ -350,7 +353,7 @@ describe('pg@7.x', () => { const query = 'SELECT NOW()'; const attributes = { ...DEFAULT_ATTRIBUTES, - [AttributeNames.DB_STATEMENT]: query, + [SemanticAttributes.DB_STATEMENT]: query, }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span');