Skip to content

Commit

Permalink
add backwards compatibily for non dataDog statsd server
Browse files Browse the repository at this point in the history
rename to DogStatsD
  • Loading branch information
Harry Bragg committed Nov 4, 2015
1 parent 285c1ea commit a14bde0
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 70 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ matrix:

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-source
- travis_retry composer install --no-interaction --prefer-source

script:
- make test
- make lint
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
.PHONY: test test-coverage test-unit test-unit-coverage install
.PHONY: test lint lint-auto-fix test-coverage test-unit test-unit-coverage install

test:
@./vendor/bin/phpunit

lint:
@./vendor/bin/phpcs -p --standard=PSR2 --warning-severity=0 src/ tests/

lint-fix:
@./vendor/bin/phpcbf -p --standard=PSR2 src/ tests/

test-coverage:
@./vendor/bin/phpunit --coverage-text --coverage-html ./tests/report

Expand Down
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/graze/statsd-dd.svg?style=flat-square)](https://packagist.org/packages/graze/statsd-dd)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/graze/statsd-dd/master.svg?style=flat-square)](https://travis-ci.org/graze/statsd-dd)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/graze/statsd-dd.svg?style=flat-square)](https://scrutinizer-ci.com/g/graze/statsd-dd/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/graze/statsd-dd.svg?style=flat-square)](https://scrutinizer-ci.com/g/graze/statsd-dd)
[![Total Downloads](https://img.shields.io/packagist/dt/graze/statsd-dd.svg?style=flat-square)](https://packagist.org/packages/graze/statsd-dd)

Client to talk to [DataDogs StatsD Agent](http://docs.datadoghq.com/guides/dogstatsd)
Client to talk to [DataDogs StatsD Agent](http://docs.datadoghq.com/guides/dogstatsd) Forked from:
[League/StatsD](https://github.com/thephpleague/statsd)

## Install

Expand All @@ -22,7 +21,7 @@ $ composer require graze/statsd-dd
### Configuring

```php
$statsd = new Graze\DDStatsD\Client();
$statsd = new Graze\DogStatsD\Client();
$statsd->configure(array(
'host' => '127.0.0.1',
'port' => 8125,
Expand All @@ -33,8 +32,8 @@ $statsd->configure(array(
OR

```php
$statsd1 = DDStatsD\Client::instance('server1')->configure(array(...));
$statsd2 = DDStatsD\Client::instance('server2')->configure(array(...));
$statsd1 = DogStatsD\Client::instance('server1')->configure(array(...));
$statsd2 = DogStatsD\Client::instance('server2')->configure(array(...));
```

The StatsD client waits for `ini_get('default_socket_timeout')` seconds when opening the socket by default. To reduce
Expand All @@ -49,7 +48,16 @@ to disable these exceptions and log a PHP warning instead if you wish. To do so,

If omitted, this option defaults to `true`.

### Counters
#### Core StatsD implementation

To use this with a core statsd implementation include the following in your configuration:
```
'dataDog' => false
```

### Methods

#### Counters

```php
$statsd->increment('web.pageview');
Expand All @@ -61,43 +69,43 @@ $statsd->increment(array(
$statsd->increment('web.clicks', 1, 0.5);
```

### Gauges
#### Gauges

```php
$statsd->gauge('api.logged_in_users', 123456);
```


### Sets
#### Sets

```php
$userID = 23;
$statsd->set('api.unique_logins', $userID);
```

### Timers
#### Timers

```php
$statsd->timing('api.response_time', 256);
```

## Timing Blocks
#### Timing Blocks

```php
$statsd->time('api.dbcall', function () {
// this code execution will be timed and recorded in ms
});
```

## Tags
#### Tags

```php
$statsd->increment('web.pageview', 1, ['page' => 'some/page']);
$statsd->guage('api.logged_in_users', 123456, ['environement' => 'live']);
$statsd->set('api.unique_logins', $userID, ['
$statsd->set('api.unique_logins', $userID, ['tag']);
$statsd->timing('api.response_time', 245, ['end-point' => 'page', 'env' => 'test']);
```

## Events
#### Events

```php
$statsd->event(
Expand All @@ -113,7 +121,7 @@ $statsd->event(
);
```

## Service Check
#### Service Check

```php
$statsd->serviceCheck(
Expand Down Expand Up @@ -151,7 +159,7 @@ If you discover any security related issues, please email harry.bragg@graze.com
### Forked from [thephpleague/statsd](https://github.com/thephpleague/statsd):

- [Marc Qualie](https://github.com/marcqualie)
- [League\statsd Contributors](https://github.com/thephpleague/statsd/contributors)
- [League/StatsD Contributors](https://github.com/thephpleague/statsd/contributors)

## License

Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"graze",
"statsd-dd",
"statsd",
"datadog"
"datadog",
"dogstatsd"
],
"homepage": "https://github.com/graze/statsd-dd",
"license": "MIT",
Expand All @@ -26,17 +27,18 @@
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit" : "4.*"
"phpunit/phpunit" : "4.*",
"squizlabs/php_codesniffer": "^2.3"
},
"autoload": {
"psr-4": {
"Graze\\DDStatsD\\": "src"
"Graze\\DogStatsD\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Graze\\DDStatsD\\Test\\": "tests/src",
"Graze\\DDStatsD\\Test\\Unit\\": "tests/unit"
"Graze\\DogStatsD\\Test\\": "tests/src",
"Graze\\DogStatsD\\Test\\Unit\\": "tests/unit"
}
}
}
27 changes: 23 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Graze\DDStatsD;
namespace Graze\DogStatsD;

use Graze\DDStatsD\Exception\ConfigurationException;
use Graze\DDStatsD\Exception\ConnectionException;
use Graze\DogStatsD\Exception\ConfigurationException;
use Graze\DogStatsD\Exception\ConnectionException;

/**
* StatsD Client Class - Modified to support DataDogs statsd server
Expand Down Expand Up @@ -116,6 +116,13 @@ class Client
'message' => 'm',
];

/**
* Is the server type DataDog implementation
*
* @var bool
*/
protected $dataDog = true;

/**
* Singleton Reference
*
Expand Down Expand Up @@ -188,6 +195,10 @@ public function configure(array $options = array())
$this->throwConnectionExceptions = $options['throwConnectionExceptions'];
}

if ((isset($options['dataDog'])) && (!$options['dataDog'])) {
$this->dataDog = false;
}

return $this;
}

Expand Down Expand Up @@ -371,6 +382,10 @@ public function set($metric, $value, array $tags = [])
*/
public function event($title, $text, array $metadata = [], array $tags = [])
{
if (!$this->dataDog) {
return $this;
}

$text = str_replace(["\r", "\n"], ['', "\\n"], $text);
$metric = sprintf('_e{%d,%d}', strlen($title), strlen($text));
$prefix = $this->namespace ? $this->namespace . '.' : '';
Expand Down Expand Up @@ -406,6 +421,10 @@ public function event($title, $text, array $metadata = [], array $tags = [])
*/
public function serviceCheck($name, $status, array $metadata = [], array $tags = [])
{
if (!$this->dataDog) {
return $this;
}

$prefix = $this->namespace ? $this->namespace . '.' : '';
$value = sprintf('_sc|%s|%d', $prefix . $name, $status);

Expand Down Expand Up @@ -433,7 +452,7 @@ public function serviceCheck($name, $status, array $metadata = [], array $tags =
*/
private function formatTags(array $tags = [])
{
if (count($tags) === 0) {
if (!$this->dataDog || count($tags) === 0) {
return '';
}

Expand Down
4 changes: 2 additions & 2 deletions src/Exception/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Graze\DDStatsD\Exception;
namespace Graze\DogStatsD\Exception;

use Exception;
use Graze\DDStatsD\Client;
use Graze\DogStatsD\Client;

/**
* Configuration Exception Class
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Graze\DDStatsD\Exception;
namespace Graze\DogStatsD\Exception;

use Exception;
use Graze\DDStatsD\Client;
use Graze\DogStatsD\Client;

/**
* Connection Exception Class
Expand Down
4 changes: 2 additions & 2 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Graze\DDStatsD\Test;
namespace Graze\DogStatsD\Test;

use Graze\DDStatsD\Client;
use Graze\DogStatsD\Client;
use PHPUnit_Framework_TestCase;

class TestCase extends PHPUnit_Framework_TestCase
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Graze\DDStatsD\Test\Unit;
namespace Graze\DogStatsD\Test\Unit;

use Graze\DDStatsD\Client;
use Graze\DDStatsD\Test\TestCase;
use Graze\DogStatsD\Client;
use Graze\DogStatsD\Test\TestCase;

class ClientTest extends TestCase
{
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Graze\DDStatsD\Test\Unit;
namespace Graze\DogStatsD\Test\Unit;

use Graze\DDStatsD\Test\TestCase;
use Graze\DogStatsD\Test\TestCase;

class ConfigurationTest extends TestCase
{
/**
* Large ports should be out of range
*
* @expectedException \Graze\DDStatsD\Exception\ConfigurationException
* @expectedException \Graze\DogStatsD\Exception\ConfigurationException
*/
public function testLargePort()
{
Expand All @@ -22,7 +22,7 @@ public function testLargePort()
/**
* Non-integer ports are not acceptable
*
* @expectedException \Graze\DDStatsD\Exception\ConfigurationException
* @expectedException \Graze\DogStatsD\Exception\ConfigurationException
*/
public function testStringPort()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Graze\DDStatsD\Test\Unit;
namespace Graze\DogStatsD\Test\Unit;

use Graze\DDStatsD\Test\TestCase;
use Graze\DogStatsD\Test\TestCase;

class ConnectionTest extends TestCase
{
/**
* Non-integer ports are not acceptable
*
* @expectedException \Graze\DDStatsD\Exception\ConnectionException
* @expectedException \Graze\DogStatsD\Exception\ConnectionException
*/
public function testInvalidHost()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/CounterTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Graze\DDStatsD\Test\Unit;
namespace Graze\DogStatsD\Test\Unit;

use Graze\DDStatsD\Test\TestCase;
use Graze\DogStatsD\Test\TestCase;

class CounterTest extends TestCase
{
Expand All @@ -23,7 +23,7 @@ public function testIncrementDelta()
public function testIncrementSample()
{
while ($this->client->getLastMessage() === '') {
$this->client->increment('test_metric', 1, 0.75);
$this->client->increment('test_metric', 1, 0.75);
}
$this->assertEquals('test_metric:1|c|@0.75', $this->client->getLastMessage());
}
Expand Down
17 changes: 14 additions & 3 deletions tests/unit/EventTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Graze\DDStatsD\Test\Unit;
namespace Graze\DogStatsD\Test\Unit;

use Graze\DDStatsD\Client;
use Graze\DDStatsD\Test\TestCase;
use Graze\DogStatsD\Client;
use Graze\DogStatsD\Test\TestCase;

class EventTest extends TestCase
{
Expand Down Expand Up @@ -54,4 +54,15 @@ public function testEventTextReplacesNewLines()
$this->client->event('some_title', "LongText\rAnd\nStuff");
$this->assertEquals("_e{10,18}:some_title|LongTextAnd\\nStuff", $this->client->getLastMessage());
}

public function testCoreStatsDImplementation()
{
$this->client->configure(array(
'host' => '127.0.0.1',
'port' => 8125,
'dataDog' => false
));
$this->client->event('some_title', 'textAndThings');
$this->assertEquals('', $this->client->getLastMessage());
}
}
Loading

0 comments on commit a14bde0

Please sign in to comment.