Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include ability to dispatch metrics as events #6

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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 @@ -14,11 +14,15 @@
* league
* datadog
* log_datadog
* event
*/
"channels" => [
"memory" => [
"adapter" => "memory",
],
"event" => [
"adapter" => "event",
],
"league" => [
// see configuration options: https://github.com/thephpleague/statsd?tab=readme-ov-file#configuring
"adapter" => "league",
Expand Down
16 changes: 16 additions & 0 deletions src/AdapterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Carbon\FactoryImmutable;
use Carbon\WrapperClock;
use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingAdapter;
use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
Expand Down Expand Up @@ -180,6 +182,20 @@ protected function createLeagueAdapter(array $config): LeagueStatsDClientAdapter
);
}

/**
* @param array<string, mixed> $config
* @return EventDispatchingAdapter
* @throws BindingResolutionException
*/
protected function createEventAdapter(array $config): EventDispatchingAdapter
{
return new EventDispatchingAdapter(
$this->getDefaultTags(),
new EventDispatchingStatsRecord($this->app->make('events')),
clock: $this->getClockImplementation()
);
}

protected function getClockImplementation(): ClockInterface
{
return new WrapperClock(FactoryImmutable::getDefaultInstance());
Expand Down
9 changes: 9 additions & 0 deletions src/Adapters/EventDispatchingAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Adapters;

use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;

class EventDispatchingAdapter extends InMemoryClientAdapter
{
}
66 changes: 66 additions & 0 deletions src/Adapters/EventDispatchingStatsRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Adapters;

use Cosmastech\LaravelStatsDAdapter\Events\CountRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\DistributionRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\GaugeRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\HistogramRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\SetRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\StatRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Events\TimingRecordedEvent;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryCountRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryDistributionRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryGaugeRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryHistogramRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemorySetRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord;
use Illuminate\Contracts\Events\Dispatcher;

class EventDispatchingStatsRecord extends InMemoryStatsRecord
{
protected readonly Dispatcher $dispatcher;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;

parent::__construct();
}

public function recordTiming(InMemoryTimingRecord $inMemoryTimingRecord): void
{
$this->dispatch(new TimingRecordedEvent($inMemoryTimingRecord));
}

public function recordCount(InMemoryCountRecord $inMemoryCountRecord): void
{
$this->dispatch(new CountRecordedEvent($inMemoryCountRecord));
}

public function recordGauge(InMemoryGaugeRecord $inMemoryGaugeRecord): void
{
$this->dispatch(new GaugeRecordedEvent($inMemoryGaugeRecord));
}

public function recordSet(InMemorySetRecord $inMemorySetRecord): void
{
$this->dispatch(new SetRecordedEvent($inMemorySetRecord));
}

public function recordHistogram(InMemoryHistogramRecord $inMemoryHistogramRecord): void
{
$this->dispatch(new HistogramRecordedEvent($inMemoryHistogramRecord));
}

public function recordDistribution(InMemoryDistributionRecord $inMemoryDistributionRecord): void
{
$this->dispatch(new DistributionRecordedEvent($inMemoryDistributionRecord));
}

protected function dispatch(StatRecordedEvent $statRecordedEvent): void
{
$this->dispatcher->dispatch($statRecordedEvent);
}
}
7 changes: 7 additions & 0 deletions src/Events/CountRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class CountRecordedEvent extends StatRecordedEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/DistributionRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class DistributionRecordedEvent extends StatRecordedEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/GaugeRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class GaugeRecordedEvent extends StatRecordedEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/HistogramRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class HistogramRecordedEvent extends StatRecordedEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/SetRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class SetRecordedEvent extends StatRecordedEvent
{
}
15 changes: 15 additions & 0 deletions src/Events/StatRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\Contracts\RecordInterface;
use Illuminate\Foundation\Events\Dispatchable;

abstract class StatRecordedEvent
{
use Dispatchable;

public function __construct(public readonly RecordInterface $record)
{
}
}
7 changes: 7 additions & 0 deletions src/Events/TimingRecordedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Events;

