Skip to content

Commit 23291c7

Browse files
authored
Add time() method; limit access to InMemoryStatsRecord arrays (#9)
* add timing * add clarifying docblock * add ClockInterface * rename merge method; clean up LeagueStatsDClientAdapter constructor * in memory clean up * clarifying docblock * clean up datadog adapter constructor * datadoglogging improvements * add CoversClass attribute * add `provide` * changes to in-memory for extensibility * count + timing * gauge * parametrized test * set * histogram * style * quality of life
1 parent a016bb7 commit 23291c7

23 files changed

+697
-143
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"league/statsd": "For generic statsd clients",
2929
"monolog/monolog": "For psr log implementation"
3030
},
31+
"provide": {
32+
"psr/clock-implementation": "3.0.0"
33+
},
3134
"autoload": {
3235
"psr-4": {
3336
"Cosmastech\\StatsDClientAdapter\\": "src"

examples/in_memory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function makeApiRequest()
2424
/** @var \Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord $stats */
2525
$stats = $inMemoryAdapter->getStats();
2626

27-
var_dump($stats->timing[0]);
27+
var_dump($stats->getTimings()[0]);
2828
/*
2929
object(Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord)#7 (5) {
3030
["stat"]=>

src/Adapters/Concerns/HasDefaultTagsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setDefaultTags(array $defaultTags = []): void
2929
* @param array<mixed, mixed> $tags
3030
* @return array<mixed, mixed>
3131
*/
32-
protected function mergeTags(array $tags): array
32+
protected function mergeWithDefaultTags(array $tags): array
3333
{
3434
return array_merge($this->getDefaultTags(), $tags);
3535
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;
4+
5+
trait TimeClosureTrait
6+
{
7+
/**
8+
* @inheritDoc
9+
*/
10+
public function time(string $stat, callable $closure, float $sampleRate = 1.0, array $tags = [])
11+
{
12+
$startTime = intval($this->clock->now()->format("Uv"));
13+
14+
$result = $closure();
15+
16+
$this->timing(
17+
$stat,
18+
(intval($this->clock->now()->format("Uv")) - $startTime),
19+
$sampleRate,
20+
$tags
21+
);
22+
23+
return $result;
24+
}
25+
}

src/Adapters/Datadog/DatadogStatsDClientAdapter.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,150 @@
44

55
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
66
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
7+
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TimeClosureTrait;
78
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\TagNormalizerAware;
89
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
910
use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
11+
use Cosmastech\StatsDClientAdapter\TagNormalizers\TagNormalizer;
12+
use Cosmastech\StatsDClientAdapter\Utility\Clock;
1013
use DataDog\DogStatsd;
14+
use Psr\Clock\ClockInterface;
1115

1216
class DatadogStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
1317
{
1418
use HasDefaultTagsTrait;
1519
use TagNormalizerAwareTrait;
20+
use TimeClosureTrait;
21+
22+
protected readonly DogStatsd $datadogClient;
23+
24+
protected readonly ClockInterface $clock;
1625

1726
/**
1827
* @param DogStatsd $datadogClient
1928
* @param array<mixed, mixed> $defaultTags
29+
* @param TagNormalizer $tagNormalizer
30+
* @param ClockInterface $clock
2031
*/
21-
public function __construct(protected readonly DogStatsd $datadogClient, array $defaultTags = [])
22-
{
23-
$this->tagNormalizer = new NoopTagNormalizer();
32+
public function __construct(
33+
DogStatsd $datadogClient,
34+
array $defaultTags = [],
35+
TagNormalizer $tagNormalizer = new NoopTagNormalizer(),
36+
ClockInterface $clock = new Clock(),
37+
) {
38+
$this->datadogClient = $datadogClient;
2439
$this->setDefaultTags($defaultTags);
40+
$this->setTagNormalizer($tagNormalizer);
41+
$this->clock = $clock;
2542
}
2643

44+
/**
45+
* @inheritDoc
46+
*/
2747
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
2848
{
2949
$this->datadogClient->timing(
3050
$stat,
3151
$durationMs,
3252
$sampleRate,
33-
$this->normalizeTags($this->mergeTags($tags))
53+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
3454
);
3555
}
3656

57+
/**
58+
* @inheritDoc
59+
*/
3760
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
3861
{
3962
$this->datadogClient->gauge(
4063
$stat,
4164
$value,
4265
$sampleRate,
43-
$this->normalizeTags($this->mergeTags($tags))
66+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
4467
);
4568
}
4669

70+
/**
71+
* @inheritDoc
72+
*/
4773
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
4874
{
4975
$this->datadogClient->histogram(
5076
$stat,
5177
$value,
5278
$sampleRate,
53-
$this->normalizeTags($this->mergeTags($tags))
79+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
5480
);
5581
}
5682

83+
/**
84+
* @inheritDoc
85+
*/
5786
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
5887
{
5988
$this->datadogClient->distribution(
6089
$stat,
6190
$value,
6291
$sampleRate,
63-
$this->normalizeTags($this->mergeTags($tags))
92+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
6493
);
6594
}
6695

96+
/**
97+
* @inheritDoc
98+
*/
6799
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
68100
{
69101
$this->datadogClient->set(
70102
$stat,
71103
$value,
72104
$sampleRate,
73-
$this->normalizeTags($this->mergeTags($tags))
105+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
74106
);
75107
}
76108

109+
/**
110+
* @inheritDoc
111+
*/
77112
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
78113
{
79114
$this->datadogClient->increment(
80115
$stats,
81116
$sampleRate,
82-
$this->normalizeTags($this->mergeTags($tags)),
117+
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
83118
$value
84119
);
85120
}
86121

122+
/**
123+
* @inheritDoc
124+
*/
87125
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
88126
{
89127
$this->datadogClient->decrement(
90128
$stats,
91129
$sampleRate,
92-
$this->normalizeTags($this->mergeTags($tags)),
130+
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
93131
$value
94132
);
95133
}
96134

135+
/**
136+
* @inheritDoc
137+
*/
97138
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
98139
{
99140
$this->datadogClient->updateStats(
100141
$stats,
101142
$delta,
102143
$sampleRate,
103-
$this->normalizeTags($this->mergeTags($tags))
144+
$this->normalizeTags($this->mergeWithDefaultTags($tags))
104145
);
105146
}
106147

148+
/**
149+
* @inheritDoc
150+
*/
107151
public function getClient(): DogStatsd
108152
{
109153
return $this->datadogClient;

0 commit comments

Comments
 (0)