这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@u5surf
Copy link
Owner

@u5surf u5surf commented Sep 18, 2025

Summary

  • Implement real-time server zone statistics collection through nginx LOG_PHASE handler
  • Add vts_update_server_stats_ffi() FFI function to receive server statistics from nginx C code
  • Update nginx wrapper to extract server name, response status, bytes transferred, and request time from nginx requests
  • Fix integer overflow in request time calculation using nginx's built-in timing functions
  • Address clippy warnings by marking FFI functions as unsafe with proper Safety documentation
  • Server zone statistics now properly collected and displayed in /status endpoint

Resolved Issues

Fixes the issue where server statistics were not being output (「serverの統計が出ておりません」). The /status endpoint now shows:

  • nginx_vts_server_requests_total - Total requests per server zone
  • nginx_vts_server_bytes_total - Bytes transferred (in/out) per server zone
  • nginx_vts_server_responses_total - Response counts by status code per server zone
  • nginx_vts_server_request_seconds - Request processing time statistics per server zone

Implementation Details

C Wrapper Changes (src/ngx_vts_wrapper.c)

  • Added vts_update_server_stats_ffi() FFI function declaration
  • Updated LOG_PHASE handler to extract server statistics from nginx request structure
  • Fixed request time calculation to prevent integer overflow
  • Extract server name from r->headers_in.server
  • Calculate bytes using r->request_length and r->connection->sent

Rust Changes (src/lib.rs, src/handlers.rs)

  • Added vts_update_server_stats_ffi() unsafe FFI function with proper Safety documentation
  • Updated existing FFI functions to be marked as unsafe to satisfy clippy warnings
  • Added Safety documentation for all unsafe FFI functions
  • Server statistics now integrate seamlessly with existing upstream and connection statistics

Test Results

# Before: Empty server metrics (headers only)
nginx_vts_server_requests_total{}
nginx_vts_server_bytes_total{}

# After: Real server statistics
nginx_vts_server_requests_total{zone="127.0.0.1"} 1
nginx_vts_server_bytes_total{zone="127.0.0.1",direction="in"} 79
nginx_vts_server_bytes_total{zone="127.0.0.1",direction="out"} 236
nginx_vts_server_responses_total{zone="127.0.0.1",status="2xx"} 1

Code Quality

  • All cargo clippy warnings fixed with -D warnings
  • Proper unsafe FFI function documentation with Safety sections
  • Request time overflow issue resolved
  • Consistent mutex handling patterns throughout codebase

🤖 Generated with Claude Code

- Add vts_update_server_stats_ffi() FFI function to collect server statistics
- Update nginx LOG_PHASE handler to track server zone metrics for all requests
- Extract server name, response status, bytes transferred, and request time
- Fix clippy warnings by marking FFI functions as unsafe with proper Safety docs
- Server statistics now properly collected and output in /status endpoint

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@u5surf u5surf requested a review from Copilot September 18, 2025 07:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Implements real-time server zone statistics collection for nginx-vts by adding a LOG_PHASE handler that captures request statistics and an FFI interface to update Rust-based metrics storage.

  • Added server statistics collection including request counts, bytes transferred, response codes, and processing times
  • Implemented FFI function vts_update_server_stats_ffi() to bridge nginx C code with Rust statistics management
  • Fixed unsafe FFI function declarations to satisfy clippy warnings with proper Safety documentation

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/ngx_vts_wrapper.c Added server statistics extraction in LOG_PHASE handler and FFI function declaration
src/lib.rs Implemented unsafe FFI function for server stats updates with Safety documentation
src/handlers.rs Updated VTS status handler to be marked as unsafe with Safety documentation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

u5surf and others added 3 commits September 18, 2025 16:38
- Disable vts_collect_nginx_connections() call in test environment to prevent interference
- Add explicit connection stats setup in test_integrated_vts_status_functionality
- All integration tests now pass consistently in parallel execution

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…wrapper.c

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@u5surf u5surf requested a review from Copilot September 18, 2025 07:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +430 to +431
// Collect current nginx connection statistics only in production
#[cfg(not(test))]
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The conditional compilation directive is placed after the comment but before the function call. Move the #[cfg(not(test))] directive to line 431 before the comment for better code organization and clarity.

Suggested change
// Collect current nginx connection statistics only in production
#[cfg(not(test))]
#[cfg(not(test))]
// Collect current nginx connection statistics only in production

Copilot uses AI. Check for mistakes.
Comment on lines +23 to 24
pub unsafe extern "C" fn vts_status_handler(r: *mut ngx_http_request_t) -> ngx_int_t {
unsafe {
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is already marked as unsafe, making the inner unsafe block redundant. Remove the inner unsafe block since the entire function body is already in an unsafe context.

Copilot uses AI. Check for mistakes.
@u5surf u5surf merged commit 5331ed1 into main Sep 18, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants