Skip to content

Commit a288410

Browse files
authored
refactor(instr-mysql): update semantic conventions (#2112)
Refs: #2025
1 parent 98fac09 commit a288410

File tree

6 files changed

+57
-42
lines changed

6 files changed

+57
-42
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-mysql/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ See [examples/mysql](https://github.com/open-telemetry/opentelemetry-js-contrib/
4949
| [`enhancedDatabaseReporting`](./src/types.ts#L24) | `boolean` | `false` | If true, an attribute containing the query's parameters will be attached the spans generated to represent the query |
5050
|
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.operation` | The name of the operation being executed. |
63+
| `db.statement` | The database statement being executed. |
64+
| `db.system` | An identifier for the database management system (DBMS) product being used. |
65+
| `db.user` | Username for accessing the database. |
66+
| `net.peer.name` | Remote hostname or similar. |
67+
| `net.peer.port` | Remote port number. |
68+
5269
## Useful links
5370

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"dependencies": {
6262
"@opentelemetry/instrumentation": "^0.50.0",
63-
"@opentelemetry/semantic-conventions": "^1.0.0",
63+
"@opentelemetry/semantic-conventions": "^1.22.0",
6464
"@types/mysql": "2.15.22"
6565
},
6666
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mysql#readme"

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import {
2929
isWrapped,
3030
} from '@opentelemetry/instrumentation';
3131
import {
32-
DbSystemValues,
33-
SemanticAttributes,
32+
DBSYSTEMVALUES_MYSQL,
33+
SEMATTRS_DB_STATEMENT,
34+
SEMATTRS_DB_SYSTEM,
3435
} from '@opentelemetry/semantic-conventions';
3536
import type * as mysqlTypes from 'mysql';
3637
import { AttributeNames } from './AttributeNames';
@@ -54,7 +55,7 @@ export class MySQLInstrumentation extends InstrumentationBase<
5455
typeof mysqlTypes
5556
> {
5657
static readonly COMMON_ATTRIBUTES = {
57-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
58+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
5859
};
5960
private _connectionsUsage!: UpDownCounter;
6061

@@ -331,10 +332,7 @@ export class MySQLInstrumentation extends InstrumentationBase<
331332
},
332333
});
333334

334-
span.setAttribute(
335-
SemanticAttributes.DB_STATEMENT,
336-
getDbStatement(query)
337-
);
335+
span.setAttribute(SEMATTRS_DB_STATEMENT, getDbStatement(query));
338336

339337
const instrumentationConfig: MySQLInstrumentationConfig =
340338
thisPlugin.getConfig();

plugins/node/opentelemetry-instrumentation-mysql/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
import type {
2026
ConnectionConfig,
2127
PoolActualConfig,
@@ -25,37 +31,29 @@ import type {
2531
import type * as mysqlTypes from 'mysql';
2632

2733
/**
28-
* Get an SpanAttributes map from a mysql connection config object
34+
* Get an Attributes map from a mysql connection config object
2935
*
3036
* @param config ConnectionConfig
3137
*/
3238
export function getConnectionAttributes(
3339
config: ConnectionConfig | PoolActualConfig
34-
): SpanAttributes {
40+
): Attributes {
3541
const { host, port, database, user } = getConfig(config);
3642
const portNumber = parseInt(port, 10);
3743
if (!isNaN(portNumber)) {
3844
return {
39-
[SemanticAttributes.NET_PEER_NAME]: host,
40-
[SemanticAttributes.NET_PEER_PORT]: portNumber,
41-
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
42-
host,
43-
port,
44-
database
45-
),
46-
[SemanticAttributes.DB_NAME]: database,
47-
[SemanticAttributes.DB_USER]: user,
45+
[SEMATTRS_NET_PEER_NAME]: host,
46+
[SEMATTRS_NET_PEER_PORT]: portNumber,
47+
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
48+
[SEMATTRS_DB_NAME]: database,
49+
[SEMATTRS_DB_USER]: user,
4850
};
4951
}
5052
return {
51-
[SemanticAttributes.NET_PEER_NAME]: host,
52-
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
53-
host,
54-
port,
55-
database
56-
),
57-
[SemanticAttributes.DB_NAME]: database,
58-
[SemanticAttributes.DB_USER]: user,
53+
[SEMATTRS_NET_PEER_NAME]: host,
54+
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
55+
[SEMATTRS_DB_NAME]: database,
56+
[SEMATTRS_DB_USER]: user,
5957
};
6058
}
6159

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
import { context, Context, trace, SpanStatusCode } from '@opentelemetry/api';
1818
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
1919
import {
20-
DbSystemValues,
21-
SemanticAttributes,
20+
DBSYSTEMVALUES_MYSQL,
21+
SEMATTRS_DB_NAME,
22+
SEMATTRS_DB_STATEMENT,
23+
SEMATTRS_DB_SYSTEM,
24+
SEMATTRS_DB_USER,
25+
SEMATTRS_NET_PEER_NAME,
26+
SEMATTRS_NET_PEER_PORT,
2227
} from '@opentelemetry/semantic-conventions';
2328
import * as testUtils from '@opentelemetry/contrib-test-utils';
2429
import {
@@ -868,15 +873,12 @@ function assertSpan(
868873
values?: any,
869874
errorMessage?: string
870875
) {
871-
assert.strictEqual(
872-
span.attributes[SemanticAttributes.DB_SYSTEM],
873-
DbSystemValues.MYSQL
874-
);
875-
assert.strictEqual(span.attributes[SemanticAttributes.DB_NAME], database);
876-
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_PORT], port);
877-
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_NAME], host);
878-
assert.strictEqual(span.attributes[SemanticAttributes.DB_USER], user);
879-
assert.strictEqual(span.attributes[SemanticAttributes.DB_STATEMENT], sql);
876+
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], DBSYSTEMVALUES_MYSQL);
877+
assert.strictEqual(span.attributes[SEMATTRS_DB_NAME], database);
878+
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_PORT], port);
879+
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_NAME], host);
880+
assert.strictEqual(span.attributes[SEMATTRS_DB_USER], user);
881+
assert.strictEqual(span.attributes[SEMATTRS_DB_STATEMENT], sql);
880882
if (errorMessage) {
881883
assert.strictEqual(span.status.message, errorMessage);
882884
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);

0 commit comments

Comments
 (0)