diff options
| author | fumingwei <[email protected]> | 2023-03-23 19:22:47 +0800 |
|---|---|---|
| committer | fumingwei <[email protected]> | 2023-03-23 19:22:47 +0800 |
| commit | a52d18031284607d4c75878b14fdb7aff0396665 (patch) | |
| tree | 2a80b58a5cac823a4078456d07f0356457dd558f | |
| parent | 0999ff92c5ac2214365416054546de4b1776f206 (diff) | |
feature:统一snprintf调用方式
| -rw-r--r-- | src/file_output.cpp | 351 | ||||
| -rw-r--r-- | src/line_protocol_output.cpp | 90 | ||||
| -rw-r--r-- | src/prometheus_output.cpp | 32 |
3 files changed, 199 insertions, 274 deletions
diff --git a/src/file_output.cpp b/src/file_output.cpp index 27187f3..d66e235 100644 --- a/src/file_output.cpp +++ b/src/file_output.cpp @@ -3,58 +3,43 @@ const char* draw_line="________________________________________________________________________________________________________________________________________________"; const char* draw_boundary="============================================================"; -static int print_buf_tag_append_position(char *tag_key[], char *tag_value[], size_t n_tag, char *print_buf_tags, unsigned int size) +static int append_tags_to_print_buf(char *tag_key[], char *tag_value[], size_t n_tag, char *print_buf, unsigned int size) { int i = 0; - char *append_pos = print_buf_tags; + int used_len = 0; if(n_tag <= 0) { return 0; } - append_pos += snprintf(append_pos, - size - (append_pos - print_buf_tags), - "{" - ); + used_len += snprintf(print_buf + used_len, size - used_len, "{"); for(i = 0; i < (int)n_tag; i++) { - append_pos += snprintf(append_pos, - size - (append_pos - print_buf_tags), - "%s=\"%s\",", - tag_key[i], - tag_value[i] - ); + used_len += snprintf(print_buf + used_len, size - used_len, "%s=\"%s\",", tag_key[i], tag_value[i]); } - if(append_pos - print_buf_tags > 0) + if(used_len > 0) { - append_pos--; + used_len--; } - append_pos += snprintf(append_pos, - size - (append_pos - print_buf_tags), - "}\t" - ); + used_len += snprintf(print_buf + used_len, size - used_len, "}\t"); - return append_pos - print_buf_tags; + return used_len; } -static int output_file_format_default_type_gauge(struct fieldstat_instance *instance,int current_metric_cnt,long long interval_ms,char *print_buf, unsigned int size) +static int output_file_format_default_type_gauge(struct fieldstat_instance *instance, int current_metric_cnt,long long interval_ms, char *print_buf, unsigned int size) { int i = 0, j = 0; - //display_manifest_t* p = NULL; - struct metric *metric = NULL; + int used_len = 0; long long value = 0; - //double ratio = 0.0; - //char* pos=print_buf; - char *append_pos = print_buf; - char print_buf_tags[1024]; + struct metric *metric = NULL; + char tags_buf[1024] = {0}; for(i = 0; i < current_metric_cnt; i++) { - //metric = instance->metric[i]; metric = get_metric(instance, i); if(metric == NULL) { @@ -73,61 +58,50 @@ static int output_file_format_default_type_gauge(struct fieldstat_instance *inst value = get_metric_unit_val(metric, FS_CALC_SPEED, 0); continue; } - /* - if(metric->is_ratio==1) - { - ratio=get_stat_ratio(_handle->display[p->numerator_id], _handle->display[p->denominator_id], NULL, - p->output_scaling, p->calc_type); - pos+=snprintf(pos,size-(pos-print_buf),"%s: %10.2e\t",p->name,ratio); - } - */ + value = get_metric_unit_val(metric, FS_CALC_CURRENT, 0); - //value=value * metric->output_scaling * 1000 / interval_ms; - memset(print_buf_tags,0, sizeof(print_buf_tags)); - print_buf_tag_append_position(metric->tag_key,metric->tag_value, metric->n_tag, print_buf_tags, sizeof(print_buf_tags)); - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), + memset(tags_buf, 0, sizeof(tags_buf)); + append_tags_to_print_buf(metric->tag_key, metric->tag_value, metric->n_tag, tags_buf, sizeof(tags_buf)); + used_len += snprintf(print_buf + used_len, + size - used_len, "%s %s: %-10lld\t", - metric->field_name, - print_buf_tags, + metric->field_name, + tags_buf, value ); j++; if(j == STATUS_PER_LINE) { - append_pos += snprintf(append_pos, size - (append_pos - print_buf),"\n"); + used_len += snprintf(print_buf + used_len, size - used_len, "\n"); j=0; } } - if(append_pos - print_buf > 0) + if(used_len > 0) { - if(*(append_pos - 1) == '\n') + if(print_buf[used_len - 1] == '\n') { - append_pos --; + used_len--; } - append_pos += snprintf(append_pos, size - (append_pos-print_buf),"\n%s\n",draw_line); + used_len += snprintf(print_buf + used_len, size - used_len, "\n%s\n", draw_line); } - return append_pos - print_buf; + return used_len; } static int output_file_format_default_type_counter(struct fieldstat_instance *instance, int current_metric_cnt, long long interval_ms,char*print_buf, unsigned int size) { - int i=0,j=0; - //display_manifest_t* p=NULL; - struct metric *metric = NULL; + int i = 0, j = 0; + int used_len = 0; + int metric_cnt = 0; long long value = 0; - //double ratio = 0.0; - char* append_pos = print_buf; + struct metric *metric = NULL; int metric_id[INIT_STAT_FIELD_NUM] = {0}; - int metric_cnt = 0; - char print_buf_tags[1024]; + char tags_buf[1024]; + for(i = 0;i < current_metric_cnt; i++) { - //p=_handle->display[i]; - //metric = instance->metric[i]; metric = get_metric(instance, i); if(metric == NULL) { @@ -143,7 +117,7 @@ static int output_file_format_default_type_counter(struct fieldstat_instance *in } if(metric->is_invisible == 1) { - get_metric_unit_val(metric,FS_CALC_CURRENT,0); + get_metric_unit_val(metric, FS_CALC_CURRENT, 0); continue; } metric_id[metric_cnt] = i; @@ -152,71 +126,62 @@ static int output_file_format_default_type_counter(struct fieldstat_instance *in for(i = 0; i < metric_cnt; i++) { - append_pos += snprintf(append_pos, size - (append_pos - print_buf),"\t"); + used_len += snprintf(print_buf + used_len, size - used_len, "\t"); for(j = 0; j < FIELD_PER_LINE && i+j < metric_cnt; j++) { - //metric = instance->metric[metric_id[i+j]]; metric = get_metric(instance, metric_id[i+j]); - memset(print_buf_tags,0, sizeof(print_buf_tags)); - print_buf_tag_append_position(metric->tag_key,metric->tag_value, metric->n_tag, print_buf_tags, sizeof(print_buf_tags)); - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), + memset(tags_buf, 0, sizeof(tags_buf)); + + append_tags_to_print_buf(metric->tag_key, metric->tag_value, metric->n_tag, tags_buf, sizeof(tags_buf)); + + used_len += snprintf(print_buf + used_len, + size - used_len, "%10s %s\t", metric->field_name, - print_buf_tags + tags_buf ); } - append_pos += snprintf(append_pos, size - (append_pos-print_buf), "\nsum\t"); + used_len += snprintf(print_buf + used_len, size - used_len, "\nsum\t"); for(j=0; j < FIELD_PER_LINE && i+j < metric_cnt; j++) { - //metric = instance->metric[metric_id[i+j]]; metric = get_metric(instance, metric_id[i+j]); value = get_metric_unit_val(metric,FS_CALC_CURRENT, 1); - append_pos += snprintf(append_pos, - sizeof(print_buf) - (append_pos - print_buf), - "%10lld\t", - value - ); + + used_len += snprintf(print_buf + used_len, size - used_len, "%10lld\t", value); } - append_pos += snprintf(append_pos, - sizeof(print_buf) - (append_pos - print_buf), - "\nspeed/s\t" - ); - for(j=0;j<FIELD_PER_LINE&&i+j<metric_cnt;j++) + used_len += snprintf(print_buf + used_len, size - used_len, "\nspeed/s\t"); + + for(j = 0; j<FIELD_PER_LINE&&i+j<metric_cnt; j++) { - //metric = instance->metric[metric_id[i+j]]; metric = get_metric(instance, metric_id[i+j]); value = get_metric_unit_val(metric,FS_CALC_SPEED, 0); - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "%10lld\t", + + used_len += snprintf(print_buf + used_len, + size - used_len, + "%10lld\t", value*1000/interval_ms ); + } i += (j-1); - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "\n" - ); + + used_len += snprintf(print_buf + used_len, size - used_len, "\n"); } - if(append_pos - print_buf > 0) + if(used_len > 0) { - if(*(append_pos - 1)=='\n') + if(print_buf[used_len - 1] == '\n') { - append_pos--; + used_len--; } - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "\n%s\n", - draw_line - ); - } - return append_pos - print_buf; + used_len += snprintf(print_buf + used_len, size - used_len, "\n%s\n", draw_line); + + } + return used_len; } static int output_file_format_default_table(struct fieldstat_instance *instance,int tables_line_cnt[], int current_table_cnt,long long interval_ms,char*print_buf, unsigned int size) @@ -227,20 +192,16 @@ static int output_file_format_default_table(struct fieldstat_instance *instance, int metric_id = 0; struct metric *metric = NULL; long long value = 0; - char* append_pos = print_buf; + int used_len = 0; for(i = 0; i < current_table_cnt; i++) //per table { table = instance->table_metrics[i]; - append_pos += snprintf(append_pos, size - (append_pos - print_buf),"%-20s\t\t",table->name); + used_len += snprintf(print_buf + used_len, size - used_len, "%-20s\t\t", table->name); for(j = 0; j < table->column_cnt; j ++) { //print table column - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "\t%10s", - table->column_name[j] - ); + used_len += snprintf(print_buf + used_len, size - used_len, "\t%10s", table->column_name[j]); } for(j = 0; j < tables_line_cnt[i]; j++) //per line { @@ -250,15 +211,10 @@ static int output_file_format_default_table(struct fieldstat_instance *instance, continue; } //print table line name + tag - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "\n%s ", - line->name - ); - - append_pos += print_buf_tag_append_position(line->tag_key, line->tag_value, line->n_tag, - append_pos, size - (append_pos - print_buf)); + used_len += snprintf(print_buf + used_len, size - used_len, "\n%s ", line->name); + used_len += append_tags_to_print_buf(line->tag_key, line->tag_value, line->n_tag, + print_buf + used_len, size - used_len); for(k = 0; k < table->column_cnt; k++) //per metric { @@ -266,42 +222,35 @@ static int output_file_format_default_table(struct fieldstat_instance *instance, metric_id = line->metric_id_belong_to_line[k]; metric = get_metric(instance,metric_id); value = get_metric_unit_val(metric, FS_CALC_CURRENT, 0); - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "%10lld\t", - value - ); + + used_len += snprintf(print_buf + used_len, size - used_len, "%10lld\t", value); } } - - if(append_pos - print_buf > 0) + if(used_len > 0) { - if(*(append_pos - 1) == '\n') + if(print_buf[used_len - 1] == '\n') { - append_pos--; + used_len--; } - append_pos += snprintf(append_pos, - size - (append_pos - print_buf), - "\n%s\n", - draw_line - ); + used_len += snprintf(print_buf + used_len, size - used_len, "\n%s\n", draw_line); } - } - return append_pos - print_buf; + return used_len; } 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]; + int used_len = 0; + char bin_format[256]; + char str_format[256]; + char buff[32]; const char* extra[]={"MAX", "MIN", "AVG", "STDDEV", "CNT"}; - char buff[STR_LEN_32]; - int i=0; + int i = 0; double * bins = metric->histogram.bins; int bins_num = metric->histogram.bins_num; + if(metric->field_type == FIELD_TYPE_SUMMARY) { snprintf(bin_format, sizeof(bin_format), "%%%d.2lf%%%%", HISTOGRAM_WIDTH-1); @@ -313,98 +262,97 @@ static int output_file_print_hdr_head(struct metric *metric, char *print_buf, si snprintf(str_format, sizeof(str_format), "%%%ds", HISTOGRAM_WIDTH); if(metric->field_type == FIELD_TYPE_SUMMARY) { - pos+=snprintf(pos,size-(pos-print_buf),"%-8s\t","summary"); + used_len += snprintf(print_buf + used_len, size - used_len, "%-8s\t", "summary"); } if(metric->field_type == FILED_TYPE_HISTOGRAM) { - pos+=snprintf(pos,size-(pos-print_buf),"%-8s\t","histogram"); + used_len += snprintf(print_buf + used_len, size - used_len, "%-8s\t", "histogram"); } for(i = 0;i < bins_num; i++) { if(metric->field_type == FIELD_TYPE_SUMMARY) { - snprintf(buff,sizeof(buff),bin_format,bins[i]*100); + snprintf(buff, sizeof(buff), bin_format, bins[i]*100); } if(metric->field_type == FILED_TYPE_HISTOGRAM) { - snprintf(buff,sizeof(buff),bin_format,bins[i]); + snprintf(buff, sizeof(buff), bin_format, bins[i]); } - pos+=snprintf(pos,size-(pos-print_buf),str_format, buff); + used_len += snprintf(print_buf + used_len, size - used_len, str_format, buff); } - for(i=0;(unsigned int)i<sizeof(extra)/sizeof(extra[0]);i++) + for( i = 0; (unsigned int) i < sizeof(extra)/sizeof(extra[0]); i++) { - pos+=snprintf(pos,size-(pos-print_buf),str_format, extra[i]); + used_len += snprintf(print_buf + used_len, size - used_len, str_format, extra[i]); } - pos+=snprintf(pos,size-(pos-print_buf),"\n"); - return pos-print_buf; + used_len += snprintf(print_buf + used_len, size - used_len, "\n"); + + return used_len; } static int output_file_print_hdr_unit(struct metric *metric, char *print_buf, size_t size) { + int used_len = 0; double * bins = metric->histogram.bins; int bins_num = metric->histogram.bins_num; - char* pos=print_buf; - long long value=0; - int i=0; + long long value = 0; + int i = 0; 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); hdr_init(h->lowest_trackable_value, h->highest_trackable_value, h->significant_figures, &(h_tmp)); - if(h->previous_changed!=NULL) hdr_close(h->previous_changed); + + if(h->previous_changed != NULL) + { + hdr_close(h->previous_changed); + } h->previous_changed=atomic_read(&(h->changing)); h_tmp=atomic_set(&(h->changing), h_tmp);// left h_tmp is used to avoid warining [-Wunused-value] hdr_add(h->accumulated, h->previous_changed); - h_out=h->accumulated; //TODO -/* - if(metric->calc_type==FS_CALC_SPEED) - { - h_out=h->previous_changed; - } - else - { - h_out=h->accumulated; - } -*/ - pos+=snprintf(pos,size-(pos-print_buf),"%-10s\t", metric->field_name); + h_out = h->accumulated; //TODO - for(i=0;i<bins_num;i++) + used_len += snprintf(print_buf + used_len, size - used_len, "%-10s\t", metric->field_name); + + for(i = 0; i < bins_num; i++) { if(metric->field_type == FIELD_TYPE_SUMMARY) { - value=(long long)hdr_value_at_percentile(h_out, bins[i]*100); + value = (long long)hdr_value_at_percentile(h_out, bins[i]*100); } if(metric->field_type == FILED_TYPE_HISTOGRAM) { - value= hdr_count_le_value(h_out, (long long)bins[i]); + value = hdr_count_le_value(h_out, (long long)bins[i]); } - pos+=snprintf(pos,size-(pos-print_buf),int_format, value); + used_len += snprintf(print_buf + used_len, size - used_len, int_format, value); } - pos+=snprintf(pos,size-(pos-print_buf),int_format,h_out->total_count==0?0:(long long)hdr_max(h_out)); - pos+=snprintf(pos,size-(pos-print_buf),int_format,h_out->total_count==0?0:(long long)hdr_min(h_out)); - pos+=snprintf(pos,size-(pos-print_buf),double_format,h_out->total_count==0?0:hdr_mean(h_out)); - pos+=snprintf(pos,size-(pos-print_buf),double_format,h_out->total_count==0?0:hdr_stddev(h_out)); - pos+=snprintf(pos,size-(pos-print_buf),int_format,(long long)h_out->total_count); - pos+=snprintf(pos,size-(pos-print_buf),"\n"); + used_len += snprintf(print_buf + used_len, size - used_len, int_format, h_out->total_count==0?0:(long long)hdr_max(h_out)); + used_len += snprintf(print_buf + used_len, size - used_len, int_format, h_out->total_count==0?0:(long long)hdr_min(h_out)); + used_len += snprintf(print_buf + used_len, size - used_len, double_format, h_out->total_count==0?0:hdr_mean(h_out)); + used_len += snprintf(print_buf + used_len, size - used_len, double_format, h_out->total_count==0?0:hdr_stddev(h_out)); + used_len += snprintf(print_buf + used_len, size - used_len, int_format, (long long)h_out->total_count); + + used_len += snprintf(print_buf + used_len, size - used_len,"\n"); - h_tmp=NULL; + h_tmp = NULL; - return pos-print_buf; + return used_len; } static int output_file_format_default_type_histogram_and_summary(struct fieldstat_instance *instance, int current_metric_cnt,long long interval_ms, char*print_buf, size_t size) { - int i = 0, j = 0, metric_num = 0; - char *pos = print_buf; - //display_manifest_t* p=NULL; + int i = 0, j = 0; + int metric_num = 0; + int used_len = 0; + int *printed_metric_id = NULL; struct metric *metric = NULL; struct metric **metric_array = NULL; - int *printed_metric_id = NULL; + if(current_metric_cnt < 1) { @@ -441,7 +389,7 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta { continue; } - pos += output_file_print_hdr_head(metric_array[i], pos, size-(pos-print_buf)); + used_len += output_file_print_hdr_head(metric_array[i], print_buf + used_len, size - used_len); for(j = i; j < metric_num; j ++) { if(metric_array[j] == NULL) @@ -456,16 +404,16 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta { continue; } - pos += output_file_print_hdr_unit(metric_array[j], pos, size-(pos-print_buf)); + used_len += output_file_print_hdr_unit(metric_array[j], print_buf + used_len, size - used_len); printed_metric_id[j] = 1; } - if(pos-print_buf>0) + if(used_len > 0) { - if(*(pos-1)=='\n') + if(print_buf[used_len - 1] == '\n') { - pos--; + used_len--; } - pos+=snprintf(pos,size-(pos-print_buf),"\n%s\n", draw_line); + used_len += snprintf(print_buf + used_len, size - used_len, "\n%s\n", draw_line); } } if(metric_array) @@ -479,21 +427,32 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta printed_metric_id = NULL; } - return pos-print_buf; + return used_len; } int fieldstat_output_file(struct fieldstat_instance *instance,long long interval_ms) { + int used_len = 0; + int current_table_cnt = 0; + int current_metric_cnt = 0; + + time_t current = 0; + char *print_buf = NULL; + + size_t print_buf_sz = 0; + char ctime_buff[32]={0}; int tables_line_cnt[TABLE_MAX_NUM]; - int current_table_cnt = instance->table_num; + + current_table_cnt = instance->table_num; get_current_table_line_cnt(instance, current_table_cnt, tables_line_cnt); - int current_metric_cnt = instance->metric_cnt; - size_t print_buf_sz = current_metric_cnt*1024; - char *print_buf = NULL; - char *append_pos = NULL; - time_t current = 0; - char ctime_buff[STR_LEN_32]={0}; + current_metric_cnt = instance->metric_cnt; + print_buf_sz = current_metric_cnt*1024; + + if(current_metric_cnt == 0) + { + return 0; + } if(instance->local_output_fp == NULL) { @@ -511,18 +470,14 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval time(¤t); ctime_r(¤t, ctime_buff); print_buf = (char*)calloc(print_buf_sz, sizeof(char)); - append_pos = print_buf; - append_pos += snprintf(append_pos, print_buf_sz - (append_pos - print_buf), "%s%s", draw_boundary, ctime_buff); - append_pos --;//jump '\n' generate by ctime() - append_pos += snprintf(append_pos, print_buf_sz - (append_pos - print_buf),"%s\n",draw_boundary); - - //pthread_mutex_lock(&(_handle->reg_lock)); //TODO - append_pos += output_file_format_default_type_gauge(instance, current_metric_cnt, interval_ms, append_pos, print_buf_sz - (append_pos - print_buf)); - append_pos += output_file_format_default_type_counter(instance, current_metric_cnt, interval_ms, append_pos, print_buf_sz - (append_pos - print_buf)); - append_pos += output_file_format_default_table(instance, tables_line_cnt, current_table_cnt, interval_ms, append_pos, print_buf_sz - (append_pos - print_buf)); - append_pos += output_file_format_default_type_histogram_and_summary(instance,current_metric_cnt,interval_ms, append_pos, print_buf_sz - (append_pos - print_buf)); - //TODO output table,output histogram,output summary - //pthread_mutex_unlock(&(_handle->reg_lock));//TODO + used_len += snprintf(print_buf + used_len, print_buf_sz - used_len, "%s%s", draw_boundary, ctime_buff); + used_len --;//jump '\n' generate by ctime() + used_len += snprintf(print_buf + used_len, print_buf_sz - used_len, "%s\n", draw_boundary); + + used_len += output_file_format_default_type_gauge(instance, current_metric_cnt, interval_ms, print_buf + used_len, print_buf_sz - used_len); + used_len += output_file_format_default_type_counter(instance, current_metric_cnt, interval_ms, print_buf + used_len, print_buf_sz - used_len); + used_len += output_file_format_default_table(instance, tables_line_cnt, current_table_cnt, interval_ms, print_buf + used_len, print_buf_sz - used_len); + used_len += output_file_format_default_type_histogram_and_summary(instance, current_metric_cnt,interval_ms, print_buf + used_len, print_buf_sz - used_len); } if(!strcmp(instance->local_output_format, "json")) @@ -533,7 +488,7 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval fseek(instance->local_output_fp, 0, SEEK_SET); if(print_buf) { - fwrite(print_buf, append_pos - print_buf, 1,instance->local_output_fp); + fwrite(print_buf, used_len, 1, instance->local_output_fp); } fflush(instance->local_output_fp); diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp index 9a9bfa9..de42873 100644 --- a/src/line_protocol_output.cpp +++ b/src/line_protocol_output.cpp @@ -1,24 +1,5 @@ #include "fieldstat_internal.h" -/* -static void flush_line_protocol_metric_send_buff(int send_socket, unsigned int server_ip, unsigned short server_port, const char *send_buff, int *send_buff_offset) -{ - int offset = *send_buff_offset; - if(offset == 0) - { - return; - } - - if(server_ip > 0 && server_port > 0) - { - send_udp(send_socket, server_ip, server_port, send_buff, offset); - } - - *send_buff_offset = 0; - memset(send_buff, 0, UDP_PAYLOAD_SIZE); -} -*/ - static void flush_line_protocol_metric_send_buff(struct fieldstat_instance *instance) { if(instance->line_protocol_send_buff_offset == 0) @@ -58,20 +39,15 @@ static void append_line_protocol_metric_to_send_buff(struct fieldstat_instance * return; } -static int output_line_protocol_tag_set_buf(char *tag_key[], char *tag_value[], int n_tag, char *tag_set_buff, unsigned int size) +static int output_line_protocol_tag_set_buf(char *tag_key[], char *tag_value[], int n_tag, char *tag_set_buf, unsigned int size) { int i = 0; - char *tag_pos = tag_set_buff; + int used_len = 0; for(i = 0; i < n_tag; i++) { - tag_pos += snprintf(tag_pos, - size - (tag_pos - tag_set_buff), - ",%s=%s", - tag_key[i], - tag_value[i] - ); + used_len += snprintf(tag_set_buf + used_len, size - used_len, ",%s=%s", tag_key[i], tag_value[i]); } - return tag_pos - tag_set_buff; + return used_len; } @@ -84,8 +60,8 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t char field_set_buff[UDP_PAYLOAD_SIZE]; char tag_set_buff[UDP_PAYLOAD_SIZE]; - char *tag_pos = tag_set_buff; - char *field_pos = field_set_buff; + int tags_used_len = 0; + int fields_used_len = 0; struct table_metric *table = NULL; struct table_line *line = NULL; @@ -102,8 +78,8 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t continue; } line_value_is_not_zero = 0; - tag_pos = tag_set_buff; - field_pos = field_set_buff; + tags_used_len = 0; + fields_used_len = 0; memset(field_set_buff, 0, sizeof(field_set_buff)); memset(tag_set_buff, 0, sizeof(tag_set_buff)); line_value_is_not_zero = 0; @@ -118,8 +94,8 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t { line_value_is_not_zero = 1; } - field_pos += snprintf(field_pos, - sizeof(field_set_buff) - (field_pos - field_set_buff), + fields_used_len += snprintf(field_set_buff + fields_used_len, + sizeof(field_set_buff) - fields_used_len, "%s=%lld,", metric->table->column_name[metric->table_column_id], value @@ -130,16 +106,16 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t { continue; } - tag_pos += snprintf(tag_pos, - sizeof(tag_set_buff) - (tag_pos - tag_set_buff), - ",app_name=%s,table_name=%s", - instance->name, - table->name - ); + tags_used_len += snprintf(tag_set_buff + tags_used_len, + sizeof(tag_set_buff) - tags_used_len, + ",app_name=%s,table_name=%s", + instance->name, + table->name + ); - tag_pos += output_line_protocol_tag_set_buf(line->tag_key, line->tag_value, line->n_tag, tag_pos, - sizeof(tag_set_buff) - (tag_pos - tag_set_buff)); - + tags_used_len += output_line_protocol_tag_set_buf(line->tag_key, line->tag_value, line->n_tag, + tag_set_buff + tags_used_len, + sizeof(tag_set_buff) - tags_used_len); // measurement,tag_set field_set append_line_protocol_metric_to_send_buff(instance, metric->field_name, tag_set_buff, field_set_buff); @@ -151,14 +127,12 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t int line_protocol_output(struct fieldstat_instance *instance) { struct metric *metric = NULL; - long long value=0; - int i=0; + long long value = 0; + int i = 0; char field_set_buff[UDP_PAYLOAD_SIZE]; char tag_set_buff[UDP_PAYLOAD_SIZE]; + int tags_used_len = 0; - memset(field_set_buff, 0, sizeof(field_set_buff)); - memset(tag_set_buff, 0, sizeof(tag_set_buff)); - char *tag_pos = tag_set_buff; //print current time instance start int tables_line_cnt[TABLE_MAX_NUM]; int current_table_cnt = instance->table_num; @@ -182,7 +156,9 @@ int line_protocol_output(struct fieldstat_instance *instance) { continue; } - + memset(field_set_buff, 0, sizeof(field_set_buff)); + memset(tag_set_buff, 0, sizeof(tag_set_buff)); + tags_used_len = 0; switch(metric->field_type) { case FIELD_TYPE_GAUGE: @@ -190,14 +166,13 @@ int line_protocol_output(struct fieldstat_instance *instance) if(value != 0) { snprintf(field_set_buff, UDP_PAYLOAD_SIZE, "%s=%lld", metric->field_name, value); - tag_pos += snprintf(tag_pos, - sizeof(tag_set_buff) - (tag_pos - tag_set_buff), + tags_used_len += snprintf(tag_set_buff + tags_used_len, + sizeof(tag_set_buff) - tags_used_len, ",app_name=%s", instance->name ); - output_line_protocol_tag_set_buf(metric->tag_key, metric->tag_value, metric->n_tag, tag_pos, sizeof(tag_set_buff) - (tag_pos - tag_set_buff)); + output_line_protocol_tag_set_buf(metric->tag_key, metric->tag_value, metric->n_tag, tag_set_buff + tags_used_len, sizeof(tag_set_buff) - tags_used_len); append_line_protocol_metric_to_send_buff(instance, metric->field_name, tag_set_buff, field_set_buff); - tag_pos = tag_set_buff; } break; @@ -206,14 +181,13 @@ int line_protocol_output(struct fieldstat_instance *instance) if(value != 0) { snprintf(field_set_buff, UDP_PAYLOAD_SIZE, "%s=%lld", metric->field_name, value); - tag_pos += snprintf(tag_pos, - sizeof(tag_set_buff) - (tag_pos - tag_set_buff), + tags_used_len += snprintf(tag_set_buff + tags_used_len, + sizeof(tag_set_buff) - tags_used_len, ",app_name=%s", instance->name ); - output_line_protocol_tag_set_buf(metric->tag_key, metric->tag_value, metric->n_tag, tag_pos, sizeof(tag_set_buff) - (tag_pos - tag_set_buff)); + output_line_protocol_tag_set_buf(metric->tag_key, metric->tag_value, metric->n_tag, tag_set_buff + tags_used_len, sizeof(tag_set_buff) - tags_used_len); append_line_protocol_metric_to_send_buff(instance, metric->field_name, tag_set_buff, field_set_buff); - tag_pos = tag_set_buff; } break; default: @@ -222,7 +196,7 @@ int line_protocol_output(struct fieldstat_instance *instance) } } - output_line_protocol_table(instance,tables_line_cnt,current_table_cnt); + output_line_protocol_table(instance, tables_line_cnt, current_table_cnt); flush_line_protocol_metric_send_buff(instance); return 0; diff --git a/src/prometheus_output.cpp b/src/prometheus_output.cpp index 13a0355..2b605e8 100644 --- a/src/prometheus_output.cpp +++ b/src/prometheus_output.cpp @@ -93,7 +93,7 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom int i = 0; int payload_len = 0; char *payload = NULL; - char *payload_append_position = NULL; + int used_len = 0; struct fieldstat_instance **fs_instance = NULL; struct http_response_s* response = NULL; @@ -101,15 +101,13 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom if(prometheus_output->fs_instance_cnt > 0) { - payload_len = prometheus_output->fs_instance_cnt * 128; //TODO using marco, len? - printf("payload_len =%d\n, n_instance =%d\n", payload_len, prometheus_output->fs_instance_cnt); + payload_len = prometheus_output->fs_instance_cnt * 128; payload = (char *)calloc(payload_len, sizeof(char)); - payload_append_position = payload; - payload_append_position += snprintf(payload_append_position, payload_len - (payload_append_position - payload),"url_path:\n\t%s\n", prometheus_output->url_path); + used_len += snprintf(payload + used_len, payload_len - used_len, "url_path:\n\t%s\n", prometheus_output->url_path); for(i = 0; i < prometheus_output->fs_instance_cnt; i++) { - payload_append_position += snprintf(payload_append_position, payload_len - (payload_append_position - payload),"\t/%s\n", fs_instance[i]->name); + used_len += snprintf(payload + used_len, payload_len - used_len, "\t/%s\n", fs_instance[i]->name); } } else @@ -128,35 +126,33 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom return; } -static int prometheus_output_read_metric_tags(struct metric *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_buf, unsigned int size) { int i = 0;//used_len = 0; char unescape[STR_LEN_256] = {0}; - char *append_pos = tags_buff; - - //used_len += snprint(tags_buff, size - used_len, "app_name=\"%s\"", instance_name); + int used_len = 0; - append_pos += snprintf(append_pos, size - (append_pos - tags_buff), "app_name=\"%s\"", instance_name); + used_len += snprintf(tags_buf + used_len, size - used_len, "app_name=\"%s\"", instance_name); if(metric->table) { - append_pos += snprintf(append_pos, size - (append_pos - tags_buff), ",line_name=\"%s\"", metric->field_name); + used_len += snprintf(tags_buf + used_len, size - used_len, ",line_name=\"%s\"", metric->field_name); } for(i = 0; i < (int)metric->n_tag; i++) { memset(unescape, 0, sizeof(unescape)); str_unescape(metric->tag_key[i], unescape, sizeof(unescape)); - append_pos += snprintf(append_pos, size - (append_pos - tags_buff), ",%s=\"%s\"", unescape, metric->tag_value[i]); + used_len += snprintf(tags_buf, size - used_len, ",%s=\"%s\"", unescape, metric->tag_value[i]); } - return append_pos - tags_buff; + return used_len; } -static int prometheus_output_read_metric_name(struct metric *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_buf, unsigned int size) { char unescape[256] = {0}; - char *append_pos = name_buff; + int used_len = 0; if(metric->table) { str_unescape(metric->table->column_name[metric->table_column_id], unescape, sizeof(unescape)); @@ -165,9 +161,9 @@ static int prometheus_output_read_metric_name(struct metric *metric, char *insta { str_unescape(metric->field_name, unescape, sizeof(unescape)); } - append_pos += snprintf(append_pos, size - (append_pos - name_buff), "%s_%s", instance_app_name, unescape); + used_len += snprintf(name_buf, size - used_len, "%s_%s", instance_app_name, unescape); - return append_pos - name_buff; + return used_len; } |
