|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Monitoring; |
| 6 | + |
| 7 | +use DataDog\BatchedDogStatsd; |
| 8 | +use DataDog\DogStatsd; |
| 9 | +use Enqueue\Client\Config; |
| 10 | +use Enqueue\Dsn\Dsn; |
| 11 | + |
| 12 | +class DatadogStorage implements StatsStorage |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + private $config; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var BatchedDogStatsd |
| 21 | + */ |
| 22 | + private $datadog; |
| 23 | + |
| 24 | + public function __construct($config = 'datadog:') |
| 25 | + { |
| 26 | + if (false === class_exists(DogStatsd::class)) { |
| 27 | + throw new \LogicException('Seems client library is not installed. Please install "datadog/php-datadogstatsd"'); |
| 28 | + } |
| 29 | + |
| 30 | + $this->config = $this->prepareConfig($config); |
| 31 | + |
| 32 | + if (null === $this->datadog) { |
| 33 | + if (true === filter_var($this->config['batched'], FILTER_VALIDATE_BOOLEAN)) { |
| 34 | + $this->datadog = new BatchedDogStatsd($this->config); |
| 35 | + } else { |
| 36 | + $this->datadog = new DogStatsd($this->config); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function pushConsumerStats(ConsumerStats $stats): void |
| 42 | + { |
| 43 | + $queues = $stats->getQueues(); |
| 44 | + array_walk($queues, function (string $queue) use ($stats) { |
| 45 | + $tags = [ |
| 46 | + 'queue' => $queue, |
| 47 | + 'consumerId' => $stats->getConsumerId(), |
| 48 | + ]; |
| 49 | + |
| 50 | + if ($stats->getFinishedAtMs()) { |
| 51 | + $values['finishedAtMs'] = $stats->getFinishedAtMs(); |
| 52 | + } |
| 53 | + |
| 54 | + $this->datadog->gauge($this->config['metric.consumers.started'], (int) $stats->isStarted(), 1, $tags); |
| 55 | + $this->datadog->gauge($this->config['metric.consumers.finished'], (int) $stats->isFinished(), 1, $tags); |
| 56 | + $this->datadog->gauge($this->config['metric.consumers.failed'], (int) $stats->isFailed(), 1, $tags); |
| 57 | + $this->datadog->gauge($this->config['metric.consumers.received'], $stats->getReceived(), 1, $tags); |
| 58 | + $this->datadog->gauge($this->config['metric.consumers.acknowledged'], $stats->getAcknowledged(), 1, $tags); |
| 59 | + $this->datadog->gauge($this->config['metric.consumers.rejected'], $stats->getRejected(), 1, $tags); |
| 60 | + $this->datadog->gauge($this->config['metric.consumers.requeued'], $stats->getRejected(), 1, $tags); |
| 61 | + $this->datadog->gauge($this->config['metric.consumers.memoryUsage'], $stats->getMemoryUsage(), 1, $tags); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + public function pushSentMessageStats(SentMessageStats $stats): void |
| 66 | + { |
| 67 | + $tags = [ |
| 68 | + 'destination' => $stats->getDestination(), |
| 69 | + ]; |
| 70 | + |
| 71 | + $properties = $stats->getProperties(); |
| 72 | + if (false === empty($properties[Config::TOPIC])) { |
| 73 | + $tags['topic'] = $properties[Config::TOPIC]; |
| 74 | + } |
| 75 | + |
| 76 | + if (false === empty($properties[Config::COMMAND])) { |
| 77 | + $tags['command'] = $properties[Config::COMMAND]; |
| 78 | + } |
| 79 | + |
| 80 | + $this->datadog->increment($this->config['metric.messages.sent'], 1, $tags); |
| 81 | + } |
| 82 | + |
| 83 | + public function pushConsumedMessageStats(ConsumedMessageStats $stats): void |
| 84 | + { |
| 85 | + $tags = [ |
| 86 | + 'queue' => $stats->getQueue(), |
| 87 | + 'status' => $stats->getStatus(), |
| 88 | + ]; |
| 89 | + |
| 90 | + if (ConsumedMessageStats::STATUS_FAILED === $stats->getStatus()) { |
| 91 | + $this->datadog->increment($this->config['metric.messages.failed'], 1, $tags); |
| 92 | + } |
| 93 | + |
| 94 | + if ($stats->isRedelivered()) { |
| 95 | + $this->datadog->increment($this->config['metric.messages.redelivered'], 1, $tags); |
| 96 | + } |
| 97 | + |
| 98 | + $runtime = $stats->getTimestampMs() - $stats->getReceivedAtMs(); |
| 99 | + $this->datadog->histogram($this->config['metric.messages.consumed'], $runtime, 1, $tags); |
| 100 | + } |
| 101 | + |
| 102 | + private function parseDsn(string $dsn): array |
| 103 | + { |
| 104 | + $dsn = Dsn::parseFirst($dsn); |
| 105 | + |
| 106 | + if ('datadog' !== $dsn->getSchemeProtocol()) { |
| 107 | + throw new \LogicException(sprintf( |
| 108 | + 'The given scheme protocol "%s" is not supported. It must be "datadog"', |
| 109 | + $dsn->getSchemeProtocol() |
| 110 | + )); |
| 111 | + } |
| 112 | + |
| 113 | + return array_filter(array_replace($dsn->getQuery(), [ |
| 114 | + 'host' => $dsn->getHost(), |
| 115 | + 'port' => $dsn->getPort(), |
| 116 | + 'global_tags' => $dsn->getString('global_tags'), |
| 117 | + 'batched' => $dsn->getString('batched'), |
| 118 | + 'metric.messages.sent' => $dsn->getString('metric.messages.sent'), |
| 119 | + 'metric.messages.consumed' => $dsn->getString('metric.messages.consumed'), |
| 120 | + 'metric.messages.redelivered' => $dsn->getString('metric.messages.redelivered'), |
| 121 | + 'metric.messages.failed' => $dsn->getString('metric.messages.failed'), |
| 122 | + 'metric.consumers.started' => $dsn->getString('metric.consumers.started'), |
| 123 | + 'metric.consumers.finished' => $dsn->getString('metric.consumers.finished'), |
| 124 | + 'metric.consumers.failed' => $dsn->getString('metric.consumers.failed'), |
| 125 | + 'metric.consumers.received' => $dsn->getString('metric.consumers.received'), |
| 126 | + 'metric.consumers.acknowledged' => $dsn->getString('metric.consumers.acknowledged'), |
| 127 | + 'metric.consumers.rejected' => $dsn->getString('metric.consumers.rejected'), |
| 128 | + 'metric.consumers.requeued' => $dsn->getString('metric.consumers.requeued'), |
| 129 | + 'metric.consumers.memoryUsage' => $dsn->getString('metric.consumers.memoryUsage'), |
| 130 | + ]), function ($value) { |
| 131 | + return null !== $value; |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param $config |
| 137 | + * |
| 138 | + * @return array |
| 139 | + */ |
| 140 | + private function prepareConfig($config): array |
| 141 | + { |
| 142 | + if (empty($config)) { |
| 143 | + $config = $this->parseDsn('datadog:'); |
| 144 | + } elseif (\is_string($config)) { |
| 145 | + $config = $this->parseDsn($config); |
| 146 | + } elseif (\is_array($config)) { |
| 147 | + $config = empty($config['dsn']) ? $config : $this->parseDsn($config['dsn']); |
| 148 | + } elseif ($config instanceof DogStatsd) { |
| 149 | + $this->datadog = $config; |
| 150 | + $config = []; |
| 151 | + } else { |
| 152 | + throw new \LogicException('The config must be either an array of options, a DSN string or null'); |
| 153 | + } |
| 154 | + |
| 155 | + return array_replace([ |
| 156 | + 'host' => 'localhost', |
| 157 | + 'port' => 8125, |
| 158 | + 'batched' => true, |
| 159 | + 'metric.messages.sent' => 'enqueue.messages.sent', |
| 160 | + 'metric.messages.consumed' => 'enqueue.messages.consumed', |
| 161 | + 'metric.messages.redelivered' => 'enqueue.messages.redelivered', |
| 162 | + 'metric.messages.failed' => 'enqueue.messages.failed', |
| 163 | + 'metric.consumers.started' => 'enqueue.consumers.started', |
| 164 | + 'metric.consumers.finished' => 'enqueue.consumers.finished', |
| 165 | + 'metric.consumers.failed' => 'enqueue.consumers.failed', |
| 166 | + 'metric.consumers.received' => 'enqueue.consumers.received', |
| 167 | + 'metric.consumers.acknowledged' => 'enqueue.consumers.acknowledged', |
| 168 | + 'metric.consumers.rejected' => 'enqueue.consumers.rejected', |
| 169 | + 'metric.consumers.requeued' => 'enqueue.consumers.requeued', |
| 170 | + 'metric.consumers.memoryUsage' => 'enqueue.consumers.memoryUsage', |
| 171 | + ], $config); |
| 172 | + } |
| 173 | +} |
0 commit comments