# 使用调试模式

APISIX 提供调试模式，帮助开发者更好地了解和排查网关的运行时行为。完整配置选项请参阅 [`debug.yaml`](https://github.com/apache/apisix/blob/3.21.0/conf/debug.yaml)。

本指南介绍如何启用调试模式，并按执行顺序检查插件阶段函数；还会说明如何添加钩子，将 APISIX 模块函数的输入参数和返回值写入日志。

## 前置条件[​](#前置条件 "前置条件的直接链接")

* 安装 [Docker](https://docs.docker.com/get-docker/)。
* 安装 [cURL](https://curl.se/)，用于向 APISIX 发送验证请求。
* 按照[快速入门教程](https://docs.apiseven.com/apisix/getting-started.md)在 Docker 或 Kubernetes 中启动新的 APISIX 实例。

## 启用基本调试模式[​](#启用基本调试模式 "启用基本调试模式的直接链接")

基本调试模式会在 `Apisix-Plugins` 响应头中报告插件阶段函数。每个条目使用 `plugin-name#phase` 形式；条目按运行时执行顺序排列，而不是按插件配置顺序排列。

调试模式默认关闭。要启用基本调试模式，请在 `debug.yaml` 中将 `basic.enable` 设为 `true`。

```
docker exec apisix-quickstart /bin/sh -c "echo '

basic:

  enable: true

http_filter:

  enable: false

hook_conf:

  enable: false

#END

' > /usr/local/apisix/conf/debug.yaml"
```

APISIX 会在启动时将 `debug.yaml` 加载到内存，并定期检查文件变更，因此更新该文件后无需手动重新加载 APISIX。

信息

`debug.yaml` 配置应以 `#END` 结尾。APISIX 使用此标记确认文件已写入完毕，再解析整个文件。

要验证配置，请创建一条未启用任何插件的路由：

* Admin API
* ADC

```
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '

{

  "id": "getting-started-anything",

  "uri": "/anything",

  "upstream": {

    "type": "roundrobin",

    "nodes": {

      "httpbin.org:80": 1

    }

  }

}'
```

为本示例使用独立的服务名称。ADC 按名称匹配服务，因此即使标签不同，复用 `httpbin Service` 也会更新既有服务。如果此前已同步过本示例，请在完成同步后删除 `httpbin Service` 上旧的 `getting-started-anything` 路由，并保留该服务上的其他所有路由。

adc-service.yaml

```
services:

  - name: Debug Mode Service

    labels:

      docs-example: debug-mode

    routes:

      - uris:

          - /anything

        name: getting-started-anything

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览该服务拥有的资源：

```
adc diff -f adc-service.yaml \

  --include-resource-type service \

  --label-selector docs-example=debug-mode
```

同步已审查的服务配置：

```
adc sync -f adc-service.yaml \

  --include-resource-type service \

  --label-selector docs-example=debug-mode
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应收到 `HTTP/1.1 200 OK` 响应，并看到以下响应头：

```
Content-Type: application/json

Content-Length: 390

Connection: keep-alive

Apisix-Plugins: no plugin

...
```

* Admin API
* ADC

更新路由并启用插件：

```
curl -i "http://127.0.0.1:9180/apisix/admin/routes/getting-started-anything" -X PATCH -d '

{

  "plugins": {

    "limit-count": {

      "count": 5,

      "time_window": 10,

      "rejected_code": 429

    }

  }

}'
```

此外，添加一个[全局插件](https://docs.apiseven.com/apisix/key-concepts/plugin-global-rules.md)：

```
curl -i "http://127.0.0.1:9180/apisix/admin/global_rules" -X PUT -d '{

  "id": "global-prometheus",

  "plugins": {

    "prometheus":{}

  }

}'
```

更新路由配置以启用插件：

adc-service.yaml

```
services:

  - name: Debug Mode Service

    labels:

      docs-example: debug-mode

    routes:

      - uris:

          - /anything

        name: getting-started-anything

        plugins:

          limit-count:

            count: 5

            time_window: 10

            rejected_code: 429

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

ADC 会将全局规则导出为一个以插件名为键的 map。导出内容不会保留原始规则 ID，也不会保留哪些插件曾归属于同一条规则。如果多条规则配置了同一个插件，导出结果只会保留一份配置。需要保留原始规则 ID 或分组时，请使用本节的 Admin API 标签页。

导出当前全局规则：

```
adc dump -o adc-global-rules.yaml \

  --include-resource-type global_rule
```

警告

ADC dump 可能包含 Admin API 返回的已解密敏感字段。请限制对导出文件的访问，并在完成变更后将其删除。

编辑导出的文件，在保留其他所有插件条目的同时添加 Prometheus 插件：

adc-global-rules.yaml (global\_rules excerpt)

```
global_rules:

  # Keep every other global plugin entry from the exported file.

  prometheus: {}
```

预览服务和全局规则，并确认差异中没有非预期的更新或删除：

```
adc diff -f adc-service.yaml \

  --include-resource-type service \

  --label-selector docs-example=debug-mode



adc diff -f adc-global-rules.yaml \

  --include-resource-type global_rule
```

同步已审查的服务和全局规则：

```
adc sync -f adc-service.yaml \

  --include-resource-type service \

  --label-selector docs-example=debug-mode



adc sync -f adc-global-rules.yaml \

  --include-resource-type global_rule
```

再次向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应收到 `HTTP/1.1 200 OK` 响应，其响应头类似如下：

```
Content-Type: application/json

Content-Length: 388

Connection: keep-alive

X-RateLimit-Limit: 5

X-RateLimit-Remaining: 4

X-RateLimit-Reset: 10

Apisix-Plugins: limit-count#access

...
```

信息

在发送响应头之前运行的阶段函数会被直接追踪。对于匹配的 Route 插件，由于 `body_filter`、`delayed_body_filter` 和 `log` 阶段稍后才运行，APISIX 会在发送响应头前推断这些条目。推断条目预期会执行，但仍可能在运行时被跳过，例如被 `_meta.filter` 跳过。

APISIX 无法在响应头之前追踪或推断的阶段函数会以 `warn` 级别写入错误日志。在此示例中，全局 Prometheus 插件在响应头之后运行，因此错误日志包含：

```
Apisix-Plugins: prometheus#log
```

由于 L4 流量没有 HTTP 响应头，Stream 插件阶段也会在错误日志中报告。

## 配置高级调试模式[​](#配置高级调试模式 "配置高级调试模式的直接链接")

`debug.yaml` 还提供了其他高级配置选项。

### 记录函数输入参数和返回值[​](#记录函数输入参数和返回值 "记录函数输入参数和返回值的直接链接")

你可以在 [APISIX 阶段](https://docs.apiseven.com/apisix/key-concepts/plugins.md#%E6%8F%92%E4%BB%B6%E6%89%A7%E8%A1%8C%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F)中添加钩子，记录输入参数和返回值，配置如下：

debug.yaml

```
basic:

  enable: true

http_filter:

  enable: false

hook_conf:

  enable: true

  name: hook_phase

  log_level: warn

  is_print_input_args: true

  is_print_return_value: true

hook_phase:

  apisix:

    - http_access_phase

    - http_header_filter_phase

    - http_body_filter_phase

    - http_log_phase

#END
```

❶ 启用钩子调试跟踪，记录目标模块函数的输入参数或返回值。

❷ 模块及函数列表的名称。

❸ 输入参数和返回值在错误日志中的严重性级别。

❹ 记录输入参数。

❺ 记录返回值。

❻ 模块名称和函数列表。

要验证配置，请向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应收到 `HTTP/1.1 200 OK` 响应，并在错误日志中看到以下信息：

```
call require("apisix").http_access_phase() args:{}

call require("apisix").http_access_phase() return:{}

call require("apisix").http_header_filter_phase() args:{}

call require("apisix").http_header_filter_phase() return:{}

call require("apisix").http_body_filter_phase() args:{}

call require("apisix").http_body_filter_phase() return:{}

call require("apisix").http_log_phase() args:{}

call require("apisix").http_log_phase() return:{}
```

### 动态应用高级调试设置[​](#动态应用高级调试设置 "动态应用高级调试设置的直接链接")

你也可以在请求包含特定请求头时，动态记录函数输入参数和返回值：

```
docker exec apisix-quickstart /bin/sh -c "echo '

basic:

  enable: true

http_filter:

  enable: true

  enable_header_name: X-APISIX-Dynamic-Debug

hook_conf:

  enable: true

  name: hook_phase

  log_level: warn

  is_print_input_args: true

  is_print_return_value: true

hook_phase:

  apisix:

    - http_access_phase

    - http_header_filter_phase

    - http_body_filter_phase

    - http_log_phase

#END

' > /usr/local/apisix/conf/debug.yaml"
```

❶ 启用 HTTP 过滤器，动态应用高级调试设置。

❷ 仅当请求包含 `X-APISIX-Dynamic-Debug` 请求头时，记录输入参数和返回值。

要验证配置，请在不携带该请求头的情况下向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应收到 `HTTP/1.1 200 OK` 响应，但错误日志中不会出现调试信息。

携带调试请求头再次发送请求：

```
curl -i "http://127.0.0.1:9080/anything" -H "X-APISIX-Dynamic-Debug: 1"
```

你应收到 `HTTP/1.1 200 OK` 响应，并在错误日志中看到以下信息：

```
call require("apisix").http_access_phase() args:{}

call require("apisix").http_access_phase() return:{}

call require("apisix").http_header_filter_phase() args:{}

call require("apisix").http_header_filter_phase() return:{}

call require("apisix").http_body_filter_phase() args:{}

call require("apisix").http_body_filter_phase() return:{}

call require("apisix").http_log_phase() args:{}

call require("apisix").http_log_phase() return:{}
```

## 按环境管理 `debug.yaml`[​](#按环境管理-debugyaml "按环境管理-debugyaml的直接链接")

你可以为不同环境使用不同的 `debug.yaml` 文件，例如开发环境使用 `conf/debug-dev.yaml`，生产环境使用 `conf/debug-prod.yaml`。设置 `APISIX_PROFILE` 变量即可选择对应文件。

有关详情，请参阅[按环境管理配置文件](https://docs.apiseven.com/apisix/reference/configuration-files.md#%E6%8C%89%E7%8E%AF%E5%A2%83%E7%AE%A1%E7%90%86%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6)。

## 下一步[​](#下一步 "下一步的直接链接")

现在你已经了解如何使用 APISIX 调试模式进行故障排查，以及该模式提供的各项调试功能。完整配置选项请参阅 [`debug.yaml`](https://github.com/apache/apisix/blob/3.21.0/conf/debug.yaml)。
