Skip to content

Commit a4daf25

Browse files
authored
Add distribution metrics to datadog client (#36)
* Updated client to support sending Distribution metrics * Added unit test for Distribution metrics * Fixed test method name
1 parent 94fe0c0 commit a4daf25

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/Client.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,27 @@ public function histogram($metric, $value, $sampleRate = 1.0, array $tags = [])
470470
return $this;
471471
}
472472

473+
/**
474+
* Distribution
475+
*
476+
* @param string $metric Metric to send
477+
* @param float $value Value to send
478+
* @param float $sampleRate Sample rate of metric
479+
* @param string[] $tags List of tags for this metric
480+
*
481+
* @return Client This instance
482+
*/
483+
public function distribution($metric, $value, $sampleRate = 1.0, array $tags = [])
484+
{
485+
if ($this->isSampled($sampleRate, $postfix)) {
486+
return $this->send(
487+
[$metric => $value . '|d' . $postfix],
488+
$tags
489+
);
490+
}
491+
return $this;
492+
}
493+
473494
/**
474495
* Sets - count the number of unique elements for a group
475496
*

tests/unit/DistributionTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* This file is part of graze/dog-statsd
4+
*
5+
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md
11+
* @link https://github.com/graze/dog-statsd
12+
*/
13+
14+
namespace Graze\DogStatsD\Test\Unit;
15+
16+
use Graze\DogStatsD\Test\TestCase;
17+
18+
class DistributionTest extends TestCase
19+
{
20+
public function testDistribution()
21+
{
22+
$this->client->distribution('test_metric', 10);
23+
$this->assertEquals('test_metric:10|d', $this->client->getLastMessage());
24+
25+
$this->client->distribution('test_metric', 1.2);
26+
$this->assertEquals('test_metric:1.2|d', $this->client->getLastMessage());
27+
}
28+
29+
public function testDistributionSample()
30+
{
31+
while ($this->client->getLastMessage() === '') {
32+
$this->client->distribution('test_metric', 5, 0.75);
33+
}
34+
$this->assertEquals('test_metric:5|d|@0.75', $this->client->getLastMessage());
35+
}
36+
37+
public function testDistributionSampleFailure()
38+
{
39+
$this->client->distribution('test_metric', 5, 0);
40+
$this->assertEquals('', $this->client->getLastMessage());
41+
}
42+
43+
public function testDistributionTags()
44+
{
45+
$this->client->distribution('test_metric', 10, 1.0, []);
46+
$this->assertEquals('test_metric:10|d', $this->client->getLastMessage());
47+
48+
$this->client->distribution('test_metric', 10, 1.0, ['tag1']);
49+
$this->assertEquals('test_metric:10|d|#tag1', $this->client->getLastMessage());
50+
51+
$this->client->distribution('test_metric', 10, 1.0, ['tag1', 'tag2']);
52+
$this->assertEquals('test_metric:10|d|#tag1,tag2', $this->client->getLastMessage());
53+
54+
$this->client->distribution('test_metric', 10, 1.0, ['tag1', 'tag1']);
55+
$this->assertEquals('test_metric:10|d|#tag1,tag1', $this->client->getLastMessage());
56+
}
57+
}

0 commit comments

Comments
 (0)