Skip to content

Commit 8f5922d

Browse files
authored
feat(tracing): Add db connection attributes for mysql spans (#8775)
Align mysql spans with https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/mssql.md needed for starfish views.
1 parent fdd17e7 commit 8f5922d

File tree

2 files changed

+42
-0
lines changed
  • packages
    • node-integration-tests/suites/tracing/auto-instrument/mysql
    • tracing-internal/src/node/integrations

2 files changed

+42
-0
lines changed

packages/node-integration-tests/suites/tracing/auto-instrument/mysql/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ test('should auto-instrument `mysql` package.', async () => {
1414
op: 'db',
1515
data: {
1616
'db.system': 'mysql',
17+
'db.user': 'root',
18+
'server.address': expect.any(String),
19+
'server.port': expect.any(Number),
1720
},
1821
},
1922

@@ -22,6 +25,9 @@ test('should auto-instrument `mysql` package.', async () => {
2225
op: 'db',
2326
data: {
2427
'db.system': 'mysql',
28+
'db.user': 'root',
29+
'server.address': expect.any(String),
30+
'server.port': expect.any(Number),
2531
},
2632
},
2733
],

packages/tracing-internal/src/node/integrations/mysql.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ import type { LazyLoadedIntegration } from './lazy';
66
import { shouldDisableAutoInstrumentation } from './utils/node-utils';
77

88
interface MysqlConnection {
9+
prototype: {
10+
connect: () => void;
11+
};
912
createQuery: () => void;
1013
}
1114

15+
interface MysqlConnectionConfig {
16+
host: string;
17+
port: number;
18+
user: string;
19+
}
20+
1221
/** Tracing integration for node-mysql package */
1322
export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
1423
/**
@@ -48,6 +57,32 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
4857
return;
4958
}
5059

60+
let mySqlConfig: MysqlConnectionConfig | undefined = undefined;
61+
62+
try {
63+
pkg.prototype.connect = new Proxy(pkg.prototype.connect, {
64+
apply(wrappingTarget, thisArg: { config: MysqlConnectionConfig }, args) {
65+
if (!mySqlConfig) {
66+
mySqlConfig = thisArg.config;
67+
}
68+
return wrappingTarget.apply(thisArg, args);
69+
},
70+
});
71+
} catch (e) {
72+
__DEBUG_BUILD__ && logger.error('Mysql Integration was unable to instrument `mysql` config.');
73+
}
74+
75+
function spanDataFromConfig(): Record<string, unknown> {
76+
if (!mySqlConfig) {
77+
return {};
78+
}
79+
return {
80+
'server.address': mySqlConfig.host,
81+
'server.port': mySqlConfig.port,
82+
'db.user': mySqlConfig.user,
83+
};
84+
}
85+
5186
// The original function will have one of these signatures:
5287
// function (callback) => void
5388
// function (options, callback) => void
@@ -60,6 +95,7 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
6095
description: typeof options === 'string' ? options : (options as { sql: string }).sql,
6196
op: 'db',
6297
data: {
98+
...spanDataFromConfig(),
6399
'db.system': 'mysql',
64100
},
65101
});

0 commit comments

Comments
 (0)