输出非结构化日志。

This commit is contained in:
zhengchao
2018-12-24 22:47:26 +06:00
parent 67c488bcc1
commit 6cbe6e7b9c
4 changed files with 107 additions and 4 deletions

View File

@@ -144,6 +144,10 @@ static void __promise_destroy(struct promise *p)
struct promise * future_to_promise(struct future * f) struct promise * future_to_promise(struct future * f)
{ {
if(f==NULL)
{
return NULL;
}
struct promise *p=__future_to_promise(f); struct promise *p=__future_to_promise(f);
p->ref_cnt++; p->ref_cnt++;
assert(p->ref_cnt==2); assert(p->ref_cnt==2);

View File

@@ -90,8 +90,9 @@ struct pangu_rt
int fs_id[__PG_STAT_MAX]; int fs_id[__PG_STAT_MAX];
struct event_base* gc_evbase; struct event_base* gc_evbase;
struct event* gcev; struct event* gcev;
int ca_store_reseting; int ca_store_reseting;
}; };
struct pangu_rt * g_pangu_rt; struct pangu_rt * g_pangu_rt;
@@ -502,7 +503,6 @@ int pangu_http_init(struct tfe_proxy * proxy)
} }
TFE_LOG_INFO(NULL, "Tango Cache Enabled."); TFE_LOG_INFO(NULL, "Tango Cache Enabled.");
} }
TFE_LOG_INFO(NULL, "Pangu HTTP init success."); TFE_LOG_INFO(NULL, "Pangu HTTP init success.");
return 0; return 0;
@@ -532,6 +532,7 @@ struct pangu_http_ctx
struct Maat_rule_t * enforce_rules; struct Maat_rule_t * enforce_rules;
size_t n_enforce; size_t n_enforce;
char * enforce_para; char * enforce_para;
struct evbuffer* log_req_body, *log_resp_body;
struct replace_ctx * rep_ctx; struct replace_ctx * rep_ctx;
@@ -547,6 +548,7 @@ struct pangu_http_ctx
size_t cache_result_declared_sz, cache_result_actual_sz; size_t cache_result_declared_sz, cache_result_actual_sz;
struct cache_write_context* cache_write_ctx; struct cache_write_context* cache_write_ctx;
int cache_wirte_result; int cache_wirte_result;
int thread_id; int thread_id;
}; };
@@ -628,7 +630,16 @@ static void pangu_http_ctx_free(struct pangu_http_ctx * ctx)
future_destroy(ctx->f_cache_pending); future_destroy(ctx->f_cache_pending);
ctx->f_cache_pending=NULL; ctx->f_cache_pending=NULL;
} }
if(ctx->log_req_body)
{
evbuffer_free(ctx->log_req_body);
ctx->log_req_body=NULL;
}
if(ctx->log_resp_body)
{
evbuffer_free(ctx->log_resp_body);
ctx->log_resp_body=NULL;
}
ctx->magic_num=0; ctx->magic_num=0;
FREE(&ctx); FREE(&ctx);
} }
@@ -1141,6 +1152,25 @@ void enforce_control_policy(const struct tfe_stream * stream, const struct tfe_h
default: assert(0); default: assert(0);
break; break;
} }
if(ctx->action!=PG_ACTION_NONE && ctx->action!=PG_ACTION_WHITELIST)
{
if (events & EV_HTTP_REQ_BODY_BEGIN)
{
ctx->log_req_body=evbuffer_new();
}
if (events & EV_HTTP_RESP_BODY_BEGIN)
{
ctx->log_resp_body=evbuffer_new();
}
if(events & EV_HTTP_REQ_BODY_CONT)
{
evbuffer_add(ctx->log_req_body, body_frag, frag_size);
}
if(events & EV_HTTP_RESP_BODY_CONT)
{
evbuffer_add(ctx->log_resp_body, body_frag, frag_size);
}
}
return; return;
} }
#define RESUMED_CB_NO_MORE_CALLS 0 #define RESUMED_CB_NO_MORE_CALLS 0
@@ -1478,7 +1508,8 @@ void pangu_on_http_end(const struct tfe_stream * stream,
FREE(&(ctx->enforce_rules)); FREE(&(ctx->enforce_rules));
} }
} }
struct pangu_log log_msg = {.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=ctx->n_enforce}; struct pangu_log log_msg = {.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=ctx->n_enforce,
.req_body= ctx->log_req_body, .resp_body=ctx->log_resp_body};
if (ctx->action != PG_ACTION_NONE&& !(ctx->action == PG_ACTION_REPLACE && ctx->n_enforce==1 && ctx->rep_ctx->actually_replaced==0)) if (ctx->action != PG_ACTION_NONE&& !(ctx->action == PG_ACTION_REPLACE && ctx->n_enforce==1 && ctx->rep_ctx->actually_replaced==0))
{ {
ret=pangu_send_log(g_pangu_rt->send_logger, &log_msg); ret=pangu_send_log(g_pangu_rt->send_logger, &log_msg);

View File

@@ -14,6 +14,8 @@
#include <errno.h> #include <errno.h>
#include <tfe_utils.h> #include <tfe_utils.h>
#include <cache_evbase_client.h>
#include "pangu_logger.h" #include "pangu_logger.h"
struct json_spec struct json_spec
@@ -40,6 +42,8 @@ struct pangu_logger
unsigned long long random_drop; unsigned long long random_drop;
unsigned long long user_abort; unsigned long long user_abort;
char local_log_path[TFE_STRING_MAX]; char local_log_path[TFE_STRING_MAX];
struct cache_evbase_instance * log_file_upload_instance;
}; };
static unsigned int get_ip_by_eth_name(const char *ifname) static unsigned int get_ip_by_eth_name(const char *ifname)
@@ -101,6 +105,7 @@ struct pangu_logger* pangu_log_handle_create(const char* profile, const char* s
{ {
int ret=-1; int ret=-1;
char nic_name[64]={0}; char nic_name[64]={0};
struct tango_cache_parameter *log_file_upload_para=NULL;
struct pangu_logger* instance=ALLOC(struct pangu_logger,1); struct pangu_logger* instance=ALLOC(struct pangu_logger,1);
instance->local_logger=local_logger; instance->local_logger=local_logger;
@@ -132,6 +137,10 @@ struct pangu_logger* pangu_log_handle_create(const char* profile, const char* s
} }
instance->topic_name="PXY-HTTP-LOG"; instance->topic_name="PXY-HTTP-LOG";
instance->kafka_topic = rd_kafka_topic_new(instance->kafka_handle,instance->topic_name, NULL); instance->kafka_topic = rd_kafka_topic_new(instance->kafka_handle,instance->topic_name, NULL);
log_file_upload_para=cache_evbase_parameter_new(profile, section, local_logger);
instance->log_file_upload_instance=cache_evbase_instance_new(log_file_upload_para, local_logger);
pthread_mutex_init(&(instance->mutex), NULL); pthread_mutex_init(&(instance->mutex), NULL);
return instance; return instance;
@@ -149,6 +158,7 @@ int pangu_send_log(struct pangu_logger* handle, const struct pangu_log* log_msg)
char* log_payload=NULL; char* log_payload=NULL;
int kafka_status=0; int kafka_status=0;
int send_cnt=0; int send_cnt=0;
int tmp=0;
time_t cur_time; time_t cur_time;
char src_ip_str[MAX(INET6_ADDRSTRLEN,INET_ADDRSTRLEN)] = {0}; char src_ip_str[MAX(INET6_ADDRSTRLEN,INET_ADDRSTRLEN)] = {0};
char dst_ip_str[MAX(INET6_ADDRSTRLEN,INET_ADDRSTRLEN)] = {0}; char dst_ip_str[MAX(INET6_ADDRSTRLEN,INET_ADDRSTRLEN)] = {0};
@@ -215,6 +225,63 @@ int pangu_send_log(struct pangu_logger* handle, const struct pangu_log* log_msg)
cJSON_AddStringToObject(common_obj,resp_fields[i].log_filed_name, tmp_val); cJSON_AddStringToObject(common_obj,resp_fields[i].log_filed_name, tmp_val);
} }
} }
char log_file_upload_path[TFE_STRING_MAX]={0}, cont_type_whole[TFE_STRING_MAX]={0};
struct tango_cache_meta_put meta;
char* log_file_key=NULL;;
const char* cont_type_val;
if(log_msg->req_body!=NULL)
{
memset(&meta, 0, sizeof(meta));
asprintf(&log_file_key, "%s.reqbody", http->req->req_spec.url);
meta.url=log_file_key;
cont_type_val=tfe_http_std_field_read(http->req, TFE_HTTP_CONT_TYPE);
if(cont_type_val!=NULL)
{
snprintf(cont_type_whole, sizeof(cont_type_whole), "Content-Type:%s", cont_type_val);
meta.std_hdr[0]=cont_type_whole;
}
tmp=cache_evbase_upload_once_evbuf(handle->log_file_upload_instance, NULL,
log_msg->req_body,
&meta,
log_file_upload_path, sizeof(log_file_upload_path));
if(tmp==0)
{
cJSON_AddStringToObject(common_obj, "req_body", log_file_upload_path);
}
else
{
TFE_LOG_ERROR(handle->local_logger, "Upload req_body failed.");
}
free(log_file_key);
}
if(log_msg->resp_body!=NULL)
{
memset(&meta, 0, sizeof(meta));
asprintf(&log_file_key, "%s.respbody", http->req->req_spec.url);
meta.url=log_file_key;
cont_type_val=tfe_http_std_field_read(http->resp, TFE_HTTP_CONT_TYPE);
if(cont_type_val!=NULL)
{
snprintf(cont_type_whole, sizeof(cont_type_whole), "Content-Type:%s", cont_type_val);
meta.std_hdr[0]=cont_type_whole;
}
tmp=cache_evbase_upload_once_evbuf(handle->log_file_upload_instance, NULL,
log_msg->resp_body,
&meta,
log_file_upload_path, sizeof(log_file_upload_path));
if(tmp==0)
{
cJSON_AddStringToObject(common_obj, "resp_body", log_file_upload_path);
}
else
{
TFE_LOG_ERROR(handle->local_logger, "Upload resp_body failed.");
}
free(log_file_key);
}
for(size_t i=0; i<log_msg->result_num; i++) for(size_t i=0; i<log_msg->result_num; i++)
{ {

View File

@@ -10,6 +10,7 @@ struct pangu_log
const struct tfe_http_session* http; const struct tfe_http_session* http;
const Maat_rule_t*result; const Maat_rule_t*result;
size_t result_num; size_t result_num;
struct evbuffer* req_body, *resp_body;
}; };
struct pangu_logger; struct pangu_logger;
struct pangu_logger* pangu_log_handle_create(const char* profile, const char* section, void* local_logger); struct pangu_logger* pangu_log_handle_create(const char* profile, const char* section, void* local_logger);