# 在访问日志中包含消费者标签

消费者标签是关联到[消费者和凭证](https://docs.apiseven.com/api7-gateway/key-concepts/consumers-and-credentials.md)的键值对，可用于表示部门、组织或服务等级等元数据。默认情况下，访问日志仅包含消费者用户名。本指南介绍如何在网关访问日志中包含消费者标签，以便进行更精细的流量分析。

## 工作原理[​](#工作原理 "工作原理的直接链接")

该方案使用以下三个组件：

1. **自定义 NGINX 变量**：在网关配置中声明变量（例如 `$consumer_company`）并设置默认值。
2. **`serverless-pre-function`**：全局插件在运行时读取消费者标签并将其赋值给该变量。
3. **访问日志格式**：在日志输出中包含自定义变量。

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

* 正在运行的 API7 网关部署。
* 可以访问网关的 `config.yaml` 配置文件。
* [从控制台获取 Token](https://docs.apiseven.com/api7-gateway/reference/obtain-dashboard-token.md)。

## 步骤一：声明自定义变量[​](#步骤一声明自定义变量 "步骤一：声明自定义变量的直接链接")

在网关的 `config.yaml` 中声明自定义 NGINX 变量并设置默认值。可以为不同的标签声明多个变量：

conf/config.yaml

```
nginx_config:

  http_server_location_configuration_snippet: |

    set $consumer_company "-";
```

## 步骤二：更新访问日志格式[​](#步骤二更新访问日志格式 "步骤二：更新访问日志格式的直接链接")

在同一配置文件中，将自定义变量添加到访问日志格式中：

conf/config.yaml

```
nginx_config:

  http:

    access_log_format: >-

      $remote_addr - $remote_user [$time_local] $http_host

      "$request" $status $body_bytes_sent $request_time

      "$http_referer" "$http_user_agent"

      $upstream_addr $upstream_status $upstream_response_time

      "$consumer_company"
```

重新加载或重启网关使配置生效。

## 步骤三：使用 Serverless Function 赋值[​](#步骤三使用-serverless-function-赋值 "步骤三：使用 Serverless Function 赋值的直接链接")

将 `serverless-pre-function` 配置为全局插件，以读取消费者标签并将其赋值给自定义变量。函数应在 `log` 阶段运行，这样才能读取身份认证时识别出的消费者：

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/global_rules/serverless-pre-function?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "plugins": {

      "serverless-pre-function": {

        "phase": "log",

        "functions": [

          "return function (conf, ctx) ngx.var.consumer_company = ctx.consumer and ctx.consumer.labels and ctx.consumer.labels[\"company\"] or \"unknown\" end"

        ]

      }

    }

  }'
```

adc.yaml

```
global_rules:

  serverless-pre-function:

    phase: log

    functions:

      - >-

        return function (conf, ctx)

          ngx.var.consumer_company = ctx.consumer

            and ctx.consumer.labels

            and ctx.consumer.labels["company"]

            or "unknown"

        end
```

此命令只更新全局规则；服务、路由和消费者保持不变。文件中未声明的全局规则会被删除。请先预览差异：

```
adc diff -f adc.yaml \

  --include-resource-type global_rule
```

审查变更后同步配置：

```
adc sync -f adc.yaml \

  --include-resource-type global_rule
```

该函数读取消费者的 `company` 标签。如果消费者没有该标签，或未识别出消费者（例如未经身份认证的请求），变量值默认为 `"unknown"`。

## 步骤四：配置消费者和受保护路由[​](#step-4-configure-a-consumer-and-a-protected-route "步骤四：配置消费者和受保护路由的直接链接")

创建带有 `company` 标签和 Key 身份认证凭证的消费者，然后创建受 `key-auth` 保护的路由：

* Admin API
* ADC

```
# 创建带标签的消费者

curl -k "https://localhost:7443/apisix/admin/consumers/john?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "username": "john",

    "labels": {

      "company": "smart-technology"

    }

  }'



# 添加 key-auth 凭证

curl -k "https://localhost:7443/apisix/admin/consumers/john/credentials/primary-key?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "primary-key",

    "plugins": {

      "key-auth": {

        "key": "john-key"

      }

    }

  }'



# 创建带上游的服务

