Skip to content

Commit

Permalink
added 7 predefined counters from the Nginx stub status module
Browse files Browse the repository at this point in the history
  • Loading branch information
lyokha committed Jul 3, 2020
1 parent 686914a commit c28e39a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Table of contents
- [Reloading Nginx configuration](#reloading-nginx-configuration)
- [Persistent counters](#persistent-counters)
- [Histograms](#histograms)
- [Nginx master uptime counters](#nginx-master-uptime-counters)
- [Predefined counters](#predefined-counters)
- [An example](#an-example)
- [Remarks on using location ifs and complex conditions](#remarks-on-using-location-ifs-and-complex-conditions)
- [See also](#see-also)
Expand Down Expand Up @@ -248,14 +248,20 @@ if the request time was more than *0.05* then its value will be *3*.

Histograms layout can be observed via predefined variable `$cnt_histograms`.

Nginx master uptime counters
----------------------------
Predefined counters
-------------------

There are two predefined counter variables `$cnt_uptime` and
`$cnt_uptime_reload` which contain the number of seconds elapsed since the start
of Nginx. Reloading Nginx with *SIGHUP* won't restart the former counter. The
counters are not associated with any counter set identifier, nor are they
collected in variable `$cnt_collection`.
There is a number of predefined counter variables: 7 counters from the Nginx
stub status module (available only when Nginx was compiled with option
*--with-http_stub_status_module*): `$cnt_stub_status_accepted`,
`$cnt_stub_status_handled`, `$cnt_stub_status_requests`,
`$cnt_stub_status_active`, `$cnt_stub_status_reading`,
`$cnt_stub_status_writing`, `$cnt_stub_status_waiting`, and 2 counters related
to Nginx master process uptime: `$cnt_uptime` and `$cnt_uptime_reload`, they
contain the number of seconds elapsed since the start of Nginx. Reloading Nginx
with *SIGHUP* won't restart the former counter. All predefined counters are not
associated with any counter set identifier, nor are they collected in variable
`$cnt_collection`.

An example
----------
Expand Down
7 changes: 6 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ http {
}

location /uptime {
echo "Uptime: $cnt_uptime ($cnt_uptime_reload)";
echo "Uptime (after reload): $cnt_uptime ($cnt_uptime_reload)";
}

location /requests {
echo -n "Requests (active): ";
echo "$cnt_stub_status_requests ($cnt_stub_status_active)";
}
}

Expand Down
83 changes: 83 additions & 0 deletions src/ngx_http_custom_counters_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ static ngx_int_t ngx_http_cnt_collection(ngx_http_request_t *r,
static void ngx_http_cnt_set_collection_buf_len(ngx_http_cnt_main_conf_t *mcf);
static ngx_int_t ngx_http_cnt_uptime(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
#if NGX_STAT_STUB
static ngx_int_t ngx_http_cnt_stub_status(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
#endif
static char *ngx_http_cnt_counter(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_cnt_early_counter(ngx_conf_t *cf, ngx_command_t *cmd,
Expand Down Expand Up @@ -168,6 +172,22 @@ static ngx_http_variable_t ngx_http_cnt_vars[] =
{ ngx_string("cnt_histograms"), NULL, ngx_http_cnt_histograms, 0, 0, 0 },
{ ngx_string("cnt_uptime"), NULL, ngx_http_cnt_uptime, 0, 0, 0 },
{ ngx_string("cnt_uptime_reload"), NULL, ngx_http_cnt_uptime, 1, 0, 0 },
#if NGX_STAT_STUB
{ ngx_string("cnt_stub_status_accepted"), NULL, ngx_http_cnt_stub_status,
0, 0, 0 },
{ ngx_string("cnt_stub_status_handled"), NULL, ngx_http_cnt_stub_status,
1, 0, 0 },
{ ngx_string("cnt_stub_status_requests"), NULL, ngx_http_cnt_stub_status,
2, 0, 0 },
{ ngx_string("cnt_stub_status_active"), NULL, ngx_http_cnt_stub_status,
3, 0, 0 },
{ ngx_string("cnt_stub_status_reading"), NULL, ngx_http_cnt_stub_status,
4, 0, 0 },
{ ngx_string("cnt_stub_status_writing"), NULL, ngx_http_cnt_stub_status,
5, 0, 0 },
{ ngx_string("cnt_stub_status_waiting"), NULL, ngx_http_cnt_stub_status,
6, 0, 0 },
#endif

{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
Expand Down Expand Up @@ -761,6 +781,69 @@ ngx_http_cnt_uptime(ngx_http_request_t *r, ngx_http_variable_value_t *v,
}


#if NGX_STAT_STUB

/* this handler was mostly taken from Nginx stub status module */

static ngx_int_t
ngx_http_cnt_stub_status(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
u_char *p;
ngx_atomic_int_t value;

p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN);
if (p == NULL) {
return NGX_ERROR;
}

switch (data) {
case 0:
value = *ngx_stat_accepted;
break;

case 1:
value = *ngx_stat_handled;
break;

case 2:
value = *ngx_stat_requests;
break;

case 3:
value = *ngx_stat_active;
break;

case 4:
value = *ngx_stat_reading;
break;

case 5:
value = *ngx_stat_writing;
break;

case 6:
value = *ngx_stat_waiting;
break;

/* suppress warning */
default:
value = 0;
break;
}

v->len = ngx_sprintf(p, "%uA", value) - p;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = p;

return NGX_OK;
}

#endif


ngx_int_t
ngx_http_cnt_counter_set_init(ngx_conf_t *cf, ngx_http_cnt_main_conf_t *mcf,
ngx_http_cnt_srv_conf_t *scf)
Expand Down

0 comments on commit c28e39a

Please sign in to comment.