Symfony捆绑软件,用于以Prometheus格式导出​​统计信息



在处理使用Symfony编写的各种微服务(并非如此)时,每次遇到导出Prometheus指标的需求时。 最初,我们只是在项目之间复制了相同的代码,但是当其中有三个以上时,我以为我不能继续这样(我不会费心解释为什么这样做不方便;我想每个人都可以自己理解)。

决定将整个经济分成一个单独的捆绑包: ArtprimaPrometheusMetricsBundle

为所有应用程序选择通用指标,我着手编写捆绑软件。 该客户端被选为Jimdo / prometheus_client_php客户端 ,它允许您用作统计存储:Redis(通过ext-redis),APCu(通过ext-apcu)和InMemory(在PHP内存中,仅在进程生命周期内)。 我将立即保留在实践中仅测试APCu(我需要具有每个实例的统计信息,而在Redis中将有共享的统计信息,除非将Redis部署到每个实例中)。 通常,InMemory,恕我直言,只能用于测试。

Jimdo / prometheus_client_php立即出现问题-很长时间没有发布,并且所需的功能(APCu)也无法作为发布使用。 在这方面,我做了client的fork ,不同之处仅在于它具有用于生成发行版的正确标签。

我遇到的第二个问题是Symfony的食谱。 您甚至不必插手正式的仓库,社区对拉取请求的请求将花费很长时间(目前该捆绑包已使用了将近一个月,而PR配方仍在考虑中)。 因此,在安装过程中,您需要手动进行必要的设置。 幸运的是,它们并不多。

实际上,您需要配置主要配置:

#config/packages/prometheus-metrics.yaml artprima_prometheus_metrics: # namespace     prometheus namespace: myapp #    type: in_memory #  : in_memory, apcu, redis #   ,         ignored_routes: [some_route_name, another_route_name] #     "redis" (   ) redis: host: 127.0.0.1 port: 6379 timeout: 0.1 read_timeout: 10 persistent_connections: false password: ~ 

然后,您需要导出路线:

 #    /metrics/prometheus app_metrics: resource: '@ArtprimaPrometheusMetricsBundle/Resources/config/routing.xml' 

或者,您可以手动配置路由:

 app_metrics: path: /mypath/mymetrics controller: Artprima\PrometheusMetricsBundle\Controller\MetricsController::prometheus 

做完了 因此,您的指标将在http [s]上提供:// your_host / metrics / prometheus(如果您未更改路径)。 一段时间后,您会得到如下信息:

