Skip to content

Commit 87283be

Browse files
author
Oleg Fedoseev
committed
little cleanup
1 parent 25acd53 commit 87283be

File tree

1 file changed

+44
-96
lines changed

1 file changed

+44
-96
lines changed

ngx_http_pinba_module.c

Lines changed: 44 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ typedef struct {
5757
} ngx_pinba_hash_sock_t;
5858

5959
typedef struct {
60-
ngx_flag_t enable;
61-
ngx_array_t *ignore_codes;
62-
ngx_url_t server;
60+
ngx_flag_t enable;
61+
ngx_array_t *ignore_codes;
62+
ngx_url_t server;
6363
ngx_array_t *tags;
6464
ngx_array_t *timers;
65-
time_t resolve_freq; /* default 60 sec */
65+
time_t resolve_freq; /* default 60 sec */
66+
ngx_http_complex_value_t *server_name;
67+
ngx_http_complex_value_t *script_name;
6668
} ngx_http_pinba_loc_conf_t;
6769

6870
typedef struct {
@@ -87,8 +89,6 @@ typedef struct {
8789

8890
ngx_pinba_hash_sock_t *g_sock_hash = NULL;
8991
char g_hostname[PINBA_STR_BUFFER_SIZE];
90-
ngx_http_complex_value_t *g_servername;
91-
ngx_http_complex_value_t *g_scriptname;
9292

9393
static void *ngx_http_pinba_create_loc_conf(ngx_conf_t *cf);
9494
static char *ngx_http_pinba_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
@@ -98,8 +98,6 @@ static char *ngx_http_pinba_tag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
9898
static char *ngx_http_pinba_buffer_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
9999
static char *ngx_http_pinba_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
100100
static char *ngx_http_pinba_timer_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
101-
static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
102-
static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
103101

104102
typedef struct {
105103
ProtobufCBuffer base;
@@ -194,16 +192,16 @@ static ngx_command_t ngx_http_pinba_commands[] = { /* {{{ */
194192

195193
{ ngx_string("pinba_server_name"),
196194
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
197-
ngx_http_pinba_servername,
195+
ngx_http_set_complex_value_slot,
198196
NGX_HTTP_LOC_CONF_OFFSET,
199-
0,
197+
offsetof(ngx_http_pinba_loc_conf_t, server_name),
200198
NULL },
201199

202200
{ ngx_string("pinba_script_name"),
203201
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
204-
ngx_http_pinba_scriptname,
202+
ngx_http_set_complex_value_slot,
205203
NGX_HTTP_LOC_CONF_OFFSET,
206-
0,
204+
offsetof(ngx_http_pinba_loc_conf_t, script_name),
207205
NULL },
208206

209207
ngx_null_command
@@ -503,66 +501,6 @@ static char *ngx_http_pinba_tag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
503501
}
504502
/* }}} */
505503

506-
507-
static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */
508-
{
509-
ngx_str_t *value, *server_name;
510-
511-
value = cf->args->elts;
512-
server_name = &value[1];
513-
514-
ngx_http_complex_value_t cv;
515-
ngx_http_compile_complex_value_t ccv;
516-
517-
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
518-
ccv.cf = cf;
519-
ccv.value = server_name;
520-
ccv.complex_value = &cv;
521-
522-
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
523-
return NGX_CONF_ERROR;
524-
}
525-
526-
g_servername = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
527-
if (g_servername == NULL) {
528-
return NGX_CONF_ERROR;
529-
}
530-
*g_servername = cv;
531-
532-
return NGX_CONF_OK;
533-
}
534-
/* }}} */
535-
536-
537-
static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */
538-
{
539-
ngx_str_t *value, *script_name;
540-
541-
value = cf->args->elts;
542-
script_name = &value[1];
543-
544-
ngx_http_complex_value_t cv;
545-
ngx_http_compile_complex_value_t ccv;
546-
547-
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
548-
ccv.cf = cf;
549-
ccv.value = script_name;
550-
ccv.complex_value = &cv;
551-
552-
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
553-
return NGX_CONF_ERROR;
554-
}
555-
556-
g_scriptname = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
557-
if (g_scriptname == NULL) {
558-
return NGX_CONF_ERROR;
559-
}
560-
*g_scriptname = cv;
561-
562-
return NGX_CONF_OK;
563-
}
564-
/* }}} */
565-
566504
static char *ngx_http_pinba_timer_handler(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) /* {{{ */
567505
{
568506
ngx_str_t *value, tag_name, tag_value;
@@ -1014,43 +952,43 @@ static ngx_int_t ngx_http_pinba_handler(ngx_http_request_t *r) /* {{{ */
1014952
request->hostname = strdup(g_hostname);
1015953
}
1016954

955+
/* server_name */
1017956
ngx_str_t v;
1018-
if (ngx_http_complex_value(r, g_servername, &v) == NGX_OK) {
957+
if (lcf->server_name != NULL && ngx_http_complex_value(r, lcf->server_name, &v) == NGX_OK && v.len > 0) {
1019958
memcpy(server_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len);
1020-
request->server_name = strdup(server_name);
1021959
} else {
1022960
memcpy(server_name, r->headers_in.server.data, (r->headers_in.server.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : r->headers_in.server.len);
1023-
request->server_name = strdup(server_name);
1024961
}
962+
request->server_name = strdup(server_name);
1025963

1026-
request_uri = ngx_http_get_variable(r, &request_uri_name, request_uri_key);
1027-
1028-
if (request_uri && !request_uri->not_found && request_uri->len > 0) {
1029-
/* try variable first */
1030-
memcpy(script_name, request_uri->data, (request_uri->len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : request_uri->len);
964+
/* script_name */
965+
if (lcf->script_name != NULL && ngx_http_complex_value(r, lcf->script_name, &v) == NGX_OK && v.len > 0) {
966+
memcpy(script_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len);
1031967
} else {
1032-
u_char *q = NULL;
1033-
int uri_len = r->unparsed_uri.len;
968+
request_uri = ngx_http_get_variable(r, &request_uri_name, request_uri_key);
1034969

1035-
/* default script_name is $request_uri with GET parameters cut off */
970+
if (request_uri && !request_uri->not_found && request_uri->len > 0) {
971+
/* try variable first */
972+
memcpy(script_name, request_uri->data, (request_uri->len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : request_uri->len);
973+
} else {
974+
u_char *q = NULL;
975+
int uri_len = r->unparsed_uri.len;
1036976

1037-
if (r->unparsed_uri.data && r->unparsed_uri.len) {
1038-
q = (u_char *)ngx_strchr(r->unparsed_uri.data, '?');
1039-
if (q) {
1040-
uri_len = q - r->unparsed_uri.data;
1041-
}
977+
/* default script_name is $request_uri with GET parameters cut off */
1042978

1043-
}
1044-
memcpy(script_name, r->unparsed_uri.data, (uri_len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : uri_len);
1045-
}
979+
if (r->unparsed_uri.data && r->unparsed_uri.len) {
980+
q = (u_char *)ngx_strchr(r->unparsed_uri.data, '?');
981+
if (q) {
982+
uri_len = q - r->unparsed_uri.data;
983+
}
1046984

1047-
if (ngx_http_complex_value(r, g_scriptname, &v) == NGX_OK) {
1048-
memcpy(script_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len);
1049-
request->script_name = strdup(script_name);
1050-
} else {
1051-
request->script_name = strdup(script_name);
985+
}
986+
memcpy(script_name, r->unparsed_uri.data, (uri_len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : uri_len);
987+
}
1052988
}
989+
request->script_name = strdup(script_name);
1053990

991+
/* schema */
1054992
request_schema = ngx_http_get_variable(r, &request_schema_name, request_schema_key);
1055993

1056994
if (request_schema && !request_schema->not_found && request_schema->len) {
@@ -1295,6 +1233,8 @@ static void *ngx_http_pinba_create_loc_conf(ngx_conf_t *cf) /* {{{ */
12951233
conf->tags = NULL;
12961234
conf->timers = NULL;
12971235
conf->resolve_freq = NGX_CONF_UNSET;
1236+
conf->script_name = NULL;
1237+
conf->server_name = NULL;
12981238

12991239
return conf;
13001240
}
@@ -1334,6 +1274,14 @@ static char *ngx_http_pinba_merge_loc_conf(ngx_conf_t *cf, void *parent, void *c
13341274
conf->server = prev->server;
13351275
}
13361276

1277+
if (conf->server_name == NULL) {
1278+
conf->server_name = prev->server_name;
1279+
}
1280+
if (conf->script_name == NULL) {
1281+
conf->script_name = prev->script_name;
1282+
}
1283+
1284+
13371285
ngx_conf_merge_sec_value(conf->resolve_freq, prev->resolve_freq, 60);
13381286

13391287
return NGX_CONF_OK;

0 commit comments

Comments
 (0)