Skip to content

Commit

Permalink
Update code to respect PSR-4
Browse files Browse the repository at this point in the history
The lib can now be auto-load when installed by composer.
  • Loading branch information
hush-hush committed Dec 26, 2017
1 parent 3b221c4 commit fb210a5
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 179 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

# 1.0.0 / 2017-12-20
* Respect PSR4, huge thanks to [@onema][]
* Library API is now object oriented

# 0.5.0 / 2017-12-20
* Update code to respect PSR1 and PSR2
* Add support for 'aggregation_key' and 'source_type_name' for UDP events
Expand Down Expand Up @@ -46,3 +50,4 @@ CHANGELOG
[@baweinbe]: https://github.com/baweinbe
[@jmparks-ebates]: https://github.com/jmparks-ebates
[@Amaroq1]: https://github.com/Amaroq1
[@onema]: https://github.com/onema
93 changes: 66 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ See [CHANGELOG.md](CHANGELOG.md) for changes.
Add the following to your `composer.json`:

```
"datadog/php-datadogstatsd": "0.3.*"
"datadog/php-datadogstatsd": "1.0.*"
```

Note: The first version shipped in composer is 0.0.3
Expand All @@ -22,34 +22,62 @@ Note: The first version shipped in composer is 0.0.3

Clone repository at [github.com/DataDog/php-datadogstatsd](https://github.com/DataDog/php-datadogstatsd)

Setup: `require './libraries/datadogstatsd.php';`
Setup: `require './src/DogStatsd.php';`

## Usage

### instantiation

To instantiate a DogStatsd object using `composer`:

```php
require __DIR__ . '/vendor/autoload.php';

use DataDog\DogStatsd;
use DataDog\BatchedDogStatsd;

$statsd = new DogStatsd();
$statsd = new BatchedDogStatsd();
```

DogStatsd constructor, takes a configuration array. The configuration can take any of the following values (all optional):

- `host`: the host of your DogStatsd server, default to `localhost`
- `port`: the port of your DogStatsd server. default to `8125`

When sending `events` over TCP the following options can be set (see [Events section](#submitting-events)):
- `api_key`: needed to send `event` over TCP
- `app_key`: needed to send `event` over TCP
- `curl_ssl_verify_host`: Config pass-through for `CURLOPT_SSL_VERIFYHOST` defaults 2
- `curl_ssl_verify_peer`: Config pass-through for `CURLOPT_SSL_VERIFYPEER` default 1
- `datadog_host`: where to send events default `https://app.datadoghq.com`

### Tags

The 'tags' argument can be a array or a string. Value can be set to `null`.

```php
# Both call will send the "app:php1" and "beta" tags.
Datadogstatsd::increment('your.data.point', 1, array('app' => 'php1', 'beta' => null));
Datadogstatsd::increment('your.data.point', 1, "app:php1,beta");
$statd->increment('your.data.point', 1, array('app' => 'php1', 'beta' => null));
$statd->increment('your.data.point', 1, "app:php1,beta");
```

### Increment

To increment things:

``` php
Datadogstatsd::increment('your.data.point');
Datadogstatsd::increment('your.data.point', .5);
Datadogstatsd::increment('your.data.point', 1, array('tagname' => 'value'));
$statd->increment('your.data.point');
$statd->increment('your.data.point', .5);
$statd->increment('your.data.point', 1, array('tagname' => 'value'));
```

### Decrement

To decrement things:

``` php
Datadogstatsd::decrement('your.data.point');
$statd->decrement('your.data.point');
```

### Timing
Expand All @@ -59,33 +87,38 @@ To time things:
``` php
$start_time = microtime(true);
run_function();
Datadogstatsd::timing('your.data.point', microtime(true) - $start_time);
$statd->timing('your.data.point', microtime(true) - $start_time);

Datadogstatsd::timing('your.data.point', microtime(true) - $start_time, 1, array('tagname' => 'value'));
$statd->timing('your.data.point', microtime(true) - $start_time, 1, array('tagname' => 'value'));
```

### Submitting events

For documentation on the values of events, see
[http://docs.datadoghq.com/api/#events/](http://docs.datadoghq.com/api/#events/).
[http://docs.datadoghq.com/api/#events](http://docs.datadoghq.com/api/#events).

**Submitting events via TCP vs UDP**

* **TCP** - High-confidence event submission. Will log errors on event submission error.
* **UDP** - "Fire and forget" event submission. Will **not** log errors on event submission error. No acknowledgement
of submitted event from Datadog.

No matter wich transport is used the `event` function has the same API.

_[Differences between TCP/UDP](http://stackoverflow.com/a/5970545)_

##### UDP Submission to local dogstatsd

Since the UDP method uses the a local dogstatsd instance we don't need to setup any additional application/api access.

```php
Datadogstatsd::event('Fire and forget!', array(
'text' => 'Sending errors via UDP is faster but less reliable!',
'alert_type' => 'success'
));
$statsd = new DogStatsd();
$statd->event('Fire and forget!',
array(
'text' => 'Sending errors via UDP is faster but less reliable!',
'alert_type' => 'success'
)
);
```

* Default method
Expand All @@ -104,18 +137,24 @@ instead of sending to a local dogstatsd instance.
You can find your api and app keys in the [API tab](https://app.datadoghq.com/account/settings#api).

```php
$apiKey = 'myApiKey';
$appKey = 'myAppKey';

Datadogstatsd::configure($apiKey, $appKey);
Datadogstatsd::event('A thing broke!', array(
'alert_type' => 'error',
'aggregation_key' => 'test_aggr'
));
Datadogstatsd::event('Now it is fixed.', array(
'alert_type' => 'success',
'aggregation_key' => 'test_aggr'
));
$statsd = new DogStatsd(
array('api_key' => 'myApiKey',
'app_key' => 'myAppKey',
)
);

$statd->event('A thing broke!',
array(
'alert_type' => 'error',
'aggregation_key' => 'test_aggr'
)
);
$statd->event('Now it is fixed.',
array(
'alert_type' => 'success',
'aggregation_key' => 'test_aggr'
)
);
```

* Slower
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"homepage": "https://www.datadoghq.com/",
"license": "MIT",
"authors": [
{
{
"name": "Alex Corley",
"email": "[email protected]",
"role": "Developer"
Expand All @@ -23,12 +23,14 @@
"php": ">=5.3.0"
},
"support": {
"email": "[email protected]",
"irc": "irc://irc.freenode.net/datadog",
"issues": "https://github.com/DataDog/php-datadogstatsd/issues",
"source": "https://github.com/DataDog/php-datadogstatsd"
"email": "[email protected]",
"irc": "irc://irc.freenode.net/datadog",
"issues": "https://github.com/DataDog/php-datadogstatsd/issues",
"source": "https://github.com/DataDog/php-datadogstatsd"
},
"autoload": {
"classmap": ["libraries/"]
"psr-4": {
"DataDog\\": "src/"
}
}
}
25 changes: 14 additions & 11 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

require '../vendor/autoload.php';

require '../libraries/datadogstatsd.php';


Datadogstatsd::increment('web.page_views');
Datadogstatsd::histogram('web.render_time', 15);
Datadogstatsd::set('web.uniques', 3 /* a unique user id */);
Datadogstatsd::service_check('my.service.check', Datadogstatsd::CRITICAL);
use DataDog\DogStatsd;
use DataDog\BatchedDogStatsd;

$statsd = new DogStatsd();
$statsd->increment('web.page_views');
$statsd->histogram('web.render_time', 15);
$statsd->set('web.uniques', 3 /* a unique user id */);
$statsd->service_check('my.service.check', DogStatsd::CRITICAL);
$statsd->event("Event title", array("text"=>"Event text"));

//All the following metrics will be sent in a single UDP packet to the statsd server
BatchedDatadogstatsd::increment('web.page_views');
BatchedDatadogstatsd::histogram('web.render_time', 15);
BatchedDatadogstatsd::set('web.uniques', 3 /* a unique user id */);
BatchedDatadogstatsd::flush_buffer(); // Necessary
$batchedStatsd = new BatchedDogStatsd();
$batchedStatsd->increment('web.page_views');
$batchedStatsd->histogram('web.render_time', 15);
$batchedStatsd->set('web.uniques', 3 /* a unique user id */);
$batchedStatsd->flush_buffer(); // Necessary
34 changes: 18 additions & 16 deletions examples/expandedExample.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

require '../libraries/datadogstatsd.php';
require '../vendor/autoload.php';

$apiKey = '046a22167c96272932f7f95753ceb81013e1fcac';
$appKey = '2167c96272932f013e1fcac7f95753ceb81046a2';
Datadogstatsd::configure($apiKey, $appKey);
use DataDog\DogStatsd;

// by setting the api_key you are switching from UDP to TCP for events
$statsd = new DogStatsd(
array('api_key' => '046a22167c96272932f7f95753ceb81013e1fcac',
'app_key' => '2167c96272932f013e1fcac7f95753ceb81046a2',
)
);

$runFor = 5; // Set to five minutes. Increase or decrease to have script run longer or shorter.
$scriptStartTime = time();
Expand All @@ -14,23 +19,20 @@
// Send metrics and events for 5 minutes.
while (time() < $scriptStartTime + ($runFor * 60)) {
$startTime1 = microtime(true);
Datadogstatsd::increment('web.page_views');
Datadogstatsd::histogram('web.render_time', 15);
Datadogstatsd::set('web.uniques', 3 /* A unique user id */);
$statsd->increment('web.page_views');
$statsd->histogram('web.render_time', 15);
$statsd->set('web.uniques', 3); // A unique user id

runFunction();
Datadogstatsd::timing('test.data.point', microtime(true) - $startTime1, 1, array('tagname' => 'php_example_tag_1'));
runFunction($statsd);
$statsd->timing('test.data.point', microtime(true) - $startTime1, 1, array('tagname' => 'php_example_tag_1'));

sleep(1); // Sleep for one second
}

echo "Script has completed.\n";

function runFunction()
function runFunction($statsd)
{
global $apiKey;
global $appKey;

$startTime = microtime(true);

$testArray = array();
Expand All @@ -40,16 +42,16 @@ function runFunction()
// Simulate an event at every 1000000th element
if ($i % 1000000 == 0) {
echo "Event simulated.\n";
Datadogstatsd::event('A thing broke!', array(
$statsd->event('A thing broke!', array(
'alert_type' => 'error',
'aggregation_key' => 'test_aggr'
));
Datadogstatsd::event('Now it is fixed.', array(
$statsd->event('Now it is fixed.', array(
'alert_type' => 'success',
'aggregation_key' => 'test_aggr'
));
}
}
unset($testArray);
Datadogstatsd::timing('test.data.point', microtime(true) - $startTime, 1, array('tagname' => 'php_example_tag_2'));
$statsd->timing('test.data.point', microtime(true) - $startTime, 1, array('tagname' => 'php_example_tag_2'));
}
26 changes: 26 additions & 0 deletions src/BatchedDogStatsd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DataDog;

class BatchedDogStatsd extends DogStatsd
{
private static $buffer = array();
private static $bufferLength = 0;
public static $maxBufferLength = 50;

public function report($udp_message)
{
static::$buffer[] = $udp_message;
static::$bufferLength++;
if (static::$bufferLength > static::$maxBufferLength) {
$this->flush_buffer();
}
}

public function flush_buffer()
{
$this->flush(join("\n", static::$buffer));
static::$buffer = array();
static::$bufferLength = 0;
}
}
Loading

0 comments on commit fb210a5

Please sign in to comment.