]> git.kaiwu.me - nginx.git/commitdiff
Stream: $upstream_bytes_sent and $upstream_bytes_received.
authorVladimir Homutov <vl@nginx.com>
Fri, 2 Sep 2016 15:27:08 +0000 (18:27 +0300)
committerVladimir Homutov <vl@nginx.com>
Fri, 2 Sep 2016 15:27:08 +0000 (18:27 +0300)
src/stream/ngx_stream_proxy_module.c
src/stream/ngx_stream_upstream.c
src/stream/ngx_stream_upstream.h

index dd8d7811d736dd0478e7b28648f914a9b6a58614..edffaf8e5f7f3d6f031462b554416f743cf15bbb 100644 (file)
@@ -1630,6 +1630,9 @@ ngx_stream_proxy_next_upstream(ngx_stream_session_t *s)
         }
 #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_session_t *s, ngx_uint_t rc)
         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);
index 801b16d639856ac6357cc7b2bb09f144cbef9ed4..69eb0c13a50fa4a0b7a9e706e115fc84b4abc25e 100644 (file)
@@ -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_upstream_vars[] = {
       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_stream_session_t *s,
 }
 
 
+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)
 {
index 22ffcb61497e6a67c3172aa1096bee7cde757413..3243faa3f681ea5b2268f1d2f382c66d17372913 100644 (file)
@@ -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;