Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions src/Rewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 26 additions & 4 deletions tests/RewriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}

Expand All @@ -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',
Expand All @@ -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');
Expand All @@ -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');
}
}
}