curl -k "https://localhost:7443/apisix/admin/services/consumer-label-demo?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "consumer-label-demo",

    "upstream": {

      "type": "roundrobin",

      "nodes": [

        { "host": "httpbin.org", "port": 80, "weight": 1 }

      ]

    }

  }'



# 创建受 key-auth 保护的路由

curl -k "https://localhost:7443/apisix/admin/routes/consumer-label-demo?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "consumer-label-demo",

    "paths": ["/anything"],

    "service_id": "consumer-label-demo",

    "plugins": {

      "key-auth": {}

    }

  }'
```

adc.yaml

```
consumers:

  - username: john

    labels:

      company: smart-technology

      docs-example: john

    credentials:

      - name: primary-key

        type: key-auth

        config:

          key: john-key

services:

  - name: consumer-label-demo

    routes:

      - name: consumer-label-demo

        uris:

          - /anything

        plugins:

          key-auth: {}

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    labels:

      docs-example: john
```

预览本示例管理的资源变更，确认没有非预期的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --include-resource-type consumer \

  --label-selector docs-example=john
```

审查变更后同步配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --include-resource-type consumer \

  --label-selector docs-example=john
```

## 步骤五：验证[​](#步骤五验证 "步骤五：验证的直接链接")

使用有效的消费者密钥发送请求：

```
curl -i "http://127.0.0.1:9080/anything" -H "apikey: john-key"
```

响应应为 `HTTP/1.1 200 OK`。访问日志条目中包含消费者标签：

```
192.168.1.1 - - [18/Mar/2025:09:17:28 +0000] 127.0.0.1:9080 "GET /anything HTTP/1.1" 200 508 1.260 "-" "curl/8.6.0" 13.210.43.76:80 200 1.153 "smart-technology"
```

发送不带密钥的请求：

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

访问日志显示默认值：

```
192.168.1.1 - - [18/Mar/2025:09:18:27 +0000] 127.0.0.1:9080 "GET /anything HTTP/1.1" 401 52 0.000 "-" "curl/8.6.0" - - - "unknown"
```

## 替代方案：通过请求头转发标签[​](#替代方案通过请求头转发标签 "替代方案：通过请求头转发标签的直接链接")

如果希望 `http-logger`、`kafka-logger` 等日志插件的结构化日志条目包含消费者标签，请使用 [`attach-consumer-label`](https://docs.apiseven.com/hub/attach-consumer-label.md) 插件将消费者标签映射到上游请求头。这些请求头随后可用作 NGINX 变量（例如 `$http_x_consumer_company`），供任意日志插件的 `log_format` 引用。

更新[步骤四](#step-4-configure-a-consumer-and-a-protected-route)中的路由，在 `key-auth` 旁添加这两个插件。以下示例使用日志端点 `http://log-collector:8080/logs`：

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/consumer-label-demo?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "consumer-label-demo",

    "paths": ["/anything"],

    "service_id": "consumer-label-demo",

    "plugins": {

      "key-auth": {},

      "attach-consumer-label": {

        "headers": {

          "X-Consumer-Company": "$company"

        }

      },

      "http-logger": {

        "uri": "http://log-collector:8080/logs",

        "log_format": {

          "consumer_company": "$http_x_consumer_company"

        }

      }

    }

  }'
```

adc.yaml

```
services:

  - name: consumer-label-demo

    routes:

      - name: consumer-label-demo

        uris:

          - /anything

        plugins:

          key-auth: {}

          attach-consumer-label:

            headers:

              X-Consumer-Company: "$company"

          http-logger:

            uri: http://log-collector:8080/logs

            log_format:

              consumer_company: "$http_x_consumer_company"

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    labels:

      docs-example: john
```

预览本示例管理的资源变更，确认没有非预期的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=john
```

审查变更后同步配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=john
```

`attach-consumer-label` 中的 `$` 前缀引用消费者标签键。来自已通过身份认证的消费者的请求会在到达上游和日志插件之前注入相应的请求头。

## 其他资源[​](#其他资源 "其他资源的直接链接")

* [消费者和凭证](https://docs.apiseven.com/api7-gateway/key-concepts/consumers-and-credentials.md)
* [配置集中式日志记录](https://docs.apiseven.com/api7-gateway/observability/logging.md)
