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