From d29f72560154487ef9166c52d564eb7c3bb7c63b Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Fri, 7 Sep 2018 11:05:36 -0700 Subject: [PATCH 1/2] Read and use request time --- README.md | 1 + Rewriter.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a4dbc3f..969c461 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ metadata: | Metric Name | Metric Type | Description | Tags | | --- | --- | --- | --- | | PREFIX.request.count | counter | Request Count | code, method, server_name | +| PREFIX.request.duration | histogram | Request Duration (total processing time) | code, method, server_name | **Warning:** These will be counted towards Datadog custom metrics, and the tags could result in many custom metrics even for a single Ingress. This could result in increased billing. diff --git a/Rewriter.php b/Rewriter.php index 14d7f6e..efb799d 100644 --- a/Rewriter.php +++ b/Rewriter.php @@ -23,11 +23,20 @@ public function rewrite(string $influxDbMessage) list($_, $serverName) = explode('=', $serverNameTagStr, 2); $tags = $this->parseTags($tagStr); - $this->datadog->increment($measurement.'.request.count', 1.0, [ + $ddTags = [ 'code' => $tags['status'], 'method' => $tags['method'], 'server_name' => $serverName, - ]); + ]; + // Simple request counter + $this->datadog->increment($measurement.'.request.count', 1.0, $ddTags); + // Total request duration (value is received in seconds) + $this->datadog->microtiming( + $measurement.'.request.duration', + (float)$tags['request_time'], + 1.0, + $ddTags + ); } private function parseTags(string $tagStr): array From 7d8d7630ad19694de42389d27caf67bf875f5a43 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Fri, 7 Sep 2018 12:01:31 -0700 Subject: [PATCH 2/2] Add timing assertions --- tests/RewriterTest.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/RewriterTest.php b/tests/RewriterTest.php index 105e07b..68456dc 100644 --- a/tests/RewriterTest.php +++ b/tests/RewriterTest.php @@ -22,7 +22,7 @@ class RewriterTest extends \PHPUnit\Framework\TestCase * @covers ::rewrite * @dataProvider influxDbMessages */ - public function testRewrite(string $message, string $prefix, array $tags) + public function testRewrite(string $message, string $prefix, float $time, array $tags) { $dd = $this->createMock(DogStatsd::class); $dd->method('microtiming') @@ -38,6 +38,7 @@ public function testRewrite(string $message, string $prefix, array $tags) $rewriter->rewrite($message); $this->expectIncrement('request.count'); + $this->expectTimer('request.duration', $time); $this->performAssertions($prefix, $tags); } @@ -51,6 +52,7 @@ public function influxDbMessages(): array 'content_type="text/plain; charset=utf-8",request_time=0.047', 'default', + 0.047, ['code' => '500', 'method' => 'GET', 'server_name' => 'amp.reefpig.com', @@ -64,6 +66,11 @@ private function expectIncrement(string $metric) $this->expectedIncrements[] = $metric; } + private function expectTimer(string $metric, float $seconds) + { + $this->expectedTimers[] = [$metric, $seconds]; + } + private function performAssertions(string $prefix, array $tags) { $this->assertCount(count($this->expectedIncrements), $this->increments, 'Incorrect increment count'); @@ -78,12 +85,27 @@ private function performAssertions(string $prefix, array $tags) break; } } - if (!$found) { - $this->fail(sprintf('Expected increment metric %s not found', $metric)); - } + $this->assertNotFalse($found, sprintf('Expected increment metric %s not found', $metric)); ksort($tags); ksort($found[2]); $this->assertEquals($tags, $found[2], 'Tags do not match'); } + + foreach ($this->expectedTimers as $et) { + list($suffix, $seconds) = $et; + $metric = sprintf('%s.%s', $prefix, $suffix); + $found = false; + foreach ($this->timers as $timer) { + if ($timer[0] === $metric) { + $found = $timer; + break; + } + } + $this->assertNotFalse($found, sprintf('Timer metric %s not found', $metric)); + $this->assertEquals($seconds, $found[1], 'Incorrect timing value'); + ksort($tags); + ksort($found[3]); + $this->assertEquals($tags, $found[3], 'Tags do not match'); + } } }