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

Fix semantic conventions for PG and PG Pool #467

Merged
merged 7 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions examples/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "file:///Users/neumanns/Projects/opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-pg",
svrnm marked this conversation as resolved.
Show resolved Hide resolved
svrnm marked this conversation as resolved.
Show resolved Hide resolved
"@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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"dependencies": {
"@opentelemetry/api": "^1.0.0-rc.0",
"@opentelemetry/instrumentation": "^0.19.0"
"@opentelemetry/instrumentation": "^0.19.0",
"@opentelemetry/semantic-conventions": "^0.19.0"
}
}
27 changes: 5 additions & 22 deletions plugins/node/opentelemetry-instrumentation-pg/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
svrnm marked this conversation as resolved.
Show resolved Hide resolved
PG_PLAN = 'db.postgresql.plan',
IDLE_TIMEOUT_MILLIS = 'db.postgresql.idle.timeout.millis',
MAX_CLIENT = 'db.postgresql.max.client',
}
18 changes: 10 additions & 8 deletions plugins/node/opentelemetry-instrumentation-pg/src/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';

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

Expand All @@ -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 = {
Expand Down Expand Up @@ -162,7 +164,7 @@ describe('[email protected]', () => {
};
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');
Expand All @@ -186,7 +188,7 @@ describe('[email protected]', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'SELECT NOW()',
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const parentSpan = provider
Expand Down Expand Up @@ -242,7 +244,7 @@ describe('[email protected]', () => {
};
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');
Expand All @@ -261,7 +263,7 @@ describe('[email protected]', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'SELECT NOW()',
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const parentSpan = provider
Expand Down
33 changes: 18 additions & 15 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 = {
Expand Down Expand Up @@ -200,7 +203,7 @@ describe('[email protected]', () => {
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');
Expand All @@ -220,7 +223,7 @@ describe('[email protected]', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: query,
[SemanticAttributes.DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -239,7 +242,7 @@ describe('[email protected]', () => {
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');
Expand All @@ -261,7 +264,7 @@ describe('[email protected]', () => {
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');
Expand All @@ -281,7 +284,7 @@ describe('[email protected]', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: query,
[SemanticAttributes.DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -301,7 +304,7 @@ describe('[email protected]', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: query,
[SemanticAttributes.DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -326,7 +329,7 @@ describe('[email protected]', () => {
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');
Expand All @@ -350,7 +353,7 @@ describe('[email protected]', () => {
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');
Expand Down