summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2024-07-18 18:03:05 +0800
committerchenzizhan <[email protected]>2024-07-18 18:03:05 +0800
commit7f415b4385607dcc29babe80356c2c973c301097 (patch)
tree14045d840ca574bf9d3a5ff971d7142cf02279a4
parent5f25467142e9bf1dfae083edcd4dc29195105b1b (diff)
fieldstat easy output flat when called actively
-rw-r--r--include/fieldstat/fieldstat_easy.h10
-rw-r--r--include/fieldstat/fieldstat_exporter.h3
-rw-r--r--src/exporter/cjson_exporter.c30
-rw-r--r--src/fieldstat_easy.c11
-rw-r--r--test/test_easy_fs.cpp38
-rw-r--r--test/test_exporter_json.cpp4
6 files changed, 39 insertions, 57 deletions
diff --git a/include/fieldstat/fieldstat_easy.h b/include/fieldstat/fieldstat_easy.h
index 067b43f..97d7394 100644
--- a/include/fieldstat/fieldstat_easy.h
+++ b/include/fieldstat/fieldstat_easy.h
@@ -29,16 +29,6 @@ void fieldstat_easy_free(struct fieldstat_easy *fse);
int fieldstat_easy_enable_auto_output(struct fieldstat_easy *pthis, const char *output_path, int interval_second);
/*
- affect the output of fieldstat_easy_output and fieldstat_easy_output_array.
- 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.
- Outputting delta value is disabled by default.
- When the fieldstat instance is reset, the delta value will be reset. (next output will be the original value)
- Only affects the metrics of counter type.
- Outputting delta value is time-consuming. Be cautious when enabling it.
-*/
-void fieldstat_easy_enable_delta_in_active_output(struct fieldstat_easy *fse);
-
-/*
* @brief add a metric to the cube of cube_id. One metric may be associated with different cells.
* @param metric_name: name of the metric. Cannot be NULL. Must be unique.
* @return metric id>=0 if success. If failed, return FS_ERR_NULL_HANDLER, FS_ERR_INVALID_KEY(when metric_name is not unique in this cube).
diff --git a/include/fieldstat/fieldstat_exporter.h b/include/fieldstat/fieldstat_exporter.h
index d314ba0..2df08ab 100644
--- a/include/fieldstat/fieldstat_exporter.h
+++ b/include/fieldstat/fieldstat_exporter.h
@@ -69,7 +69,8 @@ do not support fieldstat_json_exporter_enable_delta.
if a cell has no metric record, the cell will not be exported. (The same as fieldstat_json_exporter_export_array)
*/
-void fieldstat_json_exporter_export_flat(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp, char ***output, size_t *output_size);
+void fieldstat_json_exporter_export_flat_array(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp, char ***output, size_t *output_size);
+char *fieldstat_json_exporter_export_flat(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp);
/*
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.
Outputting delta value is disabled by default.
diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c
index 6a52c6a..b2a909d 100644
--- a/src/exporter/cjson_exporter.c
+++ b/src/exporter/cjson_exporter.c
@@ -876,7 +876,7 @@ char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *expor
int buf_len = 4096;
char *cjson_str_arr = (char *)malloc(buf_len);
- // cjson is so slow, so we construct the json array manually, the reason why we don't use json_writer is simply because I wrote this before implementing json_writer
+ // 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]);
@@ -1040,7 +1040,7 @@ void fieldstat_json_exporter_enable_delta(struct fieldstat_json_exporter *export
exporter->history = counter_history_new();
}
-void fieldstat_json_exporter_export_flat(const struct fieldstat_json_exporter *exporter, const struct fieldstat *instance, const struct timeval *timestamp, char ***output, size_t *output_size) {
+void fieldstat_json_exporter_export_flat_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);
size_t n_pair;
@@ -1090,4 +1090,30 @@ void fieldstat_json_exporter_export_flat(const struct fieldstat_json_exporter *e
}
free(tag_field_pair);
+}
+
+char *fieldstat_json_exporter_export_flat(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_flat_array(exporter, instance, timestamp, &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;
} \ No newline at end of file
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c
index 46133f4..f32dbd9 100644
--- a/src/fieldstat_easy.c
+++ b/src/fieldstat_easy.c
@@ -275,7 +275,7 @@ void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff
struct fieldstat *dest = merge_all_instance(fse);
struct timeval timestamp = get_current_timestamp();
- *buff = fieldstat_json_exporter_export(fse->exporter, dest, &timestamp);
+ *buff = fieldstat_json_exporter_export_flat(fse->exporter, dest, &timestamp);
if (*buff == NULL) {
*buff = strdup("[]");
}
@@ -289,7 +289,7 @@ void fieldstat_easy_output_array(struct fieldstat_easy *fse, char ***json_object
struct timeval timestamp = get_current_timestamp();
struct fieldstat *dest = merge_all_instance(fse);
- fieldstat_json_exporter_export_array(fse->exporter, dest, &timestamp, json_objects, n_object);
+ fieldstat_json_exporter_export_flat_array(fse->exporter, dest, &timestamp, json_objects, n_object);
fieldstat_free(dest);
}
@@ -307,7 +307,7 @@ int fieldstat_easy_output_array_and_reset(struct fieldstat_easy *fse, char ***js
}
struct timeval timestamp = get_current_timestamp();
- fieldstat_json_exporter_export_array(fse->exporter, dest, &timestamp, json_objects, n_object);
+ fieldstat_json_exporter_export_flat_array(fse->exporter, dest, &timestamp, json_objects, n_object);
fieldstat_free(dest);
return 0;
@@ -360,8 +360,3 @@ int fieldstat_easy_histogram_record(struct fieldstat_easy *fse, int thread_id, i
return ret;
}
-
-void fieldstat_easy_enable_delta_in_active_output(struct fieldstat_easy *fse)
-{
- fieldstat_json_exporter_enable_delta(fse->exporter);
-} \ No newline at end of file
diff --git a/test/test_easy_fs.cpp b/test/test_easy_fs.cpp
index ded719f..f04612e 100644
--- a/test/test_easy_fs.cpp
+++ b/test/test_easy_fs.cpp
@@ -28,39 +28,10 @@ TEST(test_easy_fieldstat, output_to_buff)
fieldstat_easy_output(fse, &buff, &buff_len);
cJSON *root = cJSON_Parse(buff);
cJSON *cell = cJSON_GetArrayItem(root, 0);
- cJSON *metric = cJSON_GetObjectItem(cell, "fields");
- long long value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
+ long long value = cJSON_GetObjectItem(cell, "metric counter")->valueint;
EXPECT_EQ(value, 1);
- cJSON *tags = cJSON_GetObjectItem(cell, "tags");
- EXPECT_EQ(cJSON_GetObjectItem(tags, TEST_TAG_INT.key)->valueint, TEST_TAG_INT.value_longlong);
- EXPECT_STREQ(cJSON_GetObjectItem(tags, TEST_TAG_STRING.key)->valuestring, TEST_TAG_STRING.value_str);
-
- cJSON_Delete(root);
- free(buff);
- fieldstat_easy_free(fse);
-}
-
-TEST(test_easy_fieldstat, output_to_buff_with_delta)
-{
- struct fieldstat_easy *fse = fieldstat_easy_new(1, NULL, NULL, 0);
- fieldstat_easy_enable_delta_in_active_output(fse);
- fieldstat_easy_register_counter(fse, "metric counter");
- fieldstat_easy_counter_incrby(fse, 0, 0, NULL, 0, 1);
- char *buff = NULL;
- size_t buff_len = 0;
- fieldstat_easy_output(fse, &buff, &buff_len);
- free(buff);
- fieldstat_easy_counter_incrby(fse, 0, 0, NULL, 0, 1);
- fieldstat_easy_output(fse, &buff, &buff_len);
-
- cJSON *root = cJSON_Parse(buff);
- cJSON *cell = cJSON_GetArrayItem(root, 0);
- cJSON *metric = cJSON_GetObjectItem(cell, "fields");
- long long value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
- EXPECT_EQ(value, 2);
- cJSON *metric_delta = cJSON_GetObjectItem(cell, "fields_delta");
- long long value_delta = cJSON_GetObjectItem(metric_delta, "metric counter")->valueint;
- EXPECT_EQ(value_delta, 1);
+ EXPECT_EQ(cJSON_GetObjectItem(cell, TEST_TAG_INT.key)->valueint, TEST_TAG_INT.value_longlong);
+ EXPECT_STREQ(cJSON_GetObjectItem(cell, TEST_TAG_STRING.key)->valuestring, TEST_TAG_STRING.value_str);
cJSON_Delete(root);
free(buff);
@@ -250,8 +221,7 @@ TEST(test_easy_fieldstat, reset)
long long get_value(const char *input, const char *key) {
cJSON *root = cJSON_Parse(input);
- cJSON *metric = cJSON_GetObjectItem(root, "fields");
- long long value = cJSON_GetObjectItem(metric, key)->valueint;
+ long long value = cJSON_GetObjectItem(root, key)->valueint;
cJSON_Delete(root);
return value;
}
diff --git a/test/test_exporter_json.cpp b/test/test_exporter_json.cpp
index ebf6750..1c0985d 100644
--- a/test/test_exporter_json.cpp
+++ b/test/test_exporter_json.cpp
@@ -1024,7 +1024,7 @@ TEST(export_test, export_flat_null_with_only_global_tag) {
char **out_array = NULL;
size_t out_array_len = 0;
- fieldstat_json_exporter_export_flat(fieldstat_json_exporter, instance, &TEST_TIMEVAL, &out_array, &out_array_len);
+ fieldstat_json_exporter_export_flat_array(fieldstat_json_exporter, instance, &TEST_TIMEVAL, &out_array, &out_array_len);
EXPECT_EQ(out_array_len, 0);
EXPECT_EQ(out_array, nullptr);
@@ -1047,7 +1047,7 @@ TEST(export_test, export_flat_many_tags_many_cell) {
char **out_array = NULL;
size_t out_array_len = 0;
- fieldstat_json_exporter_export_flat(fieldstat_json_exporter, instance, &TEST_TIMEVAL, &out_array, &out_array_len);
+ fieldstat_json_exporter_export_flat_array(fieldstat_json_exporter, instance, &TEST_TIMEVAL, &out_array, &out_array_len);
EXPECT_EQ(out_array_len, 2);
cJSON *root = cJSON_Parse(out_array[0]);