Skip to content

Commit

Permalink
Stream: upstream response time variables.
Browse files Browse the repository at this point in the history
The $upstream_connect_time, $upstream_first_byte_time and
$upstream_session_time variables keep corresponding times.
  • Loading branch information
vlhomutov committed Sep 2, 2016
1 parent 64223df commit 443b52d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/stream/ngx_stream_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ ngx_stream_proxy_connect(ngx_stream_session_t *s)

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);
if (u->state == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
Expand All @@ -692,6 +696,10 @@ ngx_stream_proxy_connect(ngx_stream_session_t *s)

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

ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc);
Expand Down Expand Up @@ -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";

if (u->upstream_buf.start == NULL) {
Expand Down Expand Up @@ -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)
{
src->read->ready = 0;
Expand Down Expand Up @@ -1664,6 +1681,8 @@ ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_uint_t rc)
pc = u->peer.connection;

if (u->state) {
u->state->response_time = ngx_current_msec - u->state->response_time;

if (pc) {
u->state->bytes_received = u->received;
u->state->bytes_sent = pc->sent;
Expand Down
81 changes: 81 additions & 0 deletions src/stream/ngx_stream_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_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,
ngx_stream_variable_value_t *v, uintptr_t data);

Expand Down Expand Up @@ -82,6 +84,18 @@ static ngx_stream_variable_t ngx_stream_upstream_vars[] = {
ngx_stream_upstream_bytes_variable, 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_stream_upstream_bytes_variable, 1,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
Expand Down Expand Up @@ -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 *
ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
Expand Down
3 changes: 3 additions & 0 deletions src/stream/ngx_stream_upstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ struct ngx_stream_upstream_srv_conf_s {


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

Expand Down

0 comments on commit 443b52d

Please sign in to comment.