[nginx] Stream: $upstream_bytes_sent and $upstream_bytes_received.

Vladimir Homutov vl at nginx.com
Fri Sep 2 15:28:34 UTC 2016


details:   http://hg.nginx.org/nginx/rev/df3a7c029dec
branches:  
changeset: 6676:df3a7c029dec
user:      Vladimir Homutov <vl at nginx.com>
date:      Fri Sep 02 18:27:08 2016 +0300
description:
Stream: $upstream_bytes_sent and $upstream_bytes_received.

diffstat:

 src/stream/ngx_stream_proxy_module.c |  14 ++++++-
 src/stream/ngx_stream_upstream.c     |  63 ++++++++++++++++++++++++++++++++++++
 src/stream/ngx_stream_upstream.h     |   3 +
 3 files changed, 78 insertions(+), 2 deletions(-)

diffs (136 lines):

diff -r ab9b4fd8c5b7 -r df3a7c029dec src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Fri Sep 02 18:27:05 2016 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Fri Sep 02 18:27:08 2016 +0300
@@ -1630,6 +1630,9 @@ ngx_stream_proxy_next_upstream(ngx_strea
         }
 #endif
 
+        u->state->bytes_received = u->received;
+        u->state->bytes_sent = pc->sent;
+
         ngx_close_connection(pc);
         u->peer.connection = NULL;
     }
@@ -1658,13 +1661,20 @@ ngx_stream_proxy_finalize(ngx_stream_ses
         u->resolved->ctx = NULL;
     }
 
+    pc = u->peer.connection;
+
+    if (u->state) {
+        if (pc) {
+            u->state->bytes_received = u->received;
+            u->state->bytes_sent = pc->sent;
+        }
+    }
+
     if (u->peer.free && u->peer.sockaddr) {
         u->peer.free(&u->peer, u->peer.data, 0);
         u->peer.sockaddr = NULL;
     }
 
-    pc = u->peer.connection;
-
     if (pc) {
         ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
                        "close stream proxy upstream connection: %d", pc->fd);
diff -r ab9b4fd8c5b7 -r df3a7c029dec src/stream/ngx_stream_upstream.c
--- a/src/stream/ngx_stream_upstream.c	Fri Sep 02 18:27:05 2016 +0300
+++ b/src/stream/ngx_stream_upstream.c	Fri Sep 02 18:27:08 2016 +0300
@@ -13,6 +13,8 @@
 static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf);
 static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
 
 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd,
     void *dummy);
@@ -76,6 +78,14 @@ static ngx_stream_variable_t  ngx_stream
       ngx_stream_upstream_addr_variable, 0,
       NGX_STREAM_VAR_NOCACHEABLE, 0 },
 
+    { ngx_string("upstream_bytes_sent"), NULL,
+      ngx_stream_upstream_bytes_variable, 0,
+      NGX_STREAM_VAR_NOCACHEABLE, 0 },
+
+    { ngx_string("upstream_bytes_received"), NULL,
+      ngx_stream_upstream_bytes_variable, 1,
+      NGX_STREAM_VAR_NOCACHEABLE, 0 },
+
     { ngx_null_string, NULL, NULL, 0, 0, 0 }
 };
 
@@ -156,6 +166,59 @@ ngx_stream_upstream_addr_variable(ngx_st
 }
 
 
+static ngx_int_t
+ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    u_char                       *p;
+    size_t                        len;
+    ngx_uint_t                    i;
+    ngx_stream_upstream_state_t  *state;
+
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
+        v->not_found = 1;
+        return NGX_OK;
+    }
+
+    len = s->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
+
+    p = ngx_pnalloc(s->connection->pool, len);
+    if (p == NULL) {
+        return NGX_ERROR;
+    }
+
+    v->data = p;
+
+    i = 0;
+    state = s->upstream_states->elts;
+
+    for ( ;; ) {
+
+        if (data == 1) {
+            p = ngx_sprintf(p, "%O", state[i].bytes_received);
+
+        } else {
+            p = ngx_sprintf(p, "%O", state[i].bytes_sent);
+        }
+
+        if (++i == s->upstream_states->nelts) {
+            break;
+        }
+
+        *p++ = ',';
+        *p++ = ' ';
+    }
+
+    v->len = p - v->data;
+
+    return NGX_OK;
+}
+
+
 static char *
 ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
 {
diff -r ab9b4fd8c5b7 -r df3a7c029dec src/stream/ngx_stream_upstream.h
--- a/src/stream/ngx_stream_upstream.h	Fri Sep 02 18:27:05 2016 +0300
+++ b/src/stream/ngx_stream_upstream.h	Fri Sep 02 18:27:08 2016 +0300
@@ -79,6 +79,9 @@ struct ngx_stream_upstream_srv_conf_s {
 
 
 typedef struct {
+    off_t                              bytes_sent;
+    off_t                              bytes_received;
+
     ngx_str_t                         *peer;
 } ngx_stream_upstream_state_t;
 



More information about the nginx-devel mailing list