Skip to content

Commit 9d0371b

Browse files
authored
chore(instrumentation-mysql2): update semconv usage to ATTR_ exports (#3054)
Refs: #2377
1 parent 30c1c65 commit 9d0371b

File tree

6 files changed

+145
-43
lines changed

6 files changed

+145
-43
lines changed

package-lock.json

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

packages/instrumentation-mysql2/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
},
6969
"dependencies": {
7070
"@opentelemetry/instrumentation": "^0.205.0",
71-
"@opentelemetry/semantic-conventions": "^1.27.0",
7271
"@opentelemetry/sql-common": "^0.41.0"
7372
},
7473
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-mysql2#readme"

packages/instrumentation-mysql2/src/instrumentation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import {
2323
safeExecuteInTheMiddle,
2424
} from '@opentelemetry/instrumentation';
2525
import {
26-
DBSYSTEMVALUES_MYSQL,
27-
SEMATTRS_DB_STATEMENT,
28-
SEMATTRS_DB_SYSTEM,
29-
} from '@opentelemetry/semantic-conventions';
26+
DB_SYSTEM_VALUE_MYSQL,
27+
ATTR_DB_STATEMENT,
28+
ATTR_DB_SYSTEM,
29+
} from './semconv';
3030
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
3131
import type * as mysqlTypes from 'mysql2';
3232
import { MySQL2InstrumentationConfig } from './types';
@@ -46,7 +46,7 @@ const supportedVersions = ['>=1.4.2 <4'];
4646

