使用Prometheus的导出器来获取KONG API的指标信息
Kong是一种开源的API Gateway工具,它拥有丰富的功能,用于管理请求处理的分配和认证管理,方便管理对多个API的访问。
本次我们将介绍如何使用Prometheus的Exporter来收集Kong的指标信息。
由于Kong已经在官方发布了插件,所以可以轻松地进行设置。
公式文档
启用插件
启用 Prometheus 插件并进行配置以便可使用。Prometheus 插件可配置为 KONG 的全局插件或针对特定由 Kong 管理的服务进行配置。
只需发送以下POST请求到KONG的管理API URL,即可激活。
$ curl -XPOST http://localhost:8001/plugins -d "name=prometheus" -d "enabled=true" | jq
{
"created_at": 1571917769,
"consumer": null,
"id": "7fb0ec16-da58-4816-a8a6-ad6caf3f2069",
"service": null,
"name": "prometheus",
"config": {},
"route": null,
"run_on": "first",
"tags": null,
"protocols": [
"http",
"https"
],
"enabled": true
}
$ curl -XPOST http://localhost:8001/services/<サービス名>/plugins -d "name=prometheus" | jq
{
"created_at": 1571930853,
"consumer": null,
"id": "b9273dac-b35c-4865-be04-3f5774296b53",
"service": {
"id": "6aa2debf-ac42-4ff2-970c-56a8d38333a7"
},
"name": "prometheus",
"config": {},
"route": null,
"run_on": "first",
"tags": null,
"protocols": [
"http",
"https"
],
"enabled": true
}
尝试获取指标信息
只需启用插件,然后在Kong的管理用API上公开/metrics路径,您就可以获取以下的指标信息。
以下是全球生效时的一个例子。
可以看到每个服务的指标都被捕获到。
$ curl -XGET http://localhost:8001/metrics
# HELP kong_bandwidth Total bandwidth in bytes consumed per service in Kong
# TYPE kong_bandwidth counter
kong_bandwidth{type="egress",service="core-api"} 228785
kong_bandwidth{type="egress",service="login"} 1074
kong_bandwidth{type="egress",service="logout"} 3754
kong_bandwidth{type="ingress",service="core-api"} 66784
kong_bandwidth{type="ingress",service="login"} 883
kong_bandwidth{type="ingress",service="logout"} 5048
# HELP kong_datastore_reachable Datastore reachable from Kong, 0 is unreachable
# TYPE kong_datastore_reachable gauge
kong_datastore_reachable 1
# HELP kong_http_status HTTP status codes per service in Kong
# TYPE kong_http_status counter
kong_http_status{code="200",service="core-api"} 108
kong_http_status{code="200",service="login"} 2
kong_http_status{code="200",service="logout"} 2
kong_http_status{code="500",service="logout"} 6
# HELP kong_latency Latency added by Kong, total request time and upstream latency for each service in Kong
# TYPE kong_latency histogram
kong_latency_bucket{type="kong",service="core-api",le="00001.0"} 84
kong_latency_bucket{type="kong",service="core-api",le="00002.0"} 100
kong_latency_bucket{type="kong",service="core-api",le="00005.0"} 104
・・・略
kong_latency_bucket{type="kong",service="core-api",le="+Inf"} 108
kong_latency_bucket{type="kong",service="login",le="00001.0"} 1
kong_latency_bucket{type="kong",service="login",le="00002.0"} 1
kong_latency_bucket{type="kong",service="login",le="00005.0"} 1
kong_latency_bucket{type="kong",service="login",le="00007.0"} 1
・・・略
kong_latency_bucket{type="kong",service="login",le="+Inf"} 2
kong_latency_bucket{type="kong",service="logout",le="00001.0"} 8
kong_latency_bucket{type="kong",service="logout",le="00002.0"} 8
kong_latency_bucket{type="kong",service="logout",le="00005.0"} 8
・・・略
kong_latency_bucket{type="kong",service="logout",le="+Inf"} 8
kong_latency_bucket{type="request",service="core-api",le="00001.0"} 42
kong_latency_bucket{type="request",service="core-api",le="00002.0"} 49
・・・略
kong_latency_bucket{type="request",service="core-api",le="+Inf"} 108
kong_latency_bucket{type="request",service="login",le="00001.0"} 1
kong_latency_bucket{type="request",service="login",le="00002.0"} 1
・・・略
kong_latency_bucket{type="request",service="login",le="+Inf"} 2
kong_latency_bucket{type="request",service="logout",le="00001.0"} 1
kong_latency_bucket{type="request",service="logout",le="00002.0"} 1
・・・略
kong_latency_bucket{type="request",service="logout",le="+Inf"} 8
kong_latency_bucket{type="upstream",service="core-api",le="00005.0"} 1
kong_latency_bucket{type="upstream",service="core-api",le="00007.0"} 1
・・・略
kong_latency_bucket{type="upstream",service="core-api",le="60000.0"} 54
kong_latency_bucket{type="upstream",service="core-api",le="+Inf"} 54
kong_latency_bucket{type="upstream",service="login",le="00070.0"} 1
・・・略
kong_latency_bucket{type="upstream",service="login",le="+Inf"} 1
kong_latency_bucket{type="upstream",service="logout",le="00020.0"} 1
kong_latency_bucket{type="upstream",service="logout",le="00025.0"} 1
・・・略
kong_latency_bucket{type="upstream",service="logout",le="+Inf"} 6
kong_latency_count{type="kong",service="core-api"} 108
kong_latency_count{type="kong",service="login"} 2
kong_latency_count{type="kong",service="logout"} 8
kong_latency_count{type="request",service="core-api"} 108
kong_latency_count{type="request",service="login"} 2
kong_latency_count{type="request",service="logout"} 8
kong_latency_count{type="upstream",service="core-api"} 54
kong_latency_count{type="upstream",service="login"} 1
kong_latency_count{type="upstream",service="logout"} 6
kong_latency_sum{type="kong",service="core-api"} 133
kong_latency_sum{type="kong",service="login"} 36
kong_latency_sum{type="kong",service="logout"} 7
kong_latency_sum{type="request",service="core-api"} 20993
kong_latency_sum{type="request",service="login"} 107
kong_latency_sum{type="request",service="logout"} 3699
kong_latency_sum{type="upstream",service="core-api"} 20817
kong_latency_sum{type="upstream",service="login"} 69
kong_latency_sum{type="upstream",service="logout"} 3392
# HELP kong_nginx_http_current_connections Number of HTTP connections
# TYPE kong_nginx_http_current_connections gauge
kong_nginx_http_current_connections{state="accepted"} 21
kong_nginx_http_current_connections{state="active"} 6
kong_nginx_http_current_connections{state="handled"} 21
kong_nginx_http_current_connections{state="reading"} 0
kong_nginx_http_current_connections{state="total"} 328
kong_nginx_http_current_connections{state="waiting"} 5
kong_nginx_http_current_connections{state="writing"} 1
# HELP kong_nginx_metric_errors_total Number of nginx-lua-prometheus errors
# TYPE kong_nginx_metric_errors_total counter
kong_nginx_metric_errors_total 0
支持的指标清单
似乎可以提取以下类似的指标。
メトリック名ラベル値の型概要kong_bandwidth{type=”egress”,service=”<サービス名>”}Counter出力パケットバイト数総量kong_bandwidth{type=”ingress”,service=”<サービス名>”}Counter入力パケットバイト数総量kong_datastore_reachable-GaugeKong用データストア(cassandra,postgresql)との接続状況(0がNG,1がOK)kong_http_status{code=”<ステータスコード>”,service=”<サービス名>”}Counterステータスコード別のアクセス件数kong_latency_bucket{type=”kong”,service=”<サービス名>”,le=”<上限値>”}HistogramKongがリクエストを受けてプラグインの処理をして振り分けを行う処理時間がleで指定した時間以内だった件数。kong_latency_bucket{type=”upstream”,service=”<サービス名>”,le=”<上限値>”}HistogramKongがリクエストをupstream先に投げて結果を受信するまでの処理時間がleで指定した時間以内だった件数。kong_latency_bucket{type=”request”,service=”<サービス名>”,le=”<上限値>”}HistogramKongがリクエストをupstream先に投げて結果を受信するまでの処理時間がleで指定した時間以内だった件数。kong_latency_count{type=<カウント対象の処理箇所タイプ>,service=”<サービス名>”Histogram該当のタイプのリクエストの受信件数合計kong_latency_sum{type=<カウント対象の処理箇所タイプ>,service=”<サービス名>”Histogram該当のタイプのリクエストのレイテンシ合計時間kong_nginx_http_current_connections{state=”<接続ステータス名>”}Gauge該当の接続ステータスのコネクション数kong_nginx_metric_errors_total-CounterPrometheus exporterのエラー件数
总结
只需启用插件,就可以轻松获取各个服务的详细信息和每个时间段的延迟次数等,可以广泛利用。由于需要通过Kong的管理API端点进行指标收集,因此需要注意对端点的访问限制。