指标示例
# HELP myapp_http_2xx_responses_total total 2xx response count
# TYPE myapp_http_2xx_responses_total counter
myapp_http_2xx_responses_total{action="GET-app_ep1"} 3911
myapp_http_2xx_responses_total{action="GET-app_ep2"} 29974
myapp_http_2xx_responses_total{action="GET-app_ep3"} 323
myapp_http_2xx_responses_total{action="GET-app_ep4"} 18
myapp_http_2xx_responses_total{action="GET-app_ep5"} 3627
myapp_http_2xx_responses_total{action="GET-app_search"} 5962
myapp_http_2xx_responses_total{action="GET-app_ep8"} 42967
myapp_http_2xx_responses_total{action="GET-app_variants"} 641
myapp_http_2xx_responses_total{action="all"} 87423
# HELP myapp_http_4xx_responses_total total 4xx response count
# TYPE myapp_http_4xx_responses_total counter
myapp_http_4xx_responses_total{action="GET-app_ep2"} 71
myapp_http_4xx_responses_total{action="GET-app_ep8"} 2584
myapp_http_4xx_responses_total{action="all"} 2660
# HELP myapp_http_requests_total total request count
# TYPE myapp_http_requests_total counter
myapp_http_requests_total{action="GET-app_ep1"} 3922
myapp_http_requests_total{action="GET-app_ep2"} 30098
myapp_http_requests_total{action="GET-app_ep3"} 468
myapp_http_requests_total{action="GET-app_ep4"} 18
myapp_http_requests_total{action="GET-app_ep5"} 3631
myapp_http_requests_total{action="GET-app_search"} 5964
myapp_http_requests_total{action="GET-app_ep8"} 45556
myapp_http_requests_total{action="GET-app_variants"} 644
myapp_http_requests_total{action="all"} 90301
# HELP myapp_instance_name app instance name
# TYPE myapp_instance_name gauge
myapp_instance_name{instance="myapp-77c4d96744-cf9nz"} 1
# HELP myapp_request_durations_histogram_seconds request durations in seconds
# TYPE myapp_request_durations_histogram_seconds histogram
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.025"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.05"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.075"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.1"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.25"} 59
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.5"} 1915
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="0.75"} 3115
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="1"} 3553
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="2.5"} 3879
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="5"} 3911
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="7.5"} 3911
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="10"} 3911
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep1",le="+Inf"} 3911
myapp_request_durations_histogram_seconds_count{action="GET-app_ep1"} 3911
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep1"} 2413.185
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.025"} 50
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.05"} 62
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.075"} 63
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.1"} 72
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.25"} 914
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.5"} 2761
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="0.75"} 14326
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="1"} 23571
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="2.5"} 29569
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="5"} 30045
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="7.5"} 30045
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="10"} 30045
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep2",le="+Inf"} 30045
myapp_request_durations_histogram_seconds_count{action="GET-app_ep2"} 30045
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep2"} 26552.86
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.005"} 141
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.01"} 144
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.025"} 144
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.05"} 144
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.075"} 145
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.1"} 145
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.25"} 145
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.5"} 199
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="0.75"} 408
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="1"} 442
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="2.5"} 468
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="5"} 468
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="7.5"} 468
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="10"} 468
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep3",le="+Inf"} 468
myapp_request_durations_histogram_seconds_count{action="GET-app_ep3"} 468
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep3"} 216.495
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.025"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.05"} 4
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.075"} 10
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.1"} 13
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.25"} 16
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.5"} 17
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="0.75"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="1"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="2.5"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="5"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="7.5"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="10"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep4",le="+Inf"} 18
myapp_request_durations_histogram_seconds_count{action="GET-app_ep4"} 18
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep4"} 2.291
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.025"} 1
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.05"} 16
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.075"} 97
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.1"} 269
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.25"} 2308
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.5"} 3351
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="0.75"} 3566
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="1"} 3612
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="2.5"} 3627
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="5"} 3627
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="7.5"} 3627
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="10"} 3627
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep5",le="+Inf"} 3627
myapp_request_durations_histogram_seconds_count{action="GET-app_ep5"} 3627
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep5"} 959.773
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.025"} 18
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.05"} 547
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.075"} 968
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.1"} 1387
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.25"} 3784
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.5"} 5377
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="0.75"} 5811
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="1"} 5913
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="2.5"} 5959
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="5"} 5960
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="7.5"} 5960
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="10"} 5960
myapp_request_durations_histogram_seconds_bucket{action="GET-app_search",le="+Inf"} 5962
myapp_request_durations_histogram_seconds_count{action="GET-app_search"} 5962
myapp_request_durations_histogram_seconds_sum{action="GET-app_search"} 1516.982
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.025"} 2724
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.05"} 8348
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.075"} 18828
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.1"} 28811
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.25"} 39709
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.5"} 43958
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="0.75"} 45161
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="1"} 45436
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="2.5"} 45541
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="5"} 45549
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="7.5"} 45549
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="10"} 45549
myapp_request_durations_histogram_seconds_bucket{action="GET-app_ep8",le="+Inf"} 45551
myapp_request_durations_histogram_seconds_count{action="GET-app_ep8"} 45551
myapp_request_durations_histogram_seconds_sum{action="GET-app_ep8"} 6125.712
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.005"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.01"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.025"} 0
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.05"} 252
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.075"} 451
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.1"} 511
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.25"} 583
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.5"} 631
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="0.75"} 640
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="1"} 641
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="2.5"} 641
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="5"} 641
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="7.5"} 641
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="10"} 641
myapp_request_durations_histogram_seconds_bucket{action="GET-app_variants",le="+Inf"} 641
myapp_request_durations_histogram_seconds_count{action="GET-app_variants"} 641
myapp_request_durations_histogram_seconds_sum{action="GET-app_variants"} 62.744
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.005"} 145
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.01"} 148
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.025"} 2942
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.05"} 9378
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.075"} 20567
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.1"} 31213
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.25"} 47523
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.5"} 58214
myapp_request_durations_histogram_seconds_bucket{action="all",le="0.75"} 73050
myapp_request_durations_histogram_seconds_bucket{action="all",le="1"} 83191
myapp_request_durations_histogram_seconds_bucket{action="all",le="2.5"} 89707
myapp_request_durations_histogram_seconds_bucket{action="all",le="5"} 90224
myapp_request_durations_histogram_seconds_bucket{action="all",le="7.5"} 90224
myapp_request_durations_histogram_seconds_bucket{action="all",le="10"} 90224
myapp_request_durations_histogram_seconds_bucket{action="all",le="+Inf"} 90228
myapp_request_durations_histogram_seconds_count{action="all"} 90228
myapp_request_durations_histogram_seconds_sum{action="all"} 37850.074


自然,很快可能没有足够的基本度量标准,因此您总是有机会编写自己的度量标准生成器(除了现有的度量标准生成器)并按如下所示导出它:

 # config/services.yaml App\Metrics\MyMetricsGenerator: tags: - { name: prometheus_metrics_bundle.metrics_generator } 

同时,您的生成器应实现Artprima\PrometheusMetricsBundle\Metrics\MetricsGeneratorInterface 。 您可以在Artprima\PrometheusMetricsBundle\Metrics\AppMetrics查看生成器的外观。 您在这里需要知道的主要事情collectRequestkernel.request事件上collectResponse方法,而在collectResponse事件上collectResponse方法。

我本质上接受批评。 都以技巧和PR的形式出现:-)

PS警告我们发明了自行车的评论:是的,我知道还有其他选择。 而且,很可能它们肯定会更好。 特别是其中之一是TweedeGolfPrometheusBundle 。 不幸的是,我不记得为什么我们没有选择这种解决方案,但是六个多月前,我们尝试了并拒绝了。 无论如何,我相信选择总是一件好事,这就是为什么我们的捆绑软件在OpenSource中发布的原因。

Source: https://habr.com/ru/post/zh-CN429800/


All Articles