Discussion:
[PATCH] Upstream: added $upstream_bytes_sent variable
Piotr Sikora via nginx-devel
2018-11-27 10:34:10 UTC
Permalink
# HG changeset patch
# User Piotr Sikora <***@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 <***@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;
Maxim Dounin
2018-11-29 15:00:51 UTC
Permalink
Hello!
Post by Piotr Sikora via nginx-devel
# HG changeset patch
# 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.
--
Maxim Dounin
http://mdounin.ru/
Neil Craig
2018-11-29 15:09:11 UTC
Permalink
Hello

FWIW, I¹d appreciate a $upstream_bytes_sent var in ngx.

Cheers

Neil Craig
Lead Technical Architect | Online Technology Group

Broadcast Centre, London W12 7TQ | BC4 A3
Twitter: https://twitter.com/tdp_org





On 29/11/2018, 15:00, "nginx-devel on behalf of Maxim Dounin"
Post by Maxim Dounin
Hello!
Post by Piotr Sikora via nginx-devel
# HG changeset patch
# 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 -
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.
--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx-devel mailing list
http://mailman.nginx.org/mailman/listinfo/nginx-devel
-----------------------------
http://www.bbc.co.uk
This e-mail (and any attachments) is confidential and
may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in
error, please delete it from your system.
Do not use, copy or disclose the
information in any way nor act in reliance on it and notify the sender
immediately.
Please note that the BBC monitors e-mails
sent or received.
Further communication will signify your consent to
this.
-----------------------------
Ruslan Ermilov
2018-11-30 15:39:32 UTC
Permalink
Post by Maxim Dounin
Hello!
Post by Piotr Sikora via nginx-devel
# HG changeset patch
# 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 -
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 <***@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 <***@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;

Loading...