TSG-14786 TFE输出Proxy Rule Hits Metric

This commit is contained in:
fengweihao
2023-04-25 10:13:38 +08:00
parent 95c80c80cf
commit 0bb38a6969
13 changed files with 229 additions and 7 deletions

View File

@@ -12,6 +12,7 @@
#include <tfe_resource.h>
#include <tfe_scan.h>
#include <tfe_types.h>
#include <tfe_fieldstat.h>
#include <MESA/MESA_handle_logger.h>
#include <MESA/MESA_prof_load.h>
@@ -158,8 +159,6 @@ struct tsg_proxy_rt
int scan_table_id[__SCAN_TABLE_MAX];
int plolicy_table_id[POLICY_PROFILE_TABLE_MAX];
ctemplate::Template * tpl_403, * tpl_404, * tpl_451;
char * reject_page;
int page_size;
long long suspend_max;
int cache_enabled;
@@ -171,6 +170,7 @@ struct tsg_proxy_rt
struct event_base* gc_evbase;
struct event* gcev;
struct tfe_fieldstat_metric_t *fieldstat;
struct tsg_lua_script lua_script;
Ratelimiter_handle_t ratelimiter;
int enable_rate;
@@ -192,6 +192,21 @@ static void proxy_http_gc_cb(evutil_socket_t fd, short what, void * arg)
return;
}
struct tfe_fieldstat_metric_t *proxy_fieldstat_init(const char* profile_path, const char *section, int max_thread)
{
int cycle=0;
unsigned short telegraf_port=0;
char telegraf_ip[TFE_STRING_MAX]={0};
char app_name[TFE_STRING_MAX]={0};
MESA_load_profile_short_nodef(profile_path, section, "telegraf_port", (short *)&(telegraf_port));
MESA_load_profile_string_nodef(profile_path, section, "telegraf_ip", telegraf_ip, sizeof(telegraf_ip));
MESA_load_profile_string_def(profile_path, section, "app_name", app_name, sizeof(app_name), "metric");
MESA_load_profile_int_def(profile_path, section, "cycle", &cycle, 1000);
return tfe_fieldstat_metric_create(telegraf_ip, telegraf_port, app_name, cycle, max_thread, g_proxy_rt->local_logger);
}
static void proxy_http_stat_init(struct tsg_proxy_rt * pangu_runtime)
{
int i=0;
@@ -1169,12 +1184,12 @@ int proxy_http_init(struct tfe_proxy * proxy)
{
goto error_out;
}
g_proxy_rt->fs_handle = tfe_proxy_get_fs_handle();
g_proxy_rt->ratelimiter=ratelimit_handle_create(profile_path, "ratelimit");
proxy_http_stat_init(g_proxy_rt);
g_proxy_rt->ratelimiter=ratelimit_handle_create(profile_path, "ratelimit");
g_proxy_rt->fieldstat=proxy_fieldstat_init(profile_path, "proxy_hits", g_proxy_rt->thread_num);
if(http_lua_handle_create(&g_proxy_rt->lua_script, g_proxy_rt->thread_num, "tfe") <0)
{
goto error_out;
@@ -3261,6 +3276,39 @@ static inline int ctx_actually_manipulate(struct proxy_http_ctx * ctx)
}
}
void proxy_send_metric_log(const struct tfe_stream * stream, struct proxy_http_ctx * ctx, unsigned int thread_id)
{
size_t i=0;
const char *proxy_action_map[__PX_ACTION_MAX];
proxy_action_map[PX_ACTION_MONIT]="monitor";
proxy_action_map[PX_ACTION_REJECT]="deny";
proxy_action_map[PX_ACTION_WHITELIST]="allow";
const char *manipulate_action_map[]= {"redirect","block","replace","hijack","insert","edit_element","run_script"};
struct tfe_fieldstat_metric_t *fieldstat = g_proxy_rt->fieldstat;
for(i=0; i< ctx->n_enforce; i++)
{
fieldstat->tags[thread_id][TAG_RULE_ID].value_int = ctx->enforce_rules[i].config_id;
fieldstat->tags[thread_id][TAG_ACTION].value_int = ctx->enforce_rules[i].action;
if(ctx->enforce_rules[i].action == PX_ACTION_MANIPULATE)
{
fieldstat->tags[thread_id][TAG_SUB_ACTION].value_str = manipulate_action_map[ctx->param->action];
}
else
{
fieldstat->tags[thread_id][TAG_SUB_ACTION].value_str = proxy_action_map[ctx->enforce_rules[i].action];
}
size_t c2s_byte_num = 0, s2c_byte_num =0;
tfe_stream_info_get(stream, INFO_FROM_DOWNSTREAM_RX_OFFSET, &c2s_byte_num, sizeof(c2s_byte_num));
tfe_stream_info_get(stream, INFO_FROM_UPSTREAM_RX_OFFSET, &s2c_byte_num, sizeof(s2c_byte_num));
tfe_fieldstat_metric_incrby(fieldstat, fieldstat->column_array[COLUMN_HIT_COUNT], 1, fieldstat->tags[thread_id], thread_id);
tfe_fieldstat_metric_incrby(fieldstat, fieldstat->column_array[COLUMN_IN_BYTES], c2s_byte_num, fieldstat->tags[thread_id], thread_id);
tfe_fieldstat_metric_incrby(fieldstat, fieldstat->column_array[COLUMN_OUT_BYTES], s2c_byte_num, fieldstat->tags[thread_id], thread_id);
}
return;
}
void proxy_on_http_end(const struct tfe_stream * stream,
const struct tfe_http_session * session, unsigned int thread_id, void ** pme)
{
@@ -3317,6 +3365,7 @@ void proxy_on_http_end(const struct tfe_stream * stream,
ATOMIC_INC(&(g_proxy_rt->stat_val[STAT_ACTION_MONIT]));
}
}
proxy_send_metric_log(stream, ctx, thread_id);
}
if(ctx->rep_ctx && ctx->rep_ctx->actually_replaced==1)