Skip to content

Commit 0edf509

Browse files
committed
disabling prometheus for development, ability to register metric twice
1 parent 1d972fc commit 0edf509

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

config/prometheus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
return [
44
'default_bag' => 'default',
5+
'enabled' => env('PROMETHEUS_ENABLED', true),
56
'bags' => [
67
'default' => [
78
'namespace' => env('PROMETHEUS_NAMESPACE', 'app'),

src/MetricsBag.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,51 @@ public function addMiddleware(string $labelProcessorClass, array $parameters = [
6262

6363
public function counter(string $name): Counter
6464
{
65-
$this->metrics[$name] = new Counter($this, $name);
65+
if (!isset($this->metrics[$name])) {
66+
$this->metrics[$name] = new Counter($this, $name);
67+
}
6668

6769
return $this->metrics[$name];
6870
}
6971

7072
public function gauge(string $name): Gauge
7173
{
72-
$this->metrics[$name] = new Gauge($this, $name);
74+
if (!isset($this->metrics[$name])) {
75+
$this->metrics[$name] = new Gauge($this, $name);
76+
}
7377

7478
return $this->metrics[$name];
7579
}
7680

7781
public function histogram(string $name, array $buckets): Histogram
7882
{
79-
$this->metrics[$name] = new Histogram($this, $name, $buckets);
83+
if (!isset($this->metrics[$name])) {
84+
$this->metrics[$name] = new Histogram($this, $name, $buckets);
85+
}
8086

8187
return $this->metrics[$name];
8288
}
8389

8490
public function summary(string $name, int $maxAgeSeconds, array $quantiles): Summary
8591
{
86-
$this->metrics[$name] = new Summary($this, $name, $maxAgeSeconds, $quantiles);
92+
if (!isset($this->metrics[$name])) {
93+
$this->metrics[$name] = new Summary($this, $name, $maxAgeSeconds, $quantiles);
94+
}
8795

8896
return $this->metrics[$name];
8997
}
9098

99+
private function isPrometheusEnabled(): bool
100+
{
101+
return config('prometheus.enabled');
102+
}
103+
91104
public function update(string $name, $value, array $labelValues = []): void
92105
{
106+
if (!$this->isPrometheusEnabled()) {
107+
return;
108+
}
109+
93110
$metric = $this->metrics[$name] ?? null;
94111
$metric?->update($value, $labelValues);
95112
}
@@ -111,6 +128,10 @@ public function addOnDemandMetric(string $onDemandMetricClass): void
111128

112129
public function dumpTxt(): string
113130
{
131+
if (!$this->isPrometheusEnabled()) {
132+
return '';
133+
}
134+
114135
$renderer = new RenderTextFormat();
115136
return $renderer->render($this->getCollectors()->getMetricFamilySamples());
116137
}
@@ -152,6 +173,10 @@ private function createStorageFromConnection(array $options): Adapter
152173

153174
public function wipe(): void
154175
{
176+
if (!$this->isPrometheusEnabled()) {
177+
return;
178+
}
179+
155180
$this->getCollectors()->wipeStorage();
156181
}
157182

tests/MetricsBagTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ public function testSummary()
261261
]);
262262
}
263263

264+
public function testNoCreateSameMetricTwice(): void
265+
{
266+
$bag = new MetricsBag([
267+
'namespace' => 'test',
268+
'memory' => true,
269+
]);
270+
271+
$counter1 = $bag->counter('my_counter');
272+
$counter2 = $bag->counter('my_counter');
273+
274+
$this->assertSame($counter1, $counter2);
275+
}
276+
264277
public function testLabelMiddleware()
265278
{
266279
config(['app.name' => 'app-name']);
@@ -327,4 +340,25 @@ public function testOnDemandMetrics()
327340

328341
$this->assertBagContainsMetric($bag, 'test_on_demand_counter', [], 1);
329342
}
343+
344+
public function testNoUpdatesWhenPrometheusDisabled(): void
345+
{
346+
config([
347+
'prometheus' => [
348+
'enabled' => false,
349+
]
350+
]);
351+
352+
$bag = new MetricsBag([
353+
'namespace' => 'test',
354+
'memory' => true,
355+
]);
356+
357+
$bag->counter('my_counter')->labels(['my_label']);
358+
359+
$bag->update('my_counter', 1, ['my-value']);
360+
361+
$bagValues = $bag->dumpTxt();
362+
$this->assertStringNotContainsString('test_my_counter', $bagValues);
363+
}
330364
}

0 commit comments

Comments
 (0)