Stream: $upstream_bytes_sent and $upstream_bytes_received.

This commit is contained in:
Vladimir Homutov 2016-09-02 18:27:08 +03:00
parent f302100397
commit 522a62f6c0
3 changed files with 78 additions and 2 deletions

View 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);

View 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)
{

View 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;