From 25acd538ea70b33929d67090c77838dc23fa8adc Mon Sep 17 00:00:00 2001 From: Oleg Fedoseev Date: Mon, 17 Aug 2015 15:57:11 +0600 Subject: [PATCH 1/2] First working draft --- ngx_http_pinba_module.c | 96 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 3 deletions(-) diff --git a/ngx_http_pinba_module.c b/ngx_http_pinba_module.c index e232dc7..da8d93c 100644 --- a/ngx_http_pinba_module.c +++ b/ngx_http_pinba_module.c @@ -87,6 +87,8 @@ typedef struct { ngx_pinba_hash_sock_t *g_sock_hash = NULL; char g_hostname[PINBA_STR_BUFFER_SIZE]; +ngx_http_complex_value_t *g_servername; +ngx_http_complex_value_t *g_scriptname; static void *ngx_http_pinba_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_pinba_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); @@ -96,6 +98,8 @@ static char *ngx_http_pinba_tag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_pinba_buffer_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_pinba_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_pinba_timer_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); typedef struct { ProtobufCBuffer base; @@ -188,6 +192,20 @@ static ngx_command_t ngx_http_pinba_commands[] = { /* {{{ */ 0, NULL }, + { ngx_string("pinba_server_name"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, + ngx_http_pinba_servername, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + + { ngx_string("pinba_script_name"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, + ngx_http_pinba_scriptname, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + ngx_null_command }; /* }}} */ @@ -485,6 +503,66 @@ static char *ngx_http_pinba_tag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } /* }}} */ + +static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */ +{ + ngx_str_t *value, *server_name; + + value = cf->args->elts; + server_name = &value[1]; + + ngx_http_complex_value_t cv; + ngx_http_compile_complex_value_t ccv; + + ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); + ccv.cf = cf; + ccv.value = server_name; + ccv.complex_value = &cv; + + if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { + return NGX_CONF_ERROR; + } + + g_servername = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); + if (g_servername == NULL) { + return NGX_CONF_ERROR; + } + *g_servername = cv; + + return NGX_CONF_OK; +} +/* }}} */ + + +static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */ +{ + ngx_str_t *value, *script_name; + + value = cf->args->elts; + script_name = &value[1]; + + ngx_http_complex_value_t cv; + ngx_http_compile_complex_value_t ccv; + + ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); + ccv.cf = cf; + ccv.value = script_name; + ccv.complex_value = &cv; + + if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { + return NGX_CONF_ERROR; + } + + g_scriptname = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); + if (g_scriptname == NULL) { + return NGX_CONF_ERROR; + } + *g_scriptname = cv; + + return NGX_CONF_OK; +} +/* }}} */ + static char *ngx_http_pinba_timer_handler(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) /* {{{ */ { ngx_str_t *value, tag_name, tag_value; @@ -936,8 +1014,14 @@ static ngx_int_t ngx_http_pinba_handler(ngx_http_request_t *r) /* {{{ */ request->hostname = strdup(g_hostname); } - 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); - request->server_name = strdup(server_name); + ngx_str_t v; + if (ngx_http_complex_value(r, g_servername, &v) == NGX_OK) { + memcpy(server_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len); + request->server_name = strdup(server_name); + } else { + 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); + request->server_name = strdup(server_name); + } request_uri = ngx_http_get_variable(r, &request_uri_name, request_uri_key); @@ -959,7 +1043,13 @@ static ngx_int_t ngx_http_pinba_handler(ngx_http_request_t *r) /* {{{ */ } memcpy(script_name, r->unparsed_uri.data, (uri_len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : uri_len); } - request->script_name = strdup(script_name); + + if (ngx_http_complex_value(r, g_scriptname, &v) == NGX_OK) { + memcpy(script_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len); + request->script_name = strdup(script_name); + } else { + request->script_name = strdup(script_name); + } request_schema = ngx_http_get_variable(r, &request_schema_name, request_schema_key); From 22404dbd6ed4adf136e853b438c41c132f1c301d Mon Sep 17 00:00:00 2001 From: Oleg Fedoseev Date: Mon, 17 Aug 2015 22:09:59 +0600 Subject: [PATCH 2/2] little cleanup --- ngx_http_pinba_module.c | 113 +++++++++++----------------------------- 1 file changed, 29 insertions(+), 84 deletions(-) diff --git a/ngx_http_pinba_module.c b/ngx_http_pinba_module.c index da8d93c..b620daa 100644 --- a/ngx_http_pinba_module.c +++ b/ngx_http_pinba_module.c @@ -57,12 +57,14 @@ typedef struct { } ngx_pinba_hash_sock_t; typedef struct { - ngx_flag_t enable; - ngx_array_t *ignore_codes; - ngx_url_t server; + ngx_flag_t enable; + ngx_array_t *ignore_codes; + ngx_url_t server; ngx_array_t *tags; ngx_array_t *timers; - time_t resolve_freq; /* default 60 sec */ + time_t resolve_freq; /* default 60 sec */ + ngx_http_complex_value_t *server_name; + ngx_http_complex_value_t *script_name; } ngx_http_pinba_loc_conf_t; typedef struct { @@ -87,8 +89,6 @@ typedef struct { ngx_pinba_hash_sock_t *g_sock_hash = NULL; char g_hostname[PINBA_STR_BUFFER_SIZE]; -ngx_http_complex_value_t *g_servername; -ngx_http_complex_value_t *g_scriptname; static void *ngx_http_pinba_create_loc_conf(ngx_conf_t *cf); 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); static char *ngx_http_pinba_buffer_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_pinba_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_pinba_timer_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); typedef struct { ProtobufCBuffer base; @@ -194,16 +192,16 @@ static ngx_command_t ngx_http_pinba_commands[] = { /* {{{ */ { ngx_string("pinba_server_name"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, - ngx_http_pinba_servername, + ngx_http_set_complex_value_slot, NGX_HTTP_LOC_CONF_OFFSET, - 0, + offsetof(ngx_http_pinba_loc_conf_t, server_name), NULL }, { ngx_string("pinba_script_name"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, - ngx_http_pinba_scriptname, + ngx_http_set_complex_value_slot, NGX_HTTP_LOC_CONF_OFFSET, - 0, + offsetof(ngx_http_pinba_loc_conf_t, script_name), NULL }, ngx_null_command @@ -503,66 +501,6 @@ static char *ngx_http_pinba_tag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } /* }}} */ - -static char *ngx_http_pinba_servername(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */ -{ - ngx_str_t *value, *server_name; - - value = cf->args->elts; - server_name = &value[1]; - - ngx_http_complex_value_t cv; - ngx_http_compile_complex_value_t ccv; - - ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); - ccv.cf = cf; - ccv.value = server_name; - ccv.complex_value = &cv; - - if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { - return NGX_CONF_ERROR; - } - - g_servername = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); - if (g_servername == NULL) { - return NGX_CONF_ERROR; - } - *g_servername = cv; - - return NGX_CONF_OK; -} -/* }}} */ - - -static char *ngx_http_pinba_scriptname(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* {{{ */ -{ - ngx_str_t *value, *script_name; - - value = cf->args->elts; - script_name = &value[1]; - - ngx_http_complex_value_t cv; - ngx_http_compile_complex_value_t ccv; - - ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); - ccv.cf = cf; - ccv.value = script_name; - ccv.complex_value = &cv; - - if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { - return NGX_CONF_ERROR; - } - - g_scriptname = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t)); - if (g_scriptname == NULL) { - return NGX_CONF_ERROR; - } - *g_scriptname = cv; - - return NGX_CONF_OK; -} -/* }}} */ - static char *ngx_http_pinba_timer_handler(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) /* {{{ */ { ngx_str_t *value, tag_name, tag_value; @@ -1014,18 +952,20 @@ static ngx_int_t ngx_http_pinba_handler(ngx_http_request_t *r) /* {{{ */ request->hostname = strdup(g_hostname); } + /* server_name */ ngx_str_t v; - if (ngx_http_complex_value(r, g_servername, &v) == NGX_OK) { + if (lcf->server_name != NULL && ngx_http_complex_value(r, lcf->server_name, &v) == NGX_OK && v.len > 0) { memcpy(server_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len); - request->server_name = strdup(server_name); } else { 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); - request->server_name = strdup(server_name); } + request->server_name = strdup(server_name); + /* script_name */ request_uri = ngx_http_get_variable(r, &request_uri_name, request_uri_key); - - if (request_uri && !request_uri->not_found && request_uri->len > 0) { + if (lcf->script_name != NULL && ngx_http_complex_value(r, lcf->script_name, &v) == NGX_OK && v.len > 0) { + memcpy(script_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len); + } else if (request_uri && !request_uri->not_found && request_uri->len > 0) { /* try variable first */ memcpy(script_name, request_uri->data, (request_uri->len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : request_uri->len); } else { @@ -1043,14 +983,9 @@ static ngx_int_t ngx_http_pinba_handler(ngx_http_request_t *r) /* {{{ */ } memcpy(script_name, r->unparsed_uri.data, (uri_len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : uri_len); } + request->script_name = strdup(script_name); - if (ngx_http_complex_value(r, g_scriptname, &v) == NGX_OK) { - memcpy(script_name, v.data, (v.len > PINBA_STR_BUFFER_SIZE) ? PINBA_STR_BUFFER_SIZE : v.len); - request->script_name = strdup(script_name); - } else { - request->script_name = strdup(script_name); - } - + /* schema */ request_schema = ngx_http_get_variable(r, &request_schema_name, request_schema_key); if (request_schema && !request_schema->not_found && request_schema->len) { @@ -1295,6 +1230,8 @@ static void *ngx_http_pinba_create_loc_conf(ngx_conf_t *cf) /* {{{ */ conf->tags = NULL; conf->timers = NULL; conf->resolve_freq = NGX_CONF_UNSET; + conf->script_name = NULL; + conf->server_name = NULL; return conf; } @@ -1334,6 +1271,14 @@ static char *ngx_http_pinba_merge_loc_conf(ngx_conf_t *cf, void *parent, void *c conf->server = prev->server; } + if (conf->server_name == NULL) { + conf->server_name = prev->server_name; + } + if (conf->script_name == NULL) { + conf->script_name = prev->script_name; + } + + ngx_conf_merge_sec_value(conf->resolve_freq, prev->resolve_freq, 60); return NGX_CONF_OK;