Skip to content

Commit d2211df

Browse files
committed
fix: dddbs service mapping
1 parent b58702a commit d2211df

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Integrations/Integrations/DatabaseIntegrationHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public static function propagateViaSqlComments($query, $databaseService, $mode =
6363
$tags = [];
6464

6565
if ($databaseService != "") {
66+
$mapping = dd_trace_env_config('DD_SERVICE_MAPPING');
67+
if (isset($mapping[$databaseService])) {
68+
$databaseService = $mapping[$databaseService];
69+
}
6670
$tags["dddbs"] = $databaseService;
6771
}
6872

tests/Integration/DatabaseMonitoringTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,67 @@ public function testInjection()
5757
]);
5858
}
5959

60+
public function testInjectionServiceMappingOnce()
61+
{
62+
try {
63+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
64+
$span = $hook->span();
65+
$span->service = "pdo";
66+
$span->name = "instrumented";
67+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
68+
});
69+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
70+
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
71+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
72+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
73+
\DDTrace\start_trace_span();
74+
$commentedQuery = $this->instrumented(0, "SELECT 1");
75+
\DDTrace\close_span();
76+
});
77+
} finally {
78+
\DDTrace\remove_hook($hook);
79+
}
80+
81+
$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'phpunit\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-a3978fb9b92502a8-01\'\*\/ SELECT 1$/', $commentedQuery);
82+
$this->assertFlameGraph($traces, [
83+
SpanAssertion::exists("phpunit")->withChildren([
84+
SpanAssertion::exists('instrumented')->withExactTags([
85+
"_dd.dbm_trace_injected" => "true",
86+
"_dd.base_service" => "mapped-service",
87+
])
88+
])
89+
]);
90+
}
91+
92+
public function testInjectionServiceMappingTwice()
93+
{
94+
try {
95+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
96+
$span = $hook->span();
97+
$span->service = "pdo";
98+
$span->name = "instrumented";
99+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
100+
});
101+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
102+
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
103+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
104+
// Note that here, we don't start a new trace, hence the service mapping should apply to both dddbs & ddps
105+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
106+
$commentedQuery = $this->instrumented(0, "SELECT 1");
107+
});
108+
} finally {
109+
\DDTrace\remove_hook($hook);
110+
}
111+
112+
$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'mapped-service\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-c151df7d6ee5e2d6-01\'\*\/ SELECT 1$/', $commentedQuery);
113+
$this->assertFlameGraph($traces, [
114+
SpanAssertion::exists('instrumented')->withExactTags([
115+
"_dd.dbm_trace_injected" => "true",
116+
"_dd.base_service" => "mapped-service",
117+
])
118+
]);
119+
}
120+
60121
public function testInjectionPeerService()
61122
{
62123
try {

0 commit comments

Comments
 (0)