This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathPredis.php
More file actions
263 lines (239 loc) · 9.16 KB
/
Predis.php
File metadata and controls
263 lines (239 loc) · 9.16 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace Prometheus\Storage;
use Prometheus\Histogram;
use Prometheus\Gauge;
use Prometheus\Counter;
use Prometheus\MetricFamilySamples;
class Predis implements Adapter {
const PROMETHEUS_METRIC_KEYS_SUFFIX = '_METRIC_KEYS';
private static $defaultOptions = array();
private static $prefix = 'PROMETHEUS_';
public function collect(){
$metrics = $this->collectHistograms();
$metrics = array_merge($metrics, $this->collectGauges());
$metrics = array_merge($metrics, $this->collectCounters());
return array_map(
function (array $metric) {
return new MetricFamilySamples($metric);
},
$metrics
);
}
public function updateHistogram(array $data){
$bucketToIncrease = '+Inf';
foreach ($data['buckets'] as $bucket) {
if ($data['value'] <= $bucket) {
$bucketToIncrease = $bucket;
break;
}
}
$metaData = $data;
unset($metaData['value']);
unset($metaData['labelValues']);
$script = <<<LUA
local increment = redis.call('hIncrByFloat', KEYS[1], KEYS[2], ARGV[1])
redis.call('hIncrBy', KEYS[1], KEYS[3], 1)
if increment == ARGV[1] then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[4], KEYS[1])
end
LUA;
app('redis')->eval($script,4,
$this->toMetricKey($data),
json_encode(array('b' => 'sum', 'labelValues' => $data['labelValues'])),
json_encode(array('b' => $bucketToIncrease, 'labelValues' => $data['labelValues'])),
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$data['value'],
json_encode($metaData)
);
}
public function updateGauge(array $data){
$metaData = $data;
unset($metaData['value']);
unset($metaData['labelValues']);
unset($metaData['command']);
$script = <<<LUA
local result = redis.call(KEYS[2], KEYS[1], KEYS[4], ARGV[1])
if KEYS[2] == 'hSet' then
if result == 1 then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
end
else
if result == ARGV[1] then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
end
end
LUA;
app('redis')->eval($script,4,
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData)
);
}
public function updateCounter(array $data){
$metaData = $data;
unset($metaData['value']);
unset($metaData['labelValues']);
unset($metaData['command']);
$script = <<<LUA
local result = redis.call(KEYS[2], KEYS[1], KEYS[4], ARGV[1])
if result == tonumber(ARGV[1]) then
redis.call('hMSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
end
return result
LUA;
$result = app('redis')->eval($script,4,
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData)
);
return $result;
}
private function collectHistograms()
{
$keys = app('redis')->smembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$histograms = array();
foreach ($keys as $key) {
$raw = app('redis')->hgetall($key);
$histogram = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$histogram['samples'] = array();
// Add the Inf bucket so we can compute it later on
$histogram['buckets'][] = '+Inf';
$allLabelValues = array();
foreach (array_keys($raw) as $k) {
$d = json_decode($k, true);
if ($d['b'] == 'sum') {
continue;
}
$allLabelValues[] = $d['labelValues'];
}
// We need set semantics.
// This is the equivalent of array_unique but for arrays of arrays.
$allLabelValues = array_map("unserialize", array_unique(array_map("serialize", $allLabelValues)));
sort($allLabelValues);
foreach ($allLabelValues as $labelValues) {
// Fill up all buckets.
// If the bucket doesn't exist fill in values from
// the previous one.
$acc = 0;
foreach ($histogram['buckets'] as $bucket) {
$bucketKey = json_encode(array('b' => $bucket, 'labelValues' => $labelValues));
if (!isset($raw[$bucketKey])) {
$histogram['samples'][] = array(
'name' => $histogram['name'] . '_bucket',
'labelNames' => array('le'),
'labelValues' => array_merge($labelValues, array($bucket)),
'value' => $acc
);
} else {
$acc += $raw[$bucketKey];
$histogram['samples'][] = array(
'name' => $histogram['name'] . '_bucket',
'labelNames' => array('le'),
'labelValues' => array_merge($labelValues, array($bucket)),
'value' => $acc
);
}
}
// Add the count
$histogram['samples'][] = array(
'name' => $histogram['name'] . '_count',
'labelNames' => array(),
'labelValues' => $labelValues,
'value' => $acc
);
// Add the sum
$histogram['samples'][] = array(
'name' => $histogram['name'] . '_sum',
'labelNames' => array(),
'labelValues' => $labelValues,
'value' => $raw[json_encode(array('b' => 'sum', 'labelValues' => $labelValues))]
);
}
$histograms[] = $histogram;
}
return $histograms;
}
private function collectGauges()
{
$keys = app('redis')->smembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$gauges = array();
foreach ($keys as $key) {
$raw = app('redis')->hgetall($key);
$gauge = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$gauge['samples'] = array();
foreach ($raw as $k => $value) {
$gauge['samples'][] = array(
'name' => $gauge['name'],
'labelNames' => array(),
'labelValues' => json_decode($k, true),
'value' => $value
);
}
usort($gauge['samples'], function($a, $b){
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
$gauges[] = $gauge;
}
return $gauges;
}
private function collectCounters()
{
$keys = app('redis')->smembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$counters = array();
foreach ($keys as $key) {
$raw = app('redis')->hgetall($key);
$counter = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$counter['samples'] = array();
foreach ($raw as $k => $value) {
$counter['samples'][] = array(
'name' => $counter['name'],
'labelNames' => array(),
'labelValues' => json_decode($k, true),
'value' => $value
);
}
usort($counter['samples'], function($a, $b){
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
$counters[] = $counter;
}
return $counters;
}
private function getRedisCommand($cmd)
{
switch ($cmd) {
case Adapter::COMMAND_INCREMENT_INTEGER:
return 'hIncrBy';
case Adapter::COMMAND_INCREMENT_FLOAT:
return 'hIncrByFloat';
case Adapter::COMMAND_SET:
return 'hSet';
default:
throw new \InvalidArgumentException("Unknown command");
}
}
/**
* @param array $data
* @return string
*/
private function toMetricKey(array $data)
{
return implode(':', array(self::$prefix, $data['type'], $data['name']));
}
}