Metrics插件

说明

  • Metrics插件插件是网关用来监控自身运行状态(JVM相关),请求的响应迟延,QPSTPS等相关metrics

技术方案

  • 流程图 Metrics插件 - 图1

  • 异步或者同步的方式,在 Apache ShenYu 网关里面进行 metrics 埋点。

  • prometheus 服务端通过 http 请求来拉取 metrics,再使用 Grafana 展示。

插件使用

  • 在网关的 pom.xml 文件中添加 metrics 的依赖。
  1. <!-- apache shenyu metrics plugin starter-->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-spring-boot-starter-plugin-metrics</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <!-- apache shenyu metrics plugin end-->
  • 在网关的配置yaml文件中编辑如下内容:
  1. shenyu:
  2. metrics:
  3. enabled: false #设置为 true 表示开启
  4. name : prometheus
  5. host: 127.0.0.1 #暴露的ip
  6. port: 8090 #暴露的端口
  7. jmxConfig: #jmx配置
  8. props:
  9. jvm_enabled: true #开启jvm的监控指标

metrics信息

  • 所有的JVM,线程,内存,等相关信息都会埋点,可以在 Grafana 面板中,新增一个 JVM 模块,则会完全展示 具体请看:https://github.com/prometheus/jmx_exporter

  • 另外还有如下自定义的 metrics

名称类型标签名称说明
shenyu_request_totalCounternonecollecting all requests of Apache ShenYu Gateway
shenyu_request_throw_totalCounternonecollecting all exception requests of Apache ShenYu Gateway
shenyu_request_type_totalCounterpath,typecollecting all matched requests of monitor
shenyu_execute_latency_millishistogramnoneShenYu gateway execute time interval

jmx 指标

nametypelabalshelp
jmx_config_reload_success_totalcounterNumber of times configuration have successfully been reloaded.
jmx_config_reload_failure_totalcounterNumber of times configuration have failed to be reloaded.
jmx_scrape_duration_secondsgaugeTime this JMX scrape took, in seconds.
jmx_scrape_errorgaugeNon-zero if this scrape failed.
jmx_scrape_cached_beansgaugeNumber of beans with their matching rule cached
jmx_scrape_duration_secondsgaugeTime this JMX scrape took, in seconds.
jmx_scrape_errorgaugeNon-zero if this scrape failed.
jmx_scrape_cached_beansgaugeNumber of beans with their matching rule cached

jvm 指标

StandardExports

nametypelabelshelp
process_cpu_seconds_totalcounterTotal user and system CPU time spent in seconds.
process_start_time_secondsgaugeStart time of the process since unix epoch in seconds.
process_open_fdsgaugeNumber of open file descriptors.
process_max_fdsgaugeMaximum number of open file descriptors.
process_virtual_memory_bytesgaugeVirtual memory size in bytes.
process_resident_memory_bytesgaugeResident memory size in bytes.

MemoryPoolsExports

nametypelabelshelp
jvm_memory_objects_pending_finalizationgauge{area=”heap|nonheap”}The number of objects waiting in the finalizer queue.
jvm_memory_bytes_usedgauge{area=”heap|nonheap”}Used bytes of a given JVM memory area.
jvm_memory_bytes_committedgauge{area=”heap|nonheap”}Committed (bytes) of a given JVM memory area.
jvm_memory_bytes_maxgauge{area=”heap|nonheap”}Max (bytes) of a given JVM memory area.
jvm_memory_bytes_initgauge{area=”heap|nonheap”}Initial bytes of a given JVM memory area.
jvm_memory_pool_bytes_usedgauge{pool}Used bytes of a given JVM memory pool.
jvm_memory_pool_bytes_committedgauge{pool}Committed bytes of a given JVM memory pool.
jvm_memory_pool_bytes_maxgauge{pool}Max bytes of a given JVM memory pool.
jvm_memory_pool_bytes_initgauge{pool}Initial bytes of a given JVM memory pool.
jvm_memory_pool_collection_used_bytesgauge{pool}Used bytes after last collection of a given JVM memory pool.
jvm_memory_pool_collection_committed_bytesgauge{pool}Committed after last collection bytes of a given JVM memory pool.
jvm_memory_pool_collection_max_bytesgauge{pool}Max bytes after last collection of a given JVM memory pool.
jvm_memory_pool_collection_init_bytesgauge{pool}Initial after last collection bytes of a given JVM memory pool.

