| 1 |
## statsd-php-client |
| 2 |
|
| 3 |
[](http://travis-ci.org/liuggio/statsd-php-client) [](https://packagist.org/packages/liuggio/statsd-php-client) [](https://packagist.org/packages/liuggio/statsd-php-client) |
| 4 |
|
| 5 |
`statsd-php-client` is an Open Source, and **Object Oriented** Client for **etsy/statsd** written in php |
| 6 |
|
| 7 |
### Install with composer |
| 8 |
|
| 9 |
```bash |
| 10 |
composer require liuggio/statsd-php-client |
| 11 |
``` |
| 12 |
|
| 13 |
### Simple Usage |
| 14 |
|
| 15 |
```php |
| 16 |
$statsd = new StatsdService(); |
| 17 |
|
| 18 |
$service->timing('usageTime', 100); |
| 19 |
$service->increment('visitor'); |
| 20 |
$service->decrement('click'); |
| 21 |
$service->gauge('gaugor', 333); |
| 22 |
$service->set('uniques', 765); |
| 23 |
|
| 24 |
$service->flush(); |
| 25 |
``` |
| 26 |
|
| 27 |
|
| 28 |
## Why use this library instead the [statsd/php-example](https://github.com/etsy/statsd/blob/master/examples/php-example.php)? |
| 29 |
|
| 30 |
- You are wise. |
| 31 |
|
| 32 |
- You could also use monolog to redirect data to statsd |
| 33 |
|
| 34 |
- This library is tested. |
| 35 |
|
| 36 |
- This library optimizes the messages to send, compressing multiple messages in individual UDP packets. |
| 37 |
|
| 38 |
- This library pays attention to the maximum length of the UDP. |
| 39 |
|
| 40 |
- This library is made by Objects not array, but it also accepts array. |
| 41 |
|
| 42 |
- 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`) |
| 43 |
|
| 44 |
|
| 45 |
### Advanced Instantiation |
| 46 |
|
| 47 |
```php |
| 48 |
use Liuggio\StatsdClient\StatsdClient, |
| 49 |
Liuggio\StatsdClient\Factory\StatsdDataFactory, |
| 50 |
Liuggio\StatsdClient\Sender\SocketSender, |
| 51 |
Liuggio\StatsdClient\Service\StatsdService; |
| 52 |
// use Liuggio\StatsdClient\Sender\SysLogSender; |
| 53 |
|
| 54 |
$sender = new SocketSender(/*'localhost', 8126, 'udp'*/); |
| 55 |
// $sender = new SysLogSender(); // enabling this, the packet will not send over the socket |
| 56 |
|
| 57 |
$client = new StatsdClient($sender); |
| 58 |
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); |
| 59 |
$service = new StatsdService($client, $factory); |
| 60 |
|
| 61 |
// create the metrics with the service |
| 62 |
$service->timing('usageTime', 100); |
| 63 |
|
| 64 |
//... |
| 65 |
|
| 66 |
// send the data to statsd |
| 67 |
$service->flush(); |
| 68 |
|
| 69 |
``` |
| 70 |
|
| 71 |
### Usage with Monolog |
| 72 |
|
| 73 |
```php |
| 74 |
use Liuggio\StatsdClient\StatsdClient, |
| 75 |
Liuggio\StatsdClient\Factory\StatsdDataFactory, |
| 76 |
Liuggio\StatsdClient\Sender\SocketSender; |
| 77 |
// use Liuggio\StatsdClient\Sender\SysLogSender; |
| 78 |
|
| 79 |
use Monolog\Logger; |
| 80 |
use Liuggio\StatsdClient\Monolog\Handler\StatsDHandler; |
| 81 |
|
| 82 |
$sender = new SocketSender(/*'localhost', 8126, 'udp'*/); |
| 83 |
// $sender = new SysLogSender(); // enabling this, the packet will not send over the socket |
| 84 |
$client = new StatsdClient($sender); |
| 85 |
$factory = new StatsdDataFactory(); |
| 86 |
|
| 87 |
$logger = new Logger('my_logger'); |
| 88 |
$logger->pushHandler(new StatsDHandler($client, $factory, 'prefix', Logger::DEBUG)); |
| 89 |
|
| 90 |
$logger->addInfo('My logger is now ready'); |
| 91 |
``` |
| 92 |
|
| 93 |
the output will be: `prefix.my_logger.INFO.My-logger:1|c" 36 Bytes` |
| 94 |
|
| 95 |
## Short Theory |
| 96 |
|
| 97 |
### Easily Install StatSD and Graphite |
| 98 |
|
| 99 |
In order to try this application monitor you have to install etsy/statsd and Graphite |
| 100 |
|
| 101 |
see this blog post to install it with vagrant [Easy install statsd graphite](http://welcometothebundle.com/easily-install-statsd-and-graphite-with-vagrant/). |
| 102 |
|
| 103 |
#### [StatsD](https://github.com/etsy/statsd) |
| 104 |
|
| 105 |
StatsD is a simple daemon for easy stats aggregation |
| 106 |
|
| 107 |
#### [Graphite](http://graphite.wikidot.com/) |
| 108 |
|
| 109 |
Graphite is a Scalable Realtime Graphing |
| 110 |
|
| 111 |
#### The Client sends data with UDP (faster) |
| 112 |
|
| 113 |
https://www.google.com/search?q=tcp+vs+udp |
| 114 |
|
| 115 |
## Contribution |
| 116 |
|
| 117 |
Active contribution and patches are very welcome. |
| 118 |
To keep things in shape we have quite a bunch of unit tests. If you're submitting pull requests please |
| 119 |
make sure that they are still passing and if you add functionality please |
| 120 |
take a look at the coverage as well it should be pretty high :) |
| 121 |
|
| 122 |
- First fork or clone the repository |
| 123 |
|
| 124 |
``` |
| 125 |
git clone git://github.com/liuggio/statsd-php-client.git |
| 126 |
cd statsd-php-client |
| 127 |
``` |
| 128 |
|
| 129 |
- Install vendors: |
| 130 |
|
| 131 |
``` bash |
| 132 |
composer.phar install |
| 133 |
``` |
| 134 |
|
| 135 |
- This will give you proper results: |
| 136 |
|
| 137 |
``` bash |
| 138 |
phpunit --coverage-html reports |
| 139 |
``` |
| 140 |
|
| 141 |
## Core developers: |
| 142 |
|
| 143 |
This project is actively mantained by David Moreau AKA @dav-m85 and @liuggio |