|
4 | 4 |
|
5 | 5 | use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
|
6 | 6 | use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
|
| 7 | +use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TimeClosureTrait; |
7 | 8 | use Cosmastech\StatsDClientAdapter\Adapters\Contracts\TagNormalizerAware;
|
8 | 9 | use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
|
9 | 10 | use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
|
| 11 | +use Cosmastech\StatsDClientAdapter\TagNormalizers\TagNormalizer; |
| 12 | +use Cosmastech\StatsDClientAdapter\Utility\Clock; |
10 | 13 | use DataDog\DogStatsd;
|
| 14 | +use Psr\Clock\ClockInterface; |
11 | 15 |
|
12 | 16 | class DatadogStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
|
13 | 17 | {
|
14 | 18 | use HasDefaultTagsTrait;
|
15 | 19 | use TagNormalizerAwareTrait;
|
| 20 | + use TimeClosureTrait; |
| 21 | + |
| 22 | + protected readonly DogStatsd $datadogClient; |
| 23 | + |
| 24 | + protected readonly ClockInterface $clock; |
16 | 25 |
|
17 | 26 | /**
|
18 | 27 | * @param DogStatsd $datadogClient
|
19 | 28 | * @param array<mixed, mixed> $defaultTags
|
| 29 | + * @param TagNormalizer $tagNormalizer |
| 30 | + * @param ClockInterface $clock |
20 | 31 | */
|
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; |
24 | 39 | $this->setDefaultTags($defaultTags);
|
| 40 | + $this->setTagNormalizer($tagNormalizer); |
| 41 | + $this->clock = $clock; |
25 | 42 | }
|
26 | 43 |
|
| 44 | + /** |
| 45 | + * @inheritDoc |
| 46 | + */ |
27 | 47 | public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
|
28 | 48 | {
|
29 | 49 | $this->datadogClient->timing(
|
30 | 50 | $stat,
|
31 | 51 | $durationMs,
|
32 | 52 | $sampleRate,
|
33 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 53 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
34 | 54 | );
|
35 | 55 | }
|
36 | 56 |
|
| 57 | + /** |
| 58 | + * @inheritDoc |
| 59 | + */ |
37 | 60 | public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
|
38 | 61 | {
|
39 | 62 | $this->datadogClient->gauge(
|
40 | 63 | $stat,
|
41 | 64 | $value,
|
42 | 65 | $sampleRate,
|
43 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 66 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
44 | 67 | );
|
45 | 68 | }
|
46 | 69 |
|
| 70 | + /** |
| 71 | + * @inheritDoc |
| 72 | + */ |
47 | 73 | public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
|
48 | 74 | {
|
49 | 75 | $this->datadogClient->histogram(
|
50 | 76 | $stat,
|
51 | 77 | $value,
|
52 | 78 | $sampleRate,
|
53 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 79 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
54 | 80 | );
|
55 | 81 | }
|
56 | 82 |
|
| 83 | + /** |
| 84 | + * @inheritDoc |
| 85 | + */ |
57 | 86 | public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
|
58 | 87 | {
|
59 | 88 | $this->datadogClient->distribution(
|
60 | 89 | $stat,
|
61 | 90 | $value,
|
62 | 91 | $sampleRate,
|
63 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 92 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
64 | 93 | );
|
65 | 94 | }
|
66 | 95 |
|
| 96 | + /** |
| 97 | + * @inheritDoc |
| 98 | + */ |
67 | 99 | public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
|
68 | 100 | {
|
69 | 101 | $this->datadogClient->set(
|
70 | 102 | $stat,
|
71 | 103 | $value,
|
72 | 104 | $sampleRate,
|
73 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 105 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
74 | 106 | );
|
75 | 107 | }
|
76 | 108 |
|
| 109 | + /** |
| 110 | + * @inheritDoc |
| 111 | + */ |
77 | 112 | public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
|
78 | 113 | {
|
79 | 114 | $this->datadogClient->increment(
|
80 | 115 | $stats,
|
81 | 116 | $sampleRate,
|
82 |
| - $this->normalizeTags($this->mergeTags($tags)), |
| 117 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)), |
83 | 118 | $value
|
84 | 119 | );
|
85 | 120 | }
|
86 | 121 |
|
| 122 | + /** |
| 123 | + * @inheritDoc |
| 124 | + */ |
87 | 125 | public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
|
88 | 126 | {
|
89 | 127 | $this->datadogClient->decrement(
|
90 | 128 | $stats,
|
91 | 129 | $sampleRate,
|
92 |
| - $this->normalizeTags($this->mergeTags($tags)), |
| 130 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)), |
93 | 131 | $value
|
94 | 132 | );
|
95 | 133 | }
|
96 | 134 |
|
| 135 | + /** |
| 136 | + * @inheritDoc |
| 137 | + */ |
97 | 138 | public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
|
98 | 139 | {
|
99 | 140 | $this->datadogClient->updateStats(
|
100 | 141 | $stats,
|
101 | 142 | $delta,
|
102 | 143 | $sampleRate,
|
103 |
| - $this->normalizeTags($this->mergeTags($tags)) |
| 144 | + $this->normalizeTags($this->mergeWithDefaultTags($tags)) |
104 | 145 | );
|
105 | 146 | }
|
106 | 147 |
|
| 148 | + /** |
| 149 | + * @inheritDoc |
| 150 | + */ |
107 | 151 | public function getClient(): DogStatsd
|
108 | 152 | {
|
109 | 153 | return $this->datadogClient;
|
|
0 commit comments