[PATCH] Upstream: added $upstream_bytes_sent variable

Piotr Sikora piotrsikora at google.com
Tue Nov 27 10:34:10 UTC 2018


# HG changeset patch
# User Piotr Sikora <piotrsikora at google.com>
# Date 1494129075 25200
#      Sat May 06 20:51:15 2017 -0700
# Node ID fafbb3ee41e5bb03bcfba73f7d4367b8ab7d36cc
# Parent  be5cb9c67c05ccaf22dab7abba78aa4c1545a8ee
Upstream: added $upstream_bytes_sent variable.

Signed-off-by: Piotr Sikora <piotrsikora at google.com>

diff -r be5cb9c67c05 -r fafbb3ee41e5 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -162,8 +162,8 @@ static ngx_int_t ngx_http_upstream_statu
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
-static ngx_int_t ngx_http_upstream_response_length_variable(
-    ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_upstream_bytes_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_upstream_trailer_variable(ngx_http_request_t *r,
@@ -402,11 +402,15 @@ static ngx_http_variable_t  ngx_http_ups
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
     { ngx_string("upstream_response_length"), NULL,
-      ngx_http_upstream_response_length_variable, 0,
+      ngx_http_upstream_bytes_variable, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
     { ngx_string("upstream_bytes_received"), NULL,
-      ngx_http_upstream_response_length_variable, 1,
+      ngx_http_upstream_bytes_variable, 1,
+      NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+    { ngx_string("upstream_bytes_sent"), NULL,
+      ngx_http_upstream_bytes_variable, 2,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
 #if (NGX_HTTP_CACHE)
@@ -4134,6 +4138,10 @@ ngx_http_upstream_next(ngx_http_request_
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "http next upstream, %xi", ft_type);
 
+    if (u->state && u->state->bytes_sent == 0 && u->peer.connection) {
+        u->state->bytes_sent = u->peer.connection->sent;
+    }
+
     if (u->peer.sockaddr) {
 
         if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_403
@@ -4319,6 +4327,10 @@ ngx_http_upstream_finalize_request(ngx_h
                                         - u->pipe->preread_size;
             u->state->response_length = u->pipe->read_length;
         }
+
+        if (u->state->bytes_sent == 0 && u->peer.connection) {
+            u->state->bytes_sent = u->peer.connection->sent;
+        }
     }
 
     u->finalize_request(r, rc);
@@ -5468,7 +5480,7 @@ ngx_http_upstream_response_time_variable
 
 
 static ngx_int_t
-ngx_http_upstream_response_length_variable(ngx_http_request_t *r,
+ngx_http_upstream_bytes_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {
     u_char                     *p;
@@ -5499,7 +5511,10 @@ ngx_http_upstream_response_length_variab
 
     for ( ;; ) {
 
-        if (data == 1) {
+        if (data == 2) {
+            p = ngx_sprintf(p, "%O", state[i].bytes_sent);
+
+        } else if (data == 1) {
             p = ngx_sprintf(p, "%O", state[i].bytes_received);
 
         } else {
diff -r be5cb9c67c05 -r fafbb3ee41e5 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -64,6 +64,7 @@ typedef struct {
     ngx_msec_t                       queue_time;
     off_t                            response_length;
     off_t                            bytes_received;
+    off_t                            bytes_sent;
 
     ngx_str_t                       *peer;
 } ngx_http_upstream_state_t;


More information about the nginx-devel mailing list