指标 (metrics)

metrics 端点提供对应用程序指标的访问,以诊断应用程序记录的指标。 此端点不应在生产环境中被"抓取"或用作指标后端。 其目的是显示当前注册的指标,以便用户可以看到可用的指标、它们的当前值,以及触发某些操作是否会导致某些值的变化。 如果您想通过应用程序收集的指标来诊断应用程序,应该使用 外部指标后端。 在这种情况下,metrics 端点仍然可以派上用场。

获取指标名称

要获取可用指标的名称,向 /actuator/metrics 发送 GET 请求,如下面的基于 curl 的示例所示:

$ curl 'http://localhost:8080/actuator/metrics' -i -X GET

响应结果类似于以下内容:

HTTP/1.1 200 OK
Content-Type: application/vnd.spring-boot.actuator.v3+json
Content-Length: 154

{
  "names" : [ "jvm.buffer.count", "jvm.buffer.memory.used", "jvm.buffer.total.capacity", "jvm.memory.committed", "jvm.memory.max", "jvm.memory.used" ]
}

响应结构

响应包含指标名称的详细信息。 下表描述了响应的结构:

Path Type Description

names

Array

Names of the known metrics.

获取指标

要获取指标,向 /actuator/metrics/{metric.name} 发送 GET 请求,如下面的基于 curl 的示例所示:

$ curl 'http://localhost:8080/actuator/metrics/jvm.memory.max' -i -X GET

前面的示例获取名为 jvm.memory.max 的指标信息。 响应结果类似于以下内容:

HTTP/1.1 200 OK
Content-Disposition: inline;filename=f.txt
Content-Type: application/vnd.spring-boot.actuator.v3+json
Content-Length: 555

{
  "name" : "jvm.memory.max",
  "description" : "The maximum amount of memory in bytes that can be used for memory management",
  "baseUnit" : "bytes",
  "measurements" : [ {
    "statistic" : "VALUE",
    "value" : 2.399141885E9
  } ],
  "availableTags" : [ {
    "tag" : "area",
    "values" : [ "heap", "nonheap" ]
  }, {
    "tag" : "id",
    "values" : [ "CodeHeap 'profiled nmethods'", "G1 Old Gen", "CodeHeap 'non-profiled nmethods'", "G1 Survivor Space", "Compressed Class Space", "Metaspace", "G1 Eden Space", "CodeHeap 'non-nmethods'" ]
  } ]
}

查询参数

端点使用查询参数通过使用标签 深入 指标。 下表显示了支持的单个查询参数:

Parameter Description

tag

A tag to use for drill-down in the form name:value.

响应结构

响应包含指标的详细信息。 下表描述了响应的结构:

Path Type Description

name

String

Name of the metric

description

String

Description of the metric

baseUnit

String

Base unit of the metric

measurements

Array

Measurements of the metric

measurements[].statistic

String

Statistic of the measurement. (TOTAL, TOTAL_TIME, COUNT, MAX, VALUE, UNKNOWN, ACTIVE_TASKS, DURATION).

measurements[].value

Number

Value of the measurement.

availableTags

Array

Tags that are available for drill-down.

availableTags[].tag

String

Name of the tag.

availableTags[].values

Array

Possible values of the tag.

深入

要深入指标,向 /actuator/metrics/{metric.name} 发送 GET 请求,使用 tag 查询参数,如下面的基于 curl 的示例所示:

$ curl 'http://localhost:8080/actuator/metrics/jvm.memory.max?tag=area%3Anonheap&tag=id%3ACompressed+Class+Space' -i -X GET

前面的示例获取 jvm.memory.max 指标,其中 area 标签的值为 nonheapid 属性的值为 Compressed Class Space。 响应结果类似于以下内容:

HTTP/1.1 200 OK
Content-Disposition: inline;filename=f.txt
Content-Type: application/vnd.spring-boot.actuator.v3+json
Content-Length: 263

{
  "name" : "jvm.memory.max",
  "description" : "The maximum amount of memory in bytes that can be used for memory management",
  "baseUnit" : "bytes",
  "measurements" : [ {
    "statistic" : "VALUE",
    "value" : 1.073741824E9
  } ],
  "availableTags" : [ ]
}