|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Okvpn\Bundle\DatadogBundle\Client; |
| 6 | + |
| 7 | +final class MockClient implements DogStatsInterface |
| 8 | +{ |
| 9 | + private $data = []; |
| 10 | + |
| 11 | + public function increment(string $metrics, int $delta = 1, float $sampleRate = 1.0, array $tags = []) |
| 12 | + { |
| 13 | + $this->data['increments'][] = [ |
| 14 | + 'metric' => $metrics, |
| 15 | + 'delta' => $delta, |
| 16 | + 'sampleRate' => $sampleRate, |
| 17 | + 'tags' => $tags, |
| 18 | + ]; |
| 19 | + |
| 20 | + return $this; |
| 21 | + } |
| 22 | + |
| 23 | + public function getIncrements(string $metric = null): array |
| 24 | + { |
| 25 | + return $this->get('increments', $metric); |
| 26 | + } |
| 27 | + |
| 28 | + public function decrement(string $metric, int $delta = 1, float $sampleRate = 1.0, array $tags = []) |
| 29 | + { |
| 30 | + $this->data['decrements'][] = [ |
| 31 | + 'metric' => $metric, |
| 32 | + 'delta' => $delta, |
| 33 | + 'sampleRate' => $sampleRate, |
| 34 | + 'tags' => $tags, |
| 35 | + ]; |
| 36 | + |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function getDecrements(string $metric = null): array |
| 41 | + { |
| 42 | + return $this->get('decrements', $metric); |
| 43 | + } |
| 44 | + |
| 45 | + public function timing(string $metric, float $time, array $tags = []) |
| 46 | + { |
| 47 | + $this->data['timings'][] = [ |
| 48 | + 'metric' => $metric, |
| 49 | + 'time' => $time, |
| 50 | + 'tags' => $tags, |
| 51 | + ]; |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + public function getTimings(string $metric = null): array |
| 57 | + { |
| 58 | + return $this->get('timings', $metric); |
| 59 | + } |
| 60 | + |
| 61 | + public function time(string $metric, callable $func, array $tags = []) |
| 62 | + { |
| 63 | + $timerStart = microtime(true); |
| 64 | + $func(); |
| 65 | + $timerEnd = microtime(true); |
| 66 | + $time = round(($timerEnd - $timerStart) * 1000, 4); |
| 67 | + |
| 68 | + return $this->timing($metric, $time, $tags); |
| 69 | + } |
| 70 | + |
| 71 | + public function gauge(string $metric, int $value, array $tags = []) |
| 72 | + { |
| 73 | + $this->data['gauges'][] = [ |
| 74 | + 'metric' => $metric, |
| 75 | + 'value' => $value, |
| 76 | + 'tags' => $tags, |
| 77 | + ]; |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + public function getGauges(string $metric = null): array |
| 83 | + { |
| 84 | + return $this->get('gauges', $metric); |
| 85 | + } |
| 86 | + |
| 87 | + public function histogram(string $metric, float $value, float $sampleRate = 1.0, array $tags = []) |
| 88 | + { |
| 89 | + $this->data['histograms'][] = [ |
| 90 | + 'metric' => $metric, |
| 91 | + 'value' => $value, |
| 92 | + 'sampleRate' => $sampleRate, |
| 93 | + 'tags' => $tags, |
| 94 | + ]; |
| 95 | + |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + public function getHistograms(string $metric = null): array |
| 100 | + { |
| 101 | + return $this->get('histograms', $metric); |
| 102 | + } |
| 103 | + |
| 104 | + public function set(string $metric, int $value, array $tags = []) |
| 105 | + { |
| 106 | + $this->data['sets'][] = [ |
| 107 | + 'metric' => $metric, |
| 108 | + 'value' => $value, |
| 109 | + 'tags' => $tags, |
| 110 | + ]; |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + public function getSets(string $metric = null): array |
| 116 | + { |
| 117 | + return $this->get('sets', $metric); |
| 118 | + } |
| 119 | + |
| 120 | + public function event(string $title, string $text, array $metadata = [], array $tags = []) |
| 121 | + { |
| 122 | + $this->data['events'][] = [ |
| 123 | + 'title' => $title, |
| 124 | + 'text' => $text, |
| 125 | + 'metadata' => $metadata, |
| 126 | + 'tags' => $tags, |
| 127 | + ]; |
| 128 | + |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + public function getEvents(string $title = null): array |
| 133 | + { |
| 134 | + return $this->get('events', $title); |
| 135 | + } |
| 136 | + |
| 137 | + public function serviceCheck(string $name, int $status, array $metadata = [], array $tags = []) |
| 138 | + { |
| 139 | + $this->data['serviceChecks'][] = [ |
| 140 | + 'name' => $name, |
| 141 | + 'status' => $status, |
| 142 | + 'metadata' => $metadata, |
| 143 | + 'tags' => $tags, |
| 144 | + ]; |
| 145 | + |
| 146 | + return $this; |
| 147 | + } |
| 148 | + |
| 149 | + public function getServiceChecks(string $name = null): array |
| 150 | + { |
| 151 | + return $this->get('serviceChecks', $name); |
| 152 | + } |
| 153 | + |
| 154 | + public function getOptions(): array |
| 155 | + { |
| 156 | + return []; |
| 157 | + } |
| 158 | + |
| 159 | + public function getOption(string $name, $default = null) |
| 160 | + { |
| 161 | + return $default; |
| 162 | + } |
| 163 | + |
| 164 | + private function get(string $type, string $metric = null): array |
| 165 | + { |
| 166 | + if ($metric === null) { |
| 167 | + return $this->data[$type]; |
| 168 | + } |
| 169 | + |
| 170 | + if ($type === 'events') { |
| 171 | + $key = 'title'; |
| 172 | + } elseif ($type === 'serviceChecks') { |
| 173 | + $key = 'name'; |
| 174 | + } else { |
| 175 | + $key = 'metric'; |
| 176 | + } |
| 177 | + |
| 178 | + return array_filter($this->data[$type], static function (array $data) use ($key, $metric) { |
| 179 | + return $data[$key] === $metric; |
| 180 | + }); |
| 181 | + } |
| 182 | +} |
0 commit comments