Skip to content

Commit c79b253

Browse files
authored
Fix dddbs service mapping (#2413)
* fix: dddbs service mapping * Refactor service mapping to a more general location * tests: Fix DBM Tests * test: Add `DD_DBM_PROPAGATION_MODE=[none|service]` scenario
1 parent f8f6d29 commit c79b253

File tree

2 files changed

+129
-3
lines changed

2 files changed

+129
-3
lines changed

src/Integrations/Integrations/Integration.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,19 @@ public static function handleInternalSpanServiceName(SpanData $span, $fallbackNa
167167
if ($flatServiceNames) {
168168
$rootSpan = \DDTrace\root_span();
169169
if ($rootSpan) {
170-
$span->service = $rootSpan->service;
170+
$service = $rootSpan->service;
171171
} else {
172-
$span->service = \ddtrace_config_app_name($fallbackName);
172+
$service = \ddtrace_config_app_name($fallbackName);
173173
}
174174
} else {
175-
$span->service = $fallbackName;
175+
$service = $fallbackName;
176176
}
177+
178+
$mapping = \dd_trace_env_config('DD_SERVICE_MAPPING');
179+
if (isset($mapping[$service])) {
180+
$service = $mapping[$service];
181+
}
182+
$span->service = $service;
177183
}
178184
}
179185

tests/Integration/DatabaseMonitoringTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DDTrace\HookData;
66
use DDTrace\Integrations\DatabaseIntegrationHelper;
7+
use DDTrace\Integrations\Integration;
78
use DDTrace\Tests\Common\IntegrationTestCase;
89
use DDTrace\Tests\Common\SpanAssertion;
910

@@ -57,6 +58,125 @@ public function testInjection()
5758
]);
5859
}
5960

61+
public function testInjectionServiceMappingOnce()
62+
{
63+
try {
64+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
65+
$span = $hook->span();
66+
Integration::handleInternalSpanServiceName($span, "pdo");
67+
$span->name = "instrumented";
68+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
69+
});
70+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
71+
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
72+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
73+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
74+
\DDTrace\start_trace_span();
75+
$commentedQuery = $this->instrumented(0, "SELECT 1");
76+
\DDTrace\close_span();
77+
});
78+
} finally {
79+
\DDTrace\remove_hook($hook);
80+
}
81+
82+
$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'phpunit\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-a3978fb9b92502a8-01\'\*\/ SELECT 1$/', $commentedQuery);
83+
$this->assertFlameGraph($traces, [
84+
SpanAssertion::exists("phpunit")->withChildren([
85+
SpanAssertion::exists('instrumented')->withExactTags([
86+
"_dd.dbm_trace_injected" => "true",
87+
"_dd.base_service" => "mapped-service",
88+
])
89+
])
90+
]);
91+
}
92+
93+
public function testInjectionServiceMappingTwice()
94+
{
95+
try {
96+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
97+
$span = $hook->span();
98+
Integration::handleInternalSpanServiceName($span, "pdo");
99+
$span->name = "instrumented";
100+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
101+
});
102+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
103+
self::putEnv("DD_DBM_PROPAGATION_MODE=full");
104+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
105+
// Note that here, we don't start a new trace, hence the service mapping should apply to both dddbs & ddps
106+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
107+
$commentedQuery = $this->instrumented(0, "SELECT 1");
108+
});
109+
} finally {
110+
\DDTrace\remove_hook($hook);
111+
}
112+
113+
$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'mapped-service\',traceparent=\'00-[0-9a-f]{16}c151df7d6ee5e2d6-c151df7d6ee5e2d6-01\'\*\/ SELECT 1$/', $commentedQuery);
114+
$this->assertFlameGraph($traces, [
115+
SpanAssertion::exists('instrumented')->withExactTags([
116+
"_dd.dbm_trace_injected" => "true",
117+
"_dd.base_service" => "mapped-service",
118+
])
119+
]);
120+
}
121+
122+
public function testInjectionServiceMappingService()
123+
{
124+
try {
125+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
126+
$span = $hook->span();
127+
Integration::handleInternalSpanServiceName($span, "pdo");
128+
$span->name = "instrumented";
129+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
130+
});
131+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
132+
self::putEnv("DD_DBM_PROPAGATION_MODE=service");
133+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
134+
// Note that here, we don't start a new trace, hence the service mapping should apply to both dddbs & ddps
135+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
136+
$commentedQuery = $this->instrumented(0, "SELECT 1");
137+
});
138+
} finally {
139+
\DDTrace\remove_hook($hook);
140+
}
141+
142+
$this->assertRegularExpression('/^\/\*dddbs=\'mapped-service\',ddps=\'mapped-service\'\*\/ SELECT 1$/', $commentedQuery);
143+
$this->assertFlameGraph($traces, [
144+
SpanAssertion::exists('instrumented')->withExactTags([
145+
"_dd.dbm_trace_injected" => "true",
146+
"_dd.base_service" => "mapped-service",
147+
])
148+
]);
149+
}
150+
151+
public function testInjectionServiceMappingNone()
152+
{
153+
try {
154+
$hook = \DDTrace\install_hook(self::class . "::instrumented", function (HookData $hook) {
155+
$span = $hook->span();
156+
Integration::handleInternalSpanServiceName($span, "pdo");
157+
$span->name = "instrumented";
158+
DatabaseIntegrationHelper::injectDatabaseIntegrationData($hook, 'mysql', 1);
159+
});
160+
self::putEnv("DD_TRACE_DEBUG_PRNG_SEED=42");
161+
self::putEnv("DD_DBM_PROPAGATION_MODE=none");
162+
self::putEnv("DD_SERVICE_MAPPING=pdo:mapped-service");
163+
// Note that here, we don't start a new trace, hence the service mapping should apply to both dddbs & ddps
164+
$traces = $this->isolateTracer(function () use (&$commentedQuery) {
165+
$commentedQuery = $this->instrumented(0, "SELECT 1");
166+
});
167+
} finally {
168+
\DDTrace\remove_hook($hook);
169+
}
170+
171+
$this->assertSame('SELECT 1', $commentedQuery);
172+
$this->assertFlameGraph($traces, [
173+
SpanAssertion::exists('instrumented')->withExactTags([
174+
"_dd.dbm_trace_injected" => "true",
175+
"_dd.base_service" => "mapped-service",
176+
])
177+
]);
178+
}
179+
60180
public function testInjectionPeerService()
61181
{
62182
try {

0 commit comments

Comments
 (0)