summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-11-13 18:21:37 +0800
committerchenzizhan <[email protected]>2023-11-13 18:21:37 +0800
commitb4fa7f4413b0770646073ab8e67affd27fd53b2f (patch)
treea8ee4d6d001c79bce1e199bbcc31065848e6e622
parente88ca3ad38a64b9c1b7b0a614473582493d3c3aa (diff)
fieldstat_easy_output_array
-rw-r--r--include/fieldstat/fieldstat_easy.h1
-rw-r--r--src/fieldstat_easy.c27
2 files changed, 26 insertions, 2 deletions
diff --git a/include/fieldstat/fieldstat_easy.h b/include/fieldstat/fieldstat_easy.h
index 451f226..2770844 100644
--- a/include/fieldstat/fieldstat_easy.h
+++ b/include/fieldstat/fieldstat_easy.h
@@ -47,6 +47,7 @@ int fieldstat_easy_register_histogram(struct fieldstat_easy *fse, const char *na
* @param buff: output buffer. User should free it after use.
*/
void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff_len);
+void fieldstat_easy_output_array(struct fieldstat_easy *fse, char ***json_objects, size_t *n_object);
/*
* @brief let the value of counter metric of cell_id increase by `increment`.
* @param thread_id: thread id. Must be in [0, max_thread_num).
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c
index fdc643c..25c004e 100644
--- a/src/fieldstat_easy.c
+++ b/src/fieldstat_easy.c
@@ -229,14 +229,18 @@ int fieldstat_easy_register_histogram(struct fieldstat_easy *fse, const char *na
return ret;
}
-// output an json string for accumulated data
-void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff_len)
+struct timeval get_current_timestamp()
{
struct timeval timestamp;
struct timespec this_output_time;
clock_gettime(CLOCK_MONOTONIC, &this_output_time); // use the same method as fs_easy_output_thread
timestamp.tv_sec = this_output_time.tv_sec;
timestamp.tv_usec = this_output_time.tv_nsec / 1000;
+ return timestamp;
+}
+
+struct fieldstat *merge_all_instance(struct fieldstat_easy *fse)
+{
struct fieldstat *dst = fieldstat_new();
// collect all the data recorded since last passive output, if passive output happened. Otherwise, its the data since the program started.
for (int i = 0; i < fse->max_thread_num; i++) {
@@ -250,6 +254,15 @@ void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff
fieldstat_merge(dst, fse->accumulate);
pthread_spin_unlock(&fse->outputting_lock);
+ return dst;
+}
+
+// output an json string for accumulated data
+void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff_len)
+{
+ struct fieldstat *dst = merge_all_instance(fse);
+ struct timeval timestamp = get_current_timestamp();
+
*buff = fieldstat_json_exporter_export(fse->exporter, dst, &timestamp);
if (*buff == NULL) {
*buff = strdup("[]");
@@ -258,6 +271,16 @@ void fieldstat_easy_output(struct fieldstat_easy *fse, char **buff, size_t *buff
*buff_len = strlen(*buff);
}
+// output many json string for accumulated data(many objects vs array)
+void fieldstat_easy_output_array(struct fieldstat_easy *fse, char ***json_objects, size_t *n_object)
+{
+ struct timeval timestamp = get_current_timestamp();
+ struct fieldstat *dst = merge_all_instance(fse);
+
+ fieldstat_json_exporter_export_array(fse->exporter, dst, &timestamp, json_objects, n_object);
+ fieldstat_free(dst);
+}
+
int fieldstat_easy_counter_incrby(struct fieldstat_easy *fse, int thread_id, int metric_id, const struct fieldstat_tag *tags, size_t n_tag, long long increment)
{
if (thread_id < 0) {