diff options
| -rwxr-xr-x | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | inc/fieldstat.h | 25 | ||||
| -rw-r--r-- | src/fieldstat_dynamic.cpp | 29 | ||||
| -rw-r--r-- | src/fieldstat_internal.h | 1 | ||||
| -rw-r--r-- | src/line_protocol_output.cpp | 127 | ||||
| -rw-r--r-- | test/src/gtest_dynamic_fieldstat.cpp | 51 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat.cpp | 1 |
7 files changed, 141 insertions, 95 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8caa3cf..0b0ebf6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 2.8) -set(lib_name fieldstat) +set(lib_name fieldstat3) project (${lib_name}) diff --git a/inc/fieldstat.h b/inc/fieldstat.h index 4c64ea8..5312549 100644 --- a/inc/fieldstat.h +++ b/inc/fieldstat.h @@ -335,6 +335,31 @@ void fieldstat_instance_free(struct fieldstat_instance *instance); * @param instance The fieldstat dynamic instance need to free. */ void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance); +/** + * fieldstat dynamic instance table metric value get operate. + * @param instance The fieldstat dynamic instance. + * @param table_id The table id. + * @param column_id The column id. + * @param row_name The row name of table. + * @param value The set value. + * @param tags The tag array. + * @param n_tag Size of tags[] + * @param thread_id The thread id of the call. + * @return long long value. + */ +long long fieldstat_dynamic_table_metric_value_get(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id); +/** + * fieldstat dynamic instance metric value set operate. metirc type in[gauge, counter] + * @param instance The fieldstat dynamic instance. + * @param field_name The metric field name. + * @param value The set value. + * @param tags The tag array. + * @param n_tag Size of tags[] + * @param thread_id The thread id of the call. + * @return long long value. + */ +long long fieldstat_dynamic_metric_value_get(struct fieldstat_dynamic_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id); + #ifdef __cplusplus } #endif
\ No newline at end of file diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp index 162e91a..5a0609e 100644 --- a/src/fieldstat_dynamic.cpp +++ b/src/fieldstat_dynamic.cpp @@ -503,3 +503,32 @@ int fieldstat_dynamic_table_metric_value_decrby(struct fieldstat_dynamic_instanc return ret; } + +static long long dynamic_metric_value_read(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *field_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id) +{ + long long value = 0; + struct metric * metric = NULL; + metric = read_dynamic_metric(instance, table_id, column_id, field_name, tags, n_tags, thread_id); + if(metric == NULL) + { + return 0; + } + metric->field_type == FIELD_TYPE_GAUGE + ?value = get_metric_unit_val(metric, FS_CALC_CURRENT, 0) + :value = get_metric_unit_val(metric, FS_CALC_SPEED, 0); + return value; +} + +long long fieldstat_dynamic_metric_value_get(struct fieldstat_dynamic_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id) +{ + long long value = 0; + value = dynamic_metric_value_read(instance, -1, -1, field_name, tags, n_tags, thread_id); + return value; +} + +long long fieldstat_dynamic_table_metric_value_get(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, const struct fieldstat_tag tags[], size_t n_tags, int thread_id) +{ + long long value = 0; + value = dynamic_metric_value_read(instance, table_id, column_id, row_name, tags, n_tags, thread_id); + return value; +} diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h index babaf4f..58bd58c 100644 --- a/src/fieldstat_internal.h +++ b/src/fieldstat_internal.h @@ -26,7 +26,6 @@ #include "uthash.h" #define INIT_STAT_FIELD_NUM 1024 -#define MAX_STAT_COLUMN_NUM 64 #define MAX_PATH_LEN 256 #define UDP_PAYLOAD_SIZE 1460 diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp index cfea9f7..bead3ca 100644 --- a/src/line_protocol_output.cpp +++ b/src/line_protocol_output.cpp @@ -93,25 +93,6 @@ static long long read_single_metric_value(struct metric *metric) return value; } -static int read_table_row_value(struct fieldstat_instance *instance, struct table_line *row, int n_column, long long *out_row_value) -{ - int i = 0; - struct metric *metric = NULL; - - if(row == NULL) - { - return -1; - } - - for(i = 0; i < n_column; i++) - { - metric = get_metric(instance, row->metric_id_belong_to_line[i]); - out_row_value[i] = read_single_metric_value(metric); - } - return 0; -} - - static int is_send_table_row(long long *row_value, int n_column) { int i = 0; @@ -187,7 +168,27 @@ static int build_single_metric_line_buf(char *instance_name, struct metric *metr return used_len; } -static int build_table_row_line_buf(struct fieldstat_instance *instance, struct table_metric *table, struct table_line *row, char *line_buf, int line_buf_size) + +static int read_table_row_value(struct metric **row_metric, int n_column, long long *out_row_value) +{ + int i = 0; + struct metric *metric = NULL; + + if(row_metric == NULL || n_column < 1) + { + return -1; + } + + for(i = 0; i < n_column; i++) + { + metric = row_metric[i]; + out_row_value[i] = read_single_metric_value(metric); + } + return 0; + +} + +static int build_table_row_line_buf(char *instance_name, struct table_metric *table, struct metric **row_metric, char *line_buf, int line_buf_size) { int used_len = 0; struct metric *metric = NULL; @@ -196,11 +197,9 @@ static int build_table_row_line_buf(struct fieldstat_instance *instance, struct { return 0; } + long long row_value[table->column_cnt]; - long long row_value[table->column_cnt] = {0}; - - - if(-1 == read_table_row_value(instance, row, table->column_cnt, row_value)) + if(-1 == read_table_row_value(row_metric, table->column_cnt, row_value)) { return 0; } @@ -210,11 +209,11 @@ static int build_table_row_line_buf(struct fieldstat_instance *instance, struct return 0; } - metric = get_metric(instance, row->metric_id_belong_to_line[0]); + metric = row_metric[0]; used_len += add_measurement(metric->field_name, line_buf, line_buf_size); - used_len += add_default_tag_set(instance->name, table->name, line_buf + used_len, line_buf_size - used_len); + used_len += add_default_tag_set(instance_name, table->name, line_buf + used_len, line_buf_size - used_len); used_len += add_user_tag_set(metric, line_buf + used_len, line_buf_size - used_len); @@ -253,35 +252,15 @@ static void output_line_protocol_single_metric(struct fieldstat_instance *instan return; } - -static int read_dynamic_table_row(struct metric **row_metric, int n_column, long long *out_row_value) -{ - int i = 0; - struct metric *metric = NULL; - - if(row_metric == NULL || n_column < 1) - { - return -1; - } - - for(i = 0; i < n_column; i++) - { - metric = row_metric[i]; - out_row_value[i] = read_single_metric_value(metric); - } - return 0; - -} - - static void output_line_protocol_table_row(struct fieldstat_instance *instance, int n_cur_table, int n_cur_table_row[]) { - int i = 0, j = 0; + int i = 0, j = 0, k = 0; int used_len = 0; char line_buf[UDP_PAYLOAD_SIZE]; struct table_metric *table = NULL; struct table_line *row = NULL; + struct metric *row_metrics[TABLE_COLUMN_SIZE]; for(i = 0; i < n_cur_table; i++) { @@ -295,7 +274,14 @@ static void output_line_protocol_table_row(struct fieldstat_instance *instance, continue; } memset(line_buf, 0, sizeof(line_buf)); - used_len = build_table_row_line_buf(instance, table, row, line_buf, sizeof(line_buf)); + + for(k = 0; k < table->column_cnt; k++) + { + row_metrics[k] = get_metric(instance, row->metric_id_belong_to_line[k]); + } + + used_len = build_table_row_line_buf(instance->name, table, row_metrics, line_buf, sizeof(line_buf)); + send_line_buf(&instance->line_protocol_output, line_buf, used_len); } } @@ -323,47 +309,6 @@ int line_protocol_output(struct fieldstat_instance *instance) return 0; } - -static int build_dynamic_table_row_line_buf(struct fieldstat_dynamic_instance *instance, struct table_metric *table, struct metric **row_metric, char *line_buf, int line_buf_size) -{ - int used_len = 0; - struct metric *metric = NULL; - - if(table->column_cnt <= 0) - { - return 0; - } - long long row_value[table->column_cnt] = {0}; - - if(-1 == read_dynamic_table_row(row_metric, table->column_cnt, row_value)) - { - return 0; - } - - if(1 != is_send_table_row(row_value, table->column_cnt)) - { - return 0; - } - - metric = row_metric[0]; - - used_len += add_measurement(metric->field_name, line_buf, line_buf_size); - - used_len += add_default_tag_set(instance->name, table->name, line_buf + used_len, line_buf_size - used_len); - - used_len += add_user_tag_set(metric, line_buf + used_len, line_buf_size - used_len); - - used_len += snprintf(line_buf + used_len, line_buf_size - used_len, " "); - - used_len += add_table_row_field_set(table->column_name, row_value, table->column_cnt, line_buf + used_len, line_buf_size - used_len); - - used_len += snprintf(line_buf + used_len, line_buf_size - used_len, "\n"); - - return used_len; -} - - - int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *instance) { struct dynamic_metric **head = NULL; @@ -385,7 +330,7 @@ int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *insta memset(line_buf, 0, sizeof(line_buf)); if(metric->table) { - used_len = build_dynamic_table_row_line_buf(instance, metric->table, metrics, line_buf, sizeof(line_buf)); + used_len = build_table_row_line_buf(instance->name, metric->table, metrics, line_buf, sizeof(line_buf)); } else { diff --git a/test/src/gtest_dynamic_fieldstat.cpp b/test/src/gtest_dynamic_fieldstat.cpp index 669a980..c58d4f3 100644 --- a/test/src/gtest_dynamic_fieldstat.cpp +++ b/test/src/gtest_dynamic_fieldstat.cpp @@ -1034,7 +1034,6 @@ void parse_telegraf_cjson_output_not_equal(const char *compare) FILE *fp; char line[1024] = {0}; char *cjson_metric_str = NULL; - int ret = 0; fp = fopen(telegraf_output_file, "r"); EXPECT_NE(nullptr, fp); @@ -2579,6 +2578,56 @@ TEST(FeildStatDynamicAPI, FieldStatDynamicInstanceMultiSetMultiMetric) } +TEST(FeildStatDynamicAPI, FieldStatDynamicMetricValueGet) +{ + int n_thread = 48; + int ret = 0; + long long value = 0; + struct fieldstat_dynamic_instance *instance = NULL; + + instance = fieldstat_dynamic_instance_new("firewall", n_thread); + EXPECT_NE(nullptr, instance); + + ret = fieldstat_dynamic_metric_value_incrby(instance, FIELD_TYPE_GAUGE, "Active_sessions", 1000, NULL, 0, 0); + EXPECT_EQ(0, ret); + value = fieldstat_dynamic_metric_value_get(instance, "Active_sessions", NULL, 0, 0); + EXPECT_EQ(1000, value); + fieldstat_dynamic_instance_free(instance); +} + + +TEST(FeildStatDynamicAPI, FieldStatDynamicTableMetricValueGet) +{ + int n_thread = 48; + int ret = 0; + int table_id = -1; + long long value = 0; + + const char *column_name[] = {"packages", "bytes"}; + enum field_type column_type[] = {FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER}; + unsigned int out_column_ids[2]; + + struct fieldstat_dynamic_instance *instance = NULL; + + instance = fieldstat_dynamic_instance_new("firewall", n_thread); + EXPECT_NE(nullptr, instance); + + table_id = fieldstat_register_dynamic_table(instance, "shaping", column_name, column_type, sizeof(column_name)/sizeof(column_name[0]), out_column_ids); + EXPECT_EQ(0, table_id); + + ret = fieldstat_dynamic_table_metric_value_incrby(instance, table_id, out_column_ids[0], "security_rule_hits", 1000, NULL, 0, 0); + EXPECT_EQ(0, ret); + ret = fieldstat_dynamic_table_metric_value_incrby(instance, table_id, out_column_ids[1], "security_rule_hits", 2000, NULL, 0, 0); + EXPECT_EQ(0, ret); + + value = fieldstat_dynamic_table_metric_value_get(instance, table_id, out_column_ids[0], "security_rule_hits", NULL, 0, 0); + EXPECT_EQ(1000, value); + + value = fieldstat_dynamic_table_metric_value_get(instance, table_id, out_column_ids[1], "security_rule_hits", NULL, 0, 0); + EXPECT_EQ(2000, value); + + fieldstat_dynamic_instance_free(instance); +} int main(int argc, char *argv[]) { diff --git a/test/src/gtest_fieldstat.cpp b/test/src/gtest_fieldstat.cpp index 1435391..609853e 100644 --- a/test/src/gtest_fieldstat.cpp +++ b/test/src/gtest_fieldstat.cpp @@ -252,7 +252,6 @@ TEST(FeildStatAPI, FieldStatLocalOutputFormatJson) TEST(FeildStatAPI, FieldStatLineProtocolOutputTableMetric) { - int metric_id = -1; int ret = 0; int n_loops = 1; struct fieldstat_instance * instance = NULL; |
