Browse Source

lib: cmetrics: upgrade to v0.1.6

Signed-off-by: Eduardo Silva <edsiper@gmail.com>
pull/3830/head
Eduardo Silva 2 months ago
parent
commit
0a15e1b404
  1. 9
      lib/cmetrics/CMakeLists.txt
  2. 3
      lib/cmetrics/include/cmetrics/cmt_counter.h
  3. 1
      lib/cmetrics/include/cmetrics/cmt_gauge.h
  4. 11
      lib/cmetrics/include/cmetrics/cmt_log.h
  5. 1
      lib/cmetrics/include/cmetrics/cmt_untyped.h
  6. 2
      lib/cmetrics/src/cmetrics.c
  7. 44
      lib/cmetrics/src/cmt_counter.c
  8. 61
      lib/cmetrics/src/cmt_encode_prometheus_remote_write.c
  9. 39
      lib/cmetrics/src/cmt_gauge.c
  10. 32
      lib/cmetrics/src/cmt_untyped.c

9
lib/cmetrics/CMakeLists.txt

@ -11,9 +11,16 @@ endif()
# CMetrics Version
set(CMT_VERSION_MAJOR 0)
set(CMT_VERSION_MINOR 1)
set(CMT_VERSION_PATCH 5)
set(CMT_VERSION_PATCH 6)
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")
# Define __FILENAME__ consistently across Operating Systems
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__=__FILE__")
endif()
# Include helpers
include(cmake/macros.cmake)
include(CheckCSourceCompiles)

3
lib/cmetrics/include/cmetrics/cmt_counter.h

@ -27,12 +27,15 @@ struct cmt_counter {
struct cmt_opts opts;
struct cmt_map *map;
struct mk_list _head;
struct cmt *cmt;
int allow_reset;
};
struct cmt_counter *cmt_counter_create(struct cmt *cmt,
char *namespace, char *subsystem,
char *name, char *help,
int label_count, char **label_keys);
void cmt_counter_allow_reset(struct cmt_counter *counter);
int cmt_counter_destroy(struct cmt_counter *counter);
int cmt_counter_inc(struct cmt_counter *counter, uint64_t timestamp,
int labels_count, char **label_vals);

1
lib/cmetrics/include/cmetrics/cmt_gauge.h

@ -26,6 +26,7 @@
struct cmt_gauge {
struct cmt_opts opts; /* Metric options */
struct cmt_map *map;
struct cmt *cmt;
struct mk_list _head; /* Link to list struct cmt->gauges */
};

11
lib/cmetrics/include/cmetrics/cmt_log.h

@ -22,6 +22,15 @@
#include <errno.h>
/* Message types */
#define CMT_LOG_OFF 0
#define CMT_LOG_ERROR 1
#define CMT_LOG_WARN 2
#define CMT_LOG_INFO 3 /* default */
#define CMT_LOG_DEBUG 4
#define CMT_LOG_TRACE 5
#define CMT_LOG_BUF_SIZE 256
void cmt_log_print(void *ctx, int level, const char *file, int line,
@ -51,7 +60,7 @@ int cmt_errno_print(int errnum, const char *file, int line);
#ifdef __FILENAME__
#define cmt_errno() cmt_errno_print(errno, __FILENAME__, __LINE__)
#else
#define cmt_errno() cmt_errno_print(errno, __FILE__, __LINE__)
#define cmt_errno() cmt_errno_print(errno, __FILENAME__, __LINE__)
#endif
#ifdef _WIN32

1
lib/cmetrics/include/cmetrics/cmt_untyped.h

@ -26,6 +26,7 @@
struct cmt_untyped {
struct cmt_opts opts;
struct cmt_map *map;
struct cmt *cmt;
struct mk_list _head;
};

2
lib/cmetrics/src/cmetrics.c

@ -55,6 +55,8 @@ struct cmt *cmt_create()
mk_list_init(&cmt->histograms);
mk_list_init(&cmt->untypeds);
cmt->log_level = CMT_LOG_ERROR;
return cmt;
}

44
lib/cmetrics/src/cmt_counter.c

