From bafac72b3a9bc399f0fc85de1eb8b3085952e695 Mon Sep 17 00:00:00 2001 From: davidzhang Date: Fri, 14 Jul 2017 11:42:46 +0800 Subject: [PATCH] [add] add post log option api [add] add bind interface to post log option --- sample/log_post_logs_sample.c | 123 +++++++++++++++++++++++++++++++++- src/aos_http_io.c | 1 + src/aos_transport.c | 5 ++ src/aos_transport.h | 6 +- src/log_api.c | 45 +++++++++++-- src/log_api.h | 11 +++ 6 files changed, 180 insertions(+), 11 deletions(-) diff --git a/sample/log_post_logs_sample.c b/sample/log_post_logs_sample.c index f4cc6c3..af45197 100644 --- a/sample/log_post_logs_sample.c +++ b/sample/log_post_logs_sample.c @@ -8,6 +8,46 @@ #include "log_config.h" #include #include + +log_post_option g_log_option; + + +void post_logs_with_json_with_option() +{ + aos_pool_t *p = NULL; + aos_status_t *s = NULL; + aos_pool_create(&p, NULL); + + g_log_option.interface = "eth0"; + cJSON *root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "__source__", "127.0.0.1"); + cJSON_AddStringToObject(root, "__topic__", "topic"); + cJSON *logs = cJSON_CreateArray(); + cJSON_AddItemToObject(root, "__logs__", logs); + cJSON *log1 = cJSON_CreateObject(); + cJSON_AddStringToObject(log1, "level", "error1"); + cJSON_AddStringToObject(log1, "message", "c sdk test message1"); + cJSON_AddNumberToObject(log1, "__time__", apr_time_now()/1000000); + cJSON *log2 = cJSON_CreateObject(); + cJSON_AddStringToObject(log2, "level", "error2"); + cJSON_AddStringToObject(log2, "message", "c sdk test message2"); + cJSON_AddNumberToObject(log2, "__time__", apr_time_now()/1000000); + cJSON_AddItemToArray(logs, log1); + cJSON_AddItemToArray(logs, log2); + s = log_post_logs_with_option(p, LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, PROJECT_NAME, LOGSTORE_NAME, root, &g_log_option); + if (aos_status_is_ok(s)) { + printf("post logs succeeded\n"); + } else { + printf("%d\n",s->code); + printf("%s\n",s->error_code); + printf("%s\n",s->error_msg); + printf("%s\n",s->req_id); + printf("put logs failed\n"); + } + cJSON_Delete(root); + aos_pool_destroy(p); +} + void post_logs_with_json() { aos_pool_t *p = NULL; @@ -109,7 +149,45 @@ double test_build_speed_second(const char* path){ } - +void post_logs_with_http_cont_log_option(){ + + if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { + exit(1); + } + + aos_status_t *s = NULL; + log_group_builder* bder = log_group_create(); + add_source(bder,"mSource",sizeof("mSource")); + add_topic(bder,"mTopic", sizeof("mTopic")); + + g_log_option.interface = "eth0"; + int i; + for(i=0;i<3;i++){ + add_log(bder); + int j; + for(j=0;j<5;j++){ + char k[] = {i*j + 'a'}; + char v[] = {2*j + 'a'} ; + add_log_key_value(bder, k, strlen(k), v, strlen(v)); + } + } + log_http_cont* cont = log_create_http_cont(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder); + s = log_post_logs_from_http_cont_with_option(cont, &g_log_option); + if (aos_status_is_ok(s)) { + printf("post logs succeeded\n"); + } else { + printf("%d\n",s->code); + printf("%s\n",s->error_code); + printf("%s\n",s->error_msg); + printf("%s\n",s->req_id); + printf("put logs failed\n"); + } + + log_clean_http_cont(cont); + + aos_http_io_deinitialize(); +} + void post_logs_with_http_cont(){ if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { @@ -147,6 +225,46 @@ void post_logs_with_http_cont(){ aos_http_io_deinitialize(); } +void post_logs_with_proto_buffer_with_option(){ + + if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { + exit(1); + } + + aos_status_t *s = NULL; + log_group_builder* bder = log_group_create(); + add_source(bder,"mSource",sizeof("mSource")); + add_topic(bder,"mTopic", sizeof("mTopic")); + + int i; + for(i=0;i<3;i++){ + add_log(bder); + int j; + for(j=0;j<5;j++){ + char k[] = {i*j + 'a'}; + char v[] = {2*j + 'a'} ; + add_log_key_value(bder, k, strlen(k), v, strlen(v)); + } + } + g_log_option.interface = "eth0"; + s = log_post_logs_from_proto_buf_with_option(LOG_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET,NULL, PROJECT_NAME, LOGSTORE_NAME, bder, &g_log_option); + + if (aos_status_is_ok(s)) { + printf("post logs succeeded\n"); + } else { + printf("%d\n",s->code); + printf("%s\n",s->error_code); + printf("%s\n",s->error_msg); + printf("%s\n",s->req_id); + printf("put logs failed\n"); + } + + log_group_destroy(bder); + + aos_http_io_deinitialize(); +} + + void post_logs_with_proto_buffer(){ if (aos_http_io_initialize("linux-x86_64", 0) != AOSE_OK) { @@ -189,6 +307,7 @@ void post_logs_with_proto_buffer(){ int main(int argc, char *argv[]) { - post_logs_with_http_cont(); + post_logs_with_proto_buffer_with_option(); + post_logs_with_http_cont_log_option(); return 0; } diff --git a/src/aos_http_io.c b/src/aos_http_io.c index ced54bc..846a196 100644 --- a/src/aos_http_io.c +++ b/src/aos_http_io.c @@ -322,6 +322,7 @@ int aos_http_send_request(aos_http_controller_t *ctl, aos_http_request_t *req, a t->req = req; t->resp = resp; t->controller = (aos_http_controller_ex_t *)ctl; + t->interface = ctl->interface; return aos_http_transport_perform(t); } diff --git a/src/aos_transport.c b/src/aos_transport.c index 08de4cd..aef59d5 100644 --- a/src/aos_transport.c +++ b/src/aos_transport.c @@ -349,6 +349,11 @@ int aos_curl_transport_setup(aos_curl_http_transport_t *t) curl_easy_setopt_safe(CURLOPT_TCP_NODELAY, 1); curl_easy_setopt_safe(CURLOPT_NETRC, CURL_NETRC_IGNORED); + // interface + if (t->interface != NULL){ + curl_easy_setopt_safe(CURLOPT_INTERFACE, t->interface); + } + // transport options curl_easy_setopt_safe(CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt_safe(CURLOPT_USERAGENT, t->options->user_agent); diff --git a/src/aos_transport.h b/src/aos_transport.h index a53e9be..31ded78 100644 --- a/src/aos_transport.h +++ b/src/aos_transport.h @@ -43,7 +43,8 @@ struct aos_http_transport_options_s { int64_t first_byte_time; \ int64_t finish_time; \ uint32_t owner:1; \ - void *user_data; + void *user_data; \ + char * interface; struct aos_http_controller_s { AOS_HTTP_BASE_CONTROLLER_DEFINE @@ -118,7 +119,8 @@ typedef enum { aos_transport_state_e state; \ aos_array_header_t *cleanup; \ aos_http_transport_options_t *options; \ - aos_http_controller_ex_t *controller; + aos_http_controller_ex_t *controller; \ + char * interface; struct aos_http_transport_s { AOS_HTTP_BASE_TRANSPORT_DEFINE diff --git a/src/log_api.c b/src/log_api.c index 17b7ce8..9b4bbf7 100644 --- a/src/log_api.c +++ b/src/log_api.c @@ -7,7 +7,7 @@ #include "log_util.h" #include "log_api.h" -aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){ +aos_status_t* log_post_logs_from_http_cont_with_option(log_http_cont* cont, log_post_option * logPostOption){ aos_http_request_t *req = (aos_http_request_t*)apr_palloc(cont->root, sizeof(aos_http_request_t)); req->pool = cont->root; @@ -22,7 +22,9 @@ aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){ options->ctl = aos_http_controller_create(cont->root, 0); options->ctl->pool = cont->root; - + if (logPostOption != NULL){ + options->ctl->interface = (char *)logPostOption->interface; + } aos_list_t buffer; aos_list_init(&buffer); @@ -38,8 +40,7 @@ aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont){ return s; } - -aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder) +aos_status_t *log_post_logs_from_proto_buf_with_option(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder, log_post_option * logPostOption) { aos_pool_t *p = bder->root; aos_string_t project_name, logstore_name; @@ -64,6 +65,9 @@ aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * ac aos_str_set(&(options->config->sts_token), stsToken); } options->ctl = aos_http_controller_create(options->pool, 0); + if (logPostOption != NULL){ + options->ctl->interface = (char *)logPostOption->interface; + } headers = aos_table_make(p, 5); apr_table_set(headers, LOG_API_VERSION, "0.6.0"); apr_table_set(headers, LOG_COMPRESS_TYPE, "lz4"); @@ -120,8 +124,7 @@ aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * ac } - -aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root) +aos_status_t *log_post_logs_with_sts_token_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption) { aos_string_t project_name, logstore_name; aos_table_t *headers = NULL; @@ -145,6 +148,9 @@ aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, aos_str_set(&(options->config->sts_token), stsToken); } options->ctl = aos_http_controller_create(options->pool, 0); + if (logPostOption != NULL){ + options->ctl->interface = (char *)logPostOption->interface; + } headers = aos_table_make(p, 5); apr_table_set(headers, LOG_API_VERSION, "0.6.0"); apr_table_set(headers, LOG_COMPRESS_TYPE, "lz4"); @@ -196,7 +202,32 @@ aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, free(body); return s; } + +aos_status_t *log_post_logs_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption) +{ + return log_post_logs_with_sts_token_with_option(p, endpoint, accesskeyId, accessKey, NULL, project, logstore, root, logPostOption); +} + + + + +aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root) +{ + return log_post_logs_with_sts_token_with_option(p, endpoint, accesskeyId, accessKey, stsToken, project, logstore, root, NULL); + +} + +aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder) +{ + return log_post_logs_from_proto_buf_with_option(endpoint, accesskeyId, accessKey, stsToken, project, logstore, bder, NULL); +} + +aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont) +{ + return log_post_logs_from_http_cont_with_option(cont, NULL); +} + aos_status_t *log_post_logs(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root) { - return log_post_logs_with_sts_token(p, endpoint, accesskeyId, accessKey, NULL, project, logstore, root); + return log_post_logs_with_option(p, endpoint, accesskeyId, accessKey, project, logstore, root, NULL); } diff --git a/src/log_api.h b/src/log_api.h index 7ad2e1c..ca77e8b 100644 --- a/src/log_api.h +++ b/src/log_api.h @@ -10,10 +10,21 @@ #include "log_http_cont.h" LOG_CPP_START +struct _log_post_option +{ + char * interface; +}; +typedef struct _log_post_option log_post_option; + aos_status_t *log_post_logs_with_sts_token(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root); aos_status_t *log_post_logs(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root); aos_status_t *log_post_logs_from_proto_buf(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder); aos_status_t* log_post_logs_from_http_cont(log_http_cont* cont); +aos_status_t *log_post_logs_with_sts_token_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption); +aos_status_t *log_post_logs_with_option(aos_pool_t *p, const char *endpoint, const char * accesskeyId, const char *accessKey, const char *project, const char *logstore, cJSON *root, log_post_option * logPostOption); +aos_status_t *log_post_logs_from_proto_buf_with_option(const char *endpoint, const char * accesskeyId, const char *accessKey, const char *stsToken, const char *project, const char *logstore, log_group_builder* bder, log_post_option * logPostOption); +aos_status_t* log_post_logs_from_http_cont_with_option(log_http_cont* cont, log_post_option * logPostOption); + LOG_CPP_END #endif