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

Allow setting default tags to StatsDClientAdapter #5

Merged
merged 5 commits into from
Jul 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"phpunit/phpunit": "^11.2.5",
"friendsofphp/php-cs-fixer": "^3.59",
"league/statsd": "^2.0.0",
"cosmastech/psr-logger-spy": "^0.0.1"
"cosmastech/psr-logger-spy": "^0.0.2"
},
"suggest": {
"datadog/php-datadogstatsd": "For DataDog stats",
Expand Down
34 changes: 34 additions & 0 deletions src/Adapters/Concerns/HasDefaultTagsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;

trait HasDefaultTagsTrait
{
protected array $defaultTags = [];

/**
* @inheritDoc
*/
public function getDefaultTags(): array
{
return $this->defaultTags;
}

/**
* @param array<string, mixed> $tags
* @return void
*/
protected function setDefaultTags(array $defaultTags = []): void
{
$this->defaultTags = $defaultTags;
}

/**
* @param array<string, mixed> $tags
* @return array<string, mixed>
*/
protected function mergeTags(array $tags): array
{
return array_merge($this->getDefaultTags(), $tags);
}
}
56 changes: 47 additions & 9 deletions src/Adapters/Datadog/DatadogStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\Datadog;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\TagNormalizerAware;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
Expand All @@ -10,51 +11,88 @@

class DatadogStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;

public function __construct(protected readonly DogStatsd $datadogClient)
public function __construct(protected readonly DogStatsd $datadogClient, array $defaultTags = [])
{
$this->tagNormalizer = new NoopTagNormalizer();
$this->setDefaultTags($defaultTags);
}

public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
{
$this->datadogClient->timing($stat, $durationMs, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->timing($stat, $durationMs, $sampleRate, $this->normalizeTags($this->mergeTags($tags)));
}

public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->datadogClient->gauge($stat, $value, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->gauge(
$stat,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->datadogClient->histogram($stat, $value, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->histogram(
$stat,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->datadogClient->distribution($stat, $value, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->distribution(
$stat,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->datadogClient->set($stat, $value, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->set(
$stat,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
{
$this->datadogClient->increment($stats, $sampleRate, $this->normalizeTags($tags), $value);
$this->datadogClient->increment(
$stats,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags)),
$value
);
}

public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
{
$this->datadogClient->decrement($stats, $sampleRate, $this->normalizeTags($tags), $value);
$this->datadogClient->decrement(
$stats,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags)),
$value
);
}

public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void
{
$this->datadogClient->updateStats($stats, $delta, $sampleRate, $this->normalizeTags($tags));
$this->datadogClient->updateStats(
$stats,
$delta,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function getClient(): DogStatsd
Expand Down
17 changes: 10 additions & 7 deletions src/Adapters/InMemory/InMemoryClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\InMemory;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\TagNormalizerAware;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryCountRecord;
Expand All @@ -17,17 +18,19 @@

class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;

protected InMemoryStatsRecord $stats;
protected readonly ClockInterface $clock;

public function __construct(ClockInterface $clock)
public function __construct(ClockInterface $clock, array $defaultTags = [])
{
$this->clock = $clock;

$this->reset();
$this->setTagNormalizer(new NoopTagNormalizer());
$this->setDefaultTags($defaultTags);
}

public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
Expand All @@ -36,7 +39,7 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
$stat,
$durationMs,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$this->clock->now()
);
}
Expand All @@ -47,7 +50,7 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
$stat,
$value,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$this->clock->now()
);
}
Expand All @@ -58,7 +61,7 @@ public function histogram(string $stat, float $value, float $sampleRate = 1.0, a
$stat,
$value,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$this->clock->now()
);
}
Expand All @@ -69,7 +72,7 @@ public function distribution(string $stat, float $value, float $sampleRate = 1.0
$stat,
$value,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$this->clock->now()
);
}
Expand All @@ -80,7 +83,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
$stat,
$value,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$this->clock->now()
);
}
Expand Down Expand Up @@ -109,7 +112,7 @@ public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1
$stat,
$delta,
$sampleRate,
$this->normalizeTags($tags),
$this->normalizeTags($this->mergeTags($tags)),
$now
);
}
Expand Down
54 changes: 45 additions & 9 deletions src/Adapters/League/LeagueStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\League;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\TagNormalizerAware;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
Expand All @@ -14,12 +15,15 @@

class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;

public function __construct(
protected readonly LeagueStatsDClientInterface $leagueStatsDClient,
protected readonly SampleRateSendDeciderInterface $sampleRateSendDecider
protected readonly SampleRateSendDeciderInterface $sampleRateSendDecider,
array $defaultTags = [],
) {
$this->setDefaultTags($defaultTags);
}

/**
Expand All @@ -28,12 +32,17 @@ public function __construct(
public static function fromConfig(
array $config,
string $instanceName = 'default',
?SampleRateSendDeciderInterface $sampleRateSendDecider = null
?SampleRateSendDeciderInterface $sampleRateSendDecider = null,
array $defaultTags = []
): static {
$instance = Client::instance($instanceName);
$instance->configure($config);

return new static($instance, $sampleRateSendDecider ?? new SampleRateSendDecider());
return new self(
$instance,
$sampleRateSendDecider ?? new SampleRateSendDecider(),
$defaultTags
);
}

/**
Expand All @@ -45,7 +54,11 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
return;
}

$this->leagueStatsDClient->timing($stat, $durationMs, $this->normalizeTags($tags));
$this->leagueStatsDClient->timing(
$stat,
$durationMs,
$this->normalizeTags($this->mergeTags($tags))
);
}

/**
Expand All @@ -57,7 +70,11 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
return;
}

$this->leagueStatsDClient->gauge($stat, $value, $tags);
$this->leagueStatsDClient->gauge(
$stat,
$value,
$this->normalizeTags($this->mergeTags($tags))
);
}

public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
Expand All @@ -79,31 +96,50 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
return;
}

$this->leagueStatsDClient->set($stat, $value, $tags);
$this->leagueStatsDClient->set(
$stat,
$value,
$this->normalizeTags($this->mergeTags($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);
$this->leagueStatsDClient->increment(
$stats,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($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);
$this->leagueStatsDClient->decrement(
$stats,
$value,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags))
);
}

/**
* @throws ConnectionException
*/
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void
{
$this->increment($stats, $sampleRate, $tags, $delta);
$this->increment(
$stats,
$sampleRate,
$this->normalizeTags($this->mergeTags($tags)),
$delta
);
}

public function getClient(): LeagueStatsDClientInterface
Expand Down
5 changes: 5 additions & 0 deletions src/Adapters/StatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void;

public function getClient(): mixed;

/**
* @return array<string, mixed>
*/
public function getDefaultTags(): array;
}
Loading
Loading