Skip to content
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
36 changes: 27 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
## statsd-php-client
## statsd-php-client v1.0.6

Be careful, see the [Upgrading section](Readme.md#upgrade) for <= v1.0.4, there's a BC.

[![Build Status](https://secure.travis-ci.org/liuggio/statsd-php-client.png)](http://travis-ci.org/liuggio/statsd-php-client)



`statsd-php-client` is an Open Source, and **Object Oriented** Client for **etsy/statsd** written in php

- `StatsdDataFactory` creates the `Liuggio\StatsdClient\Entity\StatsdDataInterface` Objects

- `Sender` just sends data over the network
- `Sender` just sends data over the network (there are many sender)

- `StatsdClient` sends the created objects via the `Sender` to the server

## Why use this library instead the [statsd/php-example](https://github.com/etsy/statsd/blob/master/examples/php-example.php)?

- You are wise.

- This library is totally tested.
- This library is tested.

- This library optimizes the messages to send, compressing multiple messages in individual UDP packets.

- This library pays attention to the maximum length of the UDP.

- This library is made by Objects not array, but it also accepts array.

- You do want to debug the packets, and using `SysLogSender` the packets will be logged in your `syslog` log (on debian-like distro: `tail -f /var/log/syslog`)


## Example

1. create the Sender

2. create the Client

3. create the Factory

4. the Factory will help you to create data

5. the Client will send the data

```php
$sender = new SocketSender();
use Liuggio\StatsdClient\StatsdClient,
Liuggio\StatsdClient\StatsdClient\Factory\StatsdDataFactory,
Liuggio\StatsdClient\StatsdClient\Sender\SocketSender,
Liuggio\StatsdClient\StatsdClient\Sender\SysLogSender;

$sender = new SocketSender('udp://localhost', 8126);
// $sender = new SysLogSender(); // enable this the packet will not send over the socket

// StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true)
$client = new StatsdClient($sender);
$factory = new StatsdDataFactory('\\Liuggio\\StatsdClient\\Entity\\StatsdData');
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData');

// create the data with the factory
$data[] = $factory->timing('usageTime', 100);
Expand All @@ -55,7 +70,6 @@ In order to try this application monitor you have to install etsy/statsd and Gra

see this blog post to install it with vagrant [Easy install statsd graphite](http://welcometothebundle.com/easily-install-statsd-and-graphite-with-vagrant/).


#### [StatsD](https://github.com/etsy/statsd)

StatsD is a simple daemon for easy stats aggregation
Expand Down Expand Up @@ -93,3 +107,7 @@ composer.phar install
``` bash
phpunit --coverage-html reports
```

## Upgrade

BC from the v1.0.4 version, [see Sender and Client differences](https://github.com/liuggio/statsd-php-client/pull/5/files).
3 changes: 1 addition & 2 deletions src/Liuggio/StatsdClient/Entity/StatsdData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Liuggio\StatsdClient\Entity\StatsdDataInterface;


class StatsdData implements StatsdDataInterface
{

Expand Down Expand Up @@ -56,6 +55,7 @@ public function getMetric()
}

/**
* @param bool $withMetric
* @return string
*/
public function getMessage($withMetric = true)
Expand All @@ -74,5 +74,4 @@ public function __toString()
{
return $this->getMessage();
}

}
4 changes: 2 additions & 2 deletions src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Liuggio\StatsdClient\Entity\StatsdDataInterface;


class StatsdDataFactory implements StatsdDataFactoryInterface
{
/**
Expand Down Expand Up @@ -86,7 +85,8 @@ public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterfac
/**
* {@inheritDoc}
**/
public function produceStatsdDataEntity() {
public function produceStatsdDataEntity()
{
$statsdData = $this->getEntityClass();
return new $statsdData();
}
Expand Down
16 changes: 6 additions & 10 deletions src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use Liuggio\StatsdClient\Entity\StatsdDataInterface;


Interface StatsdDataFactoryInterface
{

/**
* This function creates a 'timing' StatsdData
*
* @abstract
* @param string|array $stats The metric(s) to set.
* @param string|array $key The metric(s) to set.
* @param float $time The elapsed time (ms) to log
**/
function timing($key, $time);
Expand All @@ -21,7 +20,7 @@ function timing($key, $time);
* This function creates a 'gauge' StatsdData
*
* @abstract
* @param string|array $stats The metric(s) to set.
* @param string|array $key The metric(s) to set.
* @param float $value The value for the stats.
**/
function gauge($key, $value);
Expand All @@ -30,7 +29,7 @@ function gauge($key, $value);
* This function creates a 'set' StatsdData object
* A "Set" is a count of unique events.
* This data type acts like a counter, but supports counting
* of unique occurences of values between flushes. The backend
* of unique occurrences of values between flushes. The backend
* receives the number of unique events that happened since
* the last flush.
*
Expand All @@ -39,7 +38,7 @@ function gauge($key, $value);
* with each request with a key of "uniques" (or similar).
*
* @abstract
* @param string|array $stats The metric(s) to set.
* @param string|array $key The metric(s) to set.
* @param float $value The value for the stats.
* @return array
**/
Expand Down Expand Up @@ -70,12 +69,9 @@ function decrement($key);
*
* @abstract
* @param string $key The key of the metric
* @param int|1 $value The amount to increment/decrement each metric by.
* @param string|c $metric The metric type ("c" for count, "ms" for timing, "g" for gauge, "s" for set)
* @param int $value The amount to increment/decrement each metric by.
* @param string $metric The metric type ("c" for count, "ms" for timing, "g" for gauge, "s" for set)
* @return StatsdDataInterface
**/
function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT);



}
9 changes: 6 additions & 3 deletions src/Liuggio/StatsdClient/Sender/EchoSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@
/**
* {@inheritDoc}
*/
public function open($hostname, $port = null, $protocol = null) {
public function open()
{
echo "[open]";
return true;
}

/**
* {@inheritDoc}
*/
function write($handle, $message, $length = null){
function write($handle, $message, $length = null)
{
echo "[$message]";
return strlen($message);
}

/**
* {@inheritDoc}
*/
function close($handle){
function close($handle)
{
echo "[closed]";
}
}
11 changes: 5 additions & 6 deletions src/Liuggio/StatsdClient/Sender/SenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
{
/**
* @abstract
* @param $hostname
* @param null $port
* @param null $protocol
* @return mixed
*/
function open($hostname, $port = null, $protocol = null);
function open();

/**
* @abstract
* @param $handle
* @param $string
* @param null $length
*
* @return mixed
*/
function write($handle, $string, $length = null);
function write($handle, $string, $length = null);

/**
* @abstract
* @param $handle
*
* @return mixed
*/
function close($handle);
function close($handle);
}
66 changes: 54 additions & 12 deletions src/Liuggio/StatsdClient/Sender/SocketSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,84 @@
Class SocketSender implements SenderInterface
{
private $port;

private $host;
/**
* {@inheritDoc}
*/
public function open($hostname, $port = null, $protocol = null) {

private $protocol;

public function __construct($hostname, $port = null, $protocol = 'udp')
{
$this->host = $hostname;
$this->port = $port;

switch ($protocol) {
case 'udp':
$protocolSOL = SOL_UDP;
$this->protocol = SOL_UDP;
break;
case 'tcp':
$protocolSOL = SOL_TCP;
$this->protocol = SOL_TCP;
break;
default:
throw new InvalidArgumentException('use udp or tcp as protocol');
throw new InvalidArgumentException(sprintf('Use udp or tcp as protocol given %s', $protocol));
break;
}

$fp = socket_create(AF_INET, SOCK_DGRAM, $protocolSOL);
return $fp;
}

/**
* {@inheritDoc}
*/
function write($handle, $message, $length = null){
public function open()
{
$fp = socket_create(AF_INET, SOCK_DGRAM, $this->getProtocol());

return socket_sendto($handle, $message, strlen($message), 0, $this->host, $this->port);
return $fp;
}

/**
* {@inheritDoc}
*/
function close($handle){
public function write($handle, $message, $length = null)
{
return socket_sendto($handle, $message, strlen($message), 0, $this->getHost(), $this->getPort());
}

/**
* {@inheritDoc}
*/
public function close($handle)
{
socket_close($handle);
}


protected function setHost($host)
{
$this->host = $host;
}

protected function getHost()
{
return $this->host;
}

protected function setPort($port)
{
$this->port = $port;
}

protected function getPort()
{
return $this->port;
}

protected function setProtocol($protocol)
{
$this->protocol = $protocol;
}

protected function getProtocol()
{
return $this->protocol;
}
}
39 changes: 39 additions & 0 deletions src/Liuggio/StatsdClient/Sender/SysLogSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Liuggio\StatsdClient\Sender;


Class SysLogSender implements SenderInterface
{
private $priority;

public function __construct($priority = LOG_INFO)
{
$this->priority = $priority;
}
/**
* {@inheritDoc}
*/
public function open()
{
syslog($this->priority, "statsd-client-open");
return true;
}

/**
* {@inheritDoc}
*/
function write($handle, $message, $length = null)
{
syslog($this->priority, sprintf("statsd-client-write \"%s\" %d Bytes", $message, strlen($message)));
return strlen($message);
}

/**
* {@inheritDoc}
*/
function close($handle)
{
syslog($this->priority, "statsd-client-close");
}
}
Loading