4747
export class MySQL2Instrumentation extends InstrumentationBase<MySQL2InstrumentationConfig> {
4848
static readonly COMMON_ATTRIBUTES = {
49-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
49+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_MYSQL,
5050
};
5151

5252
constructor(config: MySQL2InstrumentationConfig = {}) {
@@ -144,7 +144,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
144144
attributes: {
145145
...MySQL2Instrumentation.COMMON_ATTRIBUTES,
146146
...getConnectionAttributes(this.config),
147-
[SEMATTRS_DB_STATEMENT]: getDbStatement(
147+
[ATTR_DB_STATEMENT]: getDbStatement(
148148
query,
149149
format,
150150
values,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
17+
/*
18+
* This file contains a copy of unstable semantic convention definitions
19+
* used by this package.
20+
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
21+
*/
22+
23+
/**
24+
* Deprecated, use `server.address`, `server.port` attributes instead.
25+
*
26+
* @example "Server=(localdb)\\v11.0;Integrated Security=true;"
27+
*
28+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
29+
*
30+
* @deprecated Replaced by `server.address` and `server.port`.
31+
*/
32+
export const ATTR_DB_CONNECTION_STRING = 'db.connection_string' as const;
33+
34+
/**
35+
* Deprecated, use `db.namespace` instead.
36+
*
37+
* @example customers
38+
* @example main
39+
*
40+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
41+
*
42+
* @deprecated Replaced by `db.namespace`.
43+
*/
44+
export const ATTR_DB_NAME = 'db.name' as const;
45+
46+
/**
47+
* The database statement being executed.
48+
*
49+
* @example SELECT * FROM wuser_table
50+
* @example SET mykey "WuValue"
51+
*
52+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
53+
*
54+
* @deprecated Replaced by `db.query.text`.
55+
*/
56+
export const ATTR_DB_STATEMENT = 'db.statement' as const;
57+
58+
/**
59+
* Deprecated, use `db.system.name` instead.
60+
*
61+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
62+
*
63+
* @deprecated Replaced by `db.system.name`.
64+
*/
65+
export const ATTR_DB_SYSTEM = 'db.system' as const;
66+
67+
/**
68+
* Deprecated, no replacement at this time.
69+
*
70+
* @example readonly_user
71+
* @example reporting_user
72+
*
73+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
74+
*
75+
* @deprecated Removed, no replacement at this time.
76+
*/
77+
export const ATTR_DB_USER = 'db.user' as const;
78+
79+
/**
80+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
81+
*
82+
* @example example.com
83+
*
84+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
85+
*
86+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
87+
*/
88+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
89+
90+
/**
91+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
92+
*
93+
* @example 8080
94+
*
95+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
96+
*
97+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
98+
*/
99+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;
100+
101+
/**
102+
* Enum value "mysql" for attribute {@link ATTR_DB_SYSTEM}.
103+
*
104+
* MySQL
105+
*
106+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
107+
*/
108+
export const DB_SYSTEM_VALUE_MYSQL = 'mysql' as const;

packages/instrumentation-mysql2/src/utils.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
import { Attributes } from '@opentelemetry/api';
1818
import {
19-
SEMATTRS_DB_CONNECTION_STRING,
20-
SEMATTRS_DB_NAME,
21-
SEMATTRS_DB_USER,
22-
SEMATTRS_NET_PEER_NAME,
23-
SEMATTRS_NET_PEER_PORT,
24-
} from '@opentelemetry/semantic-conventions';
19+
ATTR_DB_CONNECTION_STRING,
20+
ATTR_DB_NAME,
21+
ATTR_DB_USER,
22+
ATTR_NET_PEER_NAME,
23+
ATTR_NET_PEER_PORT,
24+
} from './semconv';
2525
import type * as mysqlTypes from 'mysql2';
2626
import { MySQL2InstrumentationQueryMaskingHook } from './types';
2727

@@ -61,18 +61,18 @@ export function getConnectionAttributes(config: Config): Attributes {
6161
const portNumber = parseInt(port, 10);
6262
if (!isNaN(portNumber)) {
6363
return {
64-
[SEMATTRS_NET_PEER_NAME]: host,
65-
[SEMATTRS_NET_PEER_PORT]: portNumber,
66-
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
67-
[SEMATTRS_DB_NAME]: database,
68-
[SEMATTRS_DB_USER]: user,
64+
[ATTR_NET_PEER_NAME]: host,
65+
[ATTR_NET_PEER_PORT]: portNumber,
66+
[ATTR_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
67+
[ATTR_DB_NAME]: database,
68+
[ATTR_DB_USER]: user,
6969
};
7070
}
7171
return {
72-
[SEMATTRS_NET_PEER_NAME]: host,
73-
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
74-
[SEMATTRS_DB_NAME]: database,
75-
[SEMATTRS_DB_USER]: user,
72+
[ATTR_NET_PEER_NAME]: host,
73+
[ATTR_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
74+
[ATTR_DB_NAME]: database,
75+
[ATTR_DB_USER]: user,
7676
};
7777
}
7878

packages/instrumentation-mysql2/test/mysql.test.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import * as semver from 'semver';
1818
import { context, trace, SpanStatusCode } from '@opentelemetry/api';
1919
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
2020
import {
21-
DBSYSTEMVALUES_MYSQL,
22-
SEMATTRS_DB_NAME,
23-
SEMATTRS_DB_STATEMENT,
24-
SEMATTRS_DB_SYSTEM,
25-
SEMATTRS_DB_USER,
26-
SEMATTRS_NET_PEER_NAME,
27-
SEMATTRS_NET_PEER_PORT,
28-
} from '@opentelemetry/semantic-conventions';
21+
DB_SYSTEM_VALUE_MYSQL,
22+
ATTR_DB_NAME,
23+
ATTR_DB_STATEMENT,
24+
ATTR_DB_SYSTEM,
25+
ATTR_DB_USER,
26+
ATTR_NET_PEER_NAME,
27+
ATTR_NET_PEER_PORT,
28+
} from '../src/semconv';
2929
import * as testUtils from '@opentelemetry/contrib-test-utils';
3030
import {
3131
BasicTracerProvider,
@@ -214,7 +214,7 @@ describe('mysql2', () => {
214214
query.on('end', () => {
215215
const spans = memoryExporter.getFinishedSpans();
216216
assert.strictEqual(spans[0].name, 'SELECT');
217-
assert.strictEqual(spans[0].attributes[SEMATTRS_DB_STATEMENT], sql);
217+
assert.strictEqual(spans[0].attributes[ATTR_DB_STATEMENT], sql);
218218
done();
219219
});
220220
});
@@ -232,7 +232,7 @@ describe('mysql2', () => {
232232
const spans = memoryExporter.getFinishedSpans();
233233
assert.strictEqual(spans[0].name, 'SELECT');
234234
assert.strictEqual(
235-
spans[0].attributes[SEMATTRS_DB_STATEMENT],
235+
spans[0].attributes[ATTR_DB_STATEMENT],
236236
query.sql
237237
);
238238
done();
@@ -1568,15 +1568,12 @@ function assertSpan(
15681568
values?: any,
15691569
errorMessage?: string
15701570
) {
1571-
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], DBSYSTEMVALUES_MYSQL);
1572-
assert.strictEqual(span.attributes[SEMATTRS_DB_NAME], database);
1573-
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_PORT], port);
1574-
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_NAME], host);
1575-
assert.strictEqual(span.attributes[SEMATTRS_DB_USER], user);
1576-
assert.strictEqual(
1577-
span.attributes[SEMATTRS_DB_STATEMENT],
1578-
format(sql, values)
1579-
);
1571+
assert.strictEqual(span.attributes[ATTR_DB_SYSTEM], DB_SYSTEM_VALUE_MYSQL);
1572+
assert.strictEqual(span.attributes[ATTR_DB_NAME], database);
1573+
assert.strictEqual(span.attributes[ATTR_NET_PEER_PORT], port);
1574+
assert.strictEqual(span.attributes[ATTR_NET_PEER_NAME], host);
1575+
assert.strictEqual(span.attributes[ATTR_DB_USER], user);
1576+
assert.strictEqual(span.attributes[ATTR_DB_STATEMENT], format(sql, values));
15801577
if (errorMessage) {
15811578
assert.strictEqual(span.status.message, errorMessage);
15821579
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);

0 commit comments

Comments
 (0)