Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DDTrace\Integrations\Memcache;

use DDTrace\Integrations\DatabaseIntegrationHelper;
use DDTrace\Integrations\Integration;
use DDTrace\Obfuscation;
use DDTrace\SpanData;
Expand Down Expand Up @@ -83,6 +84,9 @@ public function init()
$span->meta['memcache.cas_token'] = $args[4];
}
$span->meta['memcache.query'] = 'cas ?';
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->setServerTags($span, $this);
};

Expand All @@ -105,7 +109,9 @@ public function traceCommand($command)
$integration->setServerTags($span, $this);
$span->meta['memcache.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
}

if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->markForTraceAnalytics($span, $command);
};
\DDTrace\trace_method('Memcache', $command, $trace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DDTrace\Integrations\Memcached;

use DDTrace\Integrations\DatabaseIntegrationHelper;
use DDTrace\Integrations\Integration;
use DDTrace\Obfuscation;
use DDTrace\SpanData;
Expand Down Expand Up @@ -94,12 +95,19 @@ public function init()

\DDTrace\trace_method('Memcached', 'flush', function (SpanData $span) use ($integration) {
$integration->setCommonData($span, 'flush');
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->setServerTags($span, $this);
});

\DDTrace\trace_method('Memcached', 'cas', function (SpanData $span, $args) use ($integration) {
$integration->setCommonData($span, 'cas');
$span->meta['memcached.cas_token'] = $args[0];
$span->meta['memcached.query'] = 'cas ?';
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->setServerTags($span, $this);
});

Expand All @@ -108,6 +116,7 @@ public function init()
$span->meta['memcached.cas_token'] = $args[0];
$span->meta['memcached.query'] = 'casByKey ?';
$span->meta['memcached.server_key'] = $args[1];
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;

$integration->setServerTags($span, $this);
});
Expand All @@ -130,6 +139,9 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
$integration->setServerTags($span, $this);
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
}
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}

$integration->markForTraceAnalytics($span, $command);
}
Expand All @@ -152,6 +164,9 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0]);
$span->meta['memcached.server_key'] = $args[0];
}
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}

$integration->markForTraceAnalytics($span, $command);
}
Expand All @@ -169,10 +184,11 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
if ($command === 'getMulti') {
$span->metrics[Tag::DB_ROW_COUNT] = isset($retval) ? (is_array($retval) ? count($retval) : 1) : 0;
}
if (!is_array($args[0])) {
$integration->setServerTags($span, $this);
}
$integration->setServerTags($span, $this);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if check was a carryover from old code here 9924c1b, when it was actually required.

$span->meta['memcached.query'] = $command . ' ' . Obfuscation::toObfuscatedString($args[0], ',');
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->markForTraceAnalytics($span, $command);
}
);
Expand All @@ -193,6 +209,9 @@ function (SpanData $span, $args, $retval) use ($integration, $command) {
$integration->setServerTags($span, $this);
$query = "$command " . Obfuscation::toObfuscatedString($args[1], ',');
$span->meta['memcached.query'] = $query;
if (\PHP_MAJOR_VERSION > 5) {
$span->peerServiceSources = DatabaseIntegrationHelper::PEER_SERVICE_SOURCES;
}
$integration->markForTraceAnalytics($span, $command);
}
);
Expand Down
58 changes: 56 additions & 2 deletions tests/Integrations/Memcache/MemcacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ protected function ddSetUp()
});
}

protected function envsToCleanUpAtTearDown()
{
return [
'DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED',
];
}

public function testAdd()
{
$traces = $this->isolateTracer(function () {
Expand Down Expand Up @@ -254,13 +261,60 @@ public function testCas()
]);
}

private static function baseTags()
public function testCommandPeerServiceEnabled()
{
return [
$this->putEnvAndReloadConfig(['DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED=true']);

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

public function testCasPeerServiceEnabled()
{
$this->putEnvAndReloadConfig(['DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED=true']);

$this->client->set('ip_block', 'some_value');
$flags = 0;
$result = $this->client->get('ip_block', $flags, $cas);
$traces = $this->isolateTracer(function () use ($cas) {
$this->client->cas('ip_block', 'value', 0, 0, $cas);
});
$this->assertSpans($traces, [
SpanAssertion::build('Memcache.cas', 'memcache', 'memcached', 'cas')
->setTraceAnalyticsCandidate()
->withExactTags(array_merge(self::baseTags(true), [
'memcache.query' => 'cas ' . Obfuscation::toObfuscatedString('key'),
'memcache.command' => 'cas',
Tag::SPAN_KIND => 'client',
]))
->withExistingTagsNames(['memcache.cas_token']),
]);
}

private static function baseTags($expectPeerService = false)
{
$tags = [
'out.host' => self::$host,
'out.port' => self::$port,
Tag::COMPONENT => 'memcache',
Tag::DB_SYSTEM => 'memcached',
];

if ($expectPeerService) {
$tags['peer.service'] = 'memcached_integration';
$tags['_dd.peer.service.source'] = 'out.host';
}

return $tags;
}
}
Loading