Skip to content

Commit

Permalink
Add config to disable obfuscation in memcached queries (#2725)
Browse files Browse the repository at this point in the history
Add config to disable obfuscation in memcached queries
  • Loading branch information
pablomartinezbernardo authored Jun 21, 2024
1 parent 59d1238 commit 231d5bd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions ext/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ enum ddtrace_sampling_rules_format {
CONFIG(STRING, DD_VERSION, "", .ini_change = ddtrace_alter_dd_version, \
.env_config_fallback = ddtrace_conf_otel_resource_attributes_version) \
CONFIG(STRING, DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP, DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP_DEFAULT) \
CONFIG(BOOL, DD_TRACE_MEMCACHED_OBFUSCATION, "true") \
CONFIG(BOOL, DD_TRACE_CLIENT_IP_ENABLED, "false") \
CONFIG(CUSTOM(STRING), DD_TRACE_CLIENT_IP_HEADER, "", .parser = ddtrace_parse_client_ip_header_config) \
CONFIG(BOOL, DD_TRACE_FORKED_PROCESS, "true") \
Expand Down
4 changes: 3 additions & 1 deletion src/DDTrace/Integrations/Memcache/MemcacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public function traceCommand($command)
}
if (!is_array($args[0])) {
$integration->setServerTags($span, $this);
$span->meta['memcache.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
$queryParams = dd_trace_env_config("DD_TRACE_MEMCACHED_OBFUSCATION") ?
Obfuscation::toObfuscatedString($args[0]) : $args[0];
$span->meta['memcache.query'] = $command . ' ' . $queryParams;
}
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
$integration->markForTraceAnalytics($span, $command);
Expand Down
22 changes: 18 additions & 4 deletions src/DDTrace/Integrations/Memcached/MemcachedIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
}
if (!is_array($args[0])) {
$integration->setServerTags($span, $this);
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0]);
}
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;

Expand All @@ -147,7 +147,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
}
if (!is_array($args[0])) {
$integration->setServerTags($span, $this);
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0]);
$span->meta['memcached.server_key'] = $args[0];
}
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
Expand All @@ -169,7 +169,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
$span->metrics[Tag::DB_ROW_COUNT] = isset($retval) ? (is_array($retval) ? count($retval) : 1) : 0;
}
$integration->setServerTags($span, $this);
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0], ',');
$span->meta['memcached.query'] = $command . ' ' . $integration->obfuscateIfNeeded($args[0], ',');
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
$integration->markForTraceAnalytics($span, $command);
}
Expand All @@ -189,7 +189,7 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
}
$span->meta['memcached.server_key'] = $args[0];
$integration->setServerTags($span, $this);
$query = "$command " . Obfuscation::toObfuscatedString($args[1], ',');
$query = "$command " . $integration->obfuscateIfNeeded($args[1], ',');
$span->meta['memcached.query'] = $query;
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
$integration->markForTraceAnalytics($span, $command);
Expand Down Expand Up @@ -263,4 +263,18 @@ public function markForTraceAnalytics(SpanData $span, $command)
$this->addTraceAnalyticsIfEnabled($span);
}
}

/*
* Return either the obfuscated params or the params themselves, depending on the env var.
*/
public function obfuscateIfNeeded($params, $glue = ' ')
{
if (dd_trace_env_config("DD_TRACE_MEMCACHED_OBFUSCATION")) {
return Obfuscation::toObfuscatedString($params, $glue);
} elseif (is_array($params)) {
return implode($glue, $params);
} else {
return $params;
}
}
}
17 changes: 17 additions & 0 deletions tests/Integrations/Memcache/MemcacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function envsToCleanUpAtTearDown()
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
'DD_SERVICE',
'DD_TRACE_MEMCACHED_OBFUSCATION',
];
}

Expand All @@ -55,6 +56,22 @@ public function testAdd()
]);
}

public function testAddNoObfuscation()
{
$this->putEnvAndReloadConfig(['DD_TRACE_MEMCACHED_OBFUSCATION=false']);
$traces = $this->isolateTracer(function () {
$this->client->add('key', 'value');
});
$this->assertSpans($traces, [
SpanAssertion::build('Memcache.add', 'memcache', 'memcached', 'add')
->withExactTags(array_merge(self::baseTags(), [
'memcache.query' => 'add ' . 'key',
'memcache.command' => 'add',
Tag::SPAN_KIND => 'client',
]))
]);
}

public function testAppend()
{
$traces = $this->isolateTracer(function () {
Expand Down
25 changes: 25 additions & 0 deletions tests/Integrations/Memcached/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function envsToCleanUpAtTearDown()
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
'DD_SERVICE',
'DD_TRACE_MEMCACHED_OBFUSCATION',
];
}

Expand Down Expand Up @@ -530,6 +531,30 @@ public function testGetMulti()
]);
}

public function testGetMultiNoObfuscation()
{
$this->putEnvAndReloadConfig(['DD_TRACE_MEMCACHED_OBFUSCATION=false']);
$traces = $this->isolateTracer(function () {
$this->client->add('key1', 'value1');
$this->client->add('key2', 'value2');

$this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], $this->client->getMulti(['key1', 'key2']));
});
$this->assertSpans($traces, [
SpanAssertion::exists('Memcached.add'),
SpanAssertion::exists('Memcached.add'),
SpanAssertion::build('Memcached.getMulti', 'memcached', 'memcached', 'getMulti')
->withExactTags(array_merge($this->baseTags(), [
'memcached.query' => 'getMulti key1,key2',
'memcached.command' => 'getMulti',
]))->withExactMetrics([
Tag::DB_ROW_COUNT => 2,
'_dd.agent_psr' => 1.0,
'_sampling_priority_v1' => 1.0,
]),
]);
}

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

0 comments on commit 231d5bd

Please sign in to comment.