diff options
| author | chenzizhan <[email protected]> | 2023-10-08 12:22:59 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-10-08 12:22:59 +0800 |
| commit | b88f84e9d50240b26f10efe9823705b591698cc8 (patch) | |
| tree | 5fa89d0b6ef9cf7ebb590920750d0bf11efaea6e | |
| parent | 437277576e283d64ff6160ce309ab17a56ceddaf (diff) | |
exporter does not bind instance
| -rw-r--r-- | include/fieldstat/fieldstat_exporter.h | 6 | ||||
| -rw-r--r-- | src/exporter/cjson_exporter.c | 59 | ||||
| -rw-r--r-- | test/test_empty_tags.cpp | 4 | ||||
| -rw-r--r-- | test/test_exporter_json.cpp | 40 | ||||
| -rw-r--r-- | test/test_performance.cpp | 4 |
5 files changed, 56 insertions, 57 deletions
diff --git a/include/fieldstat/fieldstat_exporter.h b/include/fieldstat/fieldstat_exporter.h index e846231..2eafb0e 100644 --- a/include/fieldstat/fieldstat_exporter.h +++ b/include/fieldstat/fieldstat_exporter.h @@ -13,7 +13,7 @@ extern "C" /* -------------------------------------------------------------------------- */ struct fieldstat_json_exporter; -struct fieldstat_json_exporter *fieldstat_json_exporter_new(const struct fieldstat *instance); +struct fieldstat_json_exporter *fieldstat_json_exporter_new(); void fieldstat_json_exporter_set_global_tag(struct fieldstat_json_exporter *exporter, const struct fieldstat_tag tag_list[], size_t n_tag); void fieldstat_json_exporter_set_name(struct fieldstat_json_exporter *exporter, const char *name); void fieldstat_json_exporter_free(struct fieldstat_json_exporter *exporter); @@ -21,11 +21,11 @@ void fieldstat_json_exporter_free(struct fieldstat_json_exporter *exporter); /* Output the fieldstat instance to json string array. User must free the output string. */ -char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter, const struct timeval *timestamp); +char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp); /* after call fieldstat_json_exporter_export_array, user must free the output array and each string. */ -void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, const struct timeval *timestamp, char ***output, size_t *output_size); +void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp, char ***output, size_t *output_size); /* let json exporter output delta value by the side of the original value(accumulated). If a cell / metric is new, the delta value is the same as the original value. diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c index 4055182..86399fa 100644 --- a/src/exporter/cjson_exporter.c +++ b/src/exporter/cjson_exporter.c @@ -59,7 +59,6 @@ struct export_kv_pair { }; struct fieldstat_json_exporter { - const struct fieldstat *instance; char *name; struct fieldstat_tag_list *global_tag_list; struct counter_history *history; @@ -280,44 +279,48 @@ struct fieldstat_tag_list *my_copy_fs_tag_list(const struct fieldstat_tag_list * return dest; } -bool counter_history_check_if_need_to_update(const struct fieldstat_json_exporter *exporter) +bool counter_history_check_if_need_to_update(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance) { - if (exporter->history == NULL) { + struct counter_history *history = exporter->history; + if (history == NULL) { return false; // delta export disabled } + if (history->cube_version == NULL) { // first time + return true; + } - unsigned long cur_cell_version = fieldstat_get_version(exporter->instance); + unsigned long cur_cell_version = fieldstat_get_version(instance); const char *cur_exporter_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME; const struct fieldstat_tag_list *cur_global_tag_list = exporter->global_tag_list; - if (exporter->history->cell_version != cur_cell_version) { + if (history->cell_version != cur_cell_version) { return true; } - if (strcmp(exporter->history->exporter_name, cur_exporter_name) != 0) { + if (strcmp(history->exporter_name, cur_exporter_name) != 0) { return true; } - if (exporter->history->global_tag_list == NULL && cur_global_tag_list != NULL) { + if (history->global_tag_list == NULL && cur_global_tag_list != NULL) { return true; } - if (exporter->history->global_tag_list == NULL && cur_global_tag_list == NULL) { + if (history->global_tag_list == NULL && cur_global_tag_list == NULL) { return false; } - // global tag cant be deleted, so no need to check if cur_global_tag_list == NULL && exporter->history->global_tag_list != NULL - if (fieldstat_tag_list_cmp(cur_global_tag_list, exporter->history->global_tag_list) == false) { + // global tag cant be deleted, so no need to check if cur_global_tag_list == NULL && history->global_tag_list != NULL + if (fieldstat_tag_list_cmp(cur_global_tag_list, history->global_tag_list) == false) { return true; } int *cube_ids = NULL; int n_cube = 0; - fieldstat_get_cubes(exporter->instance, &cube_ids, &n_cube); - if (n_cube != exporter->history->n_cube) { + fieldstat_get_cubes(instance, &cube_ids, &n_cube); + if (n_cube != history->n_cube) { return true; } for (int i = 0; i < n_cube; i++) { - unsigned long cube_version = fieldstat_get_cube_version(exporter->instance, cube_ids[i]); - if (cube_version != exporter->history->cube_version[i]) { + unsigned long cube_version = fieldstat_get_cube_version(instance, cube_ids[i]); + if (cube_version != history->cube_version[i]) { return true; } } @@ -326,9 +329,9 @@ bool counter_history_check_if_need_to_update(const struct fieldstat_json_exporte return false; } -void counter_history_fill_version_info(struct counter_history *history, struct fieldstat_json_exporter *exporter) +void counter_history_fill_version_info(struct counter_history *history, struct fieldstat_json_exporter *exporter, const struct fieldstat *instance) { - unsigned long cur_cell_version = fieldstat_get_version(exporter->instance); + unsigned long cur_cell_version = fieldstat_get_version(instance); const char *cur_exporter_name = exporter->name ? exporter->name : DEFAULT_EXPORTER_NAME; const struct fieldstat_tag_list *cur_global_tag_list = exporter->global_tag_list; @@ -342,25 +345,25 @@ void counter_history_fill_version_info(struct counter_history *history, struct f int *cube_ids = NULL; int n_cube = 0; - fieldstat_get_cubes(exporter->instance, &cube_ids, &n_cube); + fieldstat_get_cubes(instance, &cube_ids, &n_cube); history->n_cube = n_cube; history->cube_version = malloc(sizeof(unsigned long) * n_cube); for (int i = 0; i < n_cube; i++) { - history->cube_version[i] = fieldstat_get_cube_version(exporter->instance, cube_ids[i]); + history->cube_version[i] = fieldstat_get_cube_version(instance, cube_ids[i]); } free(cube_ids); } -void fieldstat_json_exporter_update_history(struct fieldstat_json_exporter *exporter) +void fieldstat_json_exporter_update_history(struct fieldstat_json_exporter *exporter, const struct fieldstat *instance) { - if (counter_history_check_if_need_to_update(exporter) == false) { + if (counter_history_check_if_need_to_update(exporter, instance) == false) { return; } counter_history_free(exporter->history); exporter->history = counter_history_new(); - counter_history_fill_version_info(exporter->history, exporter); + counter_history_fill_version_info(exporter->history, exporter, instance); } void fieldstat_json_exporter_write_delta(struct fieldstat_json_exporter *exporter, struct cellwise_rec_for_export *tag_field_pair_arr, size_t arr_len) @@ -770,15 +773,13 @@ int add_object_to_json_array(char **buf, int *buf_len, int start, const char *st /* Output the fieldstat instance to json string array. User must free the output string. */ -void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, const struct timeval *timestamp, char ***output, size_t *output_size) +void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp, char ***output, size_t *output_size) { long long timestamp_ms = cal_ms_time(timestamp); if (exporter->history != NULL) { - fieldstat_json_exporter_update_history((struct fieldstat_json_exporter *)exporter); + fieldstat_json_exporter_update_history((struct fieldstat_json_exporter *)exporter, instance); } - const struct fieldstat *instance = exporter->instance; - size_t n_pair; struct cellwise_rec_for_export *tag_field_pair = read_tag_and_field(instance, &n_pair, exporter->history != NULL); if (tag_field_pair == NULL || n_pair == 0) { @@ -834,11 +835,11 @@ void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter * free(tag_field_pair); } -char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter, const struct timeval *timestamp) +char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp) { char **str_arr = NULL; size_t n_pair = 0; - fieldstat_json_exporter_export_array(exporter, timestamp, &str_arr, &n_pair); + fieldstat_json_exporter_export_array(exporter, instance, timestamp, &str_arr, &n_pair); if (str_arr == NULL || n_pair == 0) { return NULL; } @@ -860,10 +861,9 @@ char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *expor return cjson_str_arr; } -struct fieldstat_json_exporter *fieldstat_json_exporter_new(const struct fieldstat *instance) +struct fieldstat_json_exporter *fieldstat_json_exporter_new() { struct fieldstat_json_exporter *exporter = (struct fieldstat_json_exporter *)malloc(sizeof(struct fieldstat_json_exporter)); - exporter->instance = instance; exporter->name = NULL; exporter->global_tag_list = NULL; exporter->history = NULL; @@ -932,5 +932,4 @@ void fieldstat_json_exporter_enable_delta(struct fieldstat_json_exporter *export return; } exporter->history = counter_history_new(); - counter_history_fill_version_info(exporter->history, exporter); }
\ No newline at end of file diff --git a/test/test_empty_tags.cpp b/test/test_empty_tags.cpp index 8f5efb1..4f500bd 100644 --- a/test/test_empty_tags.cpp +++ b/test/test_empty_tags.cpp @@ -97,8 +97,8 @@ TEST(test_empty_tag, merge_topk) TEST(test_empty_tag, export) { struct fieldstat *instance = test_empty_my_init(); - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); // std::cout << "json_string: \n" << json_string << std::endl; cJSON *root_arr = cJSON_Parse(json_string); diff --git a/test/test_exporter_json.cpp b/test/test_exporter_json.cpp index cfa1190..44bbcdb 100644 --- a/test/test_exporter_json.cpp +++ b/test/test_exporter_json.cpp @@ -118,10 +118,10 @@ void fill_random_tag(Fieldstat_tag_list_wrapper *tags[], int tag_list_num) cJSON *test_exporter_extract_results_with_standard_global(const struct fieldstat *instance) { - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_set_global_tag(fieldstat_json_exporter, TEST_TAG_GLOBAL, 3); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); fieldstat_json_exporter_free(fieldstat_json_exporter); @@ -131,10 +131,10 @@ cJSON *test_exporter_extract_results_with_standard_global(const struct fieldstat cJSON *test_exporter_extract_results(const struct fieldstat *instance) { - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); // getchar(); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); // exit(0); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -403,9 +403,9 @@ TEST(export_test, enable_delta_and_export_twice_without_new_metric) fieldstat_counter_incrby(instance, cube_id, id_counter, &TEST_TAG_INT, 1, 1); // export test - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_enable_delta(fieldstat_json_exporter); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -432,7 +432,7 @@ TEST(export_test, enable_delta_and_export_twice_without_new_metric) struct timeval new_ts = TEST_TIMEVAL; new_ts.tv_sec += 1; - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &new_ts); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &new_ts); root_arr = cJSON_Parse(json_string); free(json_string); @@ -473,9 +473,9 @@ TEST(export_test, enable_delta_and_export_twice_with_new_metric_and_omit_histogr fieldstat_hist_record(instance, cube_id, id_histogram, &TEST_TAG_INT, 1, 123); // export test - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_enable_delta(fieldstat_json_exporter); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -508,7 +508,7 @@ TEST(export_test, enable_delta_and_export_twice_with_new_metric_and_omit_histogr fieldstat_counter_incrby(instance, cube_id, id_counter2, &TEST_TAG_INT, 1, 1); fieldstat_counter_incrby(instance, cube_id, id_counter, &TEST_TAG_INT, 1, 10); - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &new_ts); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &new_ts); root_arr = cJSON_Parse(json_string); free(json_string); @@ -553,9 +553,9 @@ TEST(export_test, enable_delta_and_export_three_times_skipping_cube_with_no_coun fieldstat_hist_record(instance, cube_id, id_histogram, &TEST_TAG_STRING, 1, 123); /* -------------------------- export test, 1st time ------------------------- */ - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_enable_delta(fieldstat_json_exporter); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); printf("%s\n", json_string); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -581,7 +581,7 @@ TEST(export_test, enable_delta_and_export_three_times_skipping_cube_with_no_coun cJSON_Delete(root_arr); /* -------------------------------- 2nd time -------------------------------- */ - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); root_arr = cJSON_Parse(json_string); free(json_string); root = cJSON_GetArrayItem(root_arr, 0); @@ -604,7 +604,7 @@ TEST(export_test, enable_delta_and_export_three_times_skipping_cube_with_no_coun // add some new recs fieldstat_counter_incrby(instance, cube_id, id_counter, &TEST_TAG_INT, 1, 10); fieldstat_hist_record(instance, cube_id, id_histogram, &TEST_TAG_STRING, 1, 1234); - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); root_arr = cJSON_Parse(json_string); free(json_string); root = cJSON_GetArrayItem(root_arr, 0); @@ -658,11 +658,11 @@ TEST(export_test, enable_delta_and_export_instance_with_many_cells_with_global_t fieldstat_counter_incrby(instance, cube_id2, id_counter1, &TEST_TAG_DOUBLE, 1, 22); // export test - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_set_global_tag(fieldstat_json_exporter, TEST_TAG_GLOBAL, 3); fieldstat_json_exporter_set_name(fieldstat_json_exporter, "test_instance"); fieldstat_json_exporter_enable_delta(fieldstat_json_exporter); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -698,7 +698,7 @@ TEST(export_test, enable_delta_and_export_instance_with_many_cells_with_global_t struct timeval new_ts = TEST_TIMEVAL; new_ts.tv_sec += 1; - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &new_ts); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &new_ts); root_arr = cJSON_Parse(json_string); free(json_string); @@ -738,9 +738,9 @@ void test_reset_one_round(std::function<void(struct fieldstat *, struct fieldsta fieldstat_counter_incrby(instance, cube_id, id_counter, &TEST_TAG_INT, 1, 11); // export test - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); fieldstat_json_exporter_enable_delta(fieldstat_json_exporter); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); cJSON *root_arr = cJSON_Parse(json_string); free(json_string); @@ -758,7 +758,7 @@ void test_reset_one_round(std::function<void(struct fieldstat *, struct fieldsta fieldstat_counter_set(instance, cube_id, id_counter, &TEST_TAG_INT, 1, 123); - json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); root_arr = cJSON_Parse(json_string); free(json_string); diff --git a/test/test_performance.cpp b/test/test_performance.cpp index fa93a8f..a5be5e8 100644 --- a/test/test_performance.cpp +++ b/test/test_performance.cpp @@ -293,10 +293,10 @@ TEST(test_performance, export_many_cells) } } - struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); + struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(); // getchar(); clock_t start = clock(); - char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, &TEST_TIMEVAL); + char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter, instance, &TEST_TIMEVAL); clock_t end = clock(); // exit(0); free(json_string); |
