butschster/prometheus-parser

使用PHP 8编写的Prometheus解析器

1.0.1 2022-11-02 21:19 UTC

This package is auto-updated.

Last update: 2024-09-14 14:05:43 UTC


README

PHP Version Require Latest Stable Version phpunit psalm Total Downloads

Github cover Prometheus parser

欢迎来到Prometheus度量解析器!此包可轻松从基于文本的Prometheus格式中的度量中提取有价值的信息。无论您是想分析度量数据、将其集成到其他系统中,还是只想更好地可视化它,此包都能满足您的需求。

只需几行代码,您就可以轻松地从Prometheus度量中提取有价值的信息。

要求

  • PHP 8.1及以上版本

快速开始

要从项目中安装此包,请在项目根目录下运行以下命令

composer require butschster/prometheus-parser

就是这样!

用法

要开始使用,只需将包含您的Prometheus度量数据的字符串传递给parse()方法。该方法将返回一个包含度量对象的模式对象,每个度量对象包含以下属性

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
);

模式数据

$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
}

享受吧!

许可证

MIT许可证(MIT)。请参阅LICENSE获取更多信息。