-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* upd: wip prometheus support * upd: wip. basic prometheus counter metric support * upd: wip. flush and get as prometheus formatted text * upd: cleanup composer. add more tests. increase php version requirement to use enums. * add: description prop to metric. wipe registry before formatting. update readme * upd: driver cleanup and description assertion in test * upd: format underscore all metric names and labels. fix tests * fix: readme * upd: remove not needed dev dependencies, we have them in suggestions so its up to the client which one to install
- Loading branch information
Showing
12 changed files
with
339 additions
and
15 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
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,79 @@ | ||
<?php | ||
|
||
namespace STS\Metrics\Drivers; | ||
|
||
use Illuminate\Support\Str; | ||
use Prometheus\Collector; | ||
use Prometheus\CollectorRegistry; | ||
use Prometheus\Exception\MetricsRegistrationException; | ||
use Prometheus\RendererInterface; | ||
use STS\Metrics\Metric; | ||
use STS\Metrics\MetricsServiceProvider; | ||
use STS\Metrics\MetricType; | ||
use UnhandledMatchError; | ||
|
||
/** | ||
* The idea of this driver is to use Prometheus\CollectorRegistry only to format the already collected metrics in prometheus format. | ||
*/ | ||
class PrometheusDriver extends AbstractDriver | ||
{ | ||
public function __construct(readonly private RendererInterface $renderer, readonly private CollectorRegistry $registry) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @throws MetricsRegistrationException | ||
* @throws UnhandledMatchError | ||
*/ | ||
public function format(Metric $metric): Collector | ||
{ | ||
$namespace = Str::snake($metric->getNamespace()); | ||
$name = Str::snake($metric->getName()); | ||
$labelKeys = array_map(fn ($tag) => Str::snake($tag) , array_keys($metric->getTags())); | ||
return match ($metric->getType()) { | ||
MetricType::COUNTER => (function () use ($namespace, $name, $labelKeys, $metric) { | ||
$counter = $this->registry->getOrRegisterCounter($namespace, $name, $metric->getDescription() ?? '', $labelKeys); | ||
$counter->incBy($metric->getValue(), array_values($metric->getTags())); | ||
return $counter; | ||
})(), | ||
MetricType::GAUGE => (function () use ($namespace, $name, $labelKeys, $metric) { | ||
$gauge = $this->registry->getOrRegisterGauge($namespace, $name, $metric->getDescription() ?? '', $labelKeys); | ||
$gauge->set($metric->getValue(), array_values($metric->getTags())); | ||
return $gauge; | ||
})(), | ||
default => throw new UnhandledMatchError($metric->getType()), | ||
}; | ||
} | ||
|
||
public function flush(): static | ||
{ | ||
$this->metrics = []; | ||
$this->registry->wipeStorage(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Renders all collected metrics in prometheus format. | ||
* The result can be directly exposed on HTTP endpoint, for polling by Prometheus. | ||
* | ||
* If execution is thrown no matter in the context of long-running process or http request, | ||
* there are handlers in @see MetricsServiceProvider to call flush and clear the state | ||
* | ||
* @return string | ||
* @throws MetricsRegistrationException | ||
* @throws UnhandledMatchError | ||
*/ | ||
public function formatted(): string | ||
{ | ||
// Always before formatting all metrics we need to wipe the registry storage and to register the metrics again. | ||
// If we don't, we will increment existing counters instead of replacing them. | ||
$this->registry->wipeStorage(); | ||
|
||
collect($this->getMetrics())->each(function (Metric $metric) { | ||
$this->format($metric); | ||
}); | ||
|
||
return $this->renderer->render($this->registry->getMetricFamilySamples()); | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace STS\Metrics; | ||
|
||
enum MetricType { | ||
case COUNTER; | ||
case SUMMARY; | ||
case GAUGE; | ||
case HISTOGRAM; | ||
} |
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
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.