-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathRedis.php
More file actions
864 lines (782 loc) · 27.7 KB
/
Redis.php
File metadata and controls
864 lines (782 loc) · 27.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
<?php
declare(strict_types=1);
namespace Prometheus\Storage;
use InvalidArgumentException;
use Prometheus\Counter;
use Prometheus\Exception\MetricJsonException;
use Prometheus\Exception\StorageException;
use Prometheus\Gauge;
use Prometheus\Histogram;
use Prometheus\Math;
use Prometheus\MetricFamilySamples;
use Prometheus\Summary;
use RuntimeException;
class Redis implements Adapter
{
const PROMETHEUS_METRIC_KEYS_SUFFIX = '_METRIC_KEYS';
/**
* @var mixed[]
*/
private static $defaultOptions = [
'host' => '127.0.0.1',
'port' => 6379,
'timeout' => 0.1,
'read_timeout' => '10',
'persistent_connections' => false,
'password' => null,
'user' => null,
'sentinel' => [ // sentinel options
'enable' => false, // if enabled uses sentinel to get the master before connecting to redis
'host' => '127.0.0.1', // phpredis sentinel address of the redis, default is the same as redis host if empty
'port' => 26379, // phpredis sentinel port of the primary redis server, default 26379 if empty.
'service' => 'myprimary', //, phpredis sentinel primary name, default myprimary
'timeout' => 0, // phpredis sentinel connection timeout
'persistent' => null, // phpredis sentinel persistence parameter
'retry_interval' => 0, // phpredis sentinel retry interval
'read_timeout' => 0, // phpredis sentinel read timeout
'reconnect' => 0, // retries after losing connection to redis asking for a new primary, if -1 will retry indefinetely
'username' => '', // phpredis sentinel auth username
'password' => '', // phpredis sentinel auth password
'ssl' => null,
]
];
// The following array contains all exception message parts which are interpreted as a connection loss or
// another unavailability of Redis.
private const ERROR_MESSAGES_INDICATING_UNAVAILABILITY = [
'connection closed',
'connection refused',
'connection lost',
'failed while reconnecting',
'is loading the dataset in memory',
'php_network_getaddresses',
'read error on connection',
'socket',
'went away',
'loading',
'readonly',
"can't write against a read only replica",
];
/**
* @var string
*/
private static $prefix = 'PROMETHEUS_';
/**
* @var mixed[]
*/
private $options = [];
/**
* @var \Redis
*/
private $redis;
/**
* @var RedisSentinel
*/
private $sentinel = null;
/**
* @var boolean
*/
private $connectionInitialized = false;
/**
* Redis constructor.
* @param mixed[] $options
*/
public function __construct(array $options = [])
{
$this->options = [...self::$defaultOptions, ...$options];
$this->options['sentinel'] = [...self::$defaultOptions['sentinel'] ?? [], ...$options['sentinel'] ?? []];
$this->redis = new \Redis();
if (boolval($this->options['sentinel']['enable'])) {
$options['sentinel']['host'] = $options['sentinel']['host'] ?? $options['host'];
$this->sentinel = new RedisSentinel($options['sentinel']);
}
}
/**
* Sentinels discoverMaster
* @return void
*/
public function updateSentinelPrimary(): void
{
$master = $this->sentinel->getMaster();
if (is_array($master)) {
$this->options['host'] = $master['ip'];
$this->options['port'] = $master['port'];
}
}
/**
* @return \RedisSentinel
*/
public function getRedisSentinel(): \RedisSentinel
{
return $this->sentinel->getSentinel();
}
/**
* @param \Redis $redis
* @param \RedisSentinel $redisSentinel
* @return self
* @throws StorageException
*/
public static function fromExistingConnection(\Redis $redis, ?\RedisSentinel $redisSentinel = null): self
{
if (isset($redisSentinel)) {
RedisSentinel::fromExistingConnection($redisSentinel);
}
if ($redis->isConnected() === false) {
throw new StorageException('Connection to Redis server not established');
}
$self = new self();
$self->connectionInitialized = true;
$self->redis = $redis;
return $self;
}
/**
* @param mixed[] $options
*/
public static function setDefaultOptions(array $options): void
{
self::$defaultOptions = array_merge(self::$defaultOptions, $options);
}
/**
* @param string $prefix
*/
public static function setPrefix(string $prefix): void
{
self::$prefix = $prefix;
}
/**
* @throws StorageException
* @deprecated use replacement method wipeStorage from Adapter interface
*/
public function flushRedis(): void
{
$this->wipeStorage();
}
/**
* @inheritDoc
*/
public function wipeStorage(): void
{
$this->ensureOpenConnection();
$searchPattern = "";
$globalPrefix = $this->redis->getOption(\Redis::OPT_PREFIX);
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
if (is_string($globalPrefix)) {
$searchPattern .= $globalPrefix;
}
$searchPattern .= self::$prefix;
$searchPattern .= '*';
$this->redis->eval(
<<<LUA
redis.replicate_commands()
local cursor = "0"
repeat
local results = redis.call('SCAN', cursor, 'MATCH', ARGV[1])
cursor = results[1]
for _, key in ipairs(results[2]) do
redis.call('DEL', key)
end
until cursor == "0"
LUA
,
[$searchPattern],
0
);
}
/**
* @param mixed[] $data
*
* @return string
*/
private function metaKey(array $data): string
{
return implode(':', [
$data['name'],
'meta'
]);
}
/**
* @param mixed[] $data
*
* @return string
*/
private function valueKey(array $data): string
{
return implode(':', [
$data['name'],
$this->encodeLabelValues($data['labelValues']),
'value'
]);
}
/**
* @return MetricFamilySamples[]
* @throws StorageException
*/
public function collect(bool $sortMetrics = true): array
{
$this->ensureOpenConnection();
$metrics = $this->collectHistograms();
$metrics = array_merge($metrics, $this->collectGauges($sortMetrics));
$metrics = array_merge($metrics, $this->collectCounters($sortMetrics));
$metrics = array_merge($metrics, $this->collectSummaries());
return array_map(
function (array $metric): MetricFamilySamples {
return new MetricFamilySamples($metric);
},
$metrics
);
}
/**
* Inspects the given exception and reconnects the client if the reported error indicates that the server
* went away or is in readonly mode, which may happen in case of a Redis Sentinel failover.
*/
private function reconnectIfRedisIsUnavailableOrReadonly(\RedisException $exception): bool
{
// We convert the exception message to lower-case in order to perform case-insensitive comparison.
$exceptionMessage = strtolower($exception->getMessage());
// Because we also match only partial exception messages, we cannot use in_array() at this point.
foreach (self::ERROR_MESSAGES_INDICATING_UNAVAILABILITY as $errorMessage) {
if (str_contains($exceptionMessage, $errorMessage)) {
// Here we reconnect through Redis Sentinel if we lost connection to the server or if another unavailability occurred.
// We may actually reconnect to the same, broken server. But after a failover occured, we should be ok.
// It may take a moment until the Sentinel returns the new master, so this may be triggered multiple times.
return true;
}
}
return false;
}
/**
* @throws StorageException
*/
private function ensureOpenConnection(): void
{
if ($this->connectionInitialized === true) {
return;
}
if ($this->sentinel !== null && boolval($this->options['sentinel']['enable'])) {
$reconnect = $this->options['sentinel']['reconnect'];
$retries = 0;
while ($retries <= $reconnect) {
try {
$this->updateSentinelPrimary();
$this->connectToServer();
break;
} catch (\RedisException $e) {
$retry = $this->reconnectIfRedisIsUnavailableOrReadonly($e);
if (!$retry) {
throw new StorageException(
sprintf("Can't connect to Redis server. %s", $e->getMessage()),
$e->getCode(),
$e
);
}
}
$retries++;
}
} else {
$this->connectToServer();
}
$authParams = [];
if (isset($this->options['user']) && $this->options['user'] !== '') {
$authParams[] = $this->options['user'];
}
if (isset($this->options['password'])) {
$authParams[] = $this->options['password'];
}
if ($authParams !== []) {
$this->redis->auth($authParams);
}
if (isset($this->options['database'])) {
$this->redis->select($this->options['database']);
}
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->options['read_timeout']);
$this->connectionInitialized = true;
}
/**
* @throws StorageException
*/
private function connectToServer(): void
{
$connection_successful = false;
if ($this->options['persistent_connections'] !== false) {
$connection_successful = $this->redis->pconnect(
$this->options['host'],
(int) $this->options['port'],
(float) $this->options['timeout']
);
} else {
try {
$connection_successful = $this->redis->connect($this->options['host'], (int) $this->options['port'], (float) $this->options['timeout']);
} catch (\RedisException $ex) {
throw new StorageException(
sprintf("Can't connect to Redis server. %s", $ex->getMessage()),
$ex->getCode()
);
}
}
if (!$connection_successful) {
throw new StorageException(
sprintf("Can't connect to Redis server. %s", $this->redis->getLastError()),
0
);
}
}
/**
* @param mixed[] $data
* @throws StorageException
*/
public function updateHistogram(array $data): void
{
$this->ensureOpenConnection();
$bucketToIncrease = '+Inf';
foreach ($data['buckets'] as $bucket) {
if ($data['value'] <= $bucket) {
$bucketToIncrease = $bucket;
break;
}
}
$metaData = $data;
unset($metaData['value'], $metaData['labelValues']);
$this->redis->eval(
<<<LUA
local result = redis.call('hIncrByFloat', KEYS[1], ARGV[1], ARGV[3])
redis.call('hIncrBy', KEYS[1], ARGV[2], 1)
if tonumber(result) >= tonumber(ARGV[3]) then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
return result
LUA
,
[
$this->toMetricKey($data),
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode(['b' => 'sum', 'labelValues' => $data['labelValues']]),
json_encode(['b' => $bucketToIncrease, 'labelValues' => $data['labelValues']]),
$data['value'],
json_encode($metaData),
],
2
);
}
/**
* @param mixed[] $data
* @throws StorageException
*/
public function updateSummary(array $data): void
{
$this->ensureOpenConnection();
// store meta
$summaryKey = self::$prefix . Summary::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX;
$metaKey = $summaryKey . ':' . $this->metaKey($data);
$json = json_encode($this->metaData($data));
if (false === $json) {
throw new RuntimeException(json_last_error_msg());
}
$this->redis->setNx($metaKey, $json); /** @phpstan-ignore-line */
// store value key
$valueKey = $summaryKey . ':' . $this->valueKey($data);
$json = json_encode($this->encodeLabelValues($data['labelValues']));
if (false === $json) {
throw new RuntimeException(json_last_error_msg());
}
$this->redis->setNx($valueKey, $json); /** @phpstan-ignore-line */
// trick to handle uniqid collision
$done = false;
while (!$done) {
$sampleKey = $valueKey . ':' . uniqid('', true);
$done = $this->redis->set($sampleKey, $data['value'], ['NX', 'EX' => $data['maxAgeSeconds']]);
}
}
/**
* @param mixed[] $data
* @throws StorageException
*/
public function updateGauge(array $data): void
{
$this->ensureOpenConnection();
$metaData = $data;
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])
if ARGV[1] == 'hSet' then
if result == 1 then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
else
if result == ARGV[3] then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
end
LUA
,
[
$this->toMetricKey($data),
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$this->getRedisCommand($data['command']),
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData),
],
2
);
}
/**
* @param mixed[] $data
* @throws StorageException
*/
public function updateCounter(array $data): void
{
$this->ensureOpenConnection();
$metaData = $data;
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[3], ARGV[2])
local added = redis.call('sAdd', KEYS[2], KEYS[1])
if added == 1 then
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
end
return result
LUA
,
[
$this->toMetricKey($data),
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$this->getRedisCommand($data['command']),
$data['value'],
json_encode($data['labelValues']),
json_encode($metaData),
],
2
);
}
/**
* @param mixed[] $data
* @return mixed[]
*/
private function metaData(array $data): array
{
$metricsMetaData = $data;
unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']);
return $metricsMetaData;
}
/**
* @return mixed[]
*/
private function collectHistograms(): array
{
$keys = $this->redis->sMembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$histograms = [];
foreach ($keys as $key) {
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
if (!isset($raw['__meta'])) {
continue;
}
$histogram = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$histogram['samples'] = [];
// Add the Inf bucket so we can compute it later on
$histogram['buckets'][] = '+Inf';
$allLabelValues = [];
foreach (array_keys($raw) as $k) {
$d = json_decode($k, true);
if ($d['b'] == 'sum') {
continue;
}
$allLabelValues[] = $d['labelValues'];
}
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key);
}
// 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(['b' => $bucket, 'labelValues' => $labelValues]);
if (!isset($raw[$bucketKey])) {
$histogram['samples'][] = [
'name' => $histogram['name'] . '_bucket',
'labelNames' => ['le'],
'labelValues' => array_merge($labelValues, [$bucket]),
'value' => $acc,
];
} else {
$acc += $raw[$bucketKey];
$histogram['samples'][] = [
'name' => $histogram['name'] . '_bucket',
'labelNames' => ['le'],
'labelValues' => array_merge($labelValues, [$bucket]),
'value' => $acc,
];
}
}
// Add the count
$histogram['samples'][] = [
'name' => $histogram['name'] . '_count',
'labelNames' => [],
'labelValues' => $labelValues,
'value' => $acc,
];
// Add the sum
$histogram['samples'][] = [
'name' => $histogram['name'] . '_sum',
'labelNames' => [],
'labelValues' => $labelValues,
'value' => $raw[json_encode(['b' => 'sum', 'labelValues' => $labelValues])],
];
}
$histograms[] = $histogram;
}
return $histograms;
}
/**
* @param string $key
*
* @return string
*/
private function removePrefixFromKey(string $key): string
{
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
if ($this->redis->getOption(\Redis::OPT_PREFIX) === null) {
return $key;
}
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
return substr($key, strlen($this->redis->getOption(\Redis::OPT_PREFIX)));
}
/**
* @return mixed[]
*/
private function collectSummaries(): array
{
$math = new Math();
$summaryKey = self::$prefix . Summary::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX;
$keys = $this->redis->keys($summaryKey . ':*:meta');
$summaries = [];
foreach ($keys as $metaKeyWithPrefix) {
$metaKey = $this->removePrefixFromKey($metaKeyWithPrefix);
$rawSummary = $this->redis->get($metaKey);
if ($rawSummary === false) {
continue;
}
$summary = json_decode($rawSummary, true);
$metaData = $summary;
$data = [
'name' => $metaData['name'],
'help' => $metaData['help'],
'type' => $metaData['type'],
'labelNames' => $metaData['labelNames'],
'maxAgeSeconds' => $metaData['maxAgeSeconds'],
'quantiles' => $metaData['quantiles'],
'samples' => [],
];
$values = $this->redis->keys($summaryKey . ':' . $metaData['name'] . ':*:value');
foreach ($values as $valueKeyWithPrefix) {
$valueKey = $this->removePrefixFromKey($valueKeyWithPrefix);
$rawValue = $this->redis->get($valueKey);
if ($rawValue === false) {
continue;
}
$value = json_decode($rawValue, true);
$encodedLabelValues = $value;
$decodedLabelValues = $this->decodeLabelValues($encodedLabelValues);
$samples = [];
$sampleValues = $this->redis->keys($summaryKey . ':' . $metaData['name'] . ':' . $encodedLabelValues . ':value:*');
foreach ($sampleValues as $sampleValueWithPrefix) {
$sampleValue = $this->removePrefixFromKey($sampleValueWithPrefix);
$samples[] = (float) $this->redis->get($sampleValue);
}
if (count($samples) === 0) {
try {
$this->redis->del($valueKey);
} catch (\RedisException $e) {
// ignore if we can't delete the key
}
continue;
}
// Compute quantiles
sort($samples);
foreach ($data['quantiles'] as $quantile) {
$data['samples'][] = [
'name' => $metaData['name'],
'labelNames' => ['quantile'],
'labelValues' => array_merge($decodedLabelValues, [$quantile]),
'value' => $math->quantile($samples, $quantile),
];
}
// Add the count
$data['samples'][] = [
'name' => $metaData['name'] . '_count',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => count($samples),
];
// Add the sum
$data['samples'][] = [
'name' => $metaData['name'] . '_sum',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => array_sum($samples),
];
}
if (count($data['samples']) > 0) {
$summaries[] = $data;
} else {
try {
$this->redis->del($metaKey);
} catch (\RedisException $e) {
// ignore if we can't delete the key
}
}
}
return $summaries;
}
/**
* @return mixed[]
*/
private function collectGauges(bool $sortMetrics = true): array
{
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$gauges = [];
foreach ($keys as $key) {
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
if (!isset($raw['__meta'])) {
continue;
}
$gauge = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$gauge['samples'] = [];
foreach ($raw as $k => $value) {
$gauge['samples'][] = [
'name' => $gauge['name'],
'labelNames' => [],
'labelValues' => json_decode($k, true),
'value' => $value,
];
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $gauge['name']);
}
}
if ($sortMetrics) {
usort($gauge['samples'], function ($a, $b): int {
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
}
$gauges[] = $gauge;
}
return $gauges;
}
/**
* @return mixed[]
* @throws MetricJsonException
*/
private function collectCounters(bool $sortMetrics = true): array
{
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$counters = [];
foreach ($keys as $key) {
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
if (!isset($raw['__meta'])) {
continue;
}
$counter = json_decode($raw['__meta'], true);
unset($raw['__meta']);
$counter['samples'] = [];
foreach ($raw as $k => $value) {
$counter['samples'][] = [
'name' => $counter['name'],
'labelNames' => [],
'labelValues' => json_decode($k, true),
'value' => $value,
];
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $counter['name']);
}
}
if ($sortMetrics) {
usort($counter['samples'], function ($a, $b): int {
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
}
$counters[] = $counter;
}
return $counters;
}
/**
* @param int $cmd
* @return string
*/
private function getRedisCommand(int $cmd): string
{
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 mixed[] $data
* @return string
*/
private function toMetricKey(array $data): string
{
return implode(':', [self::$prefix, $data['type'], $data['name']]);
}
/**
* @param mixed[] $values
* @return string
* @throws RuntimeException
*/
private function encodeLabelValues(array $values): string
{
$json = json_encode($values);
if (false === $json) {
throw new RuntimeException(json_last_error_msg());
}
return base64_encode($json);
}
/**
* @param string $values
* @return mixed[]
* @throws RuntimeException
*/
private function decodeLabelValues(string $values): array
{
$json = base64_decode($values, true);
if (false === $json) {
throw new RuntimeException('Cannot base64 decode label values');
}
$decodedValues = json_decode($json, true);
if (false === $decodedValues) {
throw new RuntimeException(json_last_error_msg());
}
return $decodedValues;
}
/**
* @param string $redisKey
* @param string|null $metricName
* @return void
* @throws MetricJsonException
*/
private function throwMetricJsonException(string $redisKey, ?string $metricName = null): void
{
$metricName = $metricName ?? 'unknown';
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' metric name: ' . $metricName;
throw new MetricJsonException($message, 0, null, $metricName);
}
}