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

chore(opentelemetry-instrumentation-pg): use exported strings #2053

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.49.1",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@opentelemetry/sql-common": "^0.40.0",
"@types/pg": "8.6.1",
"@types/pg-pool": "2.0.4"
Expand Down
36 changes: 21 additions & 15 deletions plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ import {
} from '@opentelemetry/api';
import { AttributeNames } from './enums/AttributeNames';
import {
SemanticAttributes,
DbSystemValues,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_NAME,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_DB_USER,
SEMATTRS_DB_STATEMENT,
DBSYSTEMVALUES_POSTGRESQL,
} from '@opentelemetry/semantic-conventions';
import {
PgClientExtended,
Expand Down Expand Up @@ -108,23 +114,23 @@ 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
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
[SemanticAttributes.DB_USER]: params.user,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
[SEMATTRS_DB_NAME]: params.database, // required
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
[SEMATTRS_NET_PEER_NAME]: params.host, // required
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
[SEMATTRS_DB_USER]: params.user,
};
}

export function getSemanticAttributesFromPool(params: PgPoolOptionsParams) {
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
[SemanticAttributes.NET_PEER_PORT]: getPort(params.port),
[SemanticAttributes.DB_USER]: params.user,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
[SEMATTRS_DB_NAME]: params.database, // required
[SEMATTRS_DB_CONNECTION_STRING]: getConnectionString(params), // required
[SEMATTRS_NET_PEER_NAME]: params.host, // required
[SEMATTRS_NET_PEER_PORT]: getPort(params.port),
[SEMATTRS_DB_USER]: params.user,
[AttributeNames.IDLE_TIMEOUT_MILLIS]: params.idleTimeoutMillis,
[AttributeNames.MAX_CLIENT]: params.maxClient,
};
Expand Down Expand Up @@ -163,7 +169,7 @@ export function handleConfigQuery(

// Set attributes
if (queryConfig.text) {
span.setAttribute(SemanticAttributes.DB_STATEMENT, queryConfig.text);
span.setAttribute(SEMATTRS_DB_STATEMENT, queryConfig.text);
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ import * as pgPool from 'pg-pool';
import { AttributeNames } from '../src/enums/AttributeNames';
import { TimedEvent } from './types';
import {
SemanticAttributes,
DbSystemValues,
DBSYSTEMVALUES_POSTGRESQL,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_NAME,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_DB_USER,
SEMATTRS_DB_STATEMENT,
} from '@opentelemetry/semantic-conventions';

const memoryExporter = new InMemorySpanExporter();
Expand All @@ -60,23 +66,23 @@ const CONFIG = {
};

const DEFAULT_PGPOOL_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
[SemanticAttributes.DB_NAME]: CONFIG.database,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
[SemanticAttributes.DB_USER]: CONFIG.user,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
[SEMATTRS_DB_NAME]: CONFIG.database,
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
[SEMATTRS_DB_USER]: CONFIG.user,
[AttributeNames.MAX_CLIENT]: CONFIG.maxClient,
[AttributeNames.IDLE_TIMEOUT_MILLIS]: CONFIG.idleTimeoutMillis,
};

const DEFAULT_PG_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
[SemanticAttributes.DB_NAME]: CONFIG.database,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
[SemanticAttributes.DB_USER]: CONFIG.user,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
[SEMATTRS_DB_NAME]: CONFIG.database,
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
[SEMATTRS_DB_USER]: CONFIG.user,
};

const unsetStatus: SpanStatus = {
Expand Down Expand Up @@ -179,7 +185,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const span = provider.getTracer('test-pg-pool').startSpan('test span');
Expand Down Expand Up @@ -211,7 +217,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const parentSpan = provider
Expand Down Expand Up @@ -284,7 +290,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const span = provider.getTracer('test-pg-pool').startSpan('test span');
Expand All @@ -303,7 +309,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const parentSpan = provider
Expand Down Expand Up @@ -340,7 +346,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
[dataAttributeName]: '{"rowCount":1}',
};

Expand Down Expand Up @@ -422,7 +428,7 @@ describe('pg-pool', () => {
};
const pgAttributes = {
...DEFAULT_PG_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};

beforeEach(async () => {
Expand Down
48 changes: 27 additions & 21 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ import {
import { AttributeNames } from '../src/enums/AttributeNames';
import { TimedEvent } from './types';
import {
SemanticAttributes,
DbSystemValues,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_NAME,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_DB_USER,
DBSYSTEMVALUES_POSTGRESQL,
} from '@opentelemetry/semantic-conventions';
import { addSqlCommenterComment } from '@opentelemetry/sql-common';

Expand All @@ -60,12 +66,12 @@ const CONFIG = {
};

const DEFAULT_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.POSTGRESQL,
[SemanticAttributes.DB_NAME]: CONFIG.database,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
[SemanticAttributes.DB_USER]: CONFIG.user,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_POSTGRESQL,
[SEMATTRS_DB_NAME]: CONFIG.database,
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
[SEMATTRS_DB_CONNECTION_STRING]: `postgresql://${CONFIG.host}:${CONFIG.port}/${CONFIG.database}`,
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
[SEMATTRS_DB_USER]: CONFIG.user,
};

const unsetStatus: SpanStatus = {
Expand Down Expand Up @@ -348,7 +354,7 @@ describe('pg', () => {
it('should intercept client.query(text, callback)', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: 'SELECT NOW()',
[SEMATTRS_DB_STATEMENT]: 'SELECT NOW()',
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -368,7 +374,7 @@ describe('pg', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -387,7 +393,7 @@ describe('pg', () => {
const query = 'SELECT NOW()';
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -409,7 +415,7 @@ describe('pg', () => {
const query = 'SELECT NOW()';
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -429,7 +435,7 @@ describe('pg', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -449,7 +455,7 @@ describe('pg', () => {
const values = ['0'];
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -474,7 +480,7 @@ describe('pg', () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.PG_PLAN]: name,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand All @@ -498,7 +504,7 @@ describe('pg', () => {
const query = 'SELECT NOW()';
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};
const events: TimedEvent[] = [];
const span = tracer.startSpan('test span');
Expand Down Expand Up @@ -537,7 +543,7 @@ describe('pg', () => {

const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
[AttributeNames.PG_VALUES]: [
'Hello,World',
'abc',
Expand Down Expand Up @@ -576,7 +582,7 @@ describe('pg', () => {
// span if there is no requestHook.
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};

// These are the attributes we expect on the span after the requestHook
Expand Down Expand Up @@ -669,7 +675,7 @@ describe('pg', () => {
describe('AND valid responseHook', () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
[dataAttributeName]: '{"rowCount":1}',
};
beforeEach(async () => {
Expand Down Expand Up @@ -702,7 +708,7 @@ describe('pg', () => {
it('should attach response hook data to resulting spans for query returning a Promise', async () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
[dataAttributeName]: '{"rowCount":1}',
};

Expand All @@ -727,7 +733,7 @@ describe('pg', () => {
describe('AND invalid responseHook', () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: query,
[SEMATTRS_DB_STATEMENT]: query,
};

beforeEach(async () => {
Expand Down
Loading
Loading