summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfumingwei <[email protected]>2023-03-17 23:53:11 +0800
committerfumingwei <[email protected]>2023-03-22 20:15:51 +0800
commit1b3d40eaa3db4867d71b0908f6121f1f21b8b448 (patch)
tree99f4831310ffc66d373151a4ef6cbc7ef8825e3e /src
parentea4c2b9c11ef8a02f745b514f4a54f07512a7e8b (diff)
feature:1.新增dynamic metric接口测试用例. 2.新增instance free接口.
Diffstat (limited to 'src')
-rw-r--r--src/fieldstat.cpp213
-rw-r--r--src/fieldstat_dynamic.cpp168
-rw-r--r--src/fieldstat_internal.h80
-rw-r--r--src/file_output.cpp20
-rw-r--r--src/line_protocol_output.cpp21
-rw-r--r--src/prometheus_output.cpp64
6 files changed, 486 insertions, 80 deletions
diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp
index b67e8a5..3a8747c 100644
--- a/src/fieldstat.cpp
+++ b/src/fieldstat.cpp
@@ -48,6 +48,45 @@ int is_valid_field_name(const char* name)
return 1;
}
+int is_valid_tag_value(const char* value)
+{
+ const char* reserverd="|:\n\r \t<>[]#!@";
+ unsigned int i=0,j=0;
+ for(i=0;i<strlen(value);i++)
+ {
+ for(j=0;j<strlen(reserverd);j++)
+ if(value[i]==reserverd[j])
+ {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
+int is_valid_tags(const struct fieldstat_tag *tags, size_t n_tags)
+{
+ int i = 0;
+ struct fieldstat_tag *tag = NULL;
+
+ for(i = 0; i < (int)n_tags; i++)
+ {
+ tag = (struct fieldstat_tag *)&tags[i];
+ if (0 == is_valid_field_name(tag->key))
+ {
+ return 0;
+ }
+
+ if(tag->value_type == 2)
+ {
+ if (0 == is_valid_tag_value(tag->value_str))
+ {
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
void get_current_table_line_cnt(struct fieldstat_instance *instance, int n_table, int *tables_line_cnt)
{
@@ -145,6 +184,11 @@ void metric_free(struct metric *metric)
}
metric->n_tag = 0;
+ if(metric->table)
+ {
+ metric->table = NULL;
+ }
+
free(metric);
return;
@@ -353,6 +397,11 @@ int fieldstat_register(struct fieldstat_instance *instance, enum field_type type
return -1;
}
+ if(0 == is_valid_tags(tags, n_tag))
+ {
+ return -1;
+ }
+
metric_id = atomic_inc(&instance->metric_cnt) - 1;
metric_slot = read_metric_slot(instance, metric_id);
metric = metric_new(type, field_name, tags, n_tag);
@@ -477,7 +526,7 @@ void fieldstat_instance_start(struct fieldstat_instance *instance)
clock_gettime(CLOCK_MONOTONIC,&(instance->last_output_time));
if(instance->background_thread_disable == 0)
{
- pthread_create(&(instance->cfg_mon_t), NULL, fieldstat_thread_schema_output, (void*)instance);
+ pthread_create(&(instance->background_thread), NULL, fieldstat_thread_schema_output, (void*)instance);
}
//append instance to prometheus output
}
@@ -490,6 +539,10 @@ struct fieldstat_instance * fieldstat_instance_new(const char *name)
{
return NULL;
}
+ if(!is_valid_field_name(name))
+ {
+ return NULL;
+ }
instance = (struct fieldstat_instance *)calloc(sizeof(struct fieldstat_instance),1);
strcpy(instance->name, name);
@@ -500,6 +553,65 @@ struct fieldstat_instance * fieldstat_instance_new(const char *name)
return instance;
}
+void fieldstat_instance_free(struct fieldstat_instance *instance)
+{
+ int i = 0;
+ void *pthread_ret;
+ struct metric *metric = NULL;
+ if(instance == NULL)
+ {
+ return;
+ }
+
+ if(instance->background_thread_is_created == 1)
+ {
+ pthread_cancel(instance->background_thread);
+ pthread_join(instance->background_thread, &pthread_ret);
+ instance->background_thread_is_created = 0;
+ instance->running = 0;
+ }
+
+ if(instance->local_output_fp)
+ {
+ fclose(instance->local_output_fp);
+ instance->local_output_fp = NULL;
+ instance->local_output_enable = 0;
+ }
+
+ if(instance->line_protocol_socket != -1)
+ {
+ close(instance->line_protocol_socket);
+ instance->line_protocol_socket = -1;
+ }
+
+ for(i = 0; i < instance->metric_cnt; i++)
+ {
+ metric = get_metric(instance, i);
+ metric_free(metric);
+ }
+
+ for(i = 0; i < instance->table_num; i++)
+ {
+ table_metric_free(instance->table_metrics[i]);
+ instance->table_metrics[i] = NULL;
+ }
+
+ if(instance->metric_block_list != NULL)
+ {
+ for(i = 0; i < BLOCK_LIST_SIZE; i++)
+ {
+ if(instance->metric_block_list[i] != NULL)
+ {
+ free(instance->metric_block_list[i]);
+ instance->metric_block_list[i] = NULL;
+ }
+ }
+ }
+ free(instance);
+ return;
+}
+
+
struct table_metric* table_metric_new(const char *name, const char *column_name[], enum field_type column_type[], size_t n_column)
{
int i = 0;
@@ -516,12 +628,70 @@ struct table_metric* table_metric_new(const char *name, const char *column_name[
return table_metric;
}
+void table_metric_free(struct table_metric *table)
+{
+ int i = 0;
+ struct table_line *table_line = NULL;
+
+ if(table == NULL)
+ {
+ return;
+ }
+ free(table->name);
+ table->name = NULL;
+ for(i = 0; i < (int)table->column_cnt; i++)
+ {
+ if(table->column_name[i] == NULL)
+ {
+ continue;
+ }
+ free(table->column_name[i]);
+ table->column_name[i] = NULL;
+ }
+
+ for(i = 0; i < table->line_cnt; i++)
+ {
+ table_line = read_table_line(table, i);
+ table_line_free(table_line);
+ }
+
+ if(table->line_block != NULL)
+ {
+ for(i = 0; i < BLOCK_LIST_SIZE; i++)
+ {
+ if(table->line_block[i] == NULL)
+ {
+ continue;
+ }
+ free(table->line_block[i]);
+ table->line_block[i] = NULL;
+ }
+ }
+ table->column_cnt = 0;
+ free(table);
+ return;
+}
+
+
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;
struct table_metric *table_metric = NULL;
+ if(!is_valid_field_name(table_name))
+ {
+ return -1;
+ }
+
+ for(int i = 0; i < (int)n_column; i++)
+ {
+ if(!is_valid_field_name(column_name[i]))
+ {
+ return -1;
+ }
+ }
+
if(n_column <= 0 || n_column > TABLE_COLUMN_SIZE)
{
return -1;
@@ -539,7 +709,7 @@ struct table_metric* table_metric_new(const char *name, const char *column_name[
return table_id;
}
-static struct table_line ** read_table_line_slot(struct table_metric *table, int line_id)
+struct table_line ** read_table_line_slot(struct table_metric *table, int line_id)
{
int block_index = 0;
int in_block_index = 0;
@@ -561,7 +731,7 @@ 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 struct fieldstat_tag tags[],size_t n_tag)
+struct table_line *table_line_new(const char *name, const struct fieldstat_tag tags[],size_t n_tag)
{
struct table_line *table_line = (struct table_line *)calloc(sizeof(struct table_line), 1);
@@ -574,6 +744,32 @@ static struct table_line *table_line_new(const char *name, const struct fieldsta
}
+void table_line_free(struct table_line *table_line)
+{
+ if(table_line == NULL)
+ {
+ return;
+ }
+
+ free(table_line->name);
+
+ table_line->name = NULL;
+
+ for(int i = 0; i < (int)table_line->n_tag; i++)
+ {
+ free(table_line->tag_key[i]);
+ table_line->tag_key[i] = NULL;
+
+ free(table_line->tag_value[i]);
+ table_line->tag_value[i] = NULL;
+ }
+ table_line->n_tag = 0;
+
+ free(table_line);
+}
+
+
+
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 metric_id = 0;
@@ -595,6 +791,11 @@ int fieldstat_register_table_row(struct fieldstat_instance * instance, int table
return -1;
}
+ if(0 == is_valid_tags(tags, n_tag))
+ {
+ 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);
@@ -607,11 +808,15 @@ int fieldstat_register_table_row(struct fieldstat_instance * instance, int table
table_line->metric_id_belong_to_line[i] = metric_id;
metric = get_metric(instance, metric_id);
+
+ metric->table = table;
+ metric->table_column_id = i;
+/*
metric->table_id = table_id;
metric->table_column_name = __str_dup(table->column_name[i]);
metric->table_name = __str_dup(table->name);
metric->belong_to_table = 1;
-
+*/
output_metric_ids[i] = metric_id;
}
*line_slot = table_line;
diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp
index b9f163a..72f3155 100644
--- a/src/fieldstat_dynamic.cpp
+++ b/src/fieldstat_dynamic.cpp
@@ -8,6 +8,11 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n
{
return NULL;
}
+
+ if(!is_valid_field_name(name))
+ {
+ return NULL;
+ }
instance = (struct fieldstat_dynamic_instance *)calloc(sizeof(struct fieldstat_dynamic_instance), 1);
@@ -22,9 +27,81 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n
return instance;
}
+void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance)
+{
+ int i = 0;
+ struct dynamic_metric **head = NULL;
+ struct dynamic_metric *dyn_metric, *tmp_dyn_metric;
+ void *pthread_ret;
+
+ if(instance == NULL)
+ {
+ return;
+ }
+
+ if(instance->background_thread_is_created == 1)
+ {
+ pthread_cancel(instance->background_thread);
+ pthread_join(instance->background_thread, &pthread_ret);
+ instance->background_thread_is_created = 0;
+ instance->running = 0;
+ }
+
+ if(instance->line_protocol_socket != -1)
+ {
+ close(instance->line_protocol_socket);
+ instance->line_protocol_socket = -1;
+ }
+
+ for(i = 0; i < instance->n_thread; i++)
+ {
+ head = &instance->n_thread_dynamic_metric[i];
+ HASH_ITER(hh, *head, dyn_metric, tmp_dyn_metric)
+ {
+ HASH_DEL(*head, dyn_metric);
+ if(dyn_metric->metrics)
+ {
+ int n_loop = 0;
+ struct metric *metric = dyn_metric->metrics[0];
+ if(metric->table)
+ {
+ n_loop = metric->table->column_cnt;
+ }
+ else
+ {
+ n_loop = 1;
+ }
+ for(int j = 0; j < n_loop; j ++)
+ {
+ metric_free(dyn_metric->metrics[j]);
+ dyn_metric->metrics[j] = NULL;
+ }
+ free(dyn_metric->metrics);
+ dyn_metric->metrics = NULL;
+ }
+ free(dyn_metric);
+ }
+ instance->n_thread_dynamic_metric[i] = NULL;
+ }
+
+ for(i = 0; i < instance->table_num; i++)
+ {
+ table_metric_free(instance->table_metrics[i]);
+ instance->table_metrics[i] = NULL;
+ }
+
+ free(instance->n_thread_dynamic_metric);
+ instance->n_thread_dynamic_metric = NULL;
+ free(instance);
+
+ return;
+}
+
+
+
int fieldstat_dynamic_set_line_protocol_server(struct fieldstat_dynamic_instance *instance, const char *ip, unsigned short port)
{
- if(instance->running == 1)
+ if(instance == NULL || instance->running == 1)
{
return -1;
}
@@ -85,6 +162,7 @@ void fieldstat_dynamic_passive_output(struct fieldstat_dynamic_instance *instanc
}
if(ret == -1)
{
+ printf("Passive return: output ret -1\n");
return;
}
memcpy(&(instance->last_output_time),&this_output_time, sizeof(this_output_time));
@@ -107,7 +185,8 @@ void fieldstat_dynamic_instance_start(struct fieldstat_dynamic_instance *instanc
clock_gettime(CLOCK_MONOTONIC, &(instance->last_output_time));
if(instance->background_thread_disable == 0)
{
- pthread_create(&(instance->cfg_mon_t), NULL, fieldstat_dynamic_thread_schema_output, (void*)instance);
+ pthread_create(&(instance->background_thread), NULL, fieldstat_dynamic_thread_schema_output, (void*)instance);
+ instance->background_thread_is_created = 1;
}
}
@@ -118,6 +197,19 @@ int fieldstat_register_dynamic_table(struct fieldstat_dynamic_instance *instance
struct table_metric *table_metric = NULL;
+ if(!is_valid_field_name(table_name))
+ {
+ return -1;
+ }
+
+ for(i = 0; i < (int)n_column; i++)
+ {
+ if(!is_valid_field_name(column_name[i]))
+ {
+ return -1;
+ }
+ }
+
if(n_column <= 0 || n_column > TABLE_COLUMN_SIZE)
{
return -1;
@@ -226,11 +318,15 @@ static struct metric * create_dynamic_table_metric(struct fieldstat_dynamic_inst
assert(0);
break;
}
+ metric->table = table;
+ metric->table_column_id = i;
+/*
metric->table_id = table_id;
metric->table_column_name = __str_dup(table->column_name[i]);
metric->table_name = __str_dup(table->name);
metric->belong_to_table = 1;
-
+ metric->table_column_cnt = table->column_cnt;
+*/
value->metrics[i] = metric;
}
@@ -297,21 +393,48 @@ static int fieldstat_dynamic_metric_value_operate(struct fieldstat_dynamic_insta
int fieldstat_dynamic_metric_value_incrby(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_metric_value_operate(instance, FS_OP_ADD, type, field_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(field_name))
+ {
+ return -1;
+ }
+
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+ ret = fieldstat_dynamic_metric_value_operate(instance, FS_OP_ADD, type, field_name, value, tags, n_tags, thread_id);
return ret;
}
int fieldstat_dynamic_metric_value_set(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_metric_value_operate(instance, FS_OP_SET, type, field_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(field_name))
+ {
+ return -1;
+ }
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+
+ ret = fieldstat_dynamic_metric_value_operate(instance, FS_OP_SET, type, field_name, value, tags, n_tags, thread_id);
return ret;
}
int fieldstat_dynamic_metric_value_decrby(struct fieldstat_dynamic_instance *instance, enum field_type type, const char *field_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_metric_value_operate(instance, FS_OP_SUB, type, field_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(field_name))
+ {
+ return -1;
+ }
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+
+ ret = fieldstat_dynamic_metric_value_operate(instance, FS_OP_SUB, type, field_name, value, tags, n_tags, thread_id);
return ret;
}
@@ -336,21 +459,48 @@ int fieldstat_dynamic_table_metric_value_operate(struct fieldstat_dynamic_instan
int fieldstat_dynamic_table_metric_value_incrby(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_ADD, table_id, column_id, row_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(row_name))
+ {
+ return -1;
+ }
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+
+ ret = fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_ADD, table_id, column_id, row_name, value, tags, n_tags, thread_id);
return ret;
}
int fieldstat_dynamic_table_metric_value_set(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_SET, table_id, column_id, row_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(row_name))
+ {
+ return -1;
+ }
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+
+ ret = fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_SET, table_id, column_id, row_name, value, tags, n_tags, thread_id);
return ret;
}
int fieldstat_dynamic_table_metric_value_decrby(struct fieldstat_dynamic_instance *instance, int table_id, unsigned int column_id, const char *row_name, long long value, const struct fieldstat_tag tags[], size_t n_tags, int thread_id)
{
int ret = 0;
- fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_SUB, table_id, column_id, row_name, value, tags, n_tags, thread_id);
+ if(!is_valid_field_name(row_name))
+ {
+ return -1;
+ }
+ if(0 == is_valid_tags(tags, n_tags))
+ {
+ return -1;
+ }
+
+ ret = fieldstat_dynamic_table_metric_value_operate( instance, FS_OP_SUB, table_id, column_id, row_name, value, tags, n_tags, thread_id);
return ret;
}
diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h
index 1e8fc34..97171e5 100644
--- a/src/fieldstat_internal.h
+++ b/src/fieldstat_internal.h
@@ -100,6 +100,27 @@ struct histogram_t
double *bins;
};
+struct table_line
+{
+ char *name;
+ size_t n_tag;
+ char *tag_key[N_TAG_MAX];
+ char *tag_value[N_TAG_MAX];
+
+ int metric_id_belong_to_line[TABLE_COLUMN_SIZE];
+};
+
+struct table_metric
+{
+ char *name;
+ char *column_name[TABLE_COLUMN_SIZE];
+ enum field_type column_type[TABLE_COLUMN_SIZE];
+ int column_cnt;
+
+ int line_cnt;
+ struct table_line **line_block[BLOCK_LIST_SIZE];
+};
+
struct metric
{
@@ -115,13 +136,15 @@ struct metric
int not_send_to_server;
int numerator_id;
int denominator_id;
-
+/*
int belong_to_table;
int table_id;
- size_t line_seq;
- int table_column_id;
char *table_column_name;
char *table_name;
+ int table_column_cnt;
+*/
+ struct table_metric *table;
+ int table_column_id;
union
{
@@ -131,28 +154,6 @@ struct metric
};
};
-struct table_line
-{
- char *name;
- size_t n_tag;
- char *tag_key[N_TAG_MAX];
- char *tag_value[N_TAG_MAX];
-
- int metric_id_belong_to_line[TABLE_COLUMN_SIZE];
-};
-
-struct table_metric
-{
- char *name;
- char *column_name[TABLE_COLUMN_SIZE];
- enum field_type column_type[TABLE_COLUMN_SIZE];
- int column_cnt;
-
- int line_cnt;
- struct table_line **line_block[BLOCK_LIST_SIZE];
-};
-
-
struct fieldstat_instance
{
char name[INSTANCE_NAME_LEN];
@@ -161,6 +162,7 @@ struct fieldstat_instance
unsigned int statsd_server_ip;
unsigned short statsd_server_port;
int statsd_output_enable;
+ int line_protocol_socket;
//char line_protocol_server_str_ip[LEN_IP_MAX];
unsigned int line_protocol_server_ip;
@@ -170,8 +172,12 @@ struct fieldstat_instance
char local_output_filename[LEN_PATH_MAX];
char local_output_format[LEN_FORMAT_MAX];
int local_output_enable;
+ FILE* local_output_fp;
int background_thread_disable;
+ pthread_t background_thread;
+ int background_thread_is_created;
+
int output_interval_ms;
int running;
@@ -182,10 +188,7 @@ struct fieldstat_instance
struct table_metric *table_metrics[TABLE_MAX_NUM];
- int *index_table[TABLE_MAX_NUM][TABLE_COLUMN_SIZE];
-
int table_num;
- int line_seq;
int prometheus_output_enable;
@@ -194,14 +197,9 @@ struct fieldstat_instance
struct timespec last_output_time;
- pthread_t cfg_mon_t;
char line_protocol_send_buff[UDP_PAYLOAD_SIZE];
size_t line_protocol_send_buff_offset;
- int line_protocol_socket;
- pthread_mutex_t output_lock;
-
- FILE* fp;
};
struct prometheus_endpoint_instance
@@ -209,6 +207,7 @@ struct prometheus_endpoint_instance
unsigned short port;
char *url_path;
pthread_t tid;
+ int thread_created;
int running;
struct http_server_s *server_handle;
int fs_instance_cnt;
@@ -238,7 +237,8 @@ struct fieldstat_dynamic_instance
int table_num;
struct timespec last_output_time;
- pthread_t cfg_mon_t;
+ pthread_t background_thread;
+ int background_thread_is_created;
char line_protocol_send_buff[UDP_PAYLOAD_SIZE];
size_t line_protocol_send_buff_offset;
int line_protocol_socket;
@@ -255,8 +255,10 @@ long long hdr_count_le_value(const struct hdr_histogram* h, long long value);
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);
+int is_valid_tags(const struct fieldstat_tag *tags, size_t n_tags);
struct metric ** read_metric_slot(struct fieldstat_instance *instance, int metric_id);
struct metric * metric_new(enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag);
+void metric_free(struct metric *metric);
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);
@@ -266,4 +268,12 @@ int startup_udp();
void metric_value_operate(struct metric *metric, enum field_op op, long long value);
struct table_metric* table_metric_new(const char *name, const char *column_name[], enum field_type column_type[], size_t n_column);
-int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *instance); \ No newline at end of file
+void table_metric_free(struct table_metric *table);
+
+struct table_line *table_line_new(const char *name, const struct fieldstat_tag tags[],size_t n_tag);
+void table_line_free(struct table_line *table_line);
+int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *instance);
+
+struct table_line ** read_table_line_slot(struct table_metric *table, int line_id);
+
+void fieldstat_global_disable_prometheus_endpoint(); \ No newline at end of file
diff --git a/src/file_output.cpp b/src/file_output.cpp
index 3c4c921..40d28a1 100644
--- a/src/file_output.cpp
+++ b/src/file_output.cpp
@@ -64,7 +64,7 @@ static int output_file_format_default_type_gauge(struct fieldstat_instance *inst
{
continue;
}
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
continue;
}
@@ -137,7 +137,7 @@ static int output_file_format_default_type_counter(struct fieldstat_instance *in
{
continue;
}
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
continue;
}
@@ -403,8 +403,8 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
char *pos = print_buf;
//display_manifest_t* p=NULL;
struct metric *metric = NULL;
- struct metric *metric_array[current_metric_cnt] = {NULL};
- int metric_is_print[current_metric_cnt] = {0};
+ struct metric *metric_array[current_metric_cnt];
+ int metric_is_print[current_metric_cnt];
if(instance->histogram_cnt == 0
&& instance->summary_cnt == 0)
@@ -476,10 +476,10 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval
time_t current = 0;
char ctime_buff[STR_LEN_32]={0};
- if(instance->fp == NULL)
+ if(instance->local_output_fp == NULL)
{
- instance->fp = fopen(instance->local_output_filename, "w");
- if(instance->fp == NULL)
+ instance->local_output_fp = fopen(instance->local_output_filename, "w");
+ if(instance->local_output_fp == NULL)
{
printf("Field Stat: open %s failed.\n",instance->local_output_filename);
assert(0);
@@ -511,10 +511,10 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval
//TODO from json output
}
- fseek(instance->fp,0,SEEK_SET);
- fwrite(print_buf,append_pos - print_buf,1,instance->fp);
+ fseek(instance->local_output_fp, 0, SEEK_SET);
+ fwrite(print_buf,append_pos - print_buf,1,instance->local_output_fp);
- fflush(instance->fp);
+ fflush(instance->local_output_fp);
if(print_buf)
{
diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp
index 3f4e86c..f1b2c16 100644
--- a/src/line_protocol_output.cpp
+++ b/src/line_protocol_output.cpp
@@ -121,7 +121,7 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t
field_pos += snprintf(field_pos,
sizeof(field_set_buff) - (field_set_buff - field_set_buff),
"%s=%lld,",
- metric->table_column_name,
+ metric->table->column_name[metric->table_column_id],
value
);
@@ -178,7 +178,7 @@ int line_protocol_output(struct fieldstat_instance *instance)
continue;
}
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
continue;
}
@@ -258,7 +258,7 @@ static void append_line_protocol_dynamic_row(struct fieldstat_dynamic_instance *
{
flush_line_protocol_dynamic_metric(instance);
}
- printf("Line_protocol metric: %s%s %s\n",measurement,tag_set,field_set);
+ //printf("Line_protocol metric: %s:%s %s\n",measurement,tag_set,field_set);
instance->line_protocol_send_buff_offset += snprintf(instance->line_protocol_send_buff + instance->line_protocol_send_buff_offset,
sizeof(instance->line_protocol_send_buff) - instance->line_protocol_send_buff_offset,
"%s%s %s\n",
@@ -294,16 +294,16 @@ int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *insta
metrics = dyn_metric->metrics;
metric = metrics[0];
memset(tag_set_buff, 0, sizeof(tag_set_buff));
- memset(field_set_buff, 0 ,sizeof(field_set_buff));
+ memset(field_set_buff, 0,sizeof(field_set_buff));
field_used_len = 0;
tag_used_len = 0;
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
- table = instance->table_metrics[metric->table_id];
+ table = metric->table;
for(j = 0; j < table->column_cnt; j++)
{
- metric = metrics[i];
+ metric = metrics[j];
value = metric->field_type == FIELD_TYPE_GAUGE ?
get_metric_unit_val(metric, FS_CALC_CURRENT, 0):
get_metric_unit_val(metric, FS_CALC_SPEED, 0);
@@ -314,15 +314,18 @@ int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *insta
field_used_len += snprintf(field_set_buff + field_used_len,
sizeof(field_set_buff) - field_used_len,
"%s=%lld,",
- metric->table_column_name,
+ metric->table->column_name[metric->table_column_id],
value
);
}
- if(row_value_is_not_zero == 1)
+ if(row_value_is_not_zero != 1)
{
continue;
}
+ metric = metrics[0];
+ field_used_len--;
+ field_set_buff[field_used_len] = '\0';
tag_used_len += snprintf(tag_set_buff + tag_used_len,
sizeof(tag_set_buff) - tag_used_len,
",app_name=%s,table_name=%s",
diff --git a/src/prometheus_output.cpp b/src/prometheus_output.cpp
index 151bfba..8b31ae3 100644
--- a/src/prometheus_output.cpp
+++ b/src/prometheus_output.cpp
@@ -8,6 +8,7 @@ struct prometheus_endpoint_instance g_prometheus_endpoint_instance =
NULL,
0,
0,
+ 0,
NULL,
0,
NULL,
@@ -98,17 +99,24 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom
fs_instance = prometheus_output->fs_instance;
+ 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 = (char *)calloc(1,payload_len);
+ 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);
- payload_len = prometheus_output->fs_instance_cnt * 128; //TODO using marco, len?
- payload_append_position = payload = (char *)calloc(1,payload_len);
-
- payload_append_position += snprintf(payload_append_position, payload_len - (payload_append_position - payload),"url_path:\n\t%s\n", prometheus_output->url_path);
-
- for(i = 0; i < prometheus_output->fs_instance_cnt; i++)
+ 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);
+ }
+ }
+ else
{
- payload_append_position += snprintf(payload_append_position, payload_len - (payload_append_position - payload),"\t/%s\n", fs_instance[i]->name);
+ payload = (char *)calloc(1, 128);
+ strncpy(payload, "Not Invaild instance\n", 128);
}
-
response = http_response_init();
http_response_status(response, 404);
http_response_header(response, "Content-Type", "text/plain; charset=utf-8");
@@ -130,7 +138,7 @@ static int prometheus_output_read_metric_tags(struct metric *metric, char *insta
append_pos += snprintf(append_pos, size - (append_pos - tags_buff), "app_name=\"%s\"", instance_name);
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
append_pos += snprintf(append_pos, size - (append_pos - tags_buff), ",line_name=\"%s\"", metric->field_name);
}
@@ -149,9 +157,9 @@ static int prometheus_output_read_metric_name(struct metric *metric, char *insta
{
char unescape[256] = {0};
char *append_pos = name_buff;
- if(metric->belong_to_table == 1)
+ if(metric->table)
{
- str_unescape(metric->table_column_name, unescape, sizeof(unescape));
+ str_unescape(metric->table->column_name[metric->table_column_id], unescape, sizeof(unescape));
}
else
{
@@ -417,9 +425,8 @@ int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, cons
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);
-
pthread_create(&g_prometheus_endpoint_instance.tid, NULL, prometheus_endpoint_listen_thread_entry, (void *)&g_prometheus_endpoint_instance);
-
+ g_prometheus_endpoint_instance.thread_created = 1;
return 0;
}
@@ -444,3 +451,34 @@ int fieldstat_enable_prometheus_output(struct fieldstat_instance *instance)
}
+void fieldstat_global_disable_prometheus_endpoint()
+{
+ void *pthread_ret;
+
+ if(g_prometheus_endpoint_instance.thread_created == 1)
+ {
+ pthread_cancel(g_prometheus_endpoint_instance.tid);
+ pthread_join(g_prometheus_endpoint_instance.tid, &pthread_ret);
+ g_prometheus_endpoint_instance.running = 0;
+ g_prometheus_endpoint_instance.thread_created = 0;
+ free(g_prometheus_endpoint_instance.server_handle);
+ g_prometheus_endpoint_instance.server_handle = NULL;
+ }
+
+ if(g_prometheus_endpoint_instance.url_path)
+ {
+ free(g_prometheus_endpoint_instance.url_path);
+ g_prometheus_endpoint_instance.url_path = NULL;
+ }
+
+ if(g_prometheus_endpoint_instance.fs_instance)
+ {
+ free(g_prometheus_endpoint_instance.fs_instance);
+ g_prometheus_endpoint_instance.fs_instance = NULL;
+ g_prometheus_endpoint_instance.fs_instance_cnt = 0;
+ g_prometheus_endpoint_instance.fs_instance_size = 0;
+ }
+
+ return;
+}
+