> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v5/di-16-zhang-shi-yong-springboot-actuator/16.2-xiao-fei-actuator-duan-dian/16.2.3-cha-kan-ying-yong-cheng-xu-huo-dong.md).

# 16.2.3 查看应用程序活动

关注应用程序中的活动是非常有用的。这些活动包括应用程序正在处理的各种 HTTP 请求，以及应用程序中的所有线程等等。为此，Actuator 提供了 `/httptrace`、`/threaddump` 和 `/heapdump` 端点。

`/heapdump` 端点可能是 Actuator 最难详细描述的端点。简单来说，它下载一个 gzip 压缩的 `HPROF` 堆转储文件，以便对内存或线程问题进行分析跟踪。因篇幅原因，且堆转储的使用是一个相当高级的特性，所以仅在这里提一下 `/heapdump` 端点。

**跟踪 HTTP 活动**

`/httptrace` 端点报告应用处理的最近 100 个请求。详细信息包括请求的方法和路径、时间戳、请求和响应的头信息，以及处理请求的耗时。

下面的 JSON 片段显示了 `/httptrace` 端点的返回：

```
{
  "traces": [
    {
      "timestamp": "2018-06-03T23:41:24.494Z",
      "principal": null,
      "session": null,
      "request": {
        "method": "GET",
        "uri": "http://localhost:8081/ingredients",
        "headers": {
          "Host": ["localhost:8081"],
          "User-Agent": ["curl/7.54.0"],
          "Accept": ["*/*"]
        },
        "remoteAddress": null
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": ["application/json;charset=UTF-8"]
        }
      },
      "timeTaken": 4
    },
  ...
  ]
}
```

这些信息可能对调试很有用，但更有趣的是，随着时间的推移跟踪这些数据，可以深入了解应用程序运行状况。可以了解在给定的时间段内，应用的繁忙程度，或根据响应状态的值，统计有多少成功的请求和失败的请求。在第 17 章中，您将看到 Spring Boot Admin 如何捕获这些信息，并且将这些 HTTP 跟踪信息可视化到一个运行图中。

**监视线程**

除了对 HTTP 请求进行跟踪之外，要确定应用程序的运行情况，监视线程活动状态也非常有用的。`/threaddump` 端点生成一个当前线程活动的快照。下面是 `/threaddump` 响应数据片段，可以了解此端点都提供了哪些信息：

```
{
  "threadName": "reactor-http-nio-8",
  "threadId": 338,
  "blockedTime": -1,
  "blockedCount": 0,
  "waitedTime": -1,
  "waitedCount": 0,
  "lockName": null,
  "lockOwnerId": -1,
  "lockOwnerName": null,
  "inNative": true,
  "suspended": false,
  "threadState": "RUNNABLE",
  "stackTrace": [
    {
      "methodName": "kevent0",
      "fileName": "KQueueArrayWrapper.java",
      "lineNumber": -2,
      "className": "sun.nio.ch.KQueueArrayWrapper",
      "nativeMethod": true
    },
    {
      "methodName": "poll",
      "fileName": "KQueueArrayWrapper.java",
      "lineNumber": 198,
      "className": "sun.nio.ch.KQueueArrayWrapper",
      "nativeMethod": false
    },
    ...
    ],
  "lockedMonitors": [
    {
      "className": "io.netty.channel.nio.SelectedSelectionKeySet",
      "identityHashCode": 1039768944,
      "lockedStackDepth": 3,
      "lockedStackFrame": {
        "methodName": "lockAndDoSelect",
        "fileName": "SelectorImpl.java",
        "lineNumber": 86,
        "className": "sun.nio.ch.SelectorImpl",
        "nativeMethod": false
      }
    },
  ...
  ],
  "lockedSynchronizers": [],
  "lockInfo": null
}
```

完整的线程转储报告，包括正在运行的应用程序中的每个线程。为了节省篇幅，这里的线程转储信息只显示了单个线程的一个简化条目。您可以看到，它包括线程有关的阻塞和锁定状态的详细信息，以及其他一些细节。还有一个堆栈跟踪，可以提供一些线程在代码的哪个区域所花费时间的信息。

因为 `/threaddump` 端点只会把请求时的线程快照信息取出来，很难全面了解线程的行为是如何随着时间的推移变化的。在第 17 章中，您将看到 Spring Boot Admin 如何实时监视 `/threaddump` 端点。
