Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Socket madness problem fix (#1)
Browse files Browse the repository at this point in the history
* Lazy creation of sockets
* Resend on socket errors. Default max attempts is 2. Can be configured with `max_attempts_to_send` option.
* Closing the socket in the destructor
* Fix potential loss of last buffer in BatchedDogStatsd
* php version >=7.4

* ver 1.6.0 candidate
* README: php version changed
* composer.json: repo changed
  • Loading branch information
serhii-nochevnyi authored May 1, 2023
1 parent 78c0b40 commit c97a240
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 580 deletions.
56 changes: 28 additions & 28 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.2', '8.3']
php-versions: [ '7.4', '8.0', '8.2', '8.3' ]

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: sockets

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run Tests
run: |
composer lint
composer test
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: sockets

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run Tests
run: |
composer lint
composer test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ CHANGELOG

[//]: # (comment: Don't forget to update src/DogStatsd.php:DogStatsd::version when releasing a new version)

# 1.6.0 / 2023-05-1
* Lazy creation of sockets
* Resend on socket errors. Default max attempts is 2. Can be configured with `max_attempts_to_send` option.
* Closing the socket in the destructor
* Fix potential loss of last buffer in BatchedDogStatsd
* php version >=7.4

# 1.5.6 / 2023-01-3

* Fix warnings to support PHP 8.2
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This is an extremely simple PHP [DogStatsD](https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php) client.

**Requires PHP >= 5.6.0.**
**Requires PHP >= 7.4.0.**

See [CHANGELOG.md](CHANGELOG.md) for changes.

Expand All @@ -18,7 +18,7 @@ See [CHANGELOG.md](CHANGELOG.md) for changes.
Add the following to your `composer.json`:

```
"datadog/php-datadogstatsd": "1.5.*"
"datadog/php-datadogstatsd": "1.6.*"
```
The first version shipped in composer is *0.0.3*

Expand Down
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "datadog/php-datadogstatsd",
"name": "pdffiller/php-datadogstatsd",
"type": "library",
"description": "An extremely simple PHP datadogstatsd client",
"keywords": ["datadog", "monitoring", "logging", "statsd", "error-reporting", "check", "health"],
Expand All @@ -18,15 +18,12 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=7.4.0",
"ext-sockets": "*"
},
"support": {
"email": "package@datadoghq.com",
"irc": "irc://irc.freenode.net/datadog",
"issues": "https://github.com/DataDog/php-datadogstatsd/issues",
"source": "https://github.com/DataDog/php-datadogstatsd",
"chat": "https://chat.datadoghq.com/"
"issues": "https://github.com/pdffiller/php-datadogstatsd/issues",
"source": "https://github.com/pdffiller/php-datadogstatsd"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

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

use DataDog\DogStatsd;
use DataDog\BatchedDogStatsd;
use DataDog\DogStatsd;

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

//All the following metrics will be sent in a single UDP packet to the statsd server
$batchedStatsd = new BatchedDogStatsd();
Expand Down
27 changes: 15 additions & 12 deletions examples/expandedExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,37 @@
$statsd->set('web.uniques', 3); // A unique user id

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

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

echo "Script has completed.\n";

/**
* @throws Exception
*/
function runFunction($statsd)
{
$startTime = microtime(true);

$testArray = array();
for ($i = 0; $i < rand(1, 1000000000); $i++) {
$testArray = [];
for ($i = 0; $i < random_int(1, 1000000000); $i++) {
$testArray[$i] = $i;

// Simulate an event at every 1000000th element
if ($i % 1000000 == 0) {
echo "Event simulated.\n";
$statsd->event('A thing broke!', array(
'alert_type' => 'error',
'aggregation_key' => 'test_aggr'
));
$statsd->event('Now it is fixed.', array(
'alert_type' => 'success',
'aggregation_key' => 'test_aggr'
));
$statsd->event('A thing broke!', [
'alert_type' => 'error',
'aggregation_key' => 'test_aggr',
]);
$statsd->event('Now it is fixed.', [
'alert_type' => 'success',
'aggregation_key' => 'test_aggr',
]);
}
}
unset($testArray);
$statsd->timing('test.data.point', microtime(true) - $startTime, 1, array('tagname' => 'php_example_tag_2'));
$statsd->timing('test.data.point', microtime(true) - $startTime, 1, ['tagname' => 'php_example_tag_2']);
}
24 changes: 16 additions & 8 deletions src/BatchedDogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
*/
class BatchedDogStatsd extends DogStatsd
{
private static $buffer = array();
private static $bufferLength = 0;
public static $maxBufferLength = 50;
private static $buffer = [];
private static $bufferLength = 0;


public function __construct(array $config = array())
public function __construct(array $config = [])
{
// by default the telemetry is enabled for BatchedDogStatsd
if (!isset($config["disable_telemetry"])) {
Expand All @@ -28,14 +27,23 @@ public function __construct(array $config = array())
parent::__construct($config);
}

public function __destruct()
{
if (static::$bufferLength) {
$this->flushBuffer();
}

parent::__destruct();
}

/**
* @param string $message
*/
public function report($message)
{
static::$buffer[] = $message;
static::$bufferLength++;
if (static::$bufferLength > static::$maxBufferLength) {

if (++static::$bufferLength > static::$maxBufferLength) {
$this->flushBuffer();
}
}
Expand All @@ -51,8 +59,8 @@ public function flush_buffer() // phpcs:ignore

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

0 comments on commit c97a240

Please sign in to comment.