Skip to content

Commit

Permalink
Added support for histogram. (#6)
Browse files Browse the repository at this point in the history
* Added support for histogram
  • Loading branch information
Chris Williams authored and Harry Bragg committed Dec 2, 2016
1 parent 74d0bd9 commit ebe2da0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,17 @@ public function getLastMessage()
*
* @param string|string[] $metrics Metric(s) to increment
* @param int $delta Value to decrement the metric by
* @param int $sampleRate Sample rate of metric
* @param float $sampleRate Sample rate of metric
* @param string[] $tags List of tags for this metric
*
* @return Client This instance
*/
public function increment($metrics, $delta = 1, $sampleRate = 1, array $tags = [])
public function increment($metrics, $delta = 1, $sampleRate = 1.0, array $tags = [])
{
$metrics = is_array($metrics) ? $metrics : [$metrics];

$data = [];
if ($sampleRate < 1) {
if ($sampleRate < 1.0) {
foreach ($metrics as $metric) {
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) {
$data[$metric] = $delta . '|c|@' . $sampleRate;
Expand Down Expand Up @@ -356,6 +356,30 @@ public function gauge($metric, $value, array $tags = [])
);
}

/**
* Histogram
*
* @param string $metric Metric to send
* @param float $value Value to send
* @param float $sampleRate Sample rate of metric
* @param string[] $tags List of tags for this metric
*
* @return Client This instance
*/
public function histogram($metric, $value, $sampleRate = 1.0, array $tags = [])
{
$data = [];
if ($sampleRate < 1.0) {
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) {
$data[$metric] = $value . '|h|@' . $sampleRate;
}
} else {
$data[$metric] = $value . '|h';
}

return $this->send($data, $tags);
}

/**
* Sets - count the number of unique elements for a group
*
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/HistogramTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* This file is part of graze/dog-statsd
*
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md
* @link https://github.com/graze/dog-statsd
*/

namespace Graze\DogStatsD\Test\Unit;

use Graze\DogStatsD\Test\TestCase;

class HistogramTest extends TestCase
{
public function testHistogram()
{
$this->client->histogram('test_metric', 10);
$this->assertEquals('test_metric:10|h', $this->client->getLastMessage());

$this->client->histogram('test_metric', 1.2);
$this->assertEquals('test_metric:1.2|h', $this->client->getLastMessage());
}

public function testHistogramSample()
{
while ($this->client->getLastMessage() === '') {
$this->client->histogram('test_metric', 5, 0.75);
}
$this->assertEquals('test_metric:5|h|@0.75', $this->client->getLastMessage());
}

public function testHistogramTags()
{
$this->client->histogram('test_metric', 10, 1.0, []);
$this->assertEquals('test_metric:10|h', $this->client->getLastMessage());

$this->client->histogram('test_metric', 10, 1.0, ['tag1']);
$this->assertEquals('test_metric:10|h|#tag1', $this->client->getLastMessage());

$this->client->histogram('test_metric', 10, 1.0, ['tag1', 'tag2']);
$this->assertEquals('test_metric:10|h|#tag1,tag2', $this->client->getLastMessage());

$this->client->histogram('test_metric', 10, 1.0, ['tag1', 'tag1']);
$this->assertEquals('test_metric:10|h|#tag1,tag1', $this->client->getLastMessage());
}
}

0 comments on commit ebe2da0

Please sign in to comment.