diff options
| -rw-r--r-- | include/fieldstat/fieldstat_exporter.h | 11 | ||||
| -rw-r--r-- | src/exporter/cjson_exporter.c | 51 |
2 files changed, 40 insertions, 22 deletions
diff --git a/include/fieldstat/fieldstat_exporter.h b/include/fieldstat/fieldstat_exporter.h index d18d9b0..c1aa994 100644 --- a/include/fieldstat/fieldstat_exporter.h +++ b/include/fieldstat/fieldstat_exporter.h @@ -78,13 +78,10 @@ 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); -void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, char **output, size_t *output_size); -// for (size_t i = 0; i < output_size; i++) -// { -// free(output[i]); -// } -// free(output); -// strlen(output[i]); +/* + 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, char ***output, size_t *output_size); #ifdef __cplusplus } diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c index 520cb98..f7f7d77 100644 --- a/src/exporter/cjson_exporter.c +++ b/src/exporter/cjson_exporter.c @@ -460,27 +460,23 @@ 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. */ -char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter) +void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, char ***output, size_t *output_size) { const struct fieldstat *instance = exporter->instance; size_t n_pair; struct cjson_tag_field_pair *tag_field_pair = read_tag_and_field(instance, &n_pair); - if (tag_field_pair == NULL) { - return NULL; - } - if (n_pair == 0) { - free(tag_field_pair); - return NULL; + if (tag_field_pair == NULL || n_pair == 0) { + free(tag_field_pair); // tag_field_pair is not NULL when there are registered metrics but no valid cells + *output = NULL; + *output_size = 0; + return; } fieldstat_json_exporter_write_global_tags(exporter, tag_field_pair, n_pair); - int buf_len = 4096; - char *cjson_str_arr = (char *)malloc(buf_len); - // cjson is so slow, so we construct the json array manually - int used_len = add_object_to_json_array_start(cjson_str_arr, buf_len); - cJSON *cjson_root; + char **cjson_str_arr = (char **)malloc(sizeof(char *) * n_pair); + cJSON *cjson_root; for (int i = 0; i < n_pair; i++) { struct cjson_tag_field_pair *current = &tag_field_pair[i]; cjson_root = cJSON_CreateObject(); @@ -492,12 +488,37 @@ char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *expor char *cjson_str = cJSON_PrintUnformatted(cjson_root); cJSON_Delete(cjson_root); - used_len = add_object_to_json_array(&cjson_str_arr, &buf_len, used_len, cjson_str); - free(cjson_str); + cjson_str_arr[i] = cjson_str; + // user are responsible to free the cjson_str } - add_object_to_json_array_end(&cjson_str_arr, buf_len, used_len); + *output = cjson_str_arr; + *output_size = n_pair; free(tag_field_pair); // cjson object will be freed with cjson root +} + +char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter) +{ + char **str_arr = NULL; + size_t n_pair = 0; + fieldstat_json_exporter_export_array(exporter, &str_arr, &n_pair); + if (str_arr == NULL || n_pair == 0) { + return NULL; + } + + int buf_len = 4096; + char *cjson_str_arr = (char *)malloc(buf_len); + // cjson is so slow, so we construct the json array manually + int used_len = add_object_to_json_array_start(cjson_str_arr, buf_len); + for (int i = 0; i < n_pair; i++) { + used_len = add_object_to_json_array(&cjson_str_arr, &buf_len, used_len, str_arr[i]); + } + add_object_to_json_array_end(&cjson_str_arr, buf_len, used_len); + + for (int i = 0; i < n_pair; i++) { + free(str_arr[i]); + } + free(str_arr); return cjson_str_arr; } |