MemoryAllocationExports

nametypelabelshelp
jvm_memory_pool_allocated_bytes_totalcounter{pool}Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously.

BufferPoolsExports

nametypelabelshelp
jvm_buffer_pool_used_bytesgauge{pool}Used bytes of a given JVM buffer pool.
jvm_buffer_pool_capacity_bytesgauge{pool}Bytes capacity of a given JVM buffer pool.
jvm_buffer_pool_used_buffersgauge{pool}Used buffers of a given JVM buffer pool.

GarbageCollectorExports

nametypelabelshelp
jvm_gc_collection_secondssummary{gc}Time spent in a given JVM garbage collector in seconds.

ThreadExports

nametypelabelshelp
jvm_threads_currentgaugeCurrent thread count of a JVM
jvm_threads_daemongaugeDaemon thread count of a JVM
jvm_threads_peakgaugePeak thread count of a JVM
jvm_threads_started_totalcounterStarted thread count of a JVM
jvm_threads_deadlockedgaugeCycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers
jvm_threads_deadlocked_monitorgaugeCycles of JVM-threads that are in deadlock waiting to acquire object monitors
jvm_threads_stategauge{state}Current count of threads by state

ClassLoadingExports

nametypelabelshelp
jvm_classes_loadedgaugeThe number of classes that are currently loaded in the JVM
jvm_classes_loaded_totalcounterThe total number of classes that have been loaded since the JVM has started execution
jvm_classes_unloaded_totalcounterThe total number of classes that have been unloaded since the JVM has started execution

VersionInfoExports

nametypelabelshelp
jvminfo{version(java.runtime.version),vendor(java.vm.vendor),runtime(java.runtime.name)}VM version info

收集 metrics

用户需部署Prometheus 服务来采集

windows 环境下安装Prometheus

  • 选择对应环境的 下载地址安装

  • 修改配置文件:prometheus.yml

    1. scrape_configs:
    2. - job_name: 'Apache ShenYu'
    3. # metrics_path defaults to '/metrics'
    4. # scheme defaults to 'http'.
    5. static_configs:
    6. - targets: ['localhost:8090'] # metrics of apache shenyu are exposed on port 8090 by default

注: job_namemonitor插件配置的metricsName相对应

  • 配置完成之后,在 windows 下可以直接双击 prometheus.exe 启动即可,默认启动端口为 9090 ,访问 http://localhost:9090/ ,点击status->Targets,验证是否成功。

Metrics插件 - 图2

macos 环境下安装Prometheus

  • 使用brew 安装 prometheus,安装完成后prometheushomebrew下的 Cellar 文件夹中。
  1. brew install prometheus
  • 在prometheus.yml文件所在位置执行如下命令即可启动prometheus。
  1. prometheus --config.file=prometheus.yml &

访问 http://localhost:9090/ 验证是否正常启动。

下面介绍如何安装 Grafana

windows环境下安装Grafana

  • 安装 Grafana

下载地址 解压进入 bin 目录然后双击 grafana-server.exe 运行 访问 http://localhost:3000/?orgId=1 admin/admin 验证是否成功

macos环境下安装Grafana

  • 使用brew 安装 grafana 。
  1. brew install grafana
  • 以服务方式启动grafana
  1. brew services start grafana

访问 http://localhost:3000/ 验证是否正常启动。

使用Grafana查看监控数据

配置数据源,选择prometheus,注意数据源名字为prometheus。

Metrics插件 - 图3

Metrics插件 - 图4

  • 配置自定义metric面板request_totalhttp_request_total

点击 Create - Import 输入dashboards面板json配置

最终自定义 Http 请求监控面板效果如下:

Metrics插件 - 图5

Metrics插件 - 图6

Metrics插件 - 图7

Metrics插件 - 图8

Metrics插件 - 图9