Skip to content

Commit 0502df1

Browse files
committed
Allow incrementing counters with a float
Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent ac7f0ed commit 0502df1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Prometheus/Counter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function inc(array $labels = []): void
2727
}
2828

2929
/**
30-
* @param int $count e.g. 2
30+
* @param int|float $count e.g. 2
3131
* @param mixed[] $labels e.g. ['status', 'opcode']
3232
*/
33-
public function incBy(int $count, array $labels = []): void
33+
public function incBy($count, array $labels = []): void
3434
{
3535
$this->assertLabelsAreDefinedCorrectly($labels);
3636

@@ -42,7 +42,7 @@ public function incBy(int $count, array $labels = []): void
4242
'labelNames' => $this->getLabelNames(),
4343
'labelValues' => $labels,
4444
'value' => $count,
45-
'command' => Adapter::COMMAND_INCREMENT_INTEGER,
45+
'command' => is_float($count) ? Adapter::COMMAND_INCREMENT_FLOAT : Adapter::COMMAND_INCREMENT_INTEGER,
4646
]
4747
);
4848
}

tests/Test/Prometheus/AbstractCounterTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,40 @@ public function itShouldIncreaseTheCounterByAnArbitraryInteger(): void
127127
);
128128
}
129129

130+
/**
131+
* @test
132+
*/
133+
public function itShouldIncreaseTheCounterWithAFloat(): void
134+
{
135+
$counter = new Counter($this->adapter, 'test', 'some_metric', 'this is for testing', ['foo', 'bar']);
136+
$counter->inc(['lalal', 'lululu']);
137+
$counter->incBy(1.5, ['lalal', 'lululu']);
138+
self::assertThat(
139+
$this->adapter->collect(),
140+
self::equalTo(
141+
[
142+
new MetricFamilySamples(
143+
[
144+
'type' => Counter::TYPE,
145+
'help' => 'this is for testing',
146+
'name' => 'test_some_metric',
147+
'labelNames' => ['foo', 'bar'],
148+
'samples' => [
149+
[
150+
'labelValues' => ['lalal', 'lululu'],
151+
'value' => 2.5,
152+
'name' => 'test_some_metric',
153+
'labelNames' => [],
154+
],
155+
],
156+
]
157+
),
158+
]
159+
)
160+
);
161+
}
162+
163+
130164
/**
131165
* @test
132166
*/

0 commit comments

Comments
 (0)