comparison src/stream/ngx_stream_upstream.c @ 6677:c02290241cbe

Stream: upstream response time variables. The $upstream_connect_time, $upstream_first_byte_time and $upstream_session_time variables keep corresponding times.
author Vladimir Homutov <vl@nginx.com>
date Fri, 02 Sep 2016 18:27:12 +0300
parents df3a7c029dec
children 9cf2dce316e5
comparison
equal deleted inserted replaced
6676:df3a7c029dec 6677:c02290241cbe
11 11
12 12
13 static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf); 13 static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf);
14 static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s, 14 static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
15 ngx_stream_variable_value_t *v, uintptr_t data); 15 ngx_stream_variable_value_t *v, uintptr_t data);
16 static ngx_int_t ngx_stream_upstream_response_time_variable(
17 ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
16 static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s, 18 static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
17 ngx_stream_variable_value_t *v, uintptr_t data); 19 ngx_stream_variable_value_t *v, uintptr_t data);
18 20
19 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, 21 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd,
20 void *dummy); 22 void *dummy);
80 82
81 { ngx_string("upstream_bytes_sent"), NULL, 83 { ngx_string("upstream_bytes_sent"), NULL,
82 ngx_stream_upstream_bytes_variable, 0, 84 ngx_stream_upstream_bytes_variable, 0,
83 NGX_STREAM_VAR_NOCACHEABLE, 0 }, 85 NGX_STREAM_VAR_NOCACHEABLE, 0 },
84 86
87 { ngx_string("upstream_connect_time"), NULL,
88 ngx_stream_upstream_response_time_variable, 2,
89 NGX_STREAM_VAR_NOCACHEABLE, 0 },
90
91 { ngx_string("upstream_first_byte_time"), NULL,
92 ngx_stream_upstream_response_time_variable, 1,
93 NGX_STREAM_VAR_NOCACHEABLE, 0 },
94
95 { ngx_string("upstream_session_time"), NULL,
96 ngx_stream_upstream_response_time_variable, 0,
97 NGX_STREAM_VAR_NOCACHEABLE, 0 },
98
85 { ngx_string("upstream_bytes_received"), NULL, 99 { ngx_string("upstream_bytes_received"), NULL,
86 ngx_stream_upstream_bytes_variable, 1, 100 ngx_stream_upstream_bytes_variable, 1,
87 NGX_STREAM_VAR_NOCACHEABLE, 0 }, 101 NGX_STREAM_VAR_NOCACHEABLE, 0 },
88 102
89 { ngx_null_string, NULL, NULL, 0, 0, 0 } 103 { ngx_null_string, NULL, NULL, 0, 0, 0 }
202 p = ngx_sprintf(p, "%O", state[i].bytes_received); 216 p = ngx_sprintf(p, "%O", state[i].bytes_received);
203 217
204 } else { 218 } else {
205 p = ngx_sprintf(p, "%O", state[i].bytes_sent); 219 p = ngx_sprintf(p, "%O", state[i].bytes_sent);
206 } 220 }
221
222 if (++i == s->upstream_states->nelts) {
223 break;
224 }
225
226 *p++ = ',';
227 *p++ = ' ';
228 }
229
230 v->len = p - v->data;
231
232 return NGX_OK;
233 }
234
235
236 static ngx_int_t
237 ngx_stream_upstream_response_time_variable(ngx_stream_session_t *s,
238 ngx_stream_variable_value_t *v, uintptr_t data)
239 {
240 u_char *p;
241 size_t len;
242 ngx_uint_t i;
243 ngx_msec_int_t ms;
244 ngx_stream_upstream_state_t *state;
245
246 v->valid = 1;
247 v->no_cacheable = 0;
248 v->not_found = 0;
249
250 if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
251 v->not_found = 1;
252 return NGX_OK;
253 }
254
255 len = s->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
256
257 p = ngx_pnalloc(s->connection->pool, len);
258 if (p == NULL) {
259 return NGX_ERROR;
260 }
261
262 v->data = p;
263
264 i = 0;
265 state = s->upstream_states->elts;
266
267 for ( ;; ) {
268
269 if (data == 1) {
270 if (state[i].first_byte_time == (ngx_msec_t) -1) {
271 *p++ = '-';
272 goto next;
273 }
274
275 ms = state[i].first_byte_time;
276
277 } else if (data == 2 && state[i].connect_time != (ngx_msec_t) -1) {
278 ms = state[i].connect_time;
279
280 } else {
281 ms = state[i].response_time;
282 }
283
284 ms = ngx_max(ms, 0);
285 p = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000);
286
287 next:
207 288
208 if (++i == s->upstream_states->nelts) { 289 if (++i == s->upstream_states->nelts) {
209 break; 290 break;
210 } 291 }
211 292