-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Cosmastech\StatsDClient\Clients\League; | ||
|
||
use Cosmastech\StatsDClient\Clients\Concerns\TagNormalizerAwareTrait; | ||
use Cosmastech\StatsDClient\Clients\Contracts\TagNormalizerAware; | ||
use Cosmastech\StatsDClient\Clients\StatsDClient; | ||
use Cosmastech\StatsDClient\Utility\SampleRateDecider\Contracts\SampleRateSendDecider as SampleRateSendDeciderInterface; | ||
use Cosmastech\StatsDClient\Utility\SampleRateDecider\SampleRateSendDecider; | ||
use League\StatsD\Client; | ||
use League\StatsD\Exception\ConfigurationException; | ||
use League\StatsD\Exception\ConnectionException; | ||
use League\StatsD\StatsDClient as LeagueStatsDClientInterface; | ||
|
||
class LeagueStatsDClient implements StatsDClient, TagNormalizerAware | ||
{ | ||
use TagNormalizerAwareTrait; | ||
|
||
public function __construct( | ||
protected readonly LeagueStatsDClientInterface $leagueStatsDClient, | ||
protected readonly SampleRateSendDeciderInterface $sampleRateSendDecider | ||
) { | ||
} | ||
|
||
/** | ||
* @throws ConfigurationException | ||
*/ | ||
public static function fromConfig( | ||
array $config, | ||
string $instanceName = 'default', | ||
?SampleRateSendDeciderInterface $sampleRateSendDecider = null | ||
): static { | ||
$instance = Client::instance($instanceName); | ||
$instance->configure($config); | ||
|
||
return new static($instance, $sampleRateSendDecider ?? new SampleRateSendDecider()); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void | ||
{ | ||
if (! $this->sampleRateSendDecider->decide($sampleRate)) { | ||
return; | ||
} | ||
|
||
$this->leagueStatsDClient->timing($stat, $durationMs, $this->normalizeTags($tags)); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void | ||
{ | ||
if (! $this->sampleRateSendDecider->decide($sampleRate)) { | ||
return; | ||
} | ||
|
||
$this->leagueStatsDClient->gauge($stat, $value, $tags); | ||
} | ||
|
||
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void | ||
{ | ||
trigger_error("histogram is not implemented for this client"); | ||
} | ||
|
||
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void | ||
{ | ||
trigger_error("distribution is not implemented for this client"); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void | ||
{ | ||
if (! $this->sampleRateSendDecider->decide($sampleRate)) { | ||
return; | ||
} | ||
|
||
$this->leagueStatsDClient->set($stat, $value, $tags); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void | ||
{ | ||
$this->leagueStatsDClient->increment($stats, $value, $sampleRate, $tags); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void | ||
{ | ||
$this->leagueStatsDClient->decrement($stats, $value, $sampleRate, $tags); | ||
} | ||
|
||
/** | ||
* @throws ConnectionException | ||
*/ | ||
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void | ||
{ | ||
$this->increment($stats, $sampleRate, $tags, $delta); | ||
} | ||
|
||
public function getClient(): LeagueStatsDClientInterface | ||
{ | ||
return $this->leagueStatsDClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Utility/SampleRateDecider/Contracts/SampleRateSendDecider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Cosmastech\StatsDClient\Utility\SampleRateDecider\Contracts; | ||
|
||
interface SampleRateSendDecider | ||
{ | ||
public function decide(float $sampleRate): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Cosmastech\StatsDClient\Utility\SampleRateDecider; | ||
|
||
use Cosmastech\StatsDClient\Utility\SampleRateDecider\Contracts\SampleRateSendDecider as SampleRateSendDeciderInterface; | ||
|
||
class SampleRateSendDecider implements SampleRateSendDeciderInterface | ||
{ | ||
public function decide(float $sampleRate): bool | ||
{ | ||
if ($sampleRate >= 1) { | ||
return true; | ||
} | ||
|
||
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Cosmastech\StatsDClient\Tests\Clients\League; | ||
|
||
use Cosmastech\StatsDClient\Clients\League\LeagueStatsDClient; | ||
use Cosmastech\StatsDClient\Tests\BaseTestCase; | ||
use League\StatsD\StatsDClient; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class LeagueStatsDClientTest extends BaseTestCase | ||
{ | ||
#[Test] | ||
public function getClient_returnsLeagueStatsDClient() | ||
{ | ||
// Given | ||
$leagueStatsDClient = LeagueStatsDClient::fromConfig([]); | ||
|
||
// When | ||
$client = $leagueStatsDClient->getClient(); | ||
|
||
// Then | ||
self::assertInstanceOf(StatsDClient::class, $client); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
tests/Utility/SampleRateDecider/SampleRateSendDeciderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Cosmastech\StatsDClient\Tests\Utility\SampleRateDecider; | ||
|
||
use Cosmastech\StatsDClient\Tests\BaseTestCase; | ||
use Cosmastech\StatsDClient\Utility\SampleRateDecider\SampleRateSendDecider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class SampleRateSendDeciderTest extends BaseTestCase | ||
{ | ||
#[Test] | ||
public function decide_sampleRateEquals1_returnsTrue() | ||
{ | ||
// Given | ||
$sampleRate = 1; | ||
|
||
// When | ||
$actual = (new SampleRateSendDecider())->decide($sampleRate); | ||
|
||
// Then | ||
self::assertTrue($actual); | ||
} | ||
|
||
#[Test] | ||
public function decide_sampleRateAbove1_returnsTrue() | ||
{ | ||
// Given | ||
$sampleRate = 1.01; | ||
|
||
// When | ||
$actual = (new SampleRateSendDecider())->decide($sampleRate); | ||
|
||
// Then | ||
self::assertTrue($actual); | ||
} | ||
|
||
#[Test] | ||
public function decide_sampleRateEquals0_returnsFalse() | ||
{ | ||
// Given | ||
$sampleRate = 0.0; | ||
|
||
// When | ||
$actual = (new SampleRateSendDecider())->decide($sampleRate); | ||
|
||
// Then | ||
self::assertFalse($actual); | ||
} | ||
|
||
#[Test] | ||
public function decide_sampleRateBelow0_returnsFalse() | ||
{ | ||
// Given | ||
$sampleRate = -110.0; | ||
|
||
// When | ||
$actual = (new SampleRateSendDecider())->decide($sampleRate); | ||
|
||
// Then | ||
self::assertFalse($actual); | ||
} | ||
} |