diff options
| author | fumingwei <[email protected]> | 2023-03-15 14:18:08 +0800 |
|---|---|---|
| committer | fumingwei <[email protected]> | 2023-03-15 20:57:48 +0800 |
| commit | 666234f661f5426630aa07554a67a47656bde656 (patch) | |
| tree | f52f4293c849c75841f86881b865ec1a1f1e86a9 | |
| parent | c7fbb5383dc5ebf7dd3a9e8f5b185c67d9a32768 (diff) | |
feature:review后修改
| -rw-r--r-- | inc/fieldstat.h | 151 | ||||
| -rw-r--r-- | src/fieldstat.cpp | 167 | ||||
| -rw-r--r-- | src/fieldstat_internal.h | 21 | ||||
| -rw-r--r-- | src/file_output.cpp | 18 | ||||
| -rw-r--r-- | src/line_protocol_output.cpp | 4 | ||||
| -rw-r--r-- | src/prometheus_output.cpp | 18 | ||||
| -rw-r--r-- | test/fieldstat_test.cpp | 105 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat.cpp | 101 |
8 files changed, 357 insertions, 228 deletions
diff --git a/inc/fieldstat.h b/inc/fieldstat.h index 388d1ca..7a9d8a4 100644 --- a/inc/fieldstat.h +++ b/inc/fieldstat.h @@ -16,50 +16,60 @@ enum field_type }; struct fieldstat_instance; -struct metric_id_list -{ - int count; - int id[TABLE_COLUMN_MAX_SIZE]; // define marco +struct fieldstat_tag{ + const char *key; + int value_type; // 0 int, 1 double, 2 str + union{ + long long value_int; + double value_double; + const char *value_str; + }; }; /** * Create fieldstat instance. - * @param name The instance name. - * @return Instance ptr. NULL is failed, Not NULL is successd. + * The operation should be executed in single-threaded fashion. + * @param name The instance name. + * @return Instance ptr. NULL failed, Not NULL successd. */ -struct fieldstat_instance * fieldstat_instance_create(const char *name); +struct fieldstat_instance * fieldstat_instance_new(const char *name); /** - * Create prometheus endpoint. - * @param listen_port The endpoint listen port. - * @param url The endpoint url. url is "/metrics" when url is NULL. + * Create prometheus endpoint. + * The operation should be executed in single-threaded fashion. + * @param listen_port The endpoint listen port. i.e., 80,8080 + * @param url_path The endpoint url path. url path is "/metrics" when url path is NULL. * @return -1 is failed. 0 is success. */ -int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, const char *url); +int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, const char *url_path); /** * Enable fieldstat instance output prometheus - * @param instance The Fieldstat instance. + * The operation should be executed in single-threaded fashion. + * @param instance The fieldstat instance. * @return -1 is failed. 0 is success. */ -int fieldstat_set_prometheus_output(struct fieldstat_instance *instance); +int fieldstat_enable_prometheus_output(struct fieldstat_instance *instance); /** * Enable output metric to statsd server. - * @param instance The Fieldstat instance. + * The operation should be executed in single-threaded fashion. + * @param instance The fieldstat instance. * @param ip Statsd server ip. - * @param port Statsd server port. + * @param port Statsd server port. i.e., 80,8080 * @return -1 is failed. 0 is success. */ int fieldstat_set_statsd_server(struct fieldstat_instance *instance, const char *ip, unsigned short port); /** * Enable output metric to line protocol server. - * @param instance The Fieldstat instance. + * The operation should be executed in single-threaded fashion. + * @param instance The fieldstat instance. * @param ip Line protocol server ip. - * @param port Line protocol server port. + * @param port Line protocol server port. i.e., 80,8080 * @return -1 is failed. 0 is success. */ int fieldstat_set_line_protocol_server(struct fieldstat_instance *instance, const char *ip, unsigned short port); /** * Enable output metric to file. - * @param instance The Fieldstat instance. + * The operation should be executed in single-threaded fashion. + * @param instance The fieldstat instance. * @param filename The output target filename. * @param format The output format. value in {"json","default"} * @return -1 is failed. 0 is success. @@ -67,110 +77,125 @@ int fieldstat_set_line_protocol_server(struct fieldstat_instance *instance, cons int fieldstat_set_local_output(struct fieldstat_instance *instance, const char *filename, const char *format); /** * Disable the background thread. - * @param instance The Fieldstat instance. + * The operation should be executed in single-threaded fashion. + * @param instance The fieldstat instance. * @return -1 is failed. 0 is success. */ -int fieldstat_background_thread_disable(struct fieldstat_instance *instance); +int fieldstat_disable_background_thread(struct fieldstat_instance *instance); /** - * Set the output interval in seconds - * @param seconds In seconds. + * Set the output interval in milliseconds + * The operation should be executed in single-threaded fashion. + * @param seconds In milliseconds. * @return -1 is failed. 0 is success. */ -int fieldstat_set_output_interval(struct fieldstat_instance *instance, int seconds); +int fieldstat_set_output_interval(struct fieldstat_instance *instance, int milliseconds); /** * Register counter and gauge type metrics * @param instance The fieldstat instance. - * @param type Counter, gauge. + * @param type counter, gauge. * @param field_name Metric field name. - * @param tag_key Tag key array - * @param tag_value Tag key array - * @param n_tag size of tag_key[] and tag_value[] + * @param tags The tag array. + * @param n_tag Size of tags[] * @return metric id. -1 is failed. >=0 is success. */ -int fieldstat_register(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag); +int fieldstat_register(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag); /** * Register table. Max table num is 64. Max table columns is 64. * @param instance The fieldstat instance. - * @param name The table name. + * @param table_name The table name. * @param column_name column name array. * @param column_type column type array. Type in [counter, gauge] * @param n_column size of column_type[] and column_name[] - * @return metric id. -1 is failed. >=0 is success. + * @return table id. -1 is failed. >=0 is success. */ -int fieldstat_register_table(struct fieldstat_instance *instance, const char *name, const char *column_name[], enum field_type column_type[], size_t n_column); +int fieldstat_register_table(struct fieldstat_instance *instance, const char *table_name, const char *column_name[], enum field_type column_type[], size_t n_column); /** - * Register table metric + * Register table row * @param instance The fieldstat instance. * @param table_id The table id by fieldstat_register_table create. - * @param line_name The table line name. - * @param tag_key Tag key array - * @param tag_value Tag key array - * @param n_tag size of tag_key[] and tag_value[] - * @return metric id. -1 is failed. >=0 is success. + * @param row_name The table row name. + * @param tags The tag array. + * @param n_tag Size of tags[] + * @param output_metric_ids Output metric ids + * @param output_metric_ids_cnt Output metric id count + * @return -1 is failed. 0 is success. */ -struct metric_id_list fieldstat_register_table_metrics(struct fieldstat_instance * instance, int table_id, const char *line_name, const char *tag_key[], const char *tag_value[],size_t n_tag); +int fieldstat_register_table_row(struct fieldstat_instance *instance, int table_id, const char *row_name, const struct fieldstat_tag tags[],size_t n_tag, int output_metric_ids[], int *output_metric_ids_cnt); /** - * increment filed value. + * Increment fieldstat metric value by metric_id. * @param instance The fieldstat instance. - * @param field_id The field id. - * @param increment the value to increment + * @param metric_id The metric id. + * @param increment The value to increment. * @return -1 is failed. 0 is success. */ -int fieldstat_value_incrby(struct fieldstat_instance *instance, int field_id, long long increment); +int fieldstat_value_incrby(struct fieldstat_instance *instance, int metric_id, long long increment); /** - * Set filed value. + * Set fieldstat metric value by metric id. * @param instance The fieldstat instance. - * @param field_id The field id. - * @param increment the value to set + * @param metric_id The metric id. + * @param value The value to set. * @return -1 is failed. 0 is success. */ -int fieldstat_value_set(struct fieldstat_instance *instance, int field_id, long long retain); +int fieldstat_value_set(struct fieldstat_instance *instance, int metric_id, long long value); /** - * Decrment filed value. + * Decrement fieldstat metric value. * @param instance The fieldstat instance. - * @param field_id The field id. - * @param increment the value to decrment + * @param metric_id The metric id. + * @param decrement The value to decrement. * @return -1 is failed. 0 is success. */ -int fieldstat_value_decrby(struct fieldstat_instance *instance, int field_id, long long decrment); +int fieldstat_value_decrby(struct fieldstat_instance *instance, int metric_id, long long decrment); /** * Make the fieldstat instance affect. * @param instance The fieldstat instance. */ void fieldstat_instance_start(struct fieldstat_instance *instance); /** - * passive output + * Passive output * @param instance The fieldstat instance. */ void fieldstat_passive_output(struct fieldstat_instance *instance); - /** * Register histogram metric * @param instance fieldstat instance * @param field_name field name - * @param tag_key the array of tag key - * @param tag_value the array of tag value - * @param n_tag the number of tags - * @param quantiles the quantiles of summary output + * @param tags The tag array + * @param n_tag Size of tags + * @param upper_inclusive_bounds The upper inclusive bound of histogram + * @param lowest_trackable_value The smallest possible value to be put into the + * histogram. + * @param highest_trackable_value The largest possible value to be put into the + * histogram. + * @param significant_figures The level of precision for this histogram, i.e. the number + * of figures in a decimal number that will be maintained. E.g. a value of 3 will mean + * the results from the histogram will be accurate up to the first three digits. Must + * be a value between 1 and 5 (inclusive). * @return metric id: -1 is failed, >= 0 is success * the output. */ -int fieldstat_register_histogram(struct fieldstat_instance *instance, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag, - const char * bins,const long long lowest_trackable_value,long long highest_trackable_value,int significant_figures); +int fieldstat_register_histogram(struct fieldstat_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, + const char *upper_inclusive_bounds, const long long lowest_trackable_value, long long highest_trackable_value, int significant_figures); /** * Register summary metric * @param instance fieldstat instance * @param field_name field name - * @param tag_key the array of tag key - * @param tag_value the array of tag value - * @param n_tag the number of tags + * @param tags The tag array + * @param n_tag Size of tags * @param quantiles the quantiles of summary output + * @param lowest_trackable_value The smallest possible value to be put into the + * histogram. + * @param highest_trackable_value The largest possible value to be put into the + * histogram. + * @param significant_figures The level of precision for this histogram, i.e. the number + * of figures in a decimal number that will be maintained. E.g. a value of 3 will mean + * the results from the histogram will be accurate up to the first three digits. Must + * be a value between 1 and 5 (inclusive). * @return metric id: -1 is failed, >= 0 is success * the output. */ -int fieldstat_register_summary(struct fieldstat_instance *instance, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag, - const char * bins,const long long lowest_trackable_value,long long highest_trackable_value,int significant_figures); +int fieldstat_register_summary(struct fieldstat_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, + const char *quantiles, const long long lowest_trackable_value, long long highest_trackable_value, int significant_figures); #ifdef __cplusplus } diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp index a50cb03..e6c62c4 100644 --- a/src/fieldstat.cpp +++ b/src/fieldstat.cpp @@ -61,12 +61,12 @@ void get_current_table_line_cnt(struct fieldstat_instance *instance, int n_table } } -struct metric_t * get_metric(struct fieldstat_instance *instance, int metric_id) +struct metric * get_metric(struct fieldstat_instance *instance, int metric_id) { int block_index = 0; int block_in_index = 0; - struct metric_t ** metrics_array = NULL; - struct metric_t * metric = NULL; + struct metric ** metrics_array = NULL; + struct metric * metric = NULL; if(instance == NULL) { @@ -81,26 +81,54 @@ struct metric_t * get_metric(struct fieldstat_instance *instance, int metric_id) return metric; } - -struct metric_t* metric_new(enum field_type type, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag) +void add_tags_to_metric(const struct fieldstat_tag tags[], size_t n_tag, char *tag_key[], char *tag_value[]) { int i = 0; - struct metric_t* metric=(struct metric_t*)calloc(sizeof(struct metric_t),1); - metric->field_name =__str_dup(field_name); + struct fieldstat_tag *tag = NULL; + char str_longlong[32] = {0}; + char str_double[32] = {0}; + + for(i = 0; i < (int)n_tag; i++) + { + tag = (struct fieldstat_tag *)&tags[i]; + tag_key[i] = __str_dup(tag->key); + switch(tag->value_type) + { + case 0: + memset(str_longlong, 0, sizeof(str_longlong)); + snprintf(str_longlong, sizeof(str_longlong), "%lld", tag->value_int); + tag_value[i] = __str_dup((const char *)str_longlong); + break; + case 1: + memset(str_double, 0, sizeof(str_double)); + snprintf(str_double, sizeof(str_double), "%lf", tag->value_double); + tag_value[i] = __str_dup((const char *)str_double); + break; + case 2: + tag_value[i] = __str_dup(tag->value_str); + break; + default: + assert(0); + break; + } + } +} + +struct metric * metric_new(enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag) +{ + struct metric *metric = (struct metric*)calloc(sizeof(struct metric),1); + metric->field_name = __str_dup(field_name); metric->field_type = type; metric->is_ratio = 0; metric->output_scaling = 1; metric->n_tag = n_tag; - for(i = 0; i < (int)n_tag; i++) - { - metric->tag_key[i] = __str_dup(tag_key[i]); - metric->tag_value[i] = __str_dup(tag_value[i]); - } + add_tags_to_metric(tags, n_tag, metric->tag_key, metric->tag_value); + return metric; } -void metric_free(struct metric_t* metric) +void metric_free(struct metric *metric) { int i = 0; @@ -122,11 +150,11 @@ void metric_free(struct metric_t* metric) return; } -struct metric_t ** read_metric_slot(struct fieldstat_instance *instance, int metric_id) +struct metric ** read_metric_slot(struct fieldstat_instance *instance, int metric_id) { int block_index = 0; int in_block_index = 0; - struct metric_t ** metrics_block = NULL; + struct metric ** metrics_block = NULL; if(instance == NULL) { @@ -137,15 +165,15 @@ struct metric_t ** read_metric_slot(struct fieldstat_instance *instance, int met if(in_block_index == 0) { assert(instance->metric_block_list[block_index] == NULL); - instance->metric_block_list[block_index] = (struct metric_t **)calloc(sizeof(struct metric *), NUM_INIT_METRICS); + instance->metric_block_list[block_index] = (struct metric **)calloc(sizeof(struct metric *), NUM_INIT_METRICS); } else { while (instance->metric_block_list[block_index] == NULL); } - metrics_block = (struct metric_t **)instance->metric_block_list[block_index]; + metrics_block = (struct metric **)instance->metric_block_list[block_index]; - return (struct metric_t **)&(metrics_block[in_block_index]); + return (struct metric **)&(metrics_block[in_block_index]); } @@ -227,17 +255,17 @@ int send_udp(int sd, unsigned int dest_ip, unsigned short dest_port, const char return 0; } -int fieldstat_set_output_interval(struct fieldstat_instance *instance, int seconds) +int fieldstat_set_output_interval(struct fieldstat_instance *instance, int milliseconds) { - if(instance->running == 1 || seconds <= 0 ) + if(instance->running == 1 || milliseconds <= 0 ) { return -1; } - instance->output_interval_s = seconds; + instance->output_interval_ms = milliseconds; return 0; } -int fieldstat_background_thread_disable(struct fieldstat_instance *instance) +int fieldstat_disable_background_thread(struct fieldstat_instance *instance) { if(instance->running == 1) { @@ -311,11 +339,11 @@ int fieldstat_set_statsd_server(struct fieldstat_instance *instance, const char return 0; } -int fieldstat_register(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag) +int fieldstat_register(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag) { int metric_id = 0; - struct metric_t * metric = NULL; - struct metric_t **metric_slot = NULL; + struct metric *metric = NULL; + struct metric **metric_slot = NULL; if(!is_valid_field_name(field_name)) { return -1; @@ -327,7 +355,7 @@ int fieldstat_register(struct fieldstat_instance *instance, enum field_type type metric_id = atomic_inc(&instance->metric_cnt) - 1; metric_slot = read_metric_slot(instance, metric_id); - metric = metric_new(type,field_name,tag_key,tag_value,n_tag); + metric = metric_new(type, field_name, tags, n_tag); switch(type) { @@ -344,7 +372,7 @@ int fieldstat_register(struct fieldstat_instance *instance, enum field_type type return metric_id; } -long long get_metric_unit_val(struct metric_t *metric,enum field_calc_algo calc_type,int is_refer) +long long get_metric_unit_val(struct metric *metric,enum field_calc_algo calc_type,int is_refer) { stat_unit_t* target = NULL; long long value = 0; @@ -437,7 +465,7 @@ void *fieldstat_thread_schema_output(void *arg) while(instance->background_thread_disable == 0) { fieldstat_passive_output(instance); - sleep(instance->output_interval_s); + usleep(instance->output_interval_ms * 1000); } return NULL; } @@ -454,7 +482,7 @@ void fieldstat_instance_start(struct fieldstat_instance *instance) //append instance to prometheus output } -struct fieldstat_instance * fieldstat_instance_create(const char *name) +struct fieldstat_instance * fieldstat_instance_new(const char *name) { struct fieldstat_instance *instance = NULL; @@ -466,7 +494,7 @@ struct fieldstat_instance * fieldstat_instance_create(const char *name) instance = (struct fieldstat_instance *)calloc(sizeof(struct fieldstat_instance),1); strcpy(instance->name, name); instance->running = 0; - instance->output_interval_s = 2; + instance->output_interval_ms = 2000; instance->background_thread_disable = 0; return instance; @@ -488,7 +516,7 @@ struct table_metric* table_metric_new(const char *name, const char *column_name[ return table_metric; } - int fieldstat_register_table(struct fieldstat_instance *instance, const char *name, const char *column_name[], enum field_type column_type[], size_t n_column) + int fieldstat_register_table(struct fieldstat_instance *instance, const char *table_name, const char *column_name[], enum field_type column_type[], size_t n_column) { int table_id = 0; @@ -505,7 +533,7 @@ struct table_metric* table_metric_new(const char *name, const char *column_name[ } table_id = atomic_inc(&instance->table_num) - 1; - table_metric = table_metric_new(name, column_name, column_type, n_column); + table_metric = table_metric_new(table_name, column_name, column_type, n_column); instance->table_metrics[table_id] = table_metric; return table_id; @@ -533,27 +561,23 @@ static struct table_line ** read_table_line_slot(struct table_metric *table, int return (struct table_line **)&(line[in_block_index]); } -static struct table_line *table_line_new(const char *name, const char *tag_key[], const char *tag_value[],size_t n_tag) +static struct table_line *table_line_new(const char *name, const struct fieldstat_tag tags[],size_t n_tag) { - int i = 0; struct table_line *table_line = (struct table_line *)calloc(sizeof(struct table_line), 1); table_line->name = __str_dup(name); table_line->n_tag = n_tag; - for(i = 0; i < (int)n_tag; i++) - { - table_line->tag_key[i] = __str_dup(tag_key[i]); - table_line->tag_value[i] = __str_dup(tag_value[i]); - } + add_tags_to_metric(tags, n_tag, table_line->tag_key, table_line->tag_value); + return table_line; } -struct metric_id_list fieldstat_register_table_metrics(struct fieldstat_instance * instance, int table_id, const char *line_name, const char *tag_key[], const char *tag_value[],size_t n_tag) +int fieldstat_register_table_row(struct fieldstat_instance * instance, int table_id, const char *row_name, const struct fieldstat_tag tags[], size_t n_tag, int output_metric_ids[], int *output_metric_ids_cnt) { int metric_id = 0; - struct metric_t *metric = NULL; + struct metric *metric = NULL; int line_id = 0; int i = 0; @@ -561,27 +585,24 @@ struct metric_id_list fieldstat_register_table_metrics(struct fieldstat_instance struct table_metric *table = NULL; struct table_line **line_slot = NULL; struct table_line *table_line = NULL; - struct metric_id_list ret_metric_id_list; - memset(&ret_metric_id_list, 0, sizeof(struct metric_id_list)); - if(table_id < 0 || table_id >= TABLE_MAX_NUM) { - return ret_metric_id_list; + return -1; } - if(!is_valid_field_name(line_name) || n_tag > N_TAG_MAX) + if(!is_valid_field_name(row_name) || n_tag > N_TAG_MAX) { - return ret_metric_id_list; + return -1; } table = instance->table_metrics[table_id]; line_id = atomic_inc(&(table->line_cnt)) - 1; - line_slot = read_table_line_slot(table,line_id); - table_line = table_line_new(line_name, tag_key, tag_value, n_tag); + line_slot = read_table_line_slot(table, line_id); + table_line = table_line_new(row_name, tags, n_tag); for(i = 0; i < table->column_cnt; i++) { - metric_id = fieldstat_register(instance, table->column_type[i], line_name,tag_key, tag_value, n_tag); + metric_id = fieldstat_register(instance, table->column_type[i], row_name, tags, n_tag); table_line->metric_id_belong_to_line[i] = metric_id; @@ -590,25 +611,25 @@ struct metric_id_list fieldstat_register_table_metrics(struct fieldstat_instance metric->table_column_name = __str_dup(table->column_name[i]); metric->table_name = __str_dup(table->name); metric->belong_to_table = 1; - ret_metric_id_list.id[ret_metric_id_list.count ++] = metric_id; + output_metric_ids[(*output_metric_ids_cnt)++] = metric_id; } *line_slot = table_line; - return ret_metric_id_list; + return 0; } -static int fieldstat_value_operate(struct fieldstat_instance *instance, int field_id, enum field_op op, long long value) +static int fieldstat_value_operate(struct fieldstat_instance *instance, int metric_id, enum field_op op, long long value) { - struct metric_t * metric = NULL; + struct metric * metric = NULL; struct stat_unit_t *target = NULL; - if(field_id >= instance->metric_cnt) + if(metric_id >= instance->metric_cnt) { return -1; } - metric = get_metric(instance, field_id); + metric = get_metric(instance, metric_id); switch(metric->field_type) { @@ -646,24 +667,24 @@ static int fieldstat_value_operate(struct fieldstat_instance *instance, int fiel } -int fieldstat_value_incrby(struct fieldstat_instance *instance, int field_id, long long increment) +int fieldstat_value_incrby(struct fieldstat_instance *instance, int metric_id, long long increment) { int ret = 0; - ret = fieldstat_value_operate(instance, field_id, FS_OP_ADD, increment); + ret = fieldstat_value_operate(instance, metric_id, FS_OP_ADD, increment); return ret; } -int fieldstat_value_set(struct fieldstat_instance *instance, int field_id, long long retain) +int fieldstat_value_set(struct fieldstat_instance *instance, int metric_id, long long retain) { int ret = 0; - ret = fieldstat_value_operate(instance, field_id, FS_OP_SET, retain); + ret = fieldstat_value_operate(instance, metric_id, FS_OP_SET, retain); return ret; } -int fieldstat_value_decrby(struct fieldstat_instance *instance, int field_id, long long decrment) +int fieldstat_value_decrby(struct fieldstat_instance *instance, int metric_id, long long decrment) { int ret = 0; - ret = fieldstat_value_operate(instance, field_id, FS_OP_SUB, decrment); + ret = fieldstat_value_operate(instance, metric_id, FS_OP_SUB, decrment); return ret; } @@ -710,11 +731,11 @@ error_out: } -static int fieldstat_register_histogram_and_summary(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag, - const char * bins, long long lowest_trackable_value,long long highest_trackable_value,int significant_figures) +static int fieldstat_register_histogram_and_summary(struct fieldstat_instance *instance, enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, + const char * bins, long long lowest_trackable_value, long long highest_trackable_value, int significant_figures) { - struct metric_t *metric = NULL; - struct metric_t **metric_slot = NULL; + struct metric *metric = NULL; + struct metric **metric_slot = NULL; int metric_id = -1; int ret; @@ -739,7 +760,7 @@ static int fieldstat_register_histogram_and_summary(struct fieldstat_instance *i metric_id = atomic_inc(&instance->metric_cnt) - 1; metric_slot = read_metric_slot(instance, metric_id); - metric = metric_new(type,field_name,tag_key,tag_value,n_tag); + metric = metric_new(type, field_name, tags, n_tag); metric->histogram.highest_trackable_value = (int64_t)highest_trackable_value; metric->histogram.lowest_trackable_value = (int64_t)lowest_trackable_value; @@ -767,17 +788,17 @@ static int fieldstat_register_histogram_and_summary(struct fieldstat_instance *i } -int fieldstat_register_histogram(struct fieldstat_instance *instance, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag, - const char * bins, long long lowest_trackable_value,long long highest_trackable_value,int significant_figures) +int fieldstat_register_histogram(struct fieldstat_instance *instance, const char *field_name,const struct fieldstat_tag tags[], size_t n_tag, + const char * bins, long long lowest_trackable_value, long long highest_trackable_value, int significant_figures) { - return fieldstat_register_histogram_and_summary(instance, FILED_TYPE_HISTOGRAM, field_name, tag_key, tag_value, n_tag, + return fieldstat_register_histogram_and_summary(instance, FILED_TYPE_HISTOGRAM, field_name, tags, n_tag, bins, lowest_trackable_value, highest_trackable_value, significant_figures); } -int fieldstat_register_summary(struct fieldstat_instance *instance, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag, - const char * bins,long long lowest_trackable_value,long long highest_trackable_value,int significant_figures) +int fieldstat_register_summary(struct fieldstat_instance *instance, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag, + const char * bins, long long lowest_trackable_value, long long highest_trackable_value, int significant_figures) { - return fieldstat_register_histogram_and_summary(instance, FIELD_TYPE_SUMMARY, field_name, tag_key, tag_value, n_tag, + return fieldstat_register_histogram_and_summary(instance, FIELD_TYPE_SUMMARY, field_name, tags, n_tag, bins, lowest_trackable_value, highest_trackable_value, significant_figures); } diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h index bce5bbe..8c2753f 100644 --- a/src/fieldstat_internal.h +++ b/src/fieldstat_internal.h @@ -100,7 +100,7 @@ struct histogram_t }; -struct metric_t +struct metric { char *field_name; enum field_type field_type; @@ -170,19 +170,18 @@ struct fieldstat_instance char local_output_format[LEN_FORMAT_MAX]; int local_output_enable; - int background_thread_disable; //default:1 - int output_interval_s; //default:2 + int background_thread_disable; + int output_interval_ms; int running; - struct metric_t **metric_block_list[BLOCK_LIST_SIZE]; + struct metric **metric_block_list[BLOCK_LIST_SIZE]; int metric_cnt; - //struct metric_t **metric; + //struct metric **metric; struct table_metric *table_metrics[TABLE_MAX_NUM]; int *index_table[TABLE_MAX_NUM][TABLE_COLUMN_SIZE]; - int per_table_line_number[TABLE_MAX_NUM]; int table_num; int line_seq; @@ -220,13 +219,13 @@ void prometheus_endpoint_instance_output(struct http_request_s* request); char* __str_dup(const char* str); long long hdr_count_le_value(const struct hdr_histogram* h, long long value); -struct metric_t * get_metric(struct fieldstat_instance *instance, int metric_id); -long long get_metric_unit_val(struct metric_t *metric,enum field_calc_algo calc_type,int is_refer); +struct metric * get_metric(struct fieldstat_instance *instance, int metric_id); +long long get_metric_unit_val(struct metric *metric,enum field_calc_algo calc_type,int is_refer); int is_valid_field_name(const char* name); -struct metric_t ** read_metric_slot(struct fieldstat_instance *instance, int metric_id); -struct metric_t* metric_new(enum field_type type, const char *field_name, const char *tag_key[], const char *tag_value[], size_t n_tag); +struct metric ** read_metric_slot(struct fieldstat_instance *instance, int metric_id); +struct metric * metric_new(enum field_type type, const char *field_name, struct fieldstat_tag tags[], size_t n_tag); int fieldstat_output_file(struct fieldstat_instance *instance,long long interval_ms); struct table_line * read_table_line(struct table_metric *table, int line_id); int send_udp(int sd, unsigned int dest_ip, unsigned short dest_port, const char * data, int len); int line_protocol_output(struct fieldstat_instance *instance); -void get_current_table_line_cnt(struct fieldstat_instance *instance, int n_table, int *table_line_cnt);
\ No newline at end of file +void get_current_table_line_cnt(struct fieldstat_instance *instance, int n_table, int *table_line_cnt); diff --git a/src/file_output.cpp b/src/file_output.cpp index dceb370..3c4c921 100644 --- a/src/file_output.cpp +++ b/src/file_output.cpp @@ -45,7 +45,7 @@ static int output_file_format_default_type_gauge(struct fieldstat_instance *inst { int i = 0, j = 0; //display_manifest_t* p = NULL; - metric_t *metric = NULL; + struct metric *metric = NULL; long long value = 0; //double ratio = 0.0; //char* pos=print_buf; @@ -116,7 +116,7 @@ static int output_file_format_default_type_counter(struct fieldstat_instance *in { int i=0,j=0; //display_manifest_t* p=NULL; - metric_t *metric = NULL; + struct metric *metric = NULL; long long value = 0; //double ratio = 0.0; char* append_pos = print_buf; @@ -225,7 +225,7 @@ static int output_file_format_default_table(struct fieldstat_instance *instance, struct table_metric *table = NULL; struct table_line *line = NULL; int metric_id = 0; - struct metric_t *metric = NULL; + struct metric *metric = NULL; long long value = 0; char* append_pos = print_buf; @@ -293,7 +293,7 @@ static int output_file_format_default_table(struct fieldstat_instance *instance, return append_pos - print_buf; } -static int output_file_print_hdr_head(struct metric_t *metric, char *print_buf, size_t size) +static int output_file_print_hdr_head(struct metric *metric, char *print_buf, size_t size) { char * pos = print_buf; char bin_format[STR_LEN_256], str_format[STR_LEN_256]; @@ -339,15 +339,15 @@ static int output_file_print_hdr_head(struct metric_t *metric, char *print_buf, return pos-print_buf; } -static int output_file_print_hdr_unit(struct metric_t *metric, char*print_buf, size_t size) +static int output_file_print_hdr_unit(struct metric *metric, char *print_buf, size_t size) { double * bins = metric->histogram.bins; int bins_num = metric->histogram.bins_num; char* pos=print_buf; long long value=0; int i=0; - struct histogram_t* h=&(metric->histogram); - struct hdr_histogram* h_out=NULL, *h_tmp=NULL; + struct histogram_t *h=&(metric->histogram); + struct hdr_histogram *h_out=NULL, *h_tmp=NULL; char int_format[STR_LEN_256], double_format[STR_LEN_256]; snprintf(int_format, sizeof(int_format), "%%%dlld",HISTOGRAM_WIDTH); snprintf(double_format, sizeof(double_format), "%%%d.2lf",HISTOGRAM_WIDTH); @@ -402,8 +402,8 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta int i = 0, j = 0, metric_num = 0; char *pos = print_buf; //display_manifest_t* p=NULL; - struct metric_t *metric = NULL; - struct metric_t *metric_array[current_metric_cnt] = {NULL}; + struct metric *metric = NULL; + struct metric *metric_array[current_metric_cnt] = {NULL}; int metric_is_print[current_metric_cnt] = {0}; if(instance->histogram_cnt == 0 diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp index f8b3108..56876fe 100644 --- a/src/line_protocol_output.cpp +++ b/src/line_protocol_output.cpp @@ -59,7 +59,7 @@ static int output_line_protocol_tag_set_buf(char *tag_key[], char *tag_value[], static void output_line_protocol_table(struct fieldstat_instance *instance,int tables_line_cnt[], int current_table_cnt) { int i = 0, j = 0, k = 0; - metric_t *metric = NULL; + struct metric *metric = NULL; long long value = 0; //double ratio = 0.0; char field_set_buff[UDP_PAYLOAD_SIZE]; @@ -135,7 +135,7 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t int line_protocol_output(struct fieldstat_instance *instance) { - metric_t *metric = NULL; + struct metric *metric = NULL; long long value=0; int i=0; char field_set_buff[UDP_PAYLOAD_SIZE]; diff --git a/src/prometheus_output.cpp b/src/prometheus_output.cpp index 1d4fbfd..151bfba 100644 --- a/src/prometheus_output.cpp +++ b/src/prometheus_output.cpp @@ -120,7 +120,7 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom return; } -static int prometheus_output_read_metric_tags(struct metric_t *metric, char *instance_name, char *tags_buff, unsigned int size) +static int prometheus_output_read_metric_tags(struct metric *metric, char *instance_name, char *tags_buff, unsigned int size) { int i = 0;//used_len = 0; char unescape[STR_LEN_256] = {0}; @@ -145,7 +145,7 @@ static int prometheus_output_read_metric_tags(struct metric_t *metric, char *ins } -static int prometheus_output_read_metric_name(struct metric_t *metric, char *instance_app_name, char *name_buff, unsigned int size) +static int prometheus_output_read_metric_name(struct metric *metric, char *instance_app_name, char *name_buff, unsigned int size) { char unescape[256] = {0}; char *append_pos = name_buff; @@ -163,7 +163,7 @@ static int prometheus_output_read_metric_name(struct metric_t *metric, char *ins } -static int prometheus_output_histogram_and_summary(struct metric_t *metric, char *payload, int payload_len, char *instance_app_name, char *metric_name, char *metric_tags) +static int prometheus_output_histogram_and_summary(struct metric *metric, char *payload, int payload_len, char *instance_app_name, char *metric_name, char *metric_tags) { long long value=0; long long sum = 0; @@ -241,7 +241,7 @@ static int prometheus_output_histogram_and_summary(struct metric_t *metric, char return used_len; } -static int is_output_prometheus(struct metric_t *metric) +static int is_output_prometheus(struct metric *metric) { int i = 0, ret = 0; for(i = 0; i < (int)metric->n_tag; i++) @@ -260,7 +260,7 @@ static int is_output_prometheus(struct metric_t *metric) static int prometheus_get_instance_metric_playload(struct fieldstat_instance *instance, char **payload, int *payload_size, int offset) { int i = 0; - struct metric_t *metric = NULL; + struct metric *metric = NULL; long long value = 0; char metric_name[256] = {0}; //match the regex [a-zA-Z_:][a-zA-Z0-9_:]* char instance_name[256] = {0}; @@ -385,7 +385,7 @@ void prometheus_endpoint_instance_output(struct http_request_s* request) { int fs_instance_idx = -1; struct http_string_s uri; - struct prometheus_endpoint_instance *prometheus_endpoint_instance = &g_prometheus_endpoint_instance;; + struct prometheus_endpoint_instance *prometheus_endpoint_instance = &g_prometheus_endpoint_instance; uri = http_request_target(request); fs_instance_idx = prometheus_get_fs_instance_id(prometheus_endpoint_instance, (char *)uri.buf, uri.len); @@ -405,7 +405,7 @@ void *prometheus_endpoint_listen_thread_entry(void *arg) return NULL; } -int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, const char *url) +int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, const char *url_path) { g_prometheus_endpoint_instance.server_handle = http_server_init(listen_port, prometheus_endpoint_instance_output); if(g_prometheus_endpoint_instance.server_handle == NULL) @@ -413,7 +413,7 @@ int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, cons return -1; } - g_prometheus_endpoint_instance.url_path = url == NULL ? strdup(PROMETHEUS_ENDPOINT_DEFAULT_URL):strdup(url); + g_prometheus_endpoint_instance.url_path = url_path == NULL ? strdup(PROMETHEUS_ENDPOINT_DEFAULT_URL):strdup(url_path); g_prometheus_endpoint_instance.port = listen_port; g_prometheus_endpoint_instance.running = 1; g_prometheus_endpoint_instance.fs_instance = (struct fieldstat_instance **)calloc( sizeof(struct fieldstat_instance *), g_prometheus_endpoint_instance.fs_instance_size); @@ -423,7 +423,7 @@ int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, cons return 0; } -int fieldstat_set_prometheus_output(struct fieldstat_instance *instance) +int fieldstat_enable_prometheus_output(struct fieldstat_instance *instance) { int fs_instance_id = 0; diff --git a/test/fieldstat_test.cpp b/test/fieldstat_test.cpp index eefc224..398a680 100644 --- a/test/fieldstat_test.cpp +++ b/test/fieldstat_test.cpp @@ -21,7 +21,8 @@ int g_counter_id[3]; int g_gauge_id[3]; int g_histogram_id = -1; int g_summary_id = -1; -struct metric_id_list g_id_list; +int g_id_list[32]; +int g_id_cnt; struct thread_para { @@ -58,9 +59,9 @@ static void* worker_thread(void* arg) fieldstat_value_set(instance, g_summary_id, i); } - for(i = 0; i < g_id_list.count; i++) + for(i = 0; i < g_id_cnt; i++) { - fieldstat_value_incrby(instance, g_id_list.id[i], 1); + fieldstat_value_incrby(instance, g_id_list[i], 1); } sleep(1); @@ -78,35 +79,40 @@ static void* dynamic_register(void* arg) int sce_table_id = para->sce_table_id; const char * bins_htr = "10,20,30,40,50,60,70,80,90"; const char * bins_sar = "0.1,0.5,0.8,0.9,0.95,0.99"; - const char * tags_key[] = {"disable_output_prometheus"}; - const char * tags_value[] = {"yes"}; + + struct fieldstat_tag tags[16]; + tags[0].key = "disable_output_prometheus"; + tags[0].value_type = 2; + tags[0].value_str = "yes"; while (loops > 0) { loops--; //fieldstat_value_incrby(instance,g_counter_id[i], i + 1); snprintf(name, sizeof(name), "counter_%d_%d", loops, rand()%10000); - fieldstat_register(instance,FIELD_TYPE_COUNTER,name,tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0])); + fieldstat_register(instance, FIELD_TYPE_COUNTER, name, tags, 1); memset(name, 0, sizeof(name)); snprintf(name, sizeof(name), "gauge_%d_%d", loops, rand()%10000); - fieldstat_register(instance,FIELD_TYPE_GAUGE, name, tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0])); + fieldstat_register(instance,FIELD_TYPE_GAUGE, name, tags, 1); memset(name, 0, sizeof(name)); snprintf(name, sizeof(name), "summary_%d_%d", loops, rand()%10000); - fieldstat_register_histogram(instance,name,tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0]),bins_htr,1,10000,2); + fieldstat_register_histogram(instance, name, tags, 1, bins_htr, 1, 10000, 2); memset(name, 0, sizeof(name)); snprintf(name, sizeof(name), "histogram_%d_%d", loops, rand()%10000); - fieldstat_register_summary(instance,name,tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0]),bins_sar,1,10000,2); + fieldstat_register_summary(instance, name, tags, 1, bins_sar,1,10000,2); memset(name, 0, sizeof(name)); snprintf(name, sizeof(name), "shp_%d_%d", loops, rand()%10000); - fieldstat_register_table_metrics(instance,shaping_table_id,name,tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0])); + int id_list[16]; + int id_cnt; + fieldstat_register_table_row(instance, shaping_table_id, name, tags, 1, id_list, &id_cnt); memset(name, 0, sizeof(name)); snprintf(name, sizeof(name), "sce_%d_%d", loops, rand()%10000); - fieldstat_register_table_metrics(instance,sce_table_id,name,tags_key,tags_value,sizeof(tags_key)/sizeof(tags_key[0])); + fieldstat_register_table_row(instance, sce_table_id, name, tags, 1, id_list, &id_cnt); sleep(1); } @@ -134,7 +140,7 @@ int set_instance_parameter(struct fieldstat_instance *instance) break; } - ret = fieldstat_set_output_interval(instance, 1); + ret = fieldstat_set_output_interval(instance, 1000); if(ret == -1) { printf("Set output_interval failed!\n"); @@ -147,14 +153,14 @@ int set_instance_parameter(struct fieldstat_instance *instance) printf("Failed to set line_protocol server\n"); break; } - ret = fieldstat_set_prometheus_output(instance); + ret = fieldstat_enable_prometheus_output(instance); if(ret == -1) { printf("Failed to set prometheus_output_enable!\n"); break; } /* - ret = fieldstat_background_thread_disable(instance); + ret = fieldstat_disable_background_thread(instance); if(ret == -1) { printf("Failed to disable filedstat background thread\n"); @@ -170,18 +176,20 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) { int ret = 0; //Register counter start - ret = fieldstat_register(instance, FIELD_TYPE_COUNTER, "counter_no_tags",NULL,NULL,0); + ret = fieldstat_register(instance, FIELD_TYPE_COUNTER, "counter_no_tags",NULL,0); if(ret == -1) { printf("Failed ot register counter_no_tags, type = counter\n"); return ret; } g_counter_id[0] = ret; - const char * counter_with_one_key[] = {"C_one_tag"}; - const char * counter_with_one_value[] = {"C_one_tag_yes"}; - ret = fieldstat_register(instance, FIELD_TYPE_COUNTER,"counter_with_one_tag", counter_with_one_key, - counter_with_one_value, sizeof(counter_with_one_key)/sizeof(counter_with_one_key[0])); + struct fieldstat_tag tag_counter; + tag_counter.value_type = 2; + tag_counter.value_str = "C_one_tag"; + tag_counter.key = "C_one_tag_yes"; + + ret = fieldstat_register(instance, FIELD_TYPE_COUNTER,"counter_with_one_tag", &tag_counter, 1); if(ret == -1) { printf("Failed ot register counter_with_one_tag, type = counter\n"); @@ -189,11 +197,21 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) } g_counter_id[1] = ret; - const char * counter_with_keys[] = {"C_tags_0","C_tags_1","C_tags_2"}; - const char * counter_with_values[] = {"C_tags_0_yes", "C_tags_1_yes","C_tags_2_yes"}; + struct fieldstat_tag tags_counter[3]; + tags_counter[0].value_type = 2; + tags_counter[0].value_str = "C_tags_0"; + tags_counter[0].key = "C_tags_0_yes"; + + tags_counter[1].value_type = 2; + tags_counter[1].value_str = "C_tags_1"; + tags_counter[1].key = "C_tags_1_yes"; + + tags_counter[2].value_type = 2; + tags_counter[2].value_str = "C_tags_2"; + tags_counter[2].key = "C_tags_2_yes"; + - ret = fieldstat_register(instance, FIELD_TYPE_COUNTER,"counter_with_tags", counter_with_keys, - counter_with_values, sizeof(counter_with_keys)/sizeof(counter_with_keys[0])); + ret = fieldstat_register(instance, FIELD_TYPE_COUNTER,"counter_with_tags", tags_counter, sizeof(tags_counter)/sizeof(tags_counter[0])); if(ret == -1) { printf("Failed ot register counter_with_tags, type = counter\n"); @@ -204,7 +222,7 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) //Register gauge start - ret = fieldstat_register(instance, FIELD_TYPE_GAUGE, "gauge_no_tags",NULL,NULL,0); + ret = fieldstat_register(instance, FIELD_TYPE_GAUGE, "gauge_no_tags",NULL,0); if(ret == -1) { printf("Failed ot register gauge_no_tags, type = gauge\n"); @@ -212,10 +230,12 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) } g_gauge_id[0] = ret; - const char * gauge_with_one_key[] = {"G_one_tag"}; - const char * gauge_with_one_value[] = {"G_one_tag_yes"}; - ret = fieldstat_register(instance, FIELD_TYPE_GAUGE,"gauge_with_one_tag", gauge_with_one_key, - gauge_with_one_value, sizeof(gauge_with_one_key)/sizeof(gauge_with_one_key[0])); + struct fieldstat_tag tag_gauge; + tag_gauge.value_type = 2; + tag_gauge.value_str = "G_one_tag"; + tag_gauge.key = "G_one_tag_yes"; + + ret = fieldstat_register(instance, FIELD_TYPE_GAUGE,"gauge_with_one_tag", &tag_gauge, 1); if(ret == -1) { printf("Failed ot register gauge_with_one_tag, type = gauge\n"); @@ -223,11 +243,20 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) } g_gauge_id[1] = ret; - const char * gauge_with_keys[] = {"G_tags_0","G_tags_1","G_tags_2"}; - const char * gauge_with_values[] = {"G_tags_0_yes", "G_tags_1_yes","G_tags_2_yes"}; + struct fieldstat_tag tags_gauge[3]; + tags_gauge[0].value_type = 2; + tags_gauge[0].value_str = "G_tags_0"; + tags_gauge[0].key = "G_tags_0_yes"; + + tags_gauge[1].value_type = 2; + tags_gauge[1].value_str = "G_tags_1"; + tags_gauge[1].key = "G_tags_1_yes"; - ret = fieldstat_register(instance, FIELD_TYPE_GAUGE,"gauge_with_tags", gauge_with_keys, - gauge_with_values, sizeof(gauge_with_keys)/sizeof(gauge_with_keys[0])); + tags_gauge[2].value_type = 2; + tags_gauge[2].value_str = "G_tags_2"; + tags_gauge[2].key = "G_tags_2_yes"; + + ret = fieldstat_register(instance, FIELD_TYPE_GAUGE, "gauge_with_tags", tags_gauge, sizeof(tags_gauge)/sizeof(tags_gauge[0])); if(ret == -1) { printf("Failed ot register gauge_with_tags, type = gauge\n"); @@ -238,7 +267,7 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) //Register histogram and summary start const char * bins_htr = "10,20,30,40,50,60,70,80,90"; - ret = fieldstat_register_histogram(instance, "htr",NULL,NULL,0,bins_htr,1,1000,2); + ret = fieldstat_register_histogram(instance, "htr", NULL, 0, bins_htr, 1, 1000, 2); if(ret == -1) { printf("Failed ot register htr, type = histogram\n"); @@ -246,7 +275,7 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) } g_histogram_id = ret; const char * bins_sar = "0.1,0.5,0.8,0.9,0.95,0.99"; - ret = fieldstat_register_summary(instance, "sar",NULL,NULL,0,bins_sar,1,1000,2); + ret = fieldstat_register_summary(instance, "sar", NULL, 0, bins_sar, 1, 1000, 2); if(ret == -1) { printf("Failed ot register sar, type = summary\n"); @@ -259,19 +288,17 @@ int test_register_by_fieldstat_type(struct fieldstat_instance *instance) int table_id = -1; const char *column[] = {"T_success_log","T_fail_log"}; enum field_type table_type[] = {FIELD_TYPE_COUNTER, FIELD_TYPE_GAUGE}; - struct metric_id_list id_list; table_id = fieldstat_register_table(instance, "table_test", column, table_type, sizeof(column)/sizeof(column[0])); if(table_id == -1) { printf("Failed to register metric table\n"); } - id_list = fieldstat_register_table_metrics(instance, table_id, "SUM", NULL, NULL, 0); - if(id_list.count == 0) + ret = fieldstat_register_table_row(instance, table_id, "SUM", NULL, 0, g_id_list, &g_id_cnt); + if(ret == -1) { return -1; } - g_id_list = id_list; //Register table end return ret; @@ -282,7 +309,7 @@ int main(int argc, char *argv[]) { int ret = 0; - struct fieldstat_instance *test_instance = fieldstat_instance_create("test"); + struct fieldstat_instance *test_instance = fieldstat_instance_new("test"); if(test_instance == NULL) { diff --git a/test/src/gtest_fieldstat.cpp b/test/src/gtest_fieldstat.cpp index 4bd6c3a..ee69475 100644 --- a/test/src/gtest_fieldstat.cpp +++ b/test/src/gtest_fieldstat.cpp @@ -9,7 +9,7 @@ extern struct prometheus_endpoint_instance g_prometheus_endpoint_instance; TEST(FeildStatAPI, CreateFieldStatInstance) { - struct fieldstat_instance *instance = fieldstat_instance_create("test"); + struct fieldstat_instance *instance = fieldstat_instance_new("test"); EXPECT_NE(nullptr, instance); EXPECT_STREQ("test", instance->name); } @@ -22,7 +22,7 @@ TEST(FeildStatAPI, FieldStatEnablePrometheusEndpoint) ret_enable_prometheus_endpoint = fieldstat_global_enable_prometheus_endpoint(9020, "/metrcis"); EXPECT_EQ(0, ret_enable_prometheus_endpoint); - ret_req_prometheus_endpoint = system("curl http://127.0.0.1:9020/metrics"); + ret_req_prometheus_endpoint = system("curl -s -o /dev/null http://127.0.0.1:9020/metrics"); EXPECT_EQ(0,ret_req_prometheus_endpoint); } @@ -32,13 +32,13 @@ TEST(FeildStatAPI, FieldStatSetPrometheusOutput) int ret_enable_prometheus_endpoint = 0; int ret_set_prometheus_output = 0; - instance = fieldstat_instance_create("test"); + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); ret_enable_prometheus_endpoint = fieldstat_global_enable_prometheus_endpoint(9020, "/metrcis"); EXPECT_EQ(0, ret_enable_prometheus_endpoint); - ret_set_prometheus_output = fieldstat_set_prometheus_output(instance); + ret_set_prometheus_output = fieldstat_enable_prometheus_output(instance); EXPECT_EQ(0, ret_set_prometheus_output); EXPECT_EQ(1, g_prometheus_endpoint_instance.running); @@ -51,7 +51,7 @@ TEST(FeildStatAPI, FieldStatSetLineProtocolServer) struct fieldstat_instance *instance = NULL; int ret_set_line_protocol_server = 0; - instance = fieldstat_instance_create("test"); + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); ret_set_line_protocol_server = fieldstat_set_line_protocol_server(instance, "127.0.0.1", 8001); @@ -65,7 +65,7 @@ TEST(FeildStatAPI, FieldStatSetLocalOutput) { struct fieldstat_instance *instance = NULL; int ret_set_local_output = 0; - instance = fieldstat_instance_create("test"); + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); ret_set_local_output = fieldstat_set_local_output(instance,"test.fs","default"); @@ -76,16 +76,16 @@ TEST(FeildStatAPI, FieldStatSetLocalOutput) EXPECT_EQ(1, instance->local_output_enable); } -int fieldstat_background_thread_disable(struct fieldstat_instance *instance); +int fieldstat_disable_background_thread(struct fieldstat_instance *instance); TEST(FeildStatAPI, FieldStatBackgroundThreadDisable) { struct fieldstat_instance *instance = NULL; int ret_background_thread_disable = 0; - instance = fieldstat_instance_create("test"); + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); - ret_background_thread_disable = fieldstat_background_thread_disable(instance); + ret_background_thread_disable = fieldstat_disable_background_thread(instance); EXPECT_EQ(0, ret_background_thread_disable); EXPECT_EQ(1, instance->background_thread_disable); } @@ -95,46 +95,103 @@ TEST(FeildStatAPI, FieldStatSetOutputInterval) struct fieldstat_instance *instance = NULL; int ret_set_output_interval = 0; - instance = fieldstat_instance_create("test"); + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); ret_set_output_interval = fieldstat_set_output_interval(instance, 1); EXPECT_EQ(0, ret_set_output_interval); - EXPECT_EQ(1, instance->output_interval_s); + EXPECT_EQ(1, instance->output_interval_ms); } TEST(FeildStatAPI, FieldStatRegister) { struct fieldstat_instance * instance = NULL; int metric_id = -1; - struct metric_t **metric_in_block = NULL; - const char *tag_key[] = {"dest_ip", "src_ip"}; - const char *tag_value[] = {"127.0.0.1", "127.0.0.1"}; + struct metric **in_block_metric = NULL; + struct fieldstat_tag tags[2]; + tags[0].value_type = 2; + tags[0].value_str = "127.0.0.1"; + tags[0].key = "dest_ip"; - instance = fieldstat_instance_create("test"); + tags[1].value_type = 2; + tags[1].value_str = "127.0.0.1"; + tags[1].key = "src_ip"; + + + instance = fieldstat_instance_new("test"); EXPECT_STREQ("test", instance->name); - metric_id = fieldstat_register(instance, FIELD_TYPE_COUNTER, "Traffic_bytes",NULL,NULL,0); + metric_id = fieldstat_register(instance, FIELD_TYPE_COUNTER, "Traffic_bytes",NULL,0); EXPECT_EQ(0, metric_id); EXPECT_EQ(1, instance->metric_cnt); EXPECT_NE(nullptr, instance->metric_block_list[0]); - metric_in_block = instance->metric_block_list[0]; - EXPECT_NE(nullptr, metric_in_block[0]); + in_block_metric = instance->metric_block_list[0]; + EXPECT_NE(nullptr, in_block_metric[0]); - metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "Active_sessions", NULL, NULL, 0); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "Active_sessions", NULL, 0); EXPECT_EQ(1, metric_id); EXPECT_EQ(2, instance->metric_cnt); - EXPECT_NE(nullptr, metric_in_block[1]); + EXPECT_NE(nullptr, in_block_metric[1]); - metric_id = fieldstat_register(instance, FIELD_TYPE_COUNTER, "New_sessions",tag_key, tag_value, sizeof(tag_key)/sizeof(tag_key[0])); + metric_id = fieldstat_register(instance, FIELD_TYPE_COUNTER, "New_sessions", tags, sizeof(tags)/sizeof(tags[0])); EXPECT_EQ(2, metric_id); EXPECT_EQ(3, instance->metric_cnt); - EXPECT_NE(nullptr, metric_in_block[2]); + EXPECT_NE(nullptr, in_block_metric[2]); +} + +TEST(FieldStatAPI, FieldStatRegisterTable) +{ + struct fieldstat_instance * instance = NULL; + int table_id = -1; + struct table_metric *table = NULL; + + const char *shaping_column_name[] = {"packets","bytes"}; + const char *shaping_table_name = "shaping"; + enum field_type shaping_column_type[] = {FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER}; + + const char *sc_column_name[] = {"sent_pkts", "send_bytes", "recv_pkts", "recv_bytes"}; + enum field_type sc_column_type[] = {FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER}; + const char *sc_table_name = "service_chaining"; + + instance = fieldstat_instance_new("test"); + EXPECT_STREQ("test", instance->name); + table_id = fieldstat_register_table(instance, shaping_table_name, shaping_column_name, shaping_column_type, sizeof(shaping_column_name)/sizeof(shaping_column_name[0])); + EXPECT_EQ(0, table_id); + EXPECT_NE(nullptr, instance->table_metrics[0]); + table = instance->table_metrics[0]; + EXPECT_EQ(2, table->column_cnt); + EXPECT_EQ(0, table->line_cnt); + EXPECT_EQ(nullptr, table->line_block[0]); + + table_id = fieldstat_register_table(instance, sc_table_name, sc_column_name, sc_column_type, sizeof(sc_column_name)/sizeof(sc_column_name[0])); + + EXPECT_EQ(1, table_id); + EXPECT_NE(nullptr, instance->table_metrics[1]); + table = instance->table_metrics[1]; + EXPECT_EQ(4, table->column_cnt); + EXPECT_EQ(0, table->line_cnt); + EXPECT_EQ(nullptr, table->line_block[0]); } +TEST(FieldStatAPI, FieldStatRegisterTableMetrics) +{ + struct fieldstat_instance * instance = NULL; + int table_id = -1; + struct table_metric *table = NULL; + + const char *sc_column_name[] = {"sent_pkts", "send_bytes", "recv_pkts", "recv_bytes"}; + enum field_type sc_column_type[] = {FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER,FIELD_TYPE_COUNTER}; + const char *sc_table_name = "service_chaining"; + + instance = fieldstat_instance_new("test"); + EXPECT_STREQ("test", instance->name); + + table_id = fieldstat_register_table(instance, sc_table_name, sc_column_name, sc_column_type, sizeof(sc_column_name)/sizeof(sc_column_name[0])); + +} int main(int argc, char *argv[]) |
