summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfumingwei <[email protected]>2023-09-11 16:59:48 +0800
committerfumingwei <[email protected]>2023-09-11 16:59:48 +0800
commitc8221fb80387230443727687d4425473327ba978 (patch)
treed4b733416e14d7e13582c72977caeb6c04dfa923 /src
parent159c7767eb6380dad96dc50fd9b7af54b6b8efb7 (diff)
Diffstat (limited to 'src')
-rw-r--r--src/exporter/exporter.c2
-rw-r--r--src/exporter/local_exporter.c205
-rw-r--r--src/exporter/prometheus_exporter.c66
-rw-r--r--src/fieldstat.c40
-rw-r--r--src/metrics/metric.c68
-rw-r--r--src/metrics/metric.h4
6 files changed, 316 insertions, 69 deletions
diff --git a/src/exporter/exporter.c b/src/exporter/exporter.c
index d0ba12f..0208b06 100644
--- a/src/exporter/exporter.c
+++ b/src/exporter/exporter.c
@@ -205,7 +205,7 @@ int fieldstat_exporter_set_local_exporter(struct fieldstat_exporter *exporter,
}
exporter->local_exporter_fp = fopen(filename, "w");
- if(exporter->local_exporter_fp)
+ if(exporter->local_exporter_fp == NULL)
{
printf("fieldstat: enable local exporter failed. "
"Error: open %s failed.\n", filename);
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);
diff --git a/src/exporter/prometheus_exporter.c b/src/exporter/prometheus_exporter.c
index 4ccb587..741c4b8 100644
--- a/src/exporter/prometheus_exporter.c
+++ b/src/exporter/prometheus_exporter.c
@@ -312,44 +312,44 @@ static int build_fieldstat_exporter_payload(struct fieldstat_exporter *exporter,
double hll_value = 0.0;
char cell_buf[512] = {0};
//part of one metric
- build_cell_tags_buf(tags_list->tag, tags_list->n_tag, cell_buf, sizeof(cell_buf));
+ build_cell_tags_buf(tags_list[k].tag, tags_list[k].n_tag, cell_buf, sizeof(cell_buf));
payload_realloc(&buf, &size, offset);
switch(metric_type)
{
- case METRIC_TYPE_COUNTER:
- value = fieldstat_counter_get(instance, i, j, k);
- offset += snprintf(buf + offset, size - offset, "%s{%s%s%s} %lld\n",
- name_buf, global_buf, cube_buf, cell_buf, value);
- break;
-
- case METRIC_TYPE_HLL:
- hll_value = fieldstat_hll_get(instance, i, j, k);
- offset += snprintf(buf + offset, size - offset, "%s_total_count{%s%s%s} %.0f\n",
- name_buf, global_buf, cube_buf, cell_buf, hll_value);
- break;
-
- case METRIC_TYPE_HISTOGRAM:
+ case METRIC_TYPE_COUNTER:
+ value = fieldstat_counter_get(instance, cube_ids[i], j, k);
+ offset += snprintf(buf + offset, size - offset, "%s{%s%s%s} %lld\n",
+ name_buf, global_buf, cube_buf, cell_buf, value);
+ break;
+
+ case METRIC_TYPE_HLL:
+ hll_value = fieldstat_hll_get(instance, cube_ids[i], j, k);
+ offset += snprintf(buf + offset, size - offset, "%s_total_count{%s%s%s} %.0f\n",
+ name_buf, global_buf, cube_buf, cell_buf, hll_value);
+ break;
+
+ case METRIC_TYPE_HISTOGRAM:
+
+ for(int m = 0; m < sizeof(quantiles)/sizeof(quantiles[0]); m++)
+ {
+ value = fieldstat_hist_value_at_percentile(instance, cube_ids[i], j, k, quantiles[m] * 100);
+ offset += snprintf(buf + offset, size - offset,
+ "%s{%s%s%s,quantile=\"%0.2f\"} %lld\n",
+ name_buf, global_buf, cube_buf, cell_buf, quantiles[m], value);
+ }
+
+ value = fieldstat_hist_value_total_count(instance, cube_ids[i], j, k);
+ offset += snprintf(buf + offset, size - offset, "%s_count{%s%s%s} %lld\n",
+ name_buf, global_buf, cube_buf, cell_buf, value);
- for(int m = 0; m < sizeof(quantiles)/sizeof(quantiles[0]); m++)
- {
- value = fieldstat_hist_value_at_percentile(instance, i, j, k, quantiles[m] * 100);
- offset += snprintf(buf + offset, size - offset,
- "%s{%s%s%s,quantile=\"%0.2f\"} %lld\n",
- name_buf, global_buf, cube_buf, cell_buf, quantiles[m], value);
- }
-
- value = fieldstat_hist_value_total_count(instance, i, j, k);
- offset += snprintf(buf + offset, size - offset, "%s_count{%s%s%s} %lld\n",
- name_buf, global_buf, cube_buf, cell_buf, value);
-
- value = fieldstat_hist_value_sum(instance, i, j, k);
- offset += snprintf(buf + offset, size - offset, "%s_sum{%s%s%s} %lld\n",
- name_buf, global_buf, cube_buf, cell_buf, value);
- break;
- default:
- assert(0);
- break;
+ value = fieldstat_hist_value_sum(instance, cube_ids[i], j, k);
+ offset += snprintf(buf + offset, size - offset, "%s_sum{%s%s%s} %lld\n",
+ name_buf, global_buf, cube_buf, cell_buf, value);
+ break;
+ default:
+ assert(0);
+ break;
}
}
diff --git a/src/fieldstat.c b/src/fieldstat.c
index 12ab2ed..5555d45 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -1029,6 +1029,46 @@ long long fieldstat_hist_value_at_percentile(const struct fieldstat *instance, i
return metric_histogram_value_at_percentile(metric, cell_id, percentile);
}
+long long fieldstat_hist_value_max(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id)
+{
+ const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id);
+ if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) {
+ return -1;
+ }
+
+ return metric_histogram_value_max(metric, cell_id);
+}
+
+long long fieldstat_hist_value_min(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id)
+{
+ const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id);
+ if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) {
+ return -1;
+ }
+
+ return metric_histogram_value_min(metric, cell_id);
+}
+
+double fieldstat_hist_value_mean(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id)
+{
+ const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id);
+ if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) {
+ return -1;
+ }
+
+ return metric_histogram_value_mean(metric, cell_id);
+}
+
+double fieldstat_hist_value_stddev(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id)
+{
+ const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id);
+ if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) {
+ return -1;
+ }
+
+ return metric_histogram_value_stddev(metric, cell_id);
+}
+
long long fieldstat_hist_value_total_count(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id)
{
const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id);
diff --git a/src/metrics/metric.c b/src/metrics/metric.c
index 335ee2b..294c5ab 100644
--- a/src/metrics/metric.c
+++ b/src/metrics/metric.c
@@ -815,6 +815,74 @@ long long metric_histogram_value_at_percentile(const struct metric *pthis, int c
return hdr_value_at_percentile(data->hdr, percentile);
}
+long long metric_histogram_value_max(const struct metric *pthis, int cell_id)
+{
+ const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id);
+ if (data == NULL) {
+ return -1;
+ }
+
+ if(data->hdr->total_count == 0)
+ {
+ return 0;
+ }
+ else
+ {
+ return hdr_max(data->hdr);
+ }
+}
+
+long long metric_histogram_value_min(const struct metric *pthis, int cell_id)
+{
+ const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id);
+ if (data == NULL) {
+ return -1;
+ }
+
+ if(data->hdr->total_count == 0)
+ {
+ return 0;
+ }
+ else
+ {
+ return hdr_min(data->hdr);
+ }
+}
+
+double metric_histogram_value_mean(const struct metric *pthis, int cell_id)
+{
+ const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id);
+ if (data == NULL) {
+ return -1;
+ }
+
+ if(data->hdr->total_count == 0)
+ {
+ return 0.0;
+ }
+ else
+ {
+ return hdr_mean(data->hdr);
+ }
+}
+
+double metric_histogram_value_stddev(const struct metric *pthis, int cell_id)
+{
+ const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id);
+ if (data == NULL) {
+ return -1;
+ }
+
+ if(data->hdr->total_count == 0)
+ {
+ return 0.0;
+ }
+ else
+ {
+ return hdr_stddev(data->hdr);
+ }
+}
+
long long metric_histogram_value_total_count(const struct metric *pthis, int cell_id)
{
diff --git a/src/metrics/metric.h b/src/metrics/metric.h
index 17da701..0e48bf8 100644
--- a/src/metrics/metric.h
+++ b/src/metrics/metric.h
@@ -35,6 +35,10 @@ int metric_histogram_record(struct metric *pthis, int cell_id, long long value);
long long metric_histogram_value_at_percentile(const struct metric *pthis, int cell_id, double percentile);
long long metric_histogram_value_total_count(const struct metric *pthis, int cell_id);
long long metric_histogram_value_sum(const struct metric *pthis, int cell_id);
+long long metric_histogram_value_max(const struct metric *pthis, int cell_id);
+long long metric_histogram_value_min(const struct metric *pthis, int cell_id);
+double metric_histogram_value_mean(const struct metric *pthis, int cell_id);
+double metric_histogram_value_stddev(const struct metric *pthis, int cell_id);
long long metric_histogram_count_le_value(const struct metric *pthis, int cell_id, long long value);