Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 33daf44

Browse files
authored
Read and use request time (#7)
1 parent 4f0b9f8 commit 33daf44

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ metadata:
5252
| Metric Name | Metric Type | Description | Tags |
5353
| --- | --- | --- | --- |
5454
| PREFIX.request.count | counter | Request Count | code, method, server_name |
55+
| PREFIX.request.duration | histogram | Request Duration (total processing time) | code, method, server_name |
5556

5657
**Warning:** These will be counted towards Datadog custom metrics, and the tags could result in many custom metrics even for a single Ingress.
5758
This could result in increased billing.

src/Rewriter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ public function rewrite(string $influxDbMessage)
2525
list($_, $serverName) = explode('=', $serverNameTagStr, 2);
2626
$tags = $this->parseTags($tagStr);
2727

28-
$this->datadog->increment($measurement.'.request.count', 1.0, [
28+
$ddTags = [
2929
'code' => $tags['status'],
3030
'method' => $tags['method'],
3131
'server_name' => $serverName,
32-
]);
32+
];
33+
// Simple request counter
34+
$this->datadog->increment($measurement.'.request.count', 1.0, $ddTags);
35+
// Total request duration (value is received in seconds)
36+
$this->datadog->microtiming(
37+
$measurement.'.request.duration',
38+
(float)$tags['request_time'],
39+
1.0,
40+
$ddTags
41+
);
3342
}
3443

3544
private function parseTags(string $tagStr): array

tests/RewriterTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RewriterTest extends \PHPUnit\Framework\TestCase
2222
* @covers ::rewrite
2323
* @dataProvider influxDbMessages
2424
*/
25-
public function testRewrite(string $message, string $prefix, array $tags)
25+
public function testRewrite(string $message, string $prefix, float $time, array $tags)
2626
{
2727
$dd = $this->createMock(DogStatsd::class);
2828
$dd->method('microtiming')
@@ -38,6 +38,7 @@ public function testRewrite(string $message, string $prefix, array $tags)
3838
$rewriter->rewrite($message);
3939

4040
$this->expectIncrement('request.count');
41+
$this->expectTimer('request.duration', $time);
4142
$this->performAssertions($prefix, $tags);
4243
}
4344

@@ -51,6 +52,7 @@ public function influxDbMessages(): array
5152
'content_type="text/plain; charset=utf-8",request_time=0.047',
5253

5354
'default',
55+
0.047,
5456
['code' => '500',
5557
'method' => 'GET',
5658
'server_name' => 'amp.reefpig.com',
@@ -64,6 +66,11 @@ private function expectIncrement(string $metric)
6466
$this->expectedIncrements[] = $metric;
6567
}
6668

69+
private function expectTimer(string $metric, float $seconds)
70+
{
71+
$this->expectedTimers[] = [$metric, $seconds];
72+
}
73+
6774
private function performAssertions(string $prefix, array $tags)
6875
{
6976
$this->assertCount(count($this->expectedIncrements), $this->increments, 'Incorrect increment count');
@@ -78,12 +85,27 @@ private function performAssertions(string $prefix, array $tags)
7885
break;
7986
}
8087
}
81-
if (!$found) {
82-
$this->fail(sprintf('Expected increment metric %s not found', $metric));
83-
}
88+
$this->assertNotFalse($found, sprintf('Expected increment metric %s not found', $metric));
8489
ksort($tags);
8590
ksort($found[2]);
8691
$this->assertEquals($tags, $found[2], 'Tags do not match');
8792
}
93+
94+
foreach ($this->expectedTimers as $et) {
95+
list($suffix, $seconds) = $et;
96+
$metric = sprintf('%s.%s', $prefix, $suffix);
97+
$found = false;
98+
foreach ($this->timers as $timer) {
99+
if ($timer[0] === $metric) {
100+
$found = $timer;
101+
break;
102+
}
103+
}
104+
$this->assertNotFalse($found, sprintf('Timer metric %s not found', $metric));
105+
$this->assertEquals($seconds, $found[1], 'Incorrect timing value');
106+
ksort($tags);
107+
ksort($found[3]);
108+
$this->assertEquals($tags, $found[3], 'Tags do not match');
109+
}
88110
}
89111
}

0 commit comments

Comments
 (0)