Skip to content

Commit

Permalink
fix: dddbs service mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
PROFeNoM committed Dec 7, 2023
1 parent 0e5f4d5 commit 810d6de
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Integrations/Integrations/DatabaseIntegrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public static function propagateViaSqlComments($query, $databaseService, $mode =
$tags = [];

if ($databaseService != "") {
$mapping = dd_trace_env_config('DD_SERVICE_MAPPING');
if (isset($mapping[$databaseService])) {
$databaseService = $mapping[$databaseService];
}
$tags["dddbs"] = $databaseService;
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Integration/DatabaseMonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,67 @@ public function testInjection()
]);
}

public function testInjectionServiceMappingOnce()
{
try {
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
$span = $hook->span();
$span->service = "pdo";
$span->name = "instrumented";
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
});
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
\DDTrace\start_trace_span();
$commentedQuery = $this->instrumented(0, "SELECT 1");
\DDTrace\close_span();
});
} finally {
\DDTrace\remove_hook($hook);
}

$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'phpunit\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-a3978fb9b92502a8-01\'\*\/ SELECT 1$/', $commentedQuery);
$this->assertFlameGraph($traces, [
SpanAssertion::exists("phpunit")->withChildren([
SpanAssertion::exists('instrumented')->withExactTags([
"_dd.dbm_trace_injected" => "true",
"_dd.base_service" => "mapped-service",
])
])
]);
}

public function testInjectionServiceMappingTwice()
{
try {
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
$span = $hook->span();
$span->service = "pdo";
$span->name = "instrumented";
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
});
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
// Note that here, we don't start a new trace, hence the service mapping should apply to both dddbs & ddps
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
$commentedQuery = $this->instrumented(0, "SELECT 1");
});
} finally {
\DDTrace\remove_hook($hook);
}

$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'mapped-service\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-c151df7d6ee5e2d6-01\'\*\/ SELECT 1$/', $commentedQuery);
$this->assertFlameGraph($traces, [
SpanAssertion::exists('instrumented')->withExactTags([
"_dd.dbm_trace_injected" => "true",
"_dd.base_service" => "mapped-service",
])
]);
}

public function testInjectionPeerService()
{
try {
Expand Down

0 comments on commit 810d6de

Please sign in to comment.