Skip to content

Commit

Permalink
lib: cmetrics: upgrade to v0.9.6
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
  • Loading branch information
edsiper committed Sep 16, 2024
1 parent 724940e commit c3d2417
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 74 deletions.
8 changes: 7 additions & 1 deletion lib/cmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# CMetrics Version
set(CMT_VERSION_MAJOR 0)
set(CMT_VERSION_MINOR 9)
set(CMT_VERSION_PATCH 5)
set(CMT_VERSION_PATCH 6)
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")

# Include helpers
Expand Down Expand Up @@ -69,12 +69,18 @@ endif()

# Configuration options
option(CMT_DEV "Enable development mode" No)
option(CMT_DEBUG "Enable debug mode" No)
option(CMT_TESTS "Enable unit testing" No)
option(CMT_INSTALL_TARGETS "Enable subdirectory library installations" Yes)
option(CMT_ENABLE_PROMETHEUS_DECODER "Enable prometheus decoder" Yes)

if(CMT_DEV)
set(CMT_TESTS Yes)
set(CMT_DEBUG Yes)
endif()

if(CMT_DEBUG)
set(CMAKE_BUILD_TYPE Debug)
endif()

set(FLEX_BISON_GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
10 changes: 10 additions & 0 deletions lib/cmetrics/include/cmetrics/cmt_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ static inline double cmt_math_uint64_to_d64(uint64_t val)
return u.d;
}

static inline uint64_t cmt_math_sum_native_uint64_as_d64(uint64_t dst, uint64_t src)
{
double val;

val = cmt_math_uint64_to_d64(dst);
val += cmt_math_uint64_to_d64(src);

return cmt_math_d64_to_uint64(val);
}

#endif
265 changes: 193 additions & 72 deletions lib/cmetrics/src/cmt_cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,66 @@ static int copy_label_values(struct cmt_metric *metric, char **out)
return i;
}

int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map *src)
static inline int cat_histogram_values(struct cmt_metric *metric_dst, struct cmt_histogram *histogram,
struct cmt_metric *metric_src)
{
int i;

if (!metric_dst->hist_buckets) {
metric_dst->hist_buckets = calloc(1, sizeof(uint64_t) * (histogram->buckets->count + 1));
if (!metric_dst->hist_buckets) {
return -1;
}
}

for (i = 0; i < histogram->buckets->count; i++) {
/* histogram buckets are always integers, no need to convert them */
metric_dst->hist_buckets[i] += metric_src->hist_buckets[i];
}

/* histogram count */
metric_dst->hist_count = cmt_math_sum_native_uint64_as_d64(metric_dst->hist_count,
metric_src->hist_count);

/* histoggram sum */
metric_dst->hist_sum = cmt_math_sum_native_uint64_as_d64(metric_dst->hist_sum,
metric_src->hist_sum);

return 0;
}

/*
* For summaries we don't support manual updates through the API, on concatenation we just
* keep the last values reported.
*/
static inline int cat_summary_values(struct cmt_metric *metric_dst, struct cmt_summary *summary,
struct cmt_metric *metric_src)
{
int i;

if (!metric_dst->sum_quantiles) {
metric_dst->sum_quantiles = calloc(1, sizeof(uint64_t) * (summary->quantiles_count));
if (!metric_dst->sum_quantiles) {
return -1;
}
}

for (i = 0; i < summary->quantiles_count; i++) {
/* summary quantiles are always integers, no need to convert them */
metric_dst->sum_quantiles[i] = metric_src->sum_quantiles[i];
}

metric_dst->sum_quantiles_count = metric_src->sum_quantiles_count;
metric_dst->sum_quantiles_set = metric_src->sum_quantiles_set;

metric_dst->sum_count = metric_src->sum_count;
metric_dst->sum_sum = metric_src->sum_sum;

return 0;
}

