Skip to content

Commit 75e312a

Browse files
committed
wip
1 parent 2aba355 commit 75e312a

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

config/statsd-adapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
'throwConnectionExceptions' => true,
3030
],
3131
"datadog" => [
32+
/**
33+
* By setting batch_size to a positive integer, the batched dogstatsd provider will be used.
34+
*/
35+
'batch_size' => null,
3236
// see configuration options: https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php&tab=hostagent#client-instantiation-parameters
3337
"adapter" => "datadog",
3438
"host" => env("DD_AGENT_HOST"),

src/AdapterManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
1111
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
1212
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
13+
use DataDog\BatchedDogStatsd;
1314
use DataDog\DogStatsd;
1415
use Illuminate\Contracts\Container\BindingResolutionException;
1516
use Illuminate\Support\MultipleInstanceManager;
@@ -158,8 +159,16 @@ protected function createLogDatadogAdapter(array $config): DatadogStatsDClientAd
158159
*/
159160
protected function createDatadogAdapter(array $config): DatadogStatsDClientAdapter
160161
{
162+
$batchSize = (int) ($config['batch_size'] ?? null);
163+
if ($batchSize > 0) {
164+
$client = new BatchedDogStatsd($config);
165+
BatchedDogStatsd::$maxBufferLength = $batchSize;
166+
} else {
167+
$client = new DogStatsd($config);
168+
}
169+
161170
return new DatadogStatsDClientAdapter(
162-
new DogStatsd($config),
171+
$client,
163172
$this->getDefaultTags(),
164173
clock: $this->getClockImplementation()
165174
);

src/StatsDAdapterServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
66
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
7+
use DataDog\BatchedDogStatsd;
8+
use Illuminate\Container\Container;
79
use Illuminate\Contracts\Foundation\Application;
810
use Illuminate\Contracts\Support\DeferrableProvider;
911
use Illuminate\Support\ServiceProvider;
@@ -13,6 +15,19 @@ class StatsDAdapterServiceProvider extends ServiceProvider implements Deferrable
1315
public function boot(): void
1416
{
1517
$this->offerPublishing();
18+
$this->app->terminating(static function () {
19+
$adapterInstance = Container::getInstance()
20+
->make(AdapterManager::class)
21+
->instance()
22+
->getClient();
23+
24+
if (
25+
BatchedDogStatsd::getBufferLength() > 0
26+
&& $adapterInstance instanceof BatchedDogStatsd
27+
) {
28+
$adapterInstance->flushBuffer();
29+
}
30+
});
1631
}
1732

1833
public function register(): void

tests/StatsDAdapterServiceProviderTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@
77
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
88
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
99
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
10+
use DataDog\BatchedDogStatsd;
1011
use Illuminate\Support\Facades\Config;
1112
use PHPUnit\Framework\Attributes\Test;
1213

1314
class StatsDAdapterServiceProviderTest extends AbstractTestCase
1415
{
16+
private int $initialBufferSize;
17+
18+
#[\Override]
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
$this->initialBufferSize = BatchedDogStatsd::$maxBufferLength;
23+
}
24+
25+
#[\Override]
26+
protected function tearDown(): void
27+
{
28+
BatchedDogStatsd::$maxBufferLength = $this->initialBufferSize;
29+
parent::tearDown();
30+
}
31+
1532
#[Test]
1633
public function makeStatsDClientAdapter_returnsDefaultInstance(): void
1734
{
@@ -73,4 +90,37 @@ public function inMemoryStatsRecord_isSingleton(): void
7390
// Then
7491
self::assertSame($firstRecord, $secondRecord);
7592
}
93+
94+
#[Test]
95+
public function datadogConfigHasBatchSize_resolve_setsBufferSize(): void
96+
{
97+
// Given
98+
Config::set('statsd-adapter.channels.datadog.batch_size', 2);
99+
Config::set('statsd-adapter.default', 'datadog');
100+
101+
// When
102+
$clientAdapter = $this->app->make(StatsDClientAdapter::class);
103+
104+
// Then
105+
self::assertInstanceOf(BatchedDogStatsd::class, $clientAdapter->getClient());
106+
self::assertEquals(2, BatchedDogStatsd::$maxBufferLength);
107+
}
108+
109+
#[Test]
110+
public function batchedDogStats_terminatingApp_submitsStats(): void
111+
{
112+
// Given
113+
Config::set('statsd-adapter.channels.datadog.batch_size', 2);
114+
Config::set('statsd-adapter.default', 'datadog');
115+
116+
// And
117+
$clientAdapter = $this->app->make(StatsDClientAdapter::class);
118+
$clientAdapter->increment('testing');
119+
120+
// When
121+
$this->app->terminate();
122+
123+
// Then
124+
125+
}
76126
}

0 commit comments

Comments
 (0)