Kong 指标采集

Kong 是一个微服务网关工具,在微服务架构中,由于系统和服务的细分,导致系统结构变得非常复杂, 为了跨平台,为了统一集中管理api,同时为了不暴露后置服务。甚至有时候需要对请求进行一些安全、负载均衡、限流、熔断、灰度等中间操作,基于此类种种的客观需求一个类似综合前置的系统就产生了,这就是API网关(API Gateway)。API网关作为分散在各个业务系统微服务的API聚合点和统一接入点,外部请求通过访问这个接入点,即可访问内部所有的 REST API 服务。

Kong 的官方网站是 https://konghq.com/kong/ ,针对 Kong 的监控, Kong 提供了一个 Prometheus 的插件。这个插件是默认安装 Kong 的时候自带的,这个也可以升级。当前这个 Prometheus 的监控插件最新版本是 1.4.x ,兼容 Kong 的大部分版本。这个插件以Prometheus 公开格式公开与 Kong 和代理 Upstream 服务相关的度量,可以由 Prometheus 服务器抓取。

这个插件曾经是单独维护的,仓库地址是 https://github.com/Kong/kong-plugin-prometheus ,当版本到 1.1.0 以后,这个插件维护到 Kong 的主仓库里了,不再单独维护了,地址是 https://github.com/Kong/kong/tree/master/kong/plugins/prometheus

配置

这个插件是兼容无数据库模式的。

在无数据库模式下,需要声明式地配置 Kong Gateway。因此,Admin API主要是只读的,它能执行的唯一任务都与处理声明式配置相关,包括:

  • 在负载均衡器中设置目标的健康状态
  • 根据模式验证配置的结果
  • 通过 /config 接口上传声明式的配置

当 Kong 处于无数据库模式时,Prometheus 采集的监控数据中对于数据库的监控指标永远是正常状态。

对于 Kong 的 Prometheus 插件可以通过 Admin API 来进行打开,执行下边这个请求来打开全局的监控

curl -X POST http://{HOST}:8001/plugins/ \
    --data "name=prometheus"

如果使用了 KongA 等管理工具,首先打开管理工具,打开插件页面,然后找到添加插件,搜索 Prometheus 插件,然后打开,点击创建,就可以了。

另外有一些参数可以使用在插件的配置文件中使用

  • name:字符串类型,插件的名字,在这个例子中是 Prometheus。
  • service.id:字符串类型,服务的插件标记的 ID
  • enable:布尔值,这个插件是否开启的,默认值是 true 。
  • config.per_consumer:布尔值,默认值是 false,它决定是否应该收集每个消费者的指标。如果启用,kong_http_consumer_status 指标将被添加到导出指标中。

在最新的 1.4.x 版本中,新增了一个 data_plane_cluster_cert_expiry_timestamp 指标,另外Upstream Target health 指标新增了一个 subsystem label 。

这个 Prometheus 插件记录并公开节点级别的指标,你的 Prometheus 服务器将需要通过服务发现机制发现所有的Kong节点,并使用每个节点的配置 /metrics 接口获取数据。

可用的指标

目前可用的指标:

  • Status codes :Upstream服务返回的HTTP状态码。对于每个服务、所有服务以及每个用户的路由,这些都是可用的。
  • Latencies Histograms: 在 Kong 延迟的直方统计数据
    • Request:Kong和Upstream服务提供请求所需的总时间。
    • Kong:Kong路由一个请求并运行所有配置的插件所花费的时间
    • Upstream:Upstream服务响应请求所花费的时间。
  • Bandwidth:通过 Kong 的总带宽(出口/进口)。该指标可用于每个服务,并作为所有服务的总和。
  • DB reachability:值为0或1的一种规格类型,它表示一个Kong节点是否可以到达DB。
  • Connections:各种Nginx连接指标,如活动的,读的,写的,和接受的连接数。
  • Target Health:属于给定 Upstream 的目标及其子系统(http或stream)的健康状态(healthchecks_off、健康、不健康或dns_error)。
  • Dataplane Status:将数据的最后一次显示的时间戳、配置哈希值、配置同步状态和证书过期时间戳导出到控制页面。
  • Enterprise License Information:Kong Gateway license过期日期、特性和license签名。这些指标只能在Kong Gateway上输出。

请求安装了 Prometheus 插件的 Kong 的监控数据接口可以得到如下的指标示例:

curl -i http://localhost:8001/metrics
HTTP/1.1 200 OK
Server: openresty/1.15.8.3
Date: Tue, 7 Jun 2020 16:35:40 GMT
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *

