diff options
| author | fumingwei <[email protected]> | 2023-09-11 16:59:48 +0800 |
|---|---|---|
| committer | fumingwei <[email protected]> | 2023-09-11 16:59:48 +0800 |
| commit | c8221fb80387230443727687d4425473327ba978 (patch) | |
| tree | d4b733416e14d7e13582c72977caeb6c04dfa923 /src/exporter/local_exporter.c | |
| parent | 159c7767eb6380dad96dc50fd9b7af54b6b8efb7 (diff) | |
temp 10feature-fs4
Diffstat (limited to 'src/exporter/local_exporter.c')
| -rw-r--r-- | src/exporter/local_exporter.c | 205 |
1 files changed, 170 insertions, 35 deletions
diff --git a/src/exporter/local_exporter.c b/src/exporter/local_exporter.c index 9f43125..cdaff9d 100644 --- a/src/exporter/local_exporter.c +++ b/src/exporter/local_exporter.c @@ -1,4 +1,6 @@ #include "exporter_internal.h" + +#define PER_LINE_COUNT 6 /******************************************************************************* * struct define *******************************************************************************/ @@ -47,9 +49,21 @@ static void stream_buffer_free(struct stream_buffer *buffer) } +static void fwrite_stream_buffer(struct stream_buffer *buffer, FILE *fp) +{ + if(buffer == NULL || buffer->buf == NULL || fp == NULL) + { + return; + } + + fwrite(buffer->buf, buffer->offset, 1, fp); + fflush(fp); + + return; +} -static int build_tags_buf(struct fieldstat_tag *tags, size_t n_tags, char *buf, - size_t size) +static int build_tags_buf(struct fieldstat_tag *tags, size_t n_tags, + char *buf, size_t size) { int used = 0; used = snprintf(buf, size, "{"); @@ -75,36 +89,156 @@ static int build_tags_buf(struct fieldstat_tag *tags, size_t n_tags, char *buf, } } - if(used > 0 && buf[used - 1] == ',') + if(used > 1) { used--; + used += snprintf(buf + used, size - used, "}"); + } + else + { + used--; + buf[used] = '\0'; } - used += snprintf(buf + used, size - used, "}"); return used; } -static int build_global_buf(struct fieldstat_exporter *exporter, char *buf, int size) +static void fwrite_global_buf(struct fieldstat_exporter *exporter) { int used = 0; + char buf[1024] = {0}; struct fieldstat_tag *tags = exporter->tags; size_t n_tags = exporter->n_tags; - used = snprintf(buf, size, "exporter: %s ", exporter->name); + used = snprintf(buf, sizeof(buf), "exporter: %s ", exporter->name); - used += build_tags_buf(tags, n_tags, buf + used, size - used); + used += build_tags_buf(tags, n_tags, buf + used, sizeof(buf) - used); - used += snprintf(buf + used, size - used, "\n"); + used += snprintf(buf + used, sizeof(buf) - used, "\n"); - return used; + fwrite(buf, used, 1, exporter->local_exporter_fp); + fflush(exporter->local_exporter_fp); + + return; +} + +static void filling_counter_buf(struct stream_buffer *counter, char *metric_name, + long long value, struct fieldstat_tag_list *cube_tags, + struct fieldstat_tag_list *cell_tags, int n_counters) +{ + if(counter == NULL || metric_name == NULL) + { + return; + } + + stream_buffer_realloc(counter); + + counter->offset += snprintf(counter->buf + counter->offset, + counter->size - counter->offset, + "%12s", metric_name); + + if(cube_tags != NULL) + { + counter->offset += build_tags_buf(cube_tags->tag, cube_tags->n_tag, + counter->buf + counter->offset, + counter->size - counter->offset); + } + + if(cell_tags != NULL) + { + counter->offset += build_tags_buf(cell_tags->tag, cell_tags->n_tag, + counter->buf + counter->offset, + counter->size - counter->offset); + } + + counter->offset += snprintf(counter->buf + counter->offset, + counter->size - counter->offset, + ": %-10lld", value); + + if(n_counters > 0 && n_counters % PER_LINE_COUNT == 0) + { + counter->offset += snprintf(counter->buf + counter->offset, + counter->size - counter->offset, "\n"); + } + return; } +static void filling_histogram_buf(struct stream_buffer *hist, struct fieldstat *instance, + int cube_id, int metric_id, int cell_id, + struct fieldstat_tag_list *cell_tags, int n_hists) +{ + double quantiles[6] = {0.1, 0.5, 0.8, 0.9, 0.95, 0.99}; + const char* extra[]={"MAX", "MIN", "AVG", "STDDEV", "CNT"}; + struct fieldstat_tag_list *cube_tags = fieldstat_get_shared_tags(instance, cube_id); + char *metric_name = (char *)fieldstat_get_metric_name(instance, cube_id, metric_id); + + stream_buffer_realloc(hist); + + //title start + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%12s", metric_name); + if(cube_tags != NULL) + { + hist->offset += build_tags_buf(cube_tags->tag, cube_tags->n_tag, + hist->buf + hist->offset, + hist->size - hist->offset); + } + if(cell_tags != NULL) + { + hist->offset += build_tags_buf(cell_tags->tag, cell_tags->n_tag, + hist->buf + hist->offset, + hist->size - hist->offset); + } + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, "\n"); + + //head start + for(int i = 0; i < sizeof(quantiles)/sizeof(quantiles[0]); i++) + { + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10.2lf", quantiles[i]*100); + } + for(unsigned int i = 0; i < sizeof(extra)/sizeof(extra[0]); i++) + { + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10s", extra[i]); + } + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, "\n"); + + //unit start + for(int i = 0; i < sizeof(quantiles)/sizeof(quantiles[0]); i++) + { + double value = 0.0; + value = fieldstat_hist_value_at_percentile(instance, cube_id, metric_id, + cell_id, quantiles[i] * 100); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10.2lf", value); + } + long long max = fieldstat_hist_value_max(instance, cube_id, metric_id, cell_id); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10lld", max); + long long min = fieldstat_hist_value_min(instance, cube_id, metric_id, cell_id); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10lld", min); + double avg = fieldstat_hist_value_mean(instance, cube_id, metric_id, cell_id); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10.2lf", avg); + double stddev = fieldstat_hist_value_stddev(instance, cube_id, metric_id, cell_id); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10.2lf", stddev); + long long cnt = fieldstat_hist_value_total_count(instance, cube_id, metric_id, cell_id); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, + "%10lld", cnt); + hist->offset += snprintf(hist->buf + hist->offset, hist->size - hist->offset, "\n"); + + return; +} static int build_fieldstat_buf(struct fieldstat *instance, struct stream_buffer *counter, struct stream_buffer *table, struct stream_buffer *histogram) { int *cube_ids = NULL; int n_cube_ids = 0; + int n_counters = 0; if(instance == NULL || counter == NULL || histogram == NULL || table == NULL) { @@ -120,7 +254,7 @@ static int build_fieldstat_buf(struct fieldstat *instance, struct stream_buffer for(int i = 0; i < n_cube_ids; i++) { int max_metric_id = fieldstat_get_max_metric_id(instance, cube_ids[i]); - + for(int j = 0; j <= max_metric_id; j++) { int *cell_ids = NULL; @@ -135,33 +269,29 @@ static int build_fieldstat_buf(struct fieldstat *instance, struct stream_buffer for(int k = 0; k < n_cell; k++) { - long long value = 0; + long long value = 0; double hll_value = 0.0; + struct fieldstat_tag_list *shared_tags = NULL; switch(metric_type) { - case METRIC_TYPE_COUNTER: - stream_buffer_realloc(counter); - value = fieldstat_counter_get(instance, i, j, k); - counter->offset += snprintf(counter->buf + counter->offset, - counter->size - counter->offset, - "%s", metric_name); - counter->offset += build_tags_buf(tags_list[i].tag, tags_list[j].n_tag, - counter->buf + counter->offset, - counter->size - counter->offset); - counter->offset += snprintf(counter->buf + counter->offset, - counter->size - counter->offset, - " %lld\n", value); - break; - - case METRIC_TYPE_HLL: - break; - - case METRIC_TYPE_HISTOGRAM: - - break; - default: - assert(0); - break; + case METRIC_TYPE_COUNTER: + shared_tags = fieldstat_get_shared_tags(instance, cube_ids[i]); + value = fieldstat_counter_get(instance, cube_ids[i], j, k); + n_counters++; + filling_counter_buf(counter, metric_name, value, shared_tags, &(tags_list[k]), n_counters); + break; + + case METRIC_TYPE_HLL: + + break; + + case METRIC_TYPE_HISTOGRAM: + + break; + default: + assert(0); + break; + } } @@ -196,7 +326,7 @@ int fieldstat_exporter_local_export(struct fieldstat_exporter *exporter) struct stream_buffer table = {NULL, 0, 0}; struct stream_buffer histogram = {NULL, 0, 0}; - if(exporter == NULL || exporter->is_running != 1) + if(exporter == NULL || exporter->is_running != 1 || exporter->enable_local_exporter != 1) { return -1; } @@ -207,6 +337,11 @@ int fieldstat_exporter_local_export(struct fieldstat_exporter *exporter) build_fieldstat_buf(exporter->changing, &counter, &table, &histogram); + + fseek(exporter->local_exporter_fp, 0, SEEK_SET); + fwrite_global_buf(exporter); + fwrite_stream_buffer(&counter, exporter->local_exporter_fp); + stream_buffer_free(&counter); stream_buffer_free(&table); stream_buffer_free(&histogram); |
