diff --git a/README.md b/README.md index 60fcfb4..4195711 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,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/src/Rewriter.php b/src/Rewriter.php index 72c05f8..e3a793c 100644 --- a/src/Rewriter.php +++ b/src/Rewriter.php @@ -25,11 +25,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 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'); + } } }