class TimingRecordedEvent extends StatRecordedEvent
{
}
10 changes: 9 additions & 1 deletion src/StatsDAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
Expand All @@ -25,14 +26,21 @@ public function register(): void
StatsDClientAdapter::class,
fn (Application $app) => $app->make(AdapterManager::class)->instance()
);

$this->app->singleton(
EventDispatchingStatsRecord::class,
function (Application $app): EventDispatchingStatsRecord {
return new EventDispatchingStatsRecord($app->make('events'));
}
);
}

/**
* @return array<int, string|class-string>
*/
public function provides(): array
{
return [AdapterManager::class, StatsDClientAdapter::class];
return [AdapterManager::class, StatsDClientAdapter::class, EventDispatchingStatsRecord::class];
}

protected function offerPublishing(): void
Expand Down
16 changes: 16 additions & 0 deletions tests/AdapterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
Expand Down Expand Up @@ -144,4 +145,19 @@ public function setDefaultTags_passesToNewInstance(): void
// Then
self::assertEquals(["abc" => "123"], $adapterManager->instance("memory")->getDefaultTags());
}

#[Test]
public function instance_event_returnsConfiguredEventDispatchingAdapter(): void
{
// Given
$adapterManager = $this->createAdapterManager();

// And events is configured

// When
$eventDispatchingAdapter = $adapterManager->instance('event');

// Then
self::assertInstanceOf(EventDispatchingAdapter::class, $eventDispatchingAdapter);
}
}
62 changes: 62 additions & 0 deletions tests/Adapters/EventDispatchingStatsRecordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Tests\Adapters;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\LaravelStatsDAdapter\Events\TimingRecordedEvent;
use Cosmastech\LaravelStatsDAdapter\Tests\AbstractTestCase;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Testing\Fakes\EventFake;
use PHPUnit\Framework\Attributes\Test;

class EventDispatchingStatsRecordTest extends AbstractTestCase
{
private EventFake $dispatcher;
private EventDispatchingStatsRecord $eventDispatchingStatsRecord;

protected function setUp(): void
{
parent::setUp();

$this->dispatcher = Event::fake();

$this->eventDispatchingStatsRecord = new EventDispatchingStatsRecord($this->dispatcher);
}

#[Test]
public function time_dispatchesTimingRecordedEvent(): void
{
// Given
$inMemoryClient = new InMemoryClientAdapter([], $this->eventDispatchingStatsRecord);

// And
$closure = fn () => ["hello" => "world"];

// When
$output = $inMemoryClient->time($closure, "my-timing-stat", 0.92, [
"my-tag" => "my-value",
]);

// Then output should be returned from the client
self::assertEquals(["hello" => "world"], $output);

// And an event should be dispatched
/** @var Collection<int, array<int, TimingRecordedEvent>> $eventsCollection */
$eventsCollection = $this->dispatcher->dispatched(TimingRecordedEvent::class);
self::assertCount(1, $eventsCollection);
self::assertCount(1, $eventsCollection->first());

/** @var TimingRecordedEvent $event */
$event = $eventsCollection->first()[0];

// And the record should have expected properties
$record = $event->record;
self::assertInstanceOf(InMemoryTimingRecord::class, $record);
self::assertEquals("my-timing-stat", $record->stat);
self::assertEquals(0.92, $record->sampleRate);
self::assertEquals(["my-tag" => "my-value"], $record->tags);
}
}
14 changes: 14 additions & 0 deletions tests/StatsDAdapterServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
Expand Down Expand Up @@ -59,4 +60,17 @@ public function passesDefaultTagsToAdapterManager(): void
// Then
self::assertEqualsCanonicalizing($defaultTags, $clientAdapter->getDefaultTags());
}

#[Test]
public function eventDispatchingStatsRecord_isSingleton(): void
{
// Given
$firstRecord = $this->app->make(EventDispatchingStatsRecord::class);

// When
$secondRecord = $this->app->make(EventDispatchingStatsRecord::class);

// Then
self::assertSame($firstRecord, $secondRecord);
}
}
Loading