# HELP kong_bandwidth Total bandwidth in bytes consumed per service/route in Kong
# TYPE kong_bandwidth counter
kong_bandwidth{type="egress",service="google",route="google.route-1"} 1277
kong_bandwidth{type="ingress",service="google",route="google.route-1"} 254
# HELP kong_datastore_reachable Datastore reachable from Kong, 0 is unreachable
# TYPE kong_datastore_reachable gauge
kong_datastore_reachable 1
# HELP kong_http_consumer_status HTTP status codes for customer per service/route in Kong
# TYPE kong_http_consumer_status counter
kong_http_consumer_status{service="s1",route="s1.route-1",code="200",consumer="<CONSUMER_USERNAME>"} 3
# HELP kong_http_status HTTP status codes per service/route in Kong
# TYPE kong_http_status counter
kong_http_status{code="301",service="google",route="google.route-1"} 2
# 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="google",route="google.route-1",le="00001.0"} 1
kong_latency_bucket{type="kong",service="google",route="google.route-1",le="00002.0"} 1
.
.
.
kong_latency_bucket{type="kong",service="google",route="google.route-1",le="+Inf"} 2
kong_latency_bucket{type="request",service="google",route="google.route-1",le="00300.0"} 1
kong_latency_bucket{type="request",service="google",route="google.route-1",le="00400.0"} 1
.
.
kong_latency_bucket{type="request",service="google",route="google.route-1",le="+Inf"} 2
kong_latency_bucket{type="upstream",service="google",route="google.route-1",le="00300.0"} 2
kong_latency_bucket{type="upstream",service="google",route="google.route-1",le="00400.0"} 2
.
.
kong_latency_bucket{type="upstream",service="google",route="google.route-1",le="+Inf"} 2
kong_latency_count{type="kong",service="google",route="google.route-1"} 2
kong_latency_count{type="request",service="google",route="google.route-1"} 2
kong_latency_count{type="upstream",service="google",route="google.route-1"} 2
kong_latency_sum{type="kong",service="google",route="google.route-1"} 2145
kong_latency_sum{type="request",service="google",route="google.route-1"} 2672
kong_latency_sum{type="upstream",service="google",route="google.route-1"} 527
# HELP kong_nginx_http_current_connections Number of HTTP connections
# TYPE kong_nginx_http_current_connections gauge
kong_nginx_http_current_connections{state="accepted"} 8
kong_nginx_http_current_connections{state="active"} 1
kong_nginx_http_current_connections{state="handled"} 8
kong_nginx_http_current_connections{state="reading"} 0
kong_nginx_http_current_connections{state="total"} 8
kong_nginx_http_current_connections{state="waiting"} 0
kong_nginx_http_current_connections{state="writing"} 1
# HELP kong_memory_lua_shared_dict_bytes Allocated slabs in bytes in a shared_dict
# TYPE kong_memory_lua_shared_dict_bytes gauge
kong_memory_lua_shared_dict_bytes{shared_dict="kong",kong_subsystem="http"} 40960
.
.
# HELP kong_memory_lua_shared_dict_total_bytes Total capacity in bytes of a shared_dict
# TYPE kong_memory_lua_shared_dict_total_bytes gauge
kong_memory_lua_shared_dict_total_bytes{shared_dict="kong",kong_subsystem="http"} 5242880
.
.
# HELP kong_memory_workers_lua_vms_bytes Allocated bytes in worker Lua VM
# TYPE kong_memory_workers_lua_vms_bytes gauge
kong_memory_workers_lua_vms_bytes{pid="7281",kong_subsystem="http"} 41124353
# HELP kong_data_plane_config_hash Config hash value of the data plane
# TYPE kong_data_plane_config_hash gauge
kong_data_plane_config_hash{node_id="d4e7584e-b2f2-415b-bb68-3b0936f1fde3",hostname="ubuntu-bionic",ip="127.0.0.1"} 1.7158931820287e+38
# HELP kong_data_plane_last_seen Last time data plane contacted control plane
# TYPE kong_data_plane_last_seen gauge
kong_data_plane_last_seen{node_id="d4e7584e-b2f2-415b-bb68-3b0936f1fde3",hostname="ubuntu-bionic",ip="127.0.0.1"} 1600190275
# HELP kong_data_plane_version_compatible Version compatible status of the data plane, 0 is incompatible
# TYPE kong_data_plane_version_compatible gauge
kong_data_plane_version_compatible{node_id="d4e7584e-b2f2-415b-bb68-3b0936f1fde3",hostname="ubuntu-bionic",ip="127.0.0.1",kong_version="2.4.1"} 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
# HELP kong_upstream_target_health Health status of targets of upstream. States = healthchecks_off|healthy|unhealthy|dns_error, value is 1 when state is populated.
kong_upstream_target_health{upstream="<upstream_name>",target="<target>",address="<ip>:<port>",state="healthchecks_off",subsystem="http"} 0
kong_upstream_target_health{upstream="<upstream_name>",target="<target>",address="<ip>:<port>",state="healthy",subsystem="http"} 1
kong_upstream_target_health{upstream="<upstream_name>",target="<target>",address="<ip>:<port>",state="unhealthy",subsystem="http"} 0
kong_upstream_target_health{upstream="<upstream_name>",target="<target>",address="<ip>:<port>",state="dns_error",subsystem="http"} 0

results matching ""

    No results matching ""