@ -31,11 +31,23 @@ struct cmt_counter *cmt_counter_create(struct cmt *cmt,
int ret;
struct cmt_counter *counter;
if (!name || !help) {
if (!namespace) {
cmt_log_error(cmt, "null namespace not allowed");
return NULL;
}
if (strlen(name) == 0 || strlen(help) == 0) {
if (!subsystem) {
cmt_log_error(cmt, "null subsystem not allowed");
return NULL;
}
if (!name || strlen(name) == 0) {
cmt_log_error(cmt, "undefined name");
return NULL;
}
if (!help || strlen(help) == 0) {
cmt_log_error(cmt, "undefined help");
return NULL;
}
@ -48,6 +60,7 @@ struct cmt_counter *cmt_counter_create(struct cmt *cmt,
ret = cmt_opts_init(&counter->opts, namespace, subsystem, name, help);
if (ret == -1) {
cmt_log_error(cmt, "unable to initialize options for counter");
cmt_counter_destroy(counter);
return NULL;
}
@ -55,13 +68,23 @@ struct cmt_counter *cmt_counter_create(struct cmt *cmt,
/* Create the map */
counter->map = cmt_map_create(CMT_COUNTER, &counter->opts, label_count, label_keys);
if (!counter->map) {
cmt_log_error(cmt, "unable to allocate map for counter");
cmt_counter_destroy(counter);
return NULL;
}
counter->cmt = cmt;
counter->cmt = cmt;
return counter;
}
void cmt_counter_allow_reset(struct cmt_counter *counter)
{
counter->allow_reset = 1;
}
int cmt_counter_destroy(struct cmt_counter *counter)
{
mk_list_del(&counter->_head);
@ -84,6 +107,9 @@ int cmt_counter_inc(struct cmt_counter *counter,
counter->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(counter->cmt, "unable to retrieve metric: %s for counter %s_%s_%s",
counter->map, counter->opts.namespace, counter->opts.subsystem,
counter->opts.name);
return -1;
}
cmt_metric_inc(metric, timestamp);
@ -99,6 +125,9 @@ int cmt_counter_add(struct cmt_counter *counter, uint64_t timestamp, double val,
counter->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(counter->cmt, "unable to retrieve metric: %s for counter %s_%s_%s",
counter->map, counter->opts.namespace, counter->opts.subsystem,
counter->opts.name);
return -1;
}
cmt_metric_add(metric, timestamp, val);
@ -115,10 +144,16 @@ int cmt_counter_set(struct cmt_counter *counter, uint64_t timestamp, double val,
labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(counter->cmt, "unable to retrieve metric: %s for counter %s_%s_%s",
counter->map, counter->opts.namespace, counter->opts.subsystem,
counter->opts.name);
return -1;
}
if (cmt_metric_get_value(metric) > val) {
if (cmt_metric_get_value(metric) > val && counter->allow_reset == 0) {
cmt_log_error(counter->cmt, "attempting to reset unresetable counter: %s_%s_%s",
counter->opts.namespace, counter->opts.subsystem,
counter->opts.name);
return -1;
}
cmt_metric_set(metric, timestamp, val);
@ -135,6 +170,9 @@ int cmt_counter_get_val(struct cmt_counter *counter,
counter->map, labels_count, label_vals,
&val);
if (ret == -1) {
cmt_log_error(counter->cmt, "unable to retrieve metric: %s for counter %s_%s_%s",
counter->map, counter->opts.namespace, counter->opts.subsystem,
counter->opts.name);
return -1;
}
*out_val = val;

61
lib/cmetrics/src/cmt_encode_prometheus_remote_write.c

@ -124,7 +124,7 @@ cmt_sds_t render_remote_write_context_to_sds(
result_buffer = cmt_sds_create_size(write_request_size);
if(result_buffer != NULL) {
prometheus__write_request__pack(&context->write_request, result_buffer);
prometheus__write_request__pack(&context->write_request, (uint8_t *) result_buffer);
cmt_sds_set_len(result_buffer, write_request_size);
}
@ -141,7 +141,6 @@ void cmt_destroy_prometheus_remote_write_context(
{
struct cmt_prometheus_time_series *time_series_entry;
struct cmt_prometheus_metric_metadata *metadata_entry;
size_t entry_index;
struct mk_list *head;
struct mk_list *tmp;
@ -151,11 +150,15 @@ void cmt_destroy_prometheus_remote_write_context(
if (time_series_entry->data.labels != NULL) {
destroy_prometheus_label_list(time_series_entry->data.labels,
time_series_entry->data.n_labels);
time_series_entry->data.labels = NULL;
}
if (time_series_entry->data.samples != NULL) {
destroy_prometheus_sample_list(time_series_entry->data.samples,
time_series_entry->data.n_samples);
time_series_entry->data.samples = NULL;
}
mk_list_del(&time_series_entry->_head);
@ -220,8 +223,6 @@ int append_entry_to_prometheus_label_list(Prometheus__Label **label_list,
char *name,
char *value)
{
int result;
label_list[*index] = calloc(1, sizeof(Prometheus__Label));
if (label_list[*index] == NULL) {
@ -259,39 +260,43 @@ void destroy_prometheus_sample_list(Prometheus__Sample **sample_list,
{
size_t index;
for (index = 0 ; index < entry_count ; index++) {
if (sample_list[index] != NULL) {
free(sample_list[index]);
sample_list[index] = NULL;
if (sample_list != NULL) {
for (index = 0 ; index < entry_count ; index++) {
if (sample_list[index] != NULL) {
free(sample_list[index]);
sample_list[index] = NULL;
}
}
}
free(sample_list);
free(sample_list);
}
}
void destroy_prometheus_label_list(Prometheus__Label **label_list,
size_t entry_count)
size_t entry_count)
{
size_t index;
for (index = 0 ; index < entry_count ; index++) {
if (label_list[index] != NULL) {
if (label_list[index]->name != NULL) {
cmt_sds_destroy(label_list[index]->name);
label_list[index]->name = NULL;
if (label_list != NULL) {
for (index = 0 ; index < entry_count ; index++) {
if (label_list[index] != NULL) {
if (label_list[index]->name != NULL) {
cmt_sds_destroy(label_list[index]->name);
label_list[index]->name = NULL;
}
if (label_list[index]->value != NULL) {
cmt_sds_destroy(label_list[index]->value);
label_list[index]->value = NULL;
}
free(label_list[index]);
label_list[index] = NULL;
}
if (label_list[index]->value != NULL) {
cmt_sds_destroy(label_list[index]->value);
label_list[index]->value = NULL;
}
free(label_list[index]);
label_list[index] = NULL;
}
}
free(label_list);
free(label_list);
}
}
int set_up_time_series_for_label_set(struct cmt_prometheus_remote_write_context *context,
@ -509,6 +514,7 @@ int pack_metric_metadata(struct cmt_prometheus_remote_write_context *context,
int append_metric_to_timeseries(struct cmt_prometheus_time_series *time_series,
struct cmt_metric *metric)
{
uint64_t ts;
Prometheus__Sample *sample;
sample = calloc(1, sizeof(Prometheus__Sample));
@ -522,8 +528,9 @@ int append_metric_to_timeseries(struct cmt_prometheus_time_series *time_series,
prometheus__sample__init(sample);
sample->value = cmt_metric_get_value(metric);
sample->timestamp = cmt_metric_get_timestamp(metric);
ts = cmt_metric_get_timestamp(metric);
sample->timestamp = ts / 1000000;
time_series->data.samples[time_series->entries_set++] = sample;
return CMT_ENCODE_PROMETHEUS_REMOTE_WRITE_SUCCESS;

39
lib/cmetrics/src/cmt_gauge.c

@ -31,11 +31,23 @@ struct cmt_gauge *cmt_gauge_create(struct cmt *cmt,
int ret;
struct cmt_gauge *gauge;
if (!name || !help) {
if (!namespace) {
cmt_log_error(cmt, "null namespace not allowed");
return NULL;
}
if (strlen(name) == 0 || strlen(help) == 0) {
if (!subsystem) {
cmt_log_error(cmt, "null subsystem not allowed");
return NULL;
}
if (!name || strlen(name) == 0) {
cmt_log_error(cmt, "undefined name");
return NULL;
}
if (!help || strlen(help) == 0) {
cmt_log_error(cmt, "undefined help");
return NULL;
}
@ -49,6 +61,7 @@ struct cmt_gauge *cmt_gauge_create(struct cmt *cmt,
/* Initialize options */
ret = cmt_opts_init(&gauge->opts, namespace, subsystem, name, help);
if (ret == -1) {
cmt_log_error(cmt, "unable to initialize options for gauge");
cmt_gauge_destroy(gauge);
return NULL;
}
@ -56,10 +69,13 @@ struct cmt_gauge *cmt_gauge_create(struct cmt *cmt,
/* Create the map */
gauge->map = cmt_map_create(CMT_GAUGE, &gauge->opts, label_count, label_keys);
if (!gauge->map) {
cmt_log_error(cmt, "unable to allocate map for gauge");
cmt_gauge_destroy(gauge);
return NULL;
}
gauge->cmt = cmt;
return gauge;
}
@ -82,6 +98,9 @@ int cmt_gauge_set(struct cmt_gauge *gauge, uint64_t timestamp, double val,
metric = cmt_map_metric_get(&gauge->opts, gauge->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(gauge->cmt, "unable to retrieve metric: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
cmt_metric_set(metric, timestamp, val);
@ -97,6 +116,9 @@ int cmt_gauge_inc(struct cmt_gauge *gauge, uint64_t timestamp,
metric = cmt_map_metric_get(&gauge->opts, gauge->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(gauge->cmt, "unable to retrieve metric: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
cmt_metric_inc(metric, timestamp);
@ -111,6 +133,9 @@ int cmt_gauge_dec(struct cmt_gauge *gauge, uint64_t timestamp,
metric = cmt_map_metric_get(&gauge->opts, gauge->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(gauge->cmt, "unable to retrieve metric: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
cmt_metric_dec(metric, timestamp);
@ -125,6 +150,9 @@ int cmt_gauge_add(struct cmt_gauge *gauge, uint64_t timestamp, double val,
metric = cmt_map_metric_get(&gauge->opts, gauge->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(gauge->cmt, "unable to retrieve metric: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
cmt_metric_add(metric, timestamp, val);
@ -139,6 +167,9 @@ int cmt_gauge_sub(struct cmt_gauge *gauge, uint64_t timestamp, double val,
metric = cmt_map_metric_get(&gauge->opts, gauge->map, labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(gauge->cmt, "unable to retrieve metric: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
cmt_metric_sub(metric, timestamp, val);
@ -155,6 +186,10 @@ int cmt_gauge_get_val(struct cmt_gauge *gauge,
gauge->map, labels_count, label_vals,
&val);
if (ret == -1) {
cmt_log_error(gauge->cmt,
"unable to retrieve metric value: %s for gauge %s_%s_%s",
gauge->map, gauge->opts.namespace, gauge->opts.subsystem,
gauge->opts.name);
return -1;
}
*out_val = val;

32
lib/cmetrics/src/cmt_untyped.c

@ -31,11 +31,28 @@ struct cmt_untyped *cmt_untyped_create(struct cmt *cmt,
int ret;
struct cmt_untyped *untyped;
if (!name || !help) {
if (!namespace) {
cmt_log_error(cmt, "null namespace not allowed");
return NULL;
}
if (strlen(name) == 0 || strlen(help) == 0) {
if (!subsystem) {
cmt_log_error(cmt, "null subsystem not allowed");
return NULL;
}
if (!help || strlen(help) == 0) {
cmt_log_error(cmt, "undefined help");
return NULL;
}
if (!name || strlen(name) == 0) {
cmt_log_error(cmt, "undefined name");
return NULL;
}
if (!help || strlen(help) == 0) {
cmt_log_error(cmt, "undefined help");
return NULL;
}
@ -48,6 +65,7 @@ struct cmt_untyped *cmt_untyped_create(struct cmt *cmt,
ret = cmt_opts_init(&untyped->opts, namespace, subsystem, name, help);
if (ret == -1) {
cmt_log_error(cmt, "unable to initialize options for untyped");
cmt_untyped_destroy(untyped);
return NULL;
}
@ -55,10 +73,13 @@ struct cmt_untyped *cmt_untyped_create(struct cmt *cmt,
/* Create the map */
untyped->map = cmt_map_create(CMT_UNTYPED, &untyped->opts, label_count, label_keys);
if (!untyped->map) {
cmt_log_error(cmt, "unable to allocate map for untyped");
cmt_untyped_destroy(untyped);
return NULL;
}
untyped->cmt = cmt;
return untyped;
}
@ -84,6 +105,9 @@ int cmt_untyped_set(struct cmt_untyped *untyped, uint64_t timestamp, double val,
labels_count, label_vals,
CMT_TRUE);
if (!metric) {
cmt_log_error(untyped->cmt, "unable to retrieve metric: %s for untyped %s_%s_%s",
untyped->map, untyped->opts.namespace, untyped->opts.subsystem,
untyped->opts.name);
return -1;
}
@ -104,6 +128,10 @@ int cmt_untyped_get_val(struct cmt_untyped *untyped,
untyped->map, labels_count, label_vals,
&val);
if (ret == -1) {
cmt_log_error(untyped->cmt,
"unable to retrieve metric value: %s for untyped %s_%s_%s",
untyped->map, untyped->opts.namespace, untyped->opts.subsystem,
untyped->opts.name);
return -1;
}
*out_val = val;

Loading…
Cancel
Save