プロメテウス形式で統計をエクスポートするためのSymfonyバンドル





Symfonyを使用して記述されたさまざまなマイクロ(およびそうではない)サービスに取り組んでいる間、Prometheusのメトリックをエクスポートする必要に出くわしました。 最初は、プロジェクト間で同じコードをコピーしただけでしたが、3つ以上ある場合は、このように進むことはできないと考えました(これがなぜ不便なのかを説明するつもりはありません。誰もがこれを自分で理解していると思います)。



この経済全体を個別のバンドルArtprimaPrometheusMetricsBundleに分離することが決定されました



すべてのアプリケーションに共通のメトリックを選択して、バンドルの作成に取りかかりました。 クライアントJimdo / prometheus_client_phpクライアントとして選択されまし 。これにより、Redis(ext-redis経由)、APCu(ext-apcu経由)、InMemory(PHPメモリ内、プロセスの存続期間のみ)を統計ストレージとして使用できます。 APCuのみが実際にテストされるように、予約をすぐに行います(Redisが各インスタンスにデプロイされない限り、Redisでは共有の統計が必要ですが、インスタンスごとの統計が必要です)。 また、InMemoryの一般的なIMHOは、テストにのみ使用できます。



Jimdo / prometheus_client_phpにすぐに問題がありました-長い間リリースがなく、リリースの形で必要な機能(APCu)が利用できません。 この点で、私はクライアントのフォークを作成しました。これは、リリースを生成するための適切なタグを持っているという点でのみ異なります



私が遭遇した2番目の問題は、Symfonyのレシピです。 公式リポジトリに手を加える必要さえありません。プルリクエストに対するコミュニティリクエストの完了には非常に長い時間がかかります(現在、バンドルはほぼ1か月前であり、 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\MetricsGeneratorInterface



Artprima\PrometheusMetricsBundle\Metrics\AppMetrics



でジェネレーターがどのように見えるかを確認できます。 ここで知っておくべき主なことは、 collectRequest



メソッドcollectRequest



kernel.request



イベントでcollectResponse



れ、 kernel.terminate



メソッドがkernel.terminate



イベントでcollectResponse



れることkernel.terminate







私は本質的に批判を受け入れます。 ヒントとPRの両方の形で:-)



PS私たちが自転車を発明したというコメントに警告します。はい、別の方法があることは知っています。 そして、おそらく、彼らは確かに優れています。 特に、それらの1つはTweedeGolfPrometheusBundleです。 残念ながら、なぜこのソリューションを選択しなかったのかは覚えていませんが、6か月以上前に試して拒否しました。 いずれにせよ、選択肢があることは常に良いことだと思います。それが、私たちのバンドルがOpenSourceで公開された理由です。



All Articles