Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/statsd-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
'throwConnectionExceptions' => true,
],
"datadog" => [
/**
* By setting batch_size to a positive integer, the batched dogstatsd provider will be used.
*/
'batch_size' => null,
// see configuration options: https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php&tab=hostagent#client-instantiation-parameters
"adapter" => "datadog",
"host" => env("DD_AGENT_HOST"),
Expand Down
11 changes: 10 additions & 1 deletion src/AdapterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
use DataDog\BatchedDogStatsd;
use DataDog\DogStatsd;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\MultipleInstanceManager;
Expand Down Expand Up @@ -158,8 +159,16 @@ protected function createLogDatadogAdapter(array $config): DatadogStatsDClientAd
*/
protected function createDatadogAdapter(array $config): DatadogStatsDClientAdapter
{
$batchSize = (int) ($config['batch_size'] ?? null);
if ($batchSize > 0) {
$client = new BatchedDogStatsd($config);
BatchedDogStatsd::$maxBufferLength = $batchSize;
} else {
$client = new DogStatsd($config);
}

return new DatadogStatsDClientAdapter(
new DogStatsd($config),
$client,
$this->getDefaultTags(),
clock: $this->getClockImplementation()
);
Expand Down
15 changes: 15 additions & 0 deletions src/StatsDAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use DataDog\BatchedDogStatsd;
use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
Expand All @@ -13,6 +15,19 @@ class StatsDAdapterServiceProvider extends ServiceProvider implements Deferrable
public function boot(): void
{
$this->offerPublishing();
$this->app->terminating(static function () {
$adapterInstance = Container::getInstance()
->make(AdapterManager::class)
->instance()
->getClient();

if (
BatchedDogStatsd::getBufferLength() > 0
&& $adapterInstance instanceof BatchedDogStatsd
) {
$adapterInstance->flushBuffer();
}
});
}

public function register(): void
Expand Down
50 changes: 50 additions & 0 deletions tests/StatsDAdapterServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
use DataDog\BatchedDogStatsd;
use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\Attributes\Test;

class StatsDAdapterServiceProviderTest extends AbstractTestCase
{
private int $initialBufferSize;

#[\Override]
protected function setUp(): void
{
parent::setUp();
$this->initialBufferSize = BatchedDogStatsd::$maxBufferLength;
}

#[\Override]
protected function tearDown(): void
{
BatchedDogStatsd::$maxBufferLength = $this->initialBufferSize;
parent::tearDown();
}

#[Test]
public function makeStatsDClientAdapter_returnsDefaultInstance(): void
{
Expand Down Expand Up @@ -73,4 +90,37 @@ public function inMemoryStatsRecord_isSingleton(): void
// Then
self::assertSame($firstRecord, $secondRecord);
}

#[Test]
public function datadogConfigHasBatchSize_resolve_setsBufferSize(): void
{
// Given
Config::set('statsd-adapter.channels.datadog.batch_size', 2);
Config::set('statsd-adapter.default', 'datadog');

// When
$clientAdapter = $this->app->make(StatsDClientAdapter::class);

// Then
self::assertInstanceOf(BatchedDogStatsd::class, $clientAdapter->getClient());
self::assertEquals(2, BatchedDogStatsd::$maxBufferLength);
}

#[Test]
public function batchedDogStats_terminatingApp_submitsStats(): void
{
// Given
Config::set('statsd-adapter.channels.datadog.batch_size', 2);
Config::set('statsd-adapter.default', 'datadog');

// And
$clientAdapter = $this->app->make(StatsDClientAdapter::class);
$clientAdapter->increment('testing');

// When
$this->app->terminate();

// Then

}
}
Loading