int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map *src)
{
int c;
int ret;
uint64_t ts;
Expand All @@ -107,6 +164,7 @@ int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map
struct cfl_list *head;
struct cmt_metric *metric_dst;
struct cmt_metric *metric_src;
struct cmt_summary *summary;
struct cmt_histogram *histogram;

/* Handle static metric (no labels case) */
Expand All @@ -119,33 +177,17 @@ int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map

if (src->type == CMT_HISTOGRAM) {
histogram = (struct cmt_histogram *) src->parent;

if (!metric_dst->hist_buckets) {
metric_dst->hist_buckets = calloc(1, sizeof(uint64_t) * (histogram->buckets->count + 1));
if (!metric_dst->hist_buckets) {
return -1;
}
}
for (i = 0; i < histogram->buckets->count; i++) {
metric_dst->hist_buckets[i] = metric_src->hist_buckets[i];
ret = cat_histogram_values(metric_dst, histogram, metric_src);
if (ret == -1) {
return -1;
}
metric_dst->hist_count = metric_src->hist_count;
metric_dst->hist_sum = metric_src->hist_sum;
}
else if (src->type == CMT_SUMMARY) {
metric_dst->sum_quantiles_count = metric_src->sum_quantiles_count;
metric_dst->sum_quantiles_set = metric_src->sum_quantiles_set;
if (!metric_dst->sum_quantiles) {
metric_dst->sum_quantiles = calloc(1, sizeof(uint64_t) * (metric_src->sum_quantiles_count));
if (!metric_dst->sum_quantiles) {
return -1;
}
}
for (i = 0; i < metric_src->sum_quantiles_count; i++) {
metric_dst->sum_quantiles[i] = metric_src->sum_quantiles[i];
summary = (struct cmt_summary *) src->parent;
ret = cat_summary_values(metric_dst, summary, metric_src);
if (ret == -1) {
return -1;
}
metric_dst->sum_count = metric_src->sum_count;
metric_dst->sum_sum = metric_src->sum_sum;
}

ts = cmt_metric_get_timestamp(metric_src);
Expand Down Expand Up @@ -173,34 +215,17 @@ int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map

if (src->type == CMT_HISTOGRAM) {
histogram = (struct cmt_histogram *) src->parent;

if (!metric_dst->hist_buckets) {
metric_dst->hist_buckets = calloc(1, sizeof(uint64_t) * (histogram->buckets->count + 1));
if (!metric_dst->hist_buckets) {
return -1;
}
}

for (i = 0; i < histogram->buckets->count; i++) {
metric_dst->hist_buckets[i] = metric_src->hist_buckets[i];
ret = cat_histogram_values(metric_dst, histogram, metric_src);
if (ret == -1) {
return -1;
}
metric_dst->hist_count = metric_src->hist_count;
metric_dst->hist_sum = metric_src->hist_sum;
}
else if (src->type == CMT_SUMMARY) {
metric_dst->sum_quantiles_count = metric_src->sum_quantiles_count;
metric_dst->sum_quantiles_set = metric_src->sum_quantiles_set;
if (!metric_dst->sum_quantiles) {
metric_dst->sum_quantiles = calloc(1, sizeof(uint64_t) * (metric_src->sum_quantiles_count));
if (!metric_dst->sum_quantiles) {
return -1;
}
summary = (struct cmt_summary *) src->parent;
ret = cat_summary_values(metric_dst, summary, metric_src);
if (ret == -1) {
return -1;
}
for (i = 0; i < metric_src->sum_quantiles_count; i++) {
metric_dst->sum_quantiles[i] = metric_src->sum_quantiles[i];
}
metric_dst->sum_count = metric_src->sum_count;
metric_dst->sum_sum = metric_src->sum_sum;
}

ts = cmt_metric_get_timestamp(metric_src);
Expand All @@ -213,6 +238,88 @@ int cmt_cat_copy_map(struct cmt_opts *opts, struct cmt_map *dst, struct cmt_map

}

static inline int cmt_opts_compare(struct cmt_opts *a, struct cmt_opts *b)
{
int ret;

ret = strcmp(a->ns, a->ns);
if (ret != 0) {
return ret;
}

ret = strcmp(a->subsystem, b->subsystem);
if (ret != 0) {
return ret;
}

ret = strcmp(a->name, b->name);
if (ret != 0) {
return ret;
}

return strcmp(a->description, b->description);
}

static struct cmt_counter *counter_lookup(struct cmt *cmt, struct cmt_opts *opts)
{
struct cmt_counter *counter;
struct cfl_list *head;

cfl_list_foreach(head, &cmt->counters) {
counter = cfl_list_entry(head, struct cmt_counter, _head);
if (cmt_opts_compare(&counter->opts, opts) == 0) {
return counter;
}
}

return NULL;
}

static struct cmt_gauge *gauge_lookup(struct cmt *cmt, struct cmt_opts *opts)
{
struct cmt_gauge *gauge;
struct cfl_list *head;

cfl_list_foreach(head, &cmt->gauges) {
gauge = cfl_list_entry(head, struct cmt_gauge, _head);
if (cmt_opts_compare(&gauge->opts, opts) == 0) {
return gauge;
}
}

return NULL;
}

static struct cmt_untyped *untyped_lookup(struct cmt *cmt, struct cmt_opts *opts)
{
struct cmt_untyped *untyped;
struct cfl_list *head;

cfl_list_foreach(head, &cmt->untypeds) {
untyped = cfl_list_entry(head, struct cmt_untyped, _head);
if (cmt_opts_compare(&untyped->opts, opts) == 0) {
return untyped;
}
}

return NULL;
}

static struct cmt_histogram *histogram_lookup(struct cmt *cmt, struct cmt_opts *opts)
{
struct cmt_histogram *histogram;
struct cfl_list *head;

cfl_list_foreach(head, &cmt->histograms) {
histogram = cfl_list_entry(head, struct cmt_histogram, _head);
if (cmt_opts_compare(&histogram->opts, opts) == 0) {
return histogram;
}
}

return NULL;
}

int cmt_cat_counter(struct cmt *cmt, struct cmt_counter *counter,
struct cmt_map *filtered_map)
{
Expand All @@ -230,11 +337,14 @@ int cmt_cat_counter(struct cmt *cmt, struct cmt_counter *counter,
return -1;
}

/* create counter */
c = cmt_counter_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
c = counter_lookup(cmt, opts);
if (!c) {
/* create counter */
c = cmt_counter_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
}

free(labels);
if (!c) {
Expand Down Expand Up @@ -274,11 +384,15 @@ int cmt_cat_gauge(struct cmt *cmt, struct cmt_gauge *gauge,
return -1;
}

/* create counter */
g = cmt_gauge_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
g = gauge_lookup(cmt, opts);
if (!g) {
/* create counter */
g = cmt_gauge_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
}

free(labels);
if (!g) {
return -1;
Expand Down Expand Up @@ -317,11 +431,15 @@ int cmt_cat_untyped(struct cmt *cmt, struct cmt_untyped *untyped,
return -1;
}

/* create counter */
u = cmt_untyped_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
u = untyped_lookup(cmt, opts);
if (!u) {
/* create counter */
u = cmt_untyped_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
map->label_count, labels);
}

free(labels);
if (!u) {
return -1;
Expand Down Expand Up @@ -362,16 +480,19 @@ int cmt_cat_histogram(struct cmt *cmt, struct cmt_histogram *histogram,
return -1;
}

buckets_count = histogram->buckets->count;
buckets = cmt_histogram_buckets_create_size(histogram->buckets->upper_bounds,
buckets_count);
hist = histogram_lookup(cmt, opts);
if (!hist) {
buckets_count = histogram->buckets->count;
buckets = cmt_histogram_buckets_create_size(histogram->buckets->upper_bounds,
buckets_count);

/* create histogram */
hist = cmt_histogram_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
buckets,
map->label_count, labels);
/* create histogram */
hist = cmt_histogram_create(cmt,
opts->ns, opts->subsystem,
opts->name, opts->description,
buckets,
map->label_count, labels);
}
free(labels);

if (!hist) {
Expand Down
Loading

0 comments on commit c3d2417

Please sign in to comment.