[PATCH] Upstream: added $upstream_bytes_sent variable

Ruslan Ermilov ru at nginx.com
Fri Nov 30 15:39:32 UTC 2018


On Thu, Nov 29, 2018 at 06:00:51PM +0300, Maxim Dounin wrote:
> Hello!
> 
> On Tue, Nov 27, 2018 at 02:34:10AM -0800, Piotr Sikora via nginx-devel wrote:
> 
> > # 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.
> 
> [...]
> 
> Ruslan made a similar patch a while ago.  It wasn't committed 
> since there were questions if such a variable is actually needed - 
> I think we are aware of at most one feature request for this:
> 
> http://mailman.nginx.org/pipermail/nginx/2018-March/055940.html
> 
> I've asked Ruslan to post his version of the patch (or, rather, a 
> patch series), please review. 

# HG changeset patch
# User Ruslan Ermilov <ru at nginx.com>
# Date 1543592116 -10800
#      Fri Nov 30 18:35:16 2018 +0300
# Node ID 79c7b169816cdc63044838b03084c631c0d2f0a3
# Parent  5cff15dd07cd298e4eff44c04c2833066c217318
Upstream: style.

Introduced local variable "c" in ngx_http_upstream_next() and
ngx_http_upstream_finalize_request().

No functional changes.

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -4128,8 +4128,9 @@ static void
 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
     ngx_uint_t ft_type)
 {
-    ngx_msec_t  timeout;
-    ngx_uint_t  status, state;
+    ngx_msec_t         timeout;
+    ngx_uint_t         status, state;
+    ngx_connection_t  *c;
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "http next upstream, %xi", ft_type);
@@ -4250,25 +4251,26 @@ ngx_http_upstream_next(ngx_http_request_
         return;
     }
 
-    if (u->peer.connection) {
+    c = u->peer.connection;
+
+    if (c) {
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "close http upstream connection: %d",
-                       u->peer.connection->fd);
+                       "close http upstream connection: %d", c->fd);
 #if (NGX_HTTP_SSL)
 
-        if (u->peer.connection->ssl) {
-            u->peer.connection->ssl->no_wait_shutdown = 1;
-            u->peer.connection->ssl->no_send_shutdown = 1;
-
-            (void) ngx_ssl_shutdown(u->peer.connection);
+        if (c->ssl) {
+            c->ssl->no_wait_shutdown = 1;
+            c->ssl->no_send_shutdown = 1;
+
+            (void) ngx_ssl_shutdown(c);
         }
 #endif
 
-        if (u->peer.connection->pool) {
-            ngx_destroy_pool(u->peer.connection->pool);
-        }
-
-        ngx_close_connection(u->peer.connection);
+        if (c->pool) {
+            ngx_destroy_pool(c->pool);
+        }
+
+        ngx_close_connection(c);
         u->peer.connection = NULL;
     }
 
@@ -4292,7 +4294,8 @@ static void
 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_int_t rc)
 {
-    ngx_uint_t  flush;
+    ngx_uint_t         flush;
+    ngx_connection_t  *c;
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "finalize http upstream request: %i", rc);
@@ -4328,13 +4331,15 @@ ngx_http_upstream_finalize_request(ngx_h
         u->peer.sockaddr = NULL;
     }
 
-    if (u->peer.connection) {
+    c = u->peer.connection;
+
+    if (c) {
 
 #if (NGX_HTTP_SSL)
 
         /* TODO: do not shutdown persistent connection */
 
-        if (u->peer.connection->ssl) {
+        if (c->ssl) {
 
             /*
              * We send the "close notify" shutdown alert to the upstream only
@@ -4342,21 +4347,20 @@ ngx_http_upstream_finalize_request(ngx_h
              * It is acceptable according to the TLS standard.
              */
 
-            u->peer.connection->ssl->no_wait_shutdown = 1;
-
-            (void) ngx_ssl_shutdown(u->peer.connection);
+            c->ssl->no_wait_shutdown = 1;
+
+            (void) ngx_ssl_shutdown(c);
         }
 #endif
 
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "close http upstream connection: %d",
-                       u->peer.connection->fd);
-
-        if (u->peer.connection->pool) {
-            ngx_destroy_pool(u->peer.connection->pool);
-        }
-
-        ngx_close_connection(u->peer.connection);
+                       "close http upstream connection: %d", c->fd);
+
+        if (c->pool) {
+            ngx_destroy_pool(c->pool);
+        }
+
+        ngx_close_connection(c);
     }
 
     u->peer.connection = NULL;
# HG changeset patch
# User Ruslan Ermilov <ru at nginx.com>
# Date 1543592133 -10800
#      Fri Nov 30 18:35:33 2018 +0300
# Node ID 95b0ee9297fc3b8782ee1a383e3221b935639cc3
# Parent  79c7b169816cdc63044838b03084c631c0d2f0a3
Upstream: implemented $upstream_bytes_sent.

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -162,7 +162,7 @@ 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(
+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);
@@ -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)
@@ -4137,6 +4141,8 @@ ngx_http_upstream_next(ngx_http_request_
 
     if (u->peer.sockaddr) {
 
+        u->state->bytes_sent = u->peer.connection->sent;
+
         if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_403
             || ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404)
         {
@@ -4322,6 +4328,10 @@ ngx_http_upstream_finalize_request(ngx_h
                                         - u->pipe->preread_size;
             u->state->response_length = u->pipe->read_length;
         }
+
+        if (u->peer.connection) {
+            u->state->bytes_sent = u->peer.connection->sent;
+        }
     }
 
     u->finalize_request(r, rc);
@@ -5472,7 +5482,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;
@@ -5506,6 +5516,9 @@ ngx_http_upstream_response_length_variab
         if (data == 1) {
             p = ngx_sprintf(p, "%O", state[i].bytes_received);
 
+        } else if (data == 2) {
+            p = ngx_sprintf(p, "%O", state[i].bytes_sent);
+
         } else {
             p = ngx_sprintf(p, "%O", state[i].response_length);
         }
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -63,6 +63,7 @@ typedef struct {
     ngx_msec_t                       header_time;
     ngx_msec_t                       queue_time;
     off_t                            response_length;
+    off_t                            bytes_sent;
     off_t                            bytes_received;
 
     ngx_str_t                       *peer;


More information about the nginx-devel mailing list