TSG-14786 TFE输出Proxy Rule Hits Metric
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "logger.h"
|
||||
#include <tfe_scan.h>
|
||||
#include <MESA/stream.h>
|
||||
#include <tfe_fieldstat.h>
|
||||
|
||||
extern void increase_redirect_policy_hit_num(void);
|
||||
|
||||
@@ -661,6 +662,21 @@ static void doh_process_req(const struct tfe_stream *stream, const struct tfe_ht
|
||||
}
|
||||
}
|
||||
|
||||
struct tfe_fieldstat_metric_t *doh_fieldstat_init(const char* profile, 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, section, "telegraf_port", (short *)&(telegraf_port));
|
||||
MESA_load_profile_string_nodef(profile, section, "telegraf_ip", telegraf_ip, sizeof(telegraf_ip));
|
||||
MESA_load_profile_string_def(profile, section, "app_name", app_name, sizeof(app_name), "metric");
|
||||
MESA_load_profile_int_def(profile, section, "cycle", &cycle, 1000);
|
||||
|
||||
return tfe_fieldstat_metric_create(telegraf_ip, telegraf_port, app_name, cycle, max_thread, g_doh_conf->local_logger);
|
||||
}
|
||||
|
||||
int doh_on_init(struct tfe_proxy *proxy)
|
||||
{
|
||||
const char *profile = "./conf/doh/doh.conf";
|
||||
@@ -679,6 +695,7 @@ int doh_on_init(struct tfe_proxy *proxy)
|
||||
|
||||
g_doh_conf->thread_num = tfe_proxy_get_work_thread_count();
|
||||
g_doh_conf->local_logger = MESA_create_runtime_log_handle("doh", RLOG_LV_DEBUG);
|
||||
g_doh_conf->fieldstat = doh_fieldstat_init(profile, "proxy_hits", g_doh_conf->thread_num);
|
||||
|
||||
g_doh_conf->gc_evbase = tfe_proxy_get_gc_evbase();
|
||||
g_doh_conf->fs_handle = tfe_proxy_get_fs_handle();
|
||||
@@ -856,6 +873,23 @@ int doh_on_data(const struct tfe_stream *stream, const struct tfe_http_session *
|
||||
return NO_CALL_NEXT_PLUGIN;
|
||||
}
|
||||
|
||||
void doh_send_metric_log(const struct tfe_stream * stream, struct doh_ctx *ctx, unsigned int thread_id)
|
||||
{
|
||||
size_t c2s_byte_num = 0, s2c_byte_num =0;
|
||||
struct tfe_fieldstat_metric_t *fieldstat = g_doh_conf->fieldstat;
|
||||
|
||||
fieldstat->tags[thread_id][TAG_RULE_ID].value_int = ctx->result->config_id;
|
||||
fieldstat->tags[thread_id][TAG_ACTION].value_int = 48;
|
||||
fieldstat->tags[thread_id][TAG_SUB_ACTION].value_str = "redirect";
|
||||
|
||||
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 doh_on_end(const struct tfe_stream *stream, const struct tfe_http_session *session, unsigned int thread_id, void **pme)
|
||||
{
|
||||
if (!g_doh_conf->enable)
|
||||
@@ -869,6 +903,7 @@ void doh_on_end(const struct tfe_stream *stream, const struct tfe_http_session *
|
||||
int ret = doh_send_log(g_doh_conf, session, stream, ctx);
|
||||
if (ret > 0)
|
||||
{
|
||||
doh_send_metric_log(stream, ctx, thread_id);
|
||||
ATOMIC_ADD(&(g_doh_conf->stat_val[STAT_LOG_NUM]), ret);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ struct doh_conf
|
||||
struct event *gcev;
|
||||
struct event_base *gc_evbase;
|
||||
screen_stat_handle_t fs_handle;
|
||||
struct tfe_fieldstat_metric_t *fieldstat;
|
||||
|
||||
struct maat *maat;
|
||||
struct maat_table tables[TYPE_MAX];
|
||||
|
||||
@@ -367,6 +367,7 @@ static struct maat* maat_feather_create_with_override(const char * instance_name
|
||||
if (maat_stat_on)
|
||||
{
|
||||
maat_options_set_stat_on(opts);
|
||||
maat_options_set_stat_file(opts, maat_stat_file);
|
||||
if (maat_perf_on)
|
||||
{
|
||||
maat_options_set_perf_on(opts);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user