Stream: upstream response time variables.

The $upstream_connect_time, $upstream_first_byte_time and
$upstream_session_time variables keep corresponding times.
This commit is contained in:
Vladimir Homutov 2016-09-02 18:27:12 +03:00
parent 64223df670
commit 443b52db59
3 changed files with 103 additions and 0 deletions

View File

@ -684,6 +684,10 @@ ngx_stream_proxy_connect(ngx_stream_session_t *s)
u = s->upstream; u = s->upstream;
if (u->state) {
u->state->response_time = ngx_current_msec - u->state->response_time;
}
u->state = ngx_array_push(s->upstream_states); u->state = ngx_array_push(s->upstream_states);
if (u->state == NULL) { if (u->state == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR); ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
@ -692,6 +696,10 @@ ngx_stream_proxy_connect(ngx_stream_session_t *s)
ngx_memzero(u->state, sizeof(ngx_stream_upstream_state_t)); ngx_memzero(u->state, sizeof(ngx_stream_upstream_state_t));
u->state->connect_time = (ngx_msec_t) -1;
u->state->first_byte_time = (ngx_msec_t) -1;
u->state->response_time = ngx_current_msec;
rc = ngx_event_connect_peer(&u->peer); rc = ngx_event_connect_peer(&u->peer);
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc); ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc);
@ -813,6 +821,8 @@ ngx_stream_proxy_init_upstream(ngx_stream_session_t *s)
} }
} }
u->state->connect_time = ngx_current_msec - u->state->response_time;
c->log->action = "proxying connection"; c->log->action = "proxying connection";
if (u->upstream_buf.start == NULL) { if (u->upstream_buf.start == NULL) {
@ -1518,6 +1528,13 @@ ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
} }
} }
if (from_upstream) {
if (u->state->first_byte_time == (ngx_msec_t) -1) {
u->state->first_byte_time = ngx_current_msec
- u->state->response_time;
}
}
if (c->type == SOCK_DGRAM && ++u->responses == pscf->responses) if (c->type == SOCK_DGRAM && ++u->responses == pscf->responses)
{ {
src->read->ready = 0; src->read->ready = 0;
@ -1664,6 +1681,8 @@ ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_uint_t rc)
pc = u->peer.connection; pc = u->peer.connection;
if (u->state) { if (u->state) {
u->state->response_time = ngx_current_msec - u->state->response_time;
if (pc) { if (pc) {
u->state->bytes_received = u->received; u->state->bytes_received = u->received;
u->state->bytes_sent = pc->sent; u->state->bytes_sent = pc->sent;

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_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s, static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data); ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_upstream_response_time_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, static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data); ngx_stream_variable_value_t *v, uintptr_t data);
@ -82,6 +84,18 @@ static ngx_stream_variable_t ngx_stream_upstream_vars[] = {
ngx_stream_upstream_bytes_variable, 0, ngx_stream_upstream_bytes_variable, 0,
NGX_STREAM_VAR_NOCACHEABLE, 0 }, NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_connect_time"), NULL,
ngx_stream_upstream_response_time_variable, 2,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_first_byte_time"), NULL,
ngx_stream_upstream_response_time_variable, 1,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_session_time"), NULL,
ngx_stream_upstream_response_time_variable, 0,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_bytes_received"), NULL, { ngx_string("upstream_bytes_received"), NULL,
ngx_stream_upstream_bytes_variable, 1, ngx_stream_upstream_bytes_variable, 1,
NGX_STREAM_VAR_NOCACHEABLE, 0 }, NGX_STREAM_VAR_NOCACHEABLE, 0 },
@ -219,6 +233,73 @@ ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
} }
static ngx_int_t
ngx_stream_upstream_response_time_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_msec_int_t ms;
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_TIME_T_LEN + 4 + 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) {
if (state[i].first_byte_time == (ngx_msec_t) -1) {
*p++ = '-';
goto next;
}
ms = state[i].first_byte_time;
} else if (data == 2 && state[i].connect_time != (ngx_msec_t) -1) {
ms = state[i].connect_time;
} else {
ms = state[i].response_time;
}
ms = ngx_max(ms, 0);
p = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000);
next:
if (++i == s->upstream_states->nelts) {
break;
}
*p++ = ',';
*p++ = ' ';
}
v->len = p - v->data;
return NGX_OK;
}
static char * static char *
ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) 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 { typedef struct {
ngx_msec_t response_time;
ngx_msec_t connect_time;
ngx_msec_t first_byte_time;
off_t bytes_sent; off_t bytes_sent;
off_t bytes_received; off_t bytes_received;