Skip to content

Commit

Permalink
Add Client->timings() to send multiple values at once (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
xosofox authored and marcqualie committed Jun 12, 2018
1 parent 19a39b4 commit 5b12000
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ $statsd->set('api.unique_logins', $userID);
$statsd->timing('api.response_time', 256);
```

```php
$metrics = array('api.response_time' => 256, 'api.memory' => 4096));
$statsd->timings($metrics);
```


## Timing Blocks

Expand Down
16 changes: 16 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ public function timing($metric, $time)
);
}

/**
* Send multiple timing metrics at once
* @param array $metrics key value map of metric name -> timing value
* @return Client
* @throws ConnectionException
*/
public function timings($metrics)
{
// add |ms to values
$data = [];
foreach ($metrics as $metric => $timing) {
$data[$metric] = $timing.'|ms';
}

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

/**
* Time a function
Expand Down
21 changes: 21 additions & 0 deletions tests/TimingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace League\StatsD\Test;

class TimingsTest extends TestCase
{

public function testTiming()
{
$timings = array(
'test_metric1' => 123,
'test_metric2' => 234,
'test_metric3' => .234,
);
$this->client->timings($timings);
$this->assertEquals(
'test_metric1:123|ms'.PHP_EOL.'test_metric2:234|ms'.PHP_EOL.'test_metric3:0.234|ms',
$this->client->getLastMessage()
);
}
}

0 comments on commit 5b12000

Please sign in to comment.