Skip to content

Commit

Permalink
Have datadog.trace.db_client_split_by_instance also affect mysqli and…
Browse files Browse the repository at this point in the history
… sqlsrv (#2508)

Signed-off-by: Bob Weinand <[email protected]>
  • Loading branch information
bwoebi authored Feb 7, 2024
1 parent 1621bee commit d2d9d2f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Integrations/Integrations/Mysqli/MysqliIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ public function setConnectionInfo(SpanData $span, $mysqli)
foreach ($hostInfo as $tagName => $value) {
$span->meta[$tagName] = $value;
}
if (\DDTrace\Util\Runtime::getBoolIni("datadog.trace.db_client_split_by_instance")) {
if (isset($hostInfo[Tag::TARGET_HOST])) {
$span->service .=
'-' . \DDTrace\Util\Normalizer::normalizeHostUdsAsService($hostInfo[Tag::TARGET_HOST]);
}
}
$dbName = ObjectKVStore::get($mysqli, MysqliIntegration::KEY_DATABASE_NAME);
if ($dbName) {
$span->meta[Tag::DB_NAME] = $dbName;
Expand Down
7 changes: 7 additions & 0 deletions src/Integrations/Integrations/SQLSRV/SQLSRVIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ public static function setDefaultAttributes($source, SpanData $span, $name, $que
foreach ($storedConnectionInfo as $tag => $value) {
$span->meta[$tag] = $value;
}

$targetName = $storedConnectionInfo[Tag::DB_INSTANCE] ?? $storedConnectionInfo[Tag::TARGET_HOST] ?? "<default>";
if (\DDTrace\Util\Runtime::getBoolIni("datadog.trace.db_client_split_by_instance")) {
if ($targetName !== "<default>") {
$span->service .= '-' . \DDTrace\Util\Normalizer::normalizeHostUdsAsService($targetName);
}
}
}

public static function detectError($SQLSRVRetval, SpanData $span)
Expand Down
14 changes: 14 additions & 0 deletions tests/Integrations/Mysqli/MysqliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function envsToCleanUpAtTearDown()
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
'DD_SERVICE',
'DD_SERVICE_MAPPING',
];
}

Expand Down Expand Up @@ -606,6 +607,19 @@ public function testNoFakeServices()
], true, false);
}

public function testServiceMappedSplitByDomain()
{
self::putEnv('DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE=true');
self::putEnv('DD_SERVICE_MAPPING=mysqli:my-mysqli');
$traces = $this->isolateTracer(function () {
new \mysqli(self::$host, self::$user, self::$password, self::$db);
});

$this->assertSpans($traces, [
SpanAssertion::build('mysqli.__construct', 'my-mysqli-mysql_integration', 'sql', 'mysqli.__construct', SpanAssertion::NOT_TESTED)
]);
}

private function baseTags($expectDbName = true, $expectPeerService = false)
{
$tags = [
Expand Down
15 changes: 15 additions & 0 deletions tests/Integrations/PDO/PDOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function envsToCleanUpAtTearDown()
'DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE',
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
'DD_SERVICE_MAPPING',
'DD_SERVICE',
];
}
Expand Down Expand Up @@ -166,6 +167,20 @@ public function testPDOSplitByDomain()
]);
}

public function testPDOServiceMappedSplitByDomain()
{
self::putEnv('DD_TRACE_DB_CLIENT_SPLIT_BY_INSTANCE=true');
self::putEnv('DD_SERVICE_MAPPING=pdo:my-pdo');
$traces = $this->isolateTracer(function () {
$this->pdoInstance();
});

$this->assertSpans($traces, [
SpanAssertion::build('PDO.__construct', 'my-pdo-mysql_integration', 'sql', 'PDO.__construct')
->withExactTags($this->baseTags()),
]);
}

public function testPDOConstructError()
{
$traces = $this->isolateTracer(function () {
Expand Down

0 comments on commit d2d9d2f

Please sign in to comment.