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

Commit c97a240

Browse files
Socket madness problem fix (#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 * ver 1.6.0 candidate * README: php version changed * composer.json: repo changed
1 parent 78c0b40 commit c97a240

14 files changed

+813
-580
lines changed

.github/workflows/test.yaml

+28-28
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.2', '8.3']
18+
php-versions: [ '7.4', '8.0', '8.2', '8.3' ]
1919

2020
steps:
21-
- uses: actions/checkout@v3
22-
23-
- name: Setup PHP
24-
uses: shivammathur/setup-php@v2
25-
with:
26-
php-version: ${{ matrix.php-versions }}
27-
extensions: sockets
28-
29-
- name: Validate composer.json and composer.lock
30-
run: composer validate --strict
31-
32-
- name: Cache Composer packages
33-
id: composer-cache
34-
uses: actions/cache@v3
35-
with:
36-
path: vendor
37-
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
38-
restore-keys: |
39-
${{ runner.os }}-php-
40-
41-
- name: Install dependencies
42-
run: composer install --prefer-dist --no-progress
43-
44-
- name: Run Tests
45-
run: |
46-
composer lint
47-
composer test
21+
- uses: actions/checkout@v3
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php-versions }}
27+
extensions: sockets
28+
29+
- name: Validate composer.json and composer.lock
30+
run: composer validate --strict
31+
32+
- name: Cache Composer packages
33+
id: composer-cache
34+
uses: actions/cache@v3
35+
with:
36+
path: vendor
37+
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
38+
restore-keys: |
39+
${{ runner.os }}-php-
40+
41+
- name: Install dependencies
42+
run: composer install --prefer-dist --no-progress
43+
44+
- name: Run Tests
45+
run: |
46+
composer lint
47+
composer test

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ CHANGELOG
33

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

6+
# 1.6.0 / 2023-05-1
7+
* Lazy creation of sockets
8+
* Resend on socket errors. Default max attempts is 2. Can be configured with `max_attempts_to_send` option.
9+
* Closing the socket in the destructor
10+
* Fix potential loss of last buffer in BatchedDogStatsd
11+
* php version >=7.4
12+
613
# 1.5.6 / 2023-01-3
714

815
* Fix warnings to support PHP 8.2

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

10-
**Requires PHP >= 5.6.0.**
10+
**Requires PHP >= 7.4.0.**
1111

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

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

2020
```
21-
"datadog/php-datadogstatsd": "1.5.*"
21+
"datadog/php-datadogstatsd": "1.6.*"
2222
```
2323
The first version shipped in composer is *0.0.3*
2424

composer.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "datadog/php-datadogstatsd",
2+
"name": "pdffiller/php-datadogstatsd",
33
"type": "library",
44
"description": "An extremely simple PHP datadogstatsd client",
55
"keywords": ["datadog", "monitoring", "logging", "statsd", "error-reporting", "check", "health"],
@@ -18,15 +18,12 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=5.6.0",
21+
"php": ">=7.4.0",
2222
"ext-sockets": "*"
2323
},
2424
"support": {
25-
"email": "[email protected]",
26-
"irc": "irc://irc.freenode.net/datadog",
27-
"issues": "https://github.com/DataDog/php-datadogstatsd/issues",
28-
"source": "https://github.com/DataDog/php-datadogstatsd",
29-
"chat": "https://chat.datadoghq.com/"
25+
"issues": "https://github.com/pdffiller/php-datadogstatsd/issues",
26+
"source": "https://github.com/pdffiller/php-datadogstatsd"
3027
},
3128
"autoload": {
3229
"psr-4": {

examples/example.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

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

5-
use DataDog\DogStatsd;
65
use DataDog\BatchedDogStatsd;
6+
use DataDog\DogStatsd;
77

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

1616
//All the following metrics will be sent in a single UDP packet to the statsd server
1717
$batchedStatsd = new BatchedDogStatsd();

examples/expandedExample.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,37 @@
2020
$statsd->set('web.uniques', 3); // A unique user id
2121

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

2525
sleep(1); // Sleep for one second
2626
}
2727

2828
echo "Script has completed.\n";
2929

30+
/**
31+
* @throws Exception
32+
*/
3033
function runFunction($statsd)
3134
{
3235
$startTime = microtime(true);
3336

34-
$testArray = array();
35-
for ($i = 0; $i < rand(1, 1000000000); $i++) {
37+
$testArray = [];
38+
for ($i = 0; $i < random_int(1, 1000000000); $i++) {
3639
$testArray[$i] = $i;
3740

3841
// Simulate an event at every 1000000th element
3942
if ($i % 1000000 == 0) {
4043
echo "Event simulated.\n";
41-
$statsd->event('A thing broke!', array(
42-
'alert_type' => 'error',
43-
'aggregation_key' => 'test_aggr'
44-
));
45-
$statsd->event('Now it is fixed.', array(
46-
'alert_type' => 'success',
47-
'aggregation_key' => 'test_aggr'
48-
));
44+
$statsd->event('A thing broke!', [
45+
'alert_type' => 'error',
46+
'aggregation_key' => 'test_aggr',
47+
]);
48+
$statsd->event('Now it is fixed.', [
49+
'alert_type' => 'success',
50+
'aggregation_key' => 'test_aggr',
51+
]);
4952
}
5053
}
5154
unset($testArray);
52-
$statsd->timing('test.data.point', microtime(true) - $startTime, 1, array('tagname' => 'php_example_tag_2'));
55+
$statsd->timing('test.data.point', microtime(true) - $startTime, 1, ['tagname' => 'php_example_tag_2']);
5356
}

src/BatchedDogStatsd.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
*/
1515
class BatchedDogStatsd extends DogStatsd
1616
{
17-
private static $buffer = array();
18-
private static $bufferLength = 0;
1917
public static $maxBufferLength = 50;
18+
private static $buffer = [];
19+
private static $bufferLength = 0;
2020

21-
22-
public function __construct(array $config = array())
21+
public function __construct(array $config = [])
2322
{
2423
// by default the telemetry is enabled for BatchedDogStatsd
2524
if (!isset($config["disable_telemetry"])) {
@@ -28,14 +27,23 @@ public function __construct(array $config = array())
2827
parent::__construct($config);
2928
}
3029

30+
public function __destruct()
31+
{
32+
if (static::$bufferLength) {
33+
$this->flushBuffer();
34+
}
35+
36+
parent::__destruct();
37+
}
38+
3139
/**
3240
* @param string $message
3341
*/
3442
public function report($message)
3543
{
3644
static::$buffer[] = $message;
37-
static::$bufferLength++;
38-
if (static::$bufferLength > static::$maxBufferLength) {
45+
46+
if (++static::$bufferLength > static::$maxBufferLength) {
3947
$this->flushBuffer();
4048
}
4149
}
@@ -51,8 +59,8 @@ public function flush_buffer() // phpcs:ignore
5159

5260
public function flushBuffer()
5361
{
54-
$this->flush(join("\n", static::$buffer));
55-
static::$buffer = array();
62+
$this->flush(implode("\n", static::$buffer));
63+
static::$buffer = [];
5664
static::$bufferLength = 0;
5765
}
5866
}

0 commit comments

Comments
 (0)