Skip to content

Commit 98fac09

Browse files
authored
refactor(instr-mysql2): use exported strings for attributes (#2115)
- Update @opentelemetry/semantic-conventions to ^1.22 - Replace SemanticAttributes.* with exported strings SEMATTRS_* - Update README with new semantic convention package version and key Refs: #2025
1 parent b903bce commit 98fac09

File tree

6 files changed

+58
-48
lines changed

6 files changed

+58
-48
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-mysql2/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ You can set the following instrumentation options:
4949
| `responseHook` | `MySQL2InstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response |
5050
| `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ |
5151

52+
## Semantic Conventions
53+
54+
This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)
55+
56+
Attributes collected:
57+
58+
| Attribute | Short Description |
59+
| ----------------------- | ------------------------------------------------------------------------------ |
60+
| `db.connection_string` | The connection string used to connect to the database. |
61+
| `db.name` | This attribute is used to report the name of the database being accessed. |
62+
| `db.statement` | The database statement being executed. |
63+
| `db.system` | An identifier for the database management system (DBMS) product being used. |
64+
| `db.user` | Username for accessing the database. |
65+
| `net.peer.name` | Remote hostname or similar. |
66+
| `net.peer.port` | Remote port number. |
67+
5268
## Useful links
5369

5470
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
"dependencies": {
6464
"@opentelemetry/instrumentation": "^0.50.0",
65-
"@opentelemetry/semantic-conventions": "^1.0.0",
65+
"@opentelemetry/semantic-conventions": "^1.22.0",
6666
"@opentelemetry/sql-common": "^0.40.0"
6767
},
6868
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mysql2#readme"

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import {
2222
safeExecuteInTheMiddle,
2323
} from '@opentelemetry/instrumentation';
2424
import {
25-
DbSystemValues,
26-
SemanticAttributes,
25+
DBSYSTEMVALUES_MYSQL,
26+
SEMATTRS_DB_STATEMENT,
27+
SEMATTRS_DB_SYSTEM,
2728
} from '@opentelemetry/semantic-conventions';
2829
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
2930
import type * as mysqlTypes from 'mysql2';
@@ -40,7 +41,7 @@ type formatType = typeof mysqlTypes.format;
4041

4142
export class MySQL2Instrumentation extends InstrumentationBase<any> {
4243
static readonly COMMON_ATTRIBUTES = {
43-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
44+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
4445
};
4546

4647
constructor(config?: MySQL2InstrumentationConfig) {
@@ -115,11 +116,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<any> {
115116
attributes: {
116117
...MySQL2Instrumentation.COMMON_ATTRIBUTES,
117118
...getConnectionAttributes(this.config),
118-
[SemanticAttributes.DB_STATEMENT]: getDbStatement(
119-
query,
120-
format,
121-
values
122-
),
119+
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, format, values),
123120
},
124121
});
125122

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { SpanAttributes } from '@opentelemetry/api';
18-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
17+
import { Attributes } from '@opentelemetry/api';
18+
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';
1925

2026
/*
2127
Following types declare an expectation on mysql2 types and define a subset we
@@ -42,35 +48,27 @@ interface Config {
4248
connectionConfig?: Config;
4349
}
4450
/**
45-
* Get an SpanAttributes map from a mysql connection config object
51+
* Get an Attributes map from a mysql connection config object
4652
*
4753
* @param config ConnectionConfig
4854
*/
49-
export function getConnectionAttributes(config: Config): SpanAttributes {
55+
export function getConnectionAttributes(config: Config): Attributes {
5056
const { host, port, database, user } = getConfig(config);
5157
const portNumber = parseInt(port, 10);
5258
if (!isNaN(portNumber)) {
5359
return {
54-
[SemanticAttributes.NET_PEER_NAME]: host,
55-
[SemanticAttributes.NET_PEER_PORT]: portNumber,
56-
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
57-
host,
58-
port,
59-
database
60-
),
61-
[SemanticAttributes.DB_NAME]: database,
62-
[SemanticAttributes.DB_USER]: user,
60+
[SEMATTRS_NET_PEER_NAME]: host,
61+
[SEMATTRS_NET_PEER_PORT]: portNumber,
62+
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
63+
[SEMATTRS_DB_NAME]: database,
64+
[SEMATTRS_DB_USER]: user,
6365
};
6466
}
6567
return {
66-
[SemanticAttributes.NET_PEER_NAME]: host,
67-
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
68-
host,
69-
port,
70-
database
71-
),
72-
[SemanticAttributes.DB_NAME]: database,
73-
[SemanticAttributes.DB_USER]: user,
68+
[SEMATTRS_NET_PEER_NAME]: host,
69+
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
70+
[SEMATTRS_DB_NAME]: database,
71+
[SEMATTRS_DB_USER]: user,
7472
};
7573
}
7674

plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ import * as semver from 'semver';
1818
import { context, trace, SpanStatusCode } from '@opentelemetry/api';
1919
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
2020
import {
21-
DbSystemValues,
22-
SemanticAttributes,
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,
2328
} from '@opentelemetry/semantic-conventions';
2429
import * as testUtils from '@opentelemetry/contrib-test-utils';
2530
import {
@@ -177,10 +182,7 @@ describe('[email protected]', () => {
177182
query.on('end', () => {
178183
const spans = memoryExporter.getFinishedSpans();
179184
assert.strictEqual(spans[0].name, 'SELECT');
180-
assert.strictEqual(
181-
spans[0].attributes[SemanticAttributes.DB_STATEMENT],
182-
sql
183-
);
185+
assert.strictEqual(spans[0].attributes[SEMATTRS_DB_STATEMENT], sql);
184186
done();
185187
});
186188
});
@@ -198,7 +200,7 @@ describe('[email protected]', () => {
198200
const spans = memoryExporter.getFinishedSpans();
199201
assert.strictEqual(spans[0].name, 'SELECT');
200202
assert.strictEqual(
201-
spans[0].attributes[SemanticAttributes.DB_STATEMENT],
203+
spans[0].attributes[SEMATTRS_DB_STATEMENT],
202204
query.sql
203205
);
204206
done();
@@ -1215,16 +1217,13 @@ function assertSpan(
12151217
values?: any,
12161218
errorMessage?: string
12171219
) {
1220+
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], DBSYSTEMVALUES_MYSQL);
1221+
assert.strictEqual(span.attributes[SEMATTRS_DB_NAME], database);
1222+
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_PORT], port);
1223+
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_NAME], host);
1224+
assert.strictEqual(span.attributes[SEMATTRS_DB_USER], user);
12181225
assert.strictEqual(
1219-
span.attributes[SemanticAttributes.DB_SYSTEM],
1220-
DbSystemValues.MYSQL
1221-
);
1222-
assert.strictEqual(span.attributes[SemanticAttributes.DB_NAME], database);
1223-
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_PORT], port);
1224-
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_NAME], host);
1225-
assert.strictEqual(span.attributes[SemanticAttributes.DB_USER], user);
1226-
assert.strictEqual(
1227-
span.attributes[SemanticAttributes.DB_STATEMENT],
1226+
span.attributes[SEMATTRS_DB_STATEMENT],
12281227
mysqlTypes.format(sql, values)
12291228
);
12301229
if (errorMessage) {

0 commit comments

Comments
 (0)