Skip to content

Commit 231d5bd

Browse files
Add config to disable obfuscation in memcached queries (#2725)
Add config to disable obfuscation in memcached queries
1 parent 59d1238 commit 231d5bd

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

ext/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ enum ddtrace_sampling_rules_format {
184184
CONFIG(STRING, DD_VERSION, "", .ini_change = ddtrace_alter_dd_version, \
185185
.env_config_fallback = ddtrace_conf_otel_resource_attributes_version) \
186186
CONFIG(STRING, DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP, DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP_DEFAULT) \
187+
CONFIG(BOOL, DD_TRACE_MEMCACHED_OBFUSCATION, "true") \
187188
CONFIG(BOOL, DD_TRACE_CLIENT_IP_ENABLED, "false") \
188189
CONFIG(CUSTOM(STRING), DD_TRACE_CLIENT_IP_HEADER, "", .parser = ddtrace_parse_client_ip_header_config) \
189190
CONFIG(BOOL, DD_TRACE_FORKED_PROCESS, "true") \

src/DDTrace/Integrations/Memcache/MemcacheIntegration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public function traceCommand($command)
9797
}
9898
if (!is_array($args[0])) {
9999
$integration->setServerTags($span, $this);
100-
$span->meta['memcache.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
100+
$queryParams = dd_trace_env_config("DD_TRACE_MEMCACHED_OBFUSCATION") ?
101+
Obfuscation::toObfuscatedString($args[0]) : $args[0];
102+
$span->meta['memcache.query'] = $command . ' ' . $queryParams;
101103
}
102104
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
103105
$integration->markForTraceAnalytics($span, $command);

src/DDTrace/Integrations/Memcached/MemcachedIntegration.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
125125
}
126126
if (!is_array($args[0])) {
127127
$integration->setServerTags($span, $this);
128-
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
128+
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0]);
129129
}
130130
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
131131

@@ -147,7 +147,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
147147
}
148148
if (!is_array($args[0])) {
149149
$integration->setServerTags($span, $this);
150-
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
150+
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0]);
151151
$span->meta['memcached.server_key'] = $args[0];
152152
}
153153
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
@@ -169,7 +169,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
169169
$span->metrics[Tag::DB_ROW_COUNT] = isset($retval) ? (is_array($retval) ? count($retval) : 1) : 0;
170170
}
171171
$integration->setServerTags($span, $this);
172-
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0], ',');
172+
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0], ',');
173173
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
174174
$integration->markForTraceAnalytics($span, $command);
175175
}
@@ -189,7 +189,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
189189
}
190190
$span->meta['memcached.server_key'] = $args[0];
191191
$integration->setServerTags($span, $this);
192-
$query = "$command " . Obfuscation::toObfuscatedString($args[1], ',');
192+
$query = "$command " . $integration->obfuscateIfNeeded($args[1], ',');
193193
$span->meta['memcached.query'] = $query;
194194
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
195195
$integration->markForTraceAnalytics($span, $command);
@@ -263,4 +263,18 @@ public function markForTraceAnalytics(SpanData $span, $command)
263263
$this->addTraceAnalyticsIfEnabled($span);
264264
}
265265
}
266+
267+
/*
268+
* Return either the obfuscated params or the params themselves, depending on the env var.
269+
*/
270+
public function obfuscateIfNeeded($params, $glue = ' ')
271+
{
272+
if (dd_trace_env_config("DD_TRACE_MEMCACHED_OBFUSCATION")) {
273+
return Obfuscation::toObfuscatedString($params, $glue);
274+
} elseif (is_array($params)) {
275+
return implode($glue, $params);
276+
} else {
277+
return $params;
278+
}
279+
}
266280
}

tests/Integrations/Memcache/MemcacheTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function envsToCleanUpAtTearDown()
3737
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
3838
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
3939
'DD_SERVICE',
40+
'DD_TRACE_MEMCACHED_OBFUSCATION',
4041
];
4142
}
4243

@@ -55,6 +56,22 @@ public function testAdd()
5556
]);
5657
}
5758

59+
public function testAddNoObfuscation()
60+
{
61+
$this->putEnvAndReloadConfig(['DD_TRACE_MEMCACHED_OBFUSCATION=false']);
62+
$traces = $this->isolateTracer(function () {
63+
$this->client->add('key', 'value');
64+
});
65+
$this->assertSpans($traces, [
66+
SpanAssertion::build('Memcache.add', 'memcache', 'memcached', 'add')
67+
->withExactTags(array_merge(self::baseTags(), [
68+
'memcache.query' => 'add ' . 'key',
69+
'memcache.command' => 'add',
70+
Tag::SPAN_KIND => 'client',
71+
]))
72+
]);
73+
}
74+
5875
public function testAppend()
5976
{
6077
$traces = $this->isolateTracer(function () {

tests/Integrations/Memcached/MemcachedTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function envsToCleanUpAtTearDown()
3636
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
3737
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
3838
'DD_SERVICE',
39+
'DD_TRACE_MEMCACHED_OBFUSCATION',
3940
];
4041
}
4142

@@ -530,6 +531,30 @@ public function testGetMulti()
530531
]);
531532
}
532533

534+
public function testGetMultiNoObfuscation()
535+
{
536+
$this->putEnvAndReloadConfig(['DD_TRACE_MEMCACHED_OBFUSCATION=false']);
537+
$traces = $this->isolateTracer(function () {
538+
$this->client->add('key1', 'value1');
539+
$this->client->add('key2', 'value2');
540+
541+
$this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], $this->client->getMulti(['key1', 'key2']));
542+
});
543+
$this->assertSpans($traces, [
544+
SpanAssertion::exists('Memcached.add'),
545+
SpanAssertion::exists('Memcached.add'),
546+
SpanAssertion::build('Memcached.getMulti', 'memcached', 'memcached', 'getMulti')
547+
->withExactTags(array_merge($this->baseTags(), [
548+
'memcached.query' => 'getMulti key1,key2',
549+
'memcached.command' => 'getMulti',
550+
]))->withExactMetrics([
551+
Tag::DB_ROW_COUNT => 2,
552+
'_dd.agent_psr' => 1.0,
553+
'_sampling_priority_v1' => 1.0,
554+
]),
555+
]);
556+
}
557+
533558
public function testGetMultiNotAllExist()
534559
{
535560
$traces = $this->isolateTracer(function () {

0 commit comments

Comments
 (0)