Skip to content

Commit d2d9d2f

Browse files
authored
Have datadog.trace.db_client_split_by_instance also affect mysqli and sqlsrv (#2508)
Signed-off-by: Bob Weinand <[email protected]>
1 parent 1621bee commit d2d9d2f

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/Integrations/Integrations/Mysqli/MysqliIntegration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ public function setConnectionInfo(SpanData $span, $mysqli)
424424
foreach ($hostInfo as $tagName => $value) {
425425
$span->meta[$tagName] = $value;
426426
}
427+
if (\DDTrace\Util\Runtime::getBoolIni("datadog.trace.db_client_split_by_instance")) {
428+
if (isset($hostInfo[Tag::TARGET_HOST])) {
429+
$span->service .=
430+
'-' . \DDTrace\Util\Normalizer::normalizeHostUdsAsService($hostInfo[Tag::TARGET_HOST]);
431+
}
432+
}
427433
$dbName = ObjectKVStore::get($mysqli, MysqliIntegration::KEY_DATABASE_NAME);
428434
if ($dbName) {
429435
$span->meta[Tag::DB_NAME] = $dbName;

src/Integrations/Integrations/SQLSRV/SQLSRVIntegration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ public static function setDefaultAttributes($source, SpanData $span, $name, $que
218218
foreach ($storedConnectionInfo as $tag => $value) {
219219
$span->meta[$tag] = $value;
220220
}
221+
222+
$targetName = $storedConnectionInfo[Tag::DB_INSTANCE] ?? $storedConnectionInfo[Tag::TARGET_HOST] ?? "<default>";
223+
if (\DDTrace\Util\Runtime::getBoolIni("datadog.trace.db_client_split_by_instance")) {
224+
if ($targetName !== "<default>") {
225+
$span->service .= '-' . \DDTrace\Util\Normalizer::normalizeHostUdsAsService($targetName);
226+
}
227+
}
221228
}
222229

223230
public static function detectError($SQLSRVRetval, SpanData $span)

tests/Integrations/Mysqli/MysqliTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function envsToCleanUpAtTearDown()
3434
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
3535
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
3636
'DD_SERVICE',
37+
'DD_SERVICE_MAPPING',
3738
];
3839
}
3940

@@ -606,6 +607,19 @@ public function testNoFakeServices()
606607
], true, false);
607608
}
608609

610+
public function testServiceMappedSplitByDomain()
611+
{
612+
self::putEnv('DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE=true');
613+
self::putEnv('DD_SERVICE_MAPPING=mysqli:my-mysqli');
614+
$traces = $this->isolateTracer(function () {
615+
new \mysqli(self::$host, self::$user, self::$password, self::$db);
616+
});
617+
618+
$this->assertSpans($traces, [
619+
SpanAssertion::build('mysqli.__construct', 'my-mysqli-mysql_integration', 'sql', 'mysqli.__construct', SpanAssertion::NOT_TESTED)
620+
]);
621+
}
622+
609623
private function baseTags($expectDbName = true, $expectPeerService = false)
610624
{
611625
$tags = [

tests/Integrations/PDO/PDOTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function envsToCleanUpAtTearDown()
5050
'DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE',
5151
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
5252
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
53+
'DD_SERVICE_MAPPING',
5354
'DD_SERVICE',
5455
];
5556
}
@@ -166,6 +167,20 @@ public function testPDOSplitByDomain()
166167
]);
167168
}
168169

170+
public function testPDOServiceMappedSplitByDomain()
171+
{
172+
self::putEnv('DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE=true');
173+
self::putEnv('DD_SERVICE_MAPPING=pdo:my-pdo');
174+
$traces = $this->isolateTracer(function () {
175+
$this->pdoInstance();
176+
});
177+
178+
$this->assertSpans($traces, [
179+
SpanAssertion::build('PDO.__construct', 'my-pdo-mysql_integration', 'sql', 'PDO.__construct')
180+
->withExactTags($this->baseTags()),
181+
]);
182+
}
183+
169184
public function testPDOConstructError()
170185
{
171186
$traces = $this->isolateTracer(function () {

0 commit comments

Comments
 (0)