Skip to content

Commit

Permalink
fix(histogram): Allow to use negative buckets and values (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlodes committed Jun 18, 2023
1 parent 3d27c41 commit 9d4d405
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 27 deletions.
5 changes: 0 additions & 5 deletions src/Collector/ByType/HistogramCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Zlodes\PrometheusClient\Collector\ByType;

use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
use Zlodes\PrometheusClient\Collector\WithLabels;
use Zlodes\PrometheusClient\Exception\StorageWriteException;
use Zlodes\PrometheusClient\Metric\Histogram;
Expand All @@ -26,14 +25,10 @@ public function __construct(

public function update(float|int $value): void
{
Assert::true($value >= 0, 'Value of Histogram metric MUST not be negative');

$histogram = $this->histogram;
$labels = $this->composeLabels();
$buckets = $this->histogram->getBuckets();

// TODO: check value is in range of buckets

try {
$this->storage->persistHistogram(
new MetricValue(
Expand Down
22 changes: 0 additions & 22 deletions tests/Collector/ByType/HistogramCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Zlodes\PrometheusClient\Tests\Collector\ByType;

use InvalidArgumentException;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Zlodes\PrometheusClient\Collector\ByType\HistogramCollector;
Expand Down Expand Up @@ -79,7 +77,6 @@ public function testTimer(): void
self::assertEquals(['route' => '/'], $metricValue->metricNameWithLabels->labels);
}


public function testsPersistError(): void
{
$histogram = new Histogram('response_time', 'App response time');
Expand All @@ -99,23 +96,4 @@ public function testsPersistError(): void

$collector->update(42);
}

#[DataProvider('invalidValuesDataProvider')]
public function testInvalidValues(mixed $value): void
{
$collector = new HistogramCollector(
new Histogram('response_time', 'App response time'),
Mockery::mock(Storage::class),
new NullLogger()
);

$this->expectException(InvalidArgumentException::class);

$collector->update($value);
}

public static function invalidValuesDataProvider(): iterable
{
yield 'negative' => [-1];
}
}

0 comments on commit 9d4d405

Please sign in to comment.