Skip to content

Commit 826bdf4

Browse files
committed
no crash when redis is not available
1 parent 0edf509 commit 826bdf4

File tree

6 files changed

+117
-48
lines changed

6 files changed

+117
-48
lines changed

src/Metrics/Counter.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@ public function __construct(
1717

1818
public function update($value = 1, array $labelValues = []): void
1919
{
20-
$this->getCounter()->incBy(
21-
$value,
22-
$this->enrichLabelValues($labelValues)
23-
);
20+
try {
21+
$this->getCounter()->incBy(
22+
$value,
23+
$this->enrichLabelValues($labelValues)
24+
);
25+
} catch (\RedisException $e) {
26+
}
2427
}
2528

2629
private function getCounter(): LowLevelCounter
2730
{
2831
if (!$this->counter) {
29-
$this->counter = $this->metricsBag->getCollectors()->registerCounter(
30-
$this->metricsBag->getNamespace(),
31-
$this->name,
32-
$this->help,
33-
$this->enrichLabelNames($this->labels),
34-
);
32+
try {
33+
$this->counter = $this->metricsBag->getCollectors()->registerCounter(
34+
$this->metricsBag->getNamespace(),
35+
$this->name,
36+
$this->help,
37+
$this->enrichLabelNames($this->labels),
38+
);
39+
} catch (\RedisException) {
40+
}
3541
}
3642

3743
return $this->counter;

src/Metrics/Gauge.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@ public function __construct(
1717

1818
public function update($value = 1, array $labelValues = []): void
1919
{
20-
$this->getGauge()->set(
21-
$value,
22-
$this->enrichLabelValues($labelValues)
23-
);
20+
try {
21+
$this->getGauge()->set(
22+
$value,
23+
$this->enrichLabelValues($labelValues)
24+
);
25+
} catch (\RedisException $e) {
26+
}
2427
}
2528

2629
private function getGauge(): LowLevelGauge
2730
{
2831
if (!$this->gauge) {
29-
$this->gauge = $this->metricsBag->getCollectors()->registerGauge(
30-
$this->metricsBag->getNamespace(),
31-
$this->name,
32-
$this->help,
33-
$this->enrichLabelNames($this->labels),
34-
);
32+
try {
33+
$this->gauge = $this->metricsBag->getCollectors()->registerGauge(
34+
$this->metricsBag->getNamespace(),
35+
$this->name,
36+
$this->help,
37+
$this->enrichLabelNames($this->labels),
38+
);
39+
} catch (\RedisException) {
40+
}
3541
}
3642

3743
return $this->gauge;

src/Metrics/Histogram.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@ public function __construct(
1818

1919
public function update($value = 1, array $labelValues = []): void
2020
{
21-
$this->getHistogram()->observe(
22-
$value,
23-
$this->enrichLabelValues($labelValues)
24-
);
21+
try {
22+
$this->getHistogram()->observe(
23+
$value,
24+
$this->enrichLabelValues($labelValues)
25+
);
26+
} catch (\RedisException $e) {
27+
}
2528
}
2629

2730
private function getHistogram(): LowLevelHistogram
2831
{
2932
if (!$this->histogram) {
30-
$this->histogram = $this->metricsBag->getCollectors()->registerHistogram(
31-
$this->metricsBag->getNamespace(),
32-
$this->name,
33-
$this->help,
34-
$this->enrichLabelNames($this->labels),
35-
$this->buckets,
36-
);
33+
try {
34+
$this->histogram = $this->metricsBag->getCollectors()->registerHistogram(
35+
$this->metricsBag->getNamespace(),
36+
$this->name,
37+
$this->help,
38+
$this->enrichLabelNames($this->labels),
39+
$this->buckets,
40+
);
41+
} catch (\RedisException) {
42+
}
3743
}
3844

3945
return $this->histogram;

src/Metrics/Summary.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,29 @@ public function __construct(
1919

2020
public function update($value = 1, array $labelValues = []): void
2121
{
22-
$this->getSummary()->observe(
23-
$value,
24-
$this->enrichLabelValues($labelValues)
25-
);
22+
try {
23+
$this->getSummary()->observe(
24+
$value,
25+
$this->enrichLabelValues($labelValues)
26+
);
27+
} catch (\RedisException) {
28+
}
2629
}
2730

2831
private function getSummary(): LowLevelSummary
2932
{
3033
if (!$this->summary) {
31-
$this->summary = $this->metricsBag->getCollectors()->registerSummary(
32-
$this->metricsBag->getNamespace(),
33-
$this->name,
34-
$this->help,
35-
$this->enrichLabelNames($this->labels),
36-
$this->maxAgeSeconds,
37-
$this->quantiles,
38-
);
34+
try {
35+
$this->summary = $this->metricsBag->getCollectors()->registerSummary(
36+
$this->metricsBag->getNamespace(),
37+
$this->name,
38+
$this->help,
39+
$this->enrichLabelNames($this->labels),
40+
$this->maxAgeSeconds,
41+
$this->quantiles,
42+
);
43+
} catch (\RedisException) {
44+
}
3945
}
4046

4147
return $this->summary;

src/MetricsBag.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Ensi\LaravelPrometheus\Metrics\Histogram;
1010
use Ensi\LaravelPrometheus\Metrics\Summary;
1111
use Ensi\LaravelPrometheus\OnDemandMetrics\OnDemandMetric;
12+
use Ensi\LaravelPrometheus\Storage\NullStorage;
1213
use Ensi\LaravelPrometheus\Storage\Redis;
1314
use Illuminate\Http\Request;
1415
use Illuminate\Support\Facades\Redis as RedisManager;
@@ -133,7 +134,12 @@ public function dumpTxt(): string
133134
}
134135

135136
$renderer = new RenderTextFormat();
136-
return $renderer->render($this->getCollectors()->getMetricFamilySamples());
137+
try {
138+
return $renderer->render($this->getCollectors()->getMetricFamilySamples());
139+
} catch (\RedisException) {
140+
}
141+
142+
return "";
137143
}
138144

139145
public function getCollectors(): CollectorRegistry
@@ -158,17 +164,23 @@ private function getStorage(): Adapter
158164
return new APCng($this->config['apcu-ng']['prefix']);
159165
case array_key_exists('memory', $this->config):
160166
return new InMemory();
167+
case array_key_exists('null-storage', $this->config):
168+
return new NullStorage();
161169
}
162170
throw new InvalidArgumentException("Missing storage configuration");
163171
}
164172

165173
private function createStorageFromConnection(array $options): Adapter
166174
{
167-
$redisConnection = RedisManager::connection($options['connection']);
175+
try {
176+
$redisConnection = RedisManager::connection($options['connection']);
168177

169-
return Redis::fromExistingConnection($redisConnection->client(), [
170-
'bag' => $options['bag'],
171-
]);
178+
return Redis::fromExistingConnection($redisConnection->client(), [
179+
'bag' => $options['bag'],
180+
]);
181+
} catch (\RedisException) {
182+
return new NullStorage();
183+
}
172184
}
173185

174186
public function wipe(): void

src/Storage/NullStorage.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Ensi\LaravelPrometheus\Storage;
4+
5+
use Prometheus\Storage\Adapter;
6+
7+
class NullStorage implements Adapter
8+
{
9+
public function collect(): array
10+
{
11+
return [];
12+
}
13+
14+
public function updateSummary(array $data): void
15+
{
16+
}
17+
18+
public function updateHistogram(array $data): void
19+
{
20+
}
21+
22+
public function updateGauge(array $data): void
23+
{
24+
}
25+
26+
public function updateCounter(array $data): void
27+
{
28+
}
29+
30+
public function wipeStorage(): void
31+
{
32+
}
33+
}

0 commit comments

Comments
 (0)