-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Add Summary metric type support (#9)
Closes #5
- Loading branch information
Showing
15 changed files
with
947 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zlodes\PrometheusClient\Collector\ByType; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Zlodes\PrometheusClient\Collector\Collector; | ||
use Zlodes\PrometheusClient\Exception\StorageWriteException; | ||
use Zlodes\PrometheusClient\Metric\Summary; | ||
use Zlodes\PrometheusClient\StopWatch\HRTimeStopWatch; | ||
use Zlodes\PrometheusClient\StopWatch\StopWatch; | ||
use Zlodes\PrometheusClient\Storage\DTO\MetricNameWithLabels; | ||
use Zlodes\PrometheusClient\Storage\DTO\MetricValue; | ||
use Zlodes\PrometheusClient\Storage\Storage; | ||
|
||
/** | ||
* @final | ||
*/ | ||
final class SummaryCollector extends Collector | ||
{ | ||
/** | ||
* @internal Zlodes\PrometheusClient\Collector | ||
* | ||
* @param class-string<StopWatch> $stopWatchClass | ||
*/ | ||
public function __construct( | ||
private readonly Summary $summary, | ||
private readonly Storage $storage, | ||
private readonly LoggerInterface $logger, | ||
private readonly string $stopWatchClass = HRTimeStopWatch::class, | ||
) { | ||
} | ||
|
||
public function update(float|int $value): void | ||
{ | ||
$summary = $this->summary; | ||
$labels = $this->getLabels(); | ||
|
||
try { | ||
$this->storage->persistSummary( | ||
new MetricValue( | ||
new MetricNameWithLabels($summary->getName(), $labels), | ||
$value, | ||
) | ||
); | ||
} catch (StorageWriteException $e) { | ||
$this->logger->error("Cannot persist Summary {$summary->getName()}: $e"); | ||
} | ||
} | ||
|
||
public function startTimer(): StopWatch | ||
{ | ||
$stopWatchClass = $this->stopWatchClass; | ||
|
||
return new $stopWatchClass(function (float $elapsed): void { | ||
$this->update($elapsed); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zlodes\PrometheusClient\Metric; | ||
|
||
use InvalidArgumentException; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class Summary extends Metric | ||
{ | ||
/** @var non-empty-list<float> */ | ||
private array $quantiles = [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]; | ||
|
||
public function getPrometheusType(): string | ||
{ | ||
return 'summary'; | ||
} | ||
|
||
public function getDependentMetrics(): array | ||
{ | ||
$selfName = $this->getName(); | ||
|
||
return [ | ||
"{$selfName}_sum", | ||
"{$selfName}_count", | ||
]; | ||
} | ||
|
||
/** | ||
* @return non-empty-list<float> | ||
*/ | ||
public function getQuantiles(): array | ||
{ | ||
return $this->quantiles; | ||
} | ||
|
||
/** | ||
* @param non-empty-list<float> $quantiles | ||
* | ||
* @return $this | ||
*/ | ||
public function withQuantiles(array $quantiles): self | ||
{ | ||
$this->validateQuantiles($quantiles); | ||
|
||
$summary = clone $this; | ||
$summary->quantiles = $quantiles; | ||
|
||
return $summary; | ||
} | ||
|
||
/** | ||
* @param non-empty-list<float> $quantiles | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function validateQuantiles(array $quantiles): void | ||
{ | ||
Assert::notEmpty($quantiles); | ||
Assert::allNumeric($quantiles); | ||
Assert::uniqueValues($quantiles); | ||
|
||
foreach ($quantiles as $quantile) { | ||
Assert::range($quantile, 0.0, 1.0, 'Quantile MUST be in range [0.0, 1.0]'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.