# 16.2.4 利用运行时指标

`/metrics` 端点能够报告正在运行应用程序的多项指标，包括有关内存、处理器、垃圾收集以及 HTTP 请求的相关指标。Actuator 提供了二十多种开箱即用的度量指标类别。向 `/metrics` 发出 GET 请求，返回的度量指标列表示例如下：

```
$ curl localhost:8081/actuator/metrics | jq
{
  "names": [
    "jvm.memory.max",
    "process.files.max",
    "jvm.gc.memory.promoted",
    "http.server.requests",
    "system.load.average.1m",
    "jvm.memory.used",
    "jvm.gc.max.data.size",
    "jvm.memory.committed",
    "system.cpu.count",
    "logback.events",
    "jvm.buffer.memory.used",
    "jvm.threads.daemon",
    "system.cpu.usage",
    "jvm.gc.memory.allocated",
    "jvm.threads.live",
    "jvm.threads.peak",
    "process.uptime",
    "process.cpu.usage",
    "jvm.classes.loaded",
    "jvm.gc.pause",
    "jvm.classes.unloaded",
    "jvm.gc.live.data.size",
    "process.files.open",
    "jvm.buffer.count",
    "jvm.buffer.total.capacity",
    "process.start.time"
  ]
}
```

由于涉及的指标太多，不可能在本文中全部讨论。作为如何使用 `/metrics` 端点的示例，我们仅讨论 `http.server.requests` 指标。

如果您不是简单地请求 `/metrics`，而是请求 `/metrics/{METRICS CATEGORY}`，您将收到关于该指标的更多详细信息。以 `http.server.requests` 为例，GET 请求 `/metrics/http.server.requests` 的返回的数据如下所示：

```
$ curl localhost:8081/actuator/metrics/http.server.requests
{
  "name": "http.server.requests",
  "measurements": [
    { "statistic": "COUNT", "value": 2103 },
    { "statistic": "TOTAL_TIME", "value": 18.086334315 },
    { "statistic": "MAX", "value": 0.028926313 }
  ],
  "availableTags": [
    { "tag": "exception",
      "values": [ "ResponseStatusException",
                  "IllegalArgumentException", "none" ] },
    { "tag": "method", "values": [ "GET" ] },
    { "tag": "uri",
      "values": [
        "/actuator/metrics/{requiredMetricName}",
        "/actuator/health", "/actuator/info", "/ingredients",
        "/actuator/metrics", "/**" ] },
    { "tag": "status", "values": [ "404", "500", "200" ] }
  ]
}
```

该响应最重要的部分是 measurements 部分，该部分包括请求类别的所有度量。在本例中，它报告已收到 2103 个 HTTP 请求。处理这些请求的总时间为 18.086334315 秒，处理其中某个请求所花费的最大时间为 0.028926313 秒。

查看这些通用指标很有趣，您还可以使用 availableTags 下列出的标记，进一步缩小结果范围。例如，您知道已经有 2103 个请求，但还不知道分别有多少个请求导致了 HTTP 200、HTTP 404、HTTP 500。使用状态标记，您可以获取导致 HTTP 404 状态的所有请求的指标，如下所示：

```
$ curl localhost:8081/actuator/metrics/http.server.requests?tag=status:404
{
  "name": "http.server.requests",
  "measurements": [
    { "statistic": "COUNT", "value": 31 },
    { "statistic": "TOTAL_TIME", "value": 0.522061212 },
    { "statistic": "MAX", "value": 0 }
  ],
  "availableTags": [
    { "tag": "exception",
      "values": [ "ResponseStatusException", "none" ] },
    { "tag": "method", "values": [ "GET" ] },
    { "tag": "uri",
      "values": [
            "/actuator/metrics/{requiredMetricName}", "/**" ] }
  ]
}
```

通过使用 tag 属性指定标记名称和值，您现在可以看到针对导致 HTTP 404 响应的请求度量。返回数据表明有 31 个请求导致 404，服务时间为 0.522061212 秒。此外，很明显，一些失败的请求是对 `/actuator/metrics/{requiredMetricsName}` 的 GET 请求（尽管不清楚{requiredMetricsName} 路径变量解析成了什么值）。还有一些是另一个路径，由 `/**` 通配符路径匹配处理了。

如果您想知道这些 HTTP 404 响应中有多少响应是对 `/**` 路径的调用？可进一步过滤，只需在请求中增加 tag，像这样：

```
% curl "localhost:8081/actuator/metrics/http.server.requests?tag=status:404&tag=uri:/**"
{
  "name": "http.server.requests",
  "measurements": [
    { "statistic": "COUNT", "value": 30 },
    { "statistic": "TOTAL_TIME", "value": 0.519791548 },
    { "statistic": "MAX", "value": 0 }
  ],
  "availableTags": [
    { "tag": "exception", "values": [ "ResponseStatusException" ] },
    { "tag": "method", "values": [ "GET" ] }
  ]
}
```

现在您可以看到，有 30 个请求与 `/**` 匹配，且导致了 HTTP 404 响应。处理这些响应总共花费了 0.519791548 秒。

您还将注意到，当您细化请求时，可用的标记会越少。提供的可用标记仅与显示的指标捕获的请求相匹配。在这种情况下，exception 和 method 标记都只有一个值；很明显，所有 30 个请求都是 GET 请求，都由于 ResponseStatusException 异常导致了状态 404。

浏览 `/metrics` 端点可能是一项棘手的工作，但只要稍加练习，要获得您需要的数据并非不可能。在第 17 章中，您将看到如何通过 Spring Boot Admin 来使获取 `/metrics` 端点的数据变得更加容易。

尽管 Actuator 端点提供的信息，有助于了解 Spring Boot 应用程序的内部运行情况，但它们并不适合人工使用。作为 REST 端点，它们旨在供其他应用程序使用，可能是 UI。考虑到这一点，让我们看看如何将 Actuator 信息，展示在一个用户友好的 web 应用程序中。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v5/di-16-zhang-shi-yong-springboot-actuator/16.2-xiao-fei-actuator-duan-dian/16.2.4-li-yong-yun-hang-shi-zhi-biao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
