Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Prometheus/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function inc(array $labels = []): void
}

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

Expand All @@ -42,7 +42,7 @@ public function incBy(int $count, array $labels = []): void
'labelNames' => $this->getLabelNames(),
'labelValues' => $labels,
'value' => $count,
'command' => Adapter::COMMAND_INCREMENT_INTEGER,
'command' => is_float($count) ? Adapter::COMMAND_INCREMENT_FLOAT : Adapter::COMMAND_INCREMENT_INTEGER,
]
);
}
Expand Down
36 changes: 23 additions & 13 deletions src/Prometheus/Storage/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function updateHistogram(array $data): void
{
// Initialize the sum
$sumKey = $this->histogramBucketValueKey($data, 'sum');
$new = apcu_add($sumKey, $this->toInteger(0));
$new = apcu_add($sumKey, $this->toBinaryRepresentationAsInteger(0));

// If sum does not exist, assume a new histogram and store the metadata
if ($new) {
Expand All @@ -43,7 +43,7 @@ public function updateHistogram(array $data): void
while (!$done) {
$old = apcu_fetch($sumKey);
if ($old !== false) {
$done = apcu_cas($sumKey, $old, $this->toInteger($this->fromInteger($old) + $data['value']));
$done = apcu_cas($sumKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
}
}

Expand All @@ -68,10 +68,10 @@ public function updateGauge(array $data): void
{
$valueKey = $this->valueKey($data);
if ($data['command'] === Adapter::COMMAND_SET) {
apcu_store($valueKey, $this->toInteger($data['value']));
apcu_store($valueKey, $this->toBinaryRepresentationAsInteger($data['value']));
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
} else {
$new = apcu_add($valueKey, $this->toInteger(0));
$new = apcu_add($valueKey, $this->toBinaryRepresentationAsInteger(0));
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
Expand All @@ -80,7 +80,7 @@ public function updateGauge(array $data): void
while (!$done) {
$old = apcu_fetch($valueKey);
if ($old !== false) {
$done = apcu_cas($valueKey, $old, $this->toInteger($this->fromInteger($old) + $data['value']));
$done = apcu_cas($valueKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
}
}
}
Expand All @@ -91,11 +91,21 @@ public function updateGauge(array $data): void
*/
public function updateCounter(array $data): void
{
$new = apcu_add($this->valueKey($data), 0);
if ($new) {
$valueKey = $this->valueKey($data);
// Check if value key already exists
if (apcu_exists($this->valueKey($data)) === false) {
apcu_add($this->valueKey($data), 0);
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
apcu_inc($this->valueKey($data), $data['value']);

// Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91
$done = false;
while (!$done) {
$old = apcu_fetch($valueKey);
if ($old !== false) {
$done = apcu_cas($valueKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
}
}
}

/**
Expand Down Expand Up @@ -180,7 +190,7 @@ private function collectCounters(): array
'name' => $metaData['name'],
'labelNames' => [],
'labelValues' => $this->decodeLabelValues($labelValues),
'value' => $value['value'],
'value' => $this->fromBinaryRepresentationAsInteger($value['value']),
];
}
$this->sortSamples($data['samples']);
Expand Down Expand Up @@ -211,7 +221,7 @@ private function collectGauges(): array
'name' => $metaData['name'],
'labelNames' => [],
'labelValues' => $this->decodeLabelValues($labelValues),
'value' => $this->fromInteger($value['value']),
'value' => $this->fromBinaryRepresentationAsInteger($value['value']),
];
}

Expand Down Expand Up @@ -288,7 +298,7 @@ private function collectHistograms(): array
'name' => $metaData['name'] . '_sum',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => $this->fromInteger($histogramBuckets[$labelValues]['sum']),
'value' => $this->fromBinaryRepresentationAsInteger($histogramBuckets[$labelValues]['sum']),
];
}
$histograms[] = new MetricFamilySamples($data);
Expand All @@ -300,7 +310,7 @@ private function collectHistograms(): array
* @param mixed $val
* @return int
*/
private function toInteger($val): int
private function toBinaryRepresentationAsInteger($val): int
{
return unpack('Q', pack('d', $val))[1];
}
Expand All @@ -309,7 +319,7 @@ private function toInteger($val): int
* @param mixed $val
* @return float
*/
private function fromInteger($val): float
private function fromBinaryRepresentationAsInteger($val): float
{
return unpack('d', pack('Q', $val))[1];
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Test/Prometheus/AbstractCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,40 @@ public function itShouldIncreaseTheCounterByAnArbitraryInteger(): void
);
}

/**
* @test
*/
public function itShouldIncreaseTheCounterWithAFloat(): void
{
$counter = new Counter($this->adapter, 'test', 'some_metric', 'this is for testing', ['foo', 'bar']);
$counter->inc(['lalal', 'lululu']);
$counter->incBy(1.5, ['lalal', 'lululu']);
self::assertThat(
$this->adapter->collect(),
self::equalTo(
[
new MetricFamilySamples(
[
'type' => Counter::TYPE,
'help' => 'this is for testing',
'name' => 'test_some_metric',
'labelNames' => ['foo', 'bar'],
'samples' => [
[
'labelValues' => ['lalal', 'lululu'],
'value' => 2.5,
'name' => 'test_some_metric',
'labelNames' => [],
],
],
]
),
]
)
);
}


/**
* @test
*/
Expand Down