AlinsRan opened a new pull request, #13465:
URL: https://github.com/apache/apisix/pull/13465
## Description
Add `error-log-collect` plugin that collects error logs generated during
request processing and re-logs them with request context (request ID,
timestamp, worker ID).
## Features
- **Filter by variable expressions**: Use `vars` to collect logs only for
specific requests
- **Sampling support**: Use `sample_ratio` to reduce overhead in production
- **Configurable buffer size**: Use `buffer_max_size` to control memory usage
- **Circular queue**: Prevents unbounded memory growth by overwriting old
entries
## Changes
1. **New file: `apisix/utils/circular-queue.lua`**
- Circular queue data structure for buffering error logs
2. **Modified: `apisix/core/log.lua`**
- Add buffer functionality (`reset_buffer`, `get_buffer`)
- Intercept log calls when `ngx.ctx.error_log_collecting` is true
3. **New file: `apisix/plugins/error-log-collect.lua`**
- The plugin itself with schema validation
4. **New file: `t/plugin/error-log-collect.t`**
- Test cases for the plugin
5. **Modified: `conf/config.yaml.example`**
- Enable the plugin in the plugins list
## Usage Example
```yaml
plugins:
- error-log-collect
routes:
- uri: /api/*
plugins:
error-log-collect:
vars:
- - arg_debug
- "=="
- "true"
sample_ratio: 0.1
buffer_max_size: 500
```
## How It Works
1. During the `rewrite` phase, the plugin sets `ngx.ctx.error_log_collecting
= true` on the request context (with sampling)
2. When `core.log` is called during request processing, if the flag is set
and the log level is above the current filter level, the log message is
buffered in a circular queue
3. During the `log` phase, the plugin drains the buffer and re-logs all
collected error messages with context information (timestamp, worker ID,
request ID)
## Testing
```bash
prove -r t/plugin/error-log-collect.t
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]