Skip to content

Prometheus data parser to AST written on PHP8

License

Notifications You must be signed in to change notification settings

butschster/prometheus-parser

Repository files navigation

Prometheus metrics parser for PHP

PHP Version Require Latest Stable Version phpunit psalm Total Downloads

Github cover Prometheus parser

Welcome to the Prometheus Metrics Parser! This package makes it easy to extract valuable information from metrics in the Prometheus text-based format. Whether you're looking to analyze your metrics data, integrate it into other systems, or just want a better way to visualize it, this package has you covered.

With just a few lines of code, you can easily extract valuable insights from your Prometheus metrics.

Requirements

  • PHP 8.1 and above

Quick start

To install the package, run the following command from the root directory of your project:

composer require butschster/prometheus-parser

That's it!

Usage

To get started, simply pass a string containing your Prometheus metric data to the parse() method. The method will return a schema object with metric objects, each of which contains the following properties:

use Butschster\Prometheus\ParserFactory;

$parser = ParserFactory::create();

$schema = $parser->parse(<<<SCHEMA
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000

# Escaping in label values:
msdos_file_access_time_seconds{path="C:\\DIR\\FILE.TXT",error="Cannot find file:\n\"FILE.TXT\""} 1.458255915e9

# Minimalistic line:
metric_without_timestamp_and_labels 12.47

# A weird metric from before the epoch:
something_weird{problem="division by zero"} +Inf -3982045

# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320

# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693
    SCHEMA
);

Schema data

$metrics = $schema->getMetrics(); // array of Metric

$metrics['http_requests_total']->description; // The total number of HTTP requests.
$metrics['http_requests_total']->type; // counter
$metrics['http_requests_total']->name; // http_requests_total

foreach ($metrics['go_gc_duration_seconds'] as $metric) {
    $metric->name; // go_gc_duration_seconds
    $metric->value; // Value
    $metric->timestamp; // Timestamp
    $metric->lables; // Array of labels
}

Enjoy!


License

The MIT License (MIT). Please see LICENSE for more information.