From bab8c29dc98dfa4515fffa077e0f234b541560f3 Mon Sep 17 00:00:00 2001 From: chenzizhan Date: Tue, 16 Apr 2024 15:40:09 +0800 Subject: can enable delta in fse active output --- include/fieldstat/fieldstat_easy.h | 11 +++++++++++ src/fieldstat_easy.c | 5 +++++ test/test_easy_fs.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/fieldstat/fieldstat_easy.h b/include/fieldstat/fieldstat_easy.h index 3cb9429..7403c3f 100644 --- a/include/fieldstat/fieldstat_easy.h +++ b/include/fieldstat/fieldstat_easy.h @@ -27,6 +27,17 @@ void fieldstat_easy_free(struct fieldstat_easy *fse); * @return: 0 if success, -1 if failed to open file. -2 if the output is already enabled. */ 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. diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c index 1b56688..b8909a0 100644 --- a/src/fieldstat_easy.c +++ b/src/fieldstat_easy.c @@ -361,3 +361,8 @@ 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 44dbed9..0595593 100644 --- a/test/test_easy_fs.cpp +++ b/test/test_easy_fs.cpp @@ -40,6 +40,33 @@ TEST(test_easy_fieldstat, output_to_buff) fieldstat_easy_free(fse); } +TEST(test_easy_fieldstat, output_to_buff_with_delta) +{ + struct fieldstat_easy *fse = fieldstat_easy_new(10, NULL, &TEST_TAG_STRING, 1); + fieldstat_easy_enable_delta_in_active_output(fse); + fieldstat_easy_register_counter(fse, "metric counter"); + fieldstat_easy_counter_incrby(fse, 0, 0, &TEST_TAG_INT, 1, 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, &TEST_TAG_INT, 1, 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); + + cJSON_Delete(root); + free(buff); + fieldstat_easy_free(fse); +} + cJSON *read_file() { std::ifstream ifs(FILENAME); -- cgit v1.2.3