Skip to content

Commit

Permalink
lib: cmetrics: upgrade to v0.3.7
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Sep 9, 2022
1 parent d9c7c2c commit ccb6ec5
Show file tree
Hide file tree
Showing 11 changed files with 666 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/cmetrics/.github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Build on ${{ matrix.os }} with ${{ matrix.compiler }}
uses: uraimo/[email protected].0
uses: uraimo/[email protected].1
with:
arch: aarch64
distro: ubuntu20.04
Expand Down
2 changes: 1 addition & 1 deletion lib/cmetrics/.github/workflows/packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
format: [ rpm, deb ]
steps:
- uses: actions/checkout@v2
- uses: uraimo/[email protected].0
- uses: uraimo/[email protected].1
name: Build the ${{matrix.format}} packages
with:
arch: aarch64
Expand Down
37 changes: 20 additions & 17 deletions lib/cmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ endif()
# CMetrics Version
set(CMT_VERSION_MAJOR 0)
set(CMT_VERSION_MINOR 3)
set(CMT_VERSION_PATCH 6)
set(CMT_VERSION_PATCH 7)
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")

# Define __FILENAME__ consistently across Operating Systems
# Define __CMT_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 $<))\"'")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__CMT_FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__=__FILE__")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__CMT_FILENAME__=__FILE__")
endif()

# Configuration options
option(CMT_DEV "Enable development mode" No)
option(CMT_TESTS "Enable unit testing" No)
option(CMT_INSTALL_TARGETS "Enable subdirectory library installations" Yes)
option(CMT_DEV "Enable development 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)
Expand Down Expand Up @@ -119,18 +120,20 @@ check_c_source_compiles("
return 0;
}" CMT_HAVE_MSGPACK)

# Flex and Bison: check if the variables has not been defined before by
# a parent project to avoid conflicts.
if(NOT FLEX_FOUND)
find_package(FLEX 2)
endif()
if(CMT_ENABLE_PROMETHEUS_DECODER)
# Flex and Bison: check if the variables has not been defined before by
# a parent project to avoid conflicts.
if(NOT FLEX_FOUND)
find_package(FLEX 2)
endif()

if(NOT BISON_FOUND)
find_package(BISON 3)
endif()
if(NOT BISON_FOUND)
find_package(BISON 3)
endif()

if(FLEX_FOUND AND BISON_FOUND)
set(CMT_BUILD_PROMETHEUS_DECODER 1)
if(FLEX_FOUND AND BISON_FOUND)
set(CMT_BUILD_PROMETHEUS_DECODER 1)
endif()
endif()

configure_file(
Expand Down
3 changes: 1 addition & 2 deletions lib/cmetrics/include/cmetrics/cmt_decode_prometheus.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#define CMT_DECODE_PROMETHEUS_PARSE_VALUE_FAILED 60
#define CMT_DECODE_PROMETHEUS_PARSE_TIMESTAMP_FAILED 70

#define CMT_DECODE_PROMETHEUS_DUPLICATE_SUM_COUNT 10000

#define CMT_DECODE_PROMETHEUS_MAX_LABEL_COUNT 128

enum cmt_decode_prometheus_context_sample_type {
Expand Down Expand Up @@ -70,6 +68,7 @@ struct cmt_decode_prometheus_context_metric {
struct cmt_decode_prometheus_parse_opts {
int start_token;
uint64_t default_timestamp;
uint64_t override_timestamp;
char *errbuf;
size_t errbuf_size;
};
Expand Down
24 changes: 12 additions & 12 deletions lib/cmetrics/include/cmetrics/cmt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ void cmt_log_print(void *ctx, int level, const char *file, int line,
const char *fmt, ...);
int cmt_errno_print(int errnum, const char *file, int line);

#define cmt_log_error(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_ERROR, __FILENAME__, \
#define cmt_log_error(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_ERROR, __CMT_FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cmt_log_warn(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_WARN, __FILENAME__, \
#define cmt_log_warn(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_WARN, __CMT_FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cmt_log_info(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_INFO, __FILENAME__, \
#define cmt_log_info(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_INFO, __CMT_FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cmt_log_debug(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_DEBUG, __FILENAME__, \
#define cmt_log_debug(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_DEBUG, __CMT_FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#define cmt_log_trace(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_TRACE, __FILENAME__, \
#define cmt_log_trace(ctx, fmt, ...) \
cmt_log_print(ctx, CMT_LOG_TRACE, __CMT_FILENAME__, \
__LINE__, fmt, ##__VA_ARGS__)

#ifdef __FILENAME__
#define cmt_errno() cmt_errno_print(errno, __FILENAME__, __LINE__)
#ifdef __CMT_FILENAME__
#define cmt_errno() cmt_errno_print(errno, __CMT_FILENAME__, __LINE__)
#else
#define cmt_errno() cmt_errno_print(errno, __FILE__, __LINE__)
#endif
Expand Down
79 changes: 51 additions & 28 deletions lib/cmetrics/src/cmt_decode_prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ static int parse_value_timestamp(
"value", sample->value1);
}

if (!strlen(sample->value2)) {
if (context->opts.override_timestamp) {
*timestamp = context->opts.override_timestamp;
}
else if (!strlen(sample->value2)) {
/* No timestamp was specified, use default value */
*timestamp = context->opts.default_timestamp;
return 0;
Expand Down Expand Up @@ -454,7 +457,7 @@ static int add_metric_histogram(struct cmt_decode_prometheus_context *context)
* - sum
* - count */
bucket_count = mk_list_size(&context->metric.samples) - 3;
timestamp = 0;
timestamp = context->opts.override_timestamp;

bucket_defaults = calloc(bucket_count + 1, sizeof(*bucket_defaults));
if (!bucket_defaults) {
Expand Down Expand Up @@ -589,16 +592,16 @@ static int add_metric_histogram(struct cmt_decode_prometheus_context *context)
timestamp = context->opts.default_timestamp;
}

cmt_buckets = cmt_histogram_buckets_create_size(buckets, bucket_count);
if (!cmt_buckets) {
ret = report_error(context,
CMT_DECODE_PROMETHEUS_CMT_CREATE_ERROR,
"cmt_histogram_buckets_create_size failed");
goto end;
}

h = context->current.histogram;
if (!h) {
cmt_buckets = cmt_histogram_buckets_create_size(buckets, bucket_count);
if (!cmt_buckets) {
ret = report_error(context,
CMT_DECODE_PROMETHEUS_CMT_CREATE_ERROR,
"cmt_histogram_buckets_create_size failed");
goto end;
}

h = cmt_histogram_create(context->cmt,
context->metric.ns,
context->metric.subsystem,
Expand Down Expand Up @@ -670,7 +673,7 @@ static int add_metric_summary(struct cmt_decode_prometheus_context *context)
* - sum
* - count */
quantile_count = mk_list_size(&context->metric.samples) - 2;
timestamp = 0;
timestamp = context->opts.override_timestamp;

quantile_defaults = calloc(quantile_count, sizeof(*quantile_defaults));
if (!quantile_defaults) {
Expand Down Expand Up @@ -853,7 +856,8 @@ static int add_metric_summary(struct cmt_decode_prometheus_context *context)
}

static int finish_metric(struct cmt_decode_prometheus_context *context,
bool reset_summary)
bool reset_summary,
cmt_sds_t current_metric_name)
{
int rv = 0;

Expand Down Expand Up @@ -881,6 +885,15 @@ static int finish_metric(struct cmt_decode_prometheus_context *context,

end:
reset_context(context, reset_summary);

if (current_metric_name) {
context->metric.name_orig = current_metric_name;
rv = split_metric_name(context,
current_metric_name,
&(context->metric.ns),
&(context->metric.subsystem),
&(context->metric.name));
}
return rv;
}

Expand All @@ -894,26 +907,29 @@ static int finish_duplicate_histogram_summary_sum_count(
int current_metric_type;
cmt_sds_t current_metric_docstring;

if (type == CMT_DECODE_PROMETHEUS_CONTEXT_SAMPLE_TYPE_COUNT) {
cmt_sds_set_len(metric_name, cmt_sds_len(metric_name) - 6);
} else if (type == CMT_DECODE_PROMETHEUS_CONTEXT_SAMPLE_TYPE_SUM) {
cmt_sds_set_len(metric_name, cmt_sds_len(metric_name) - 4);
} else {
cmt_sds_set_len(metric_name, cmt_sds_len(metric_name) - 7);
}
metric_name[cmt_sds_len(metric_name)] = 0;

current_metric_type = context->metric.type;
current_metric_docstring = cmt_sds_create(context->metric.docstring);

rv = finish_metric(context, false);
rv = finish_metric(context, false, metric_name);
if (rv) {
cmt_sds_destroy(current_metric_docstring);
return rv;
}

if (type == CMT_DECODE_PROMETHEUS_CONTEXT_SAMPLE_TYPE_COUNT) {
metric_name[cmt_sds_len(metric_name) - 6] = 0;
} else {
metric_name[cmt_sds_len(metric_name) - 4] = 0;
}

context->metric.type = current_metric_type;
context->metric.docstring = current_metric_docstring;
context->metric.current_sample_type = type;

return CMT_DECODE_PROMETHEUS_DUPLICATE_SUM_COUNT;
return 0;
}

static int parse_histogram_summary_name(
Expand All @@ -933,15 +949,16 @@ static int parse_histogram_summary_name(
if (current_name_len < parsed_name_len) {
/* current name length cannot be less than the length already parsed. That means
* another metric has started */
return finish_metric(context, true);
return finish_metric(context, true, metric_name);
}

if (strncmp(context->metric.name_orig, metric_name, parsed_name_len)) {
/* the name prefix must be the same or we are starting a new metric */
return finish_metric(context, true);
return finish_metric(context, true, metric_name);
}
else if (parsed_name_len == current_name_len) {
/* parsing HELP after TYPE */
cmt_sds_destroy(metric_name);
return 0;
}

Expand All @@ -965,6 +982,13 @@ static int parse_histogram_summary_name(

/* invalid histogram/summary suffix, treat it as a different metric */
if (!strcmp(metric_name + parsed_name_len, "_bucket")) {
if (sum_found && count_found) {
/* already found both sum and count, so this is a new metric */
return finish_duplicate_histogram_summary_sum_count(
context,
metric_name,
CMT_DECODE_PROMETHEUS_CONTEXT_SAMPLE_TYPE_BUCKET);
}
context->metric.current_sample_type = CMT_DECODE_PROMETHEUS_CONTEXT_SAMPLE_TYPE_BUCKET;
}
else if (!strcmp(metric_name + parsed_name_len, "_sum")) {
Expand All @@ -991,10 +1015,11 @@ static int parse_histogram_summary_name(
}
else {
/* invalid histogram/summary suffix, treat it as a different metric */
return finish_metric(context, true);
return finish_metric(context, true, metric_name);
}

/* still in the same metric */
cmt_sds_destroy(metric_name);
return 0;
}

Expand All @@ -1006,18 +1031,16 @@ static int parse_metric_name(

if (context->metric.name_orig) {
if (strcmp(context->metric.name_orig, metric_name)) {
if (context->metric.type == HISTOGRAM ||
context->metric.type == SUMMARY) {
if (context->metric.type == HISTOGRAM || context->metric.type == SUMMARY) {
ret = parse_histogram_summary_name(context, metric_name);
if (!ret) {
/* bucket/sum/count parsed */
cmt_sds_destroy(metric_name);
return ret;
}
}
else {
/* new metric name means the current metric is finished */
ret = finish_metric(context, true);
return finish_metric(context, true, metric_name);
}
}
else {
Expand All @@ -1027,7 +1050,7 @@ static int parse_metric_name(
}
}

if (!ret || ret == CMT_DECODE_PROMETHEUS_DUPLICATE_SUM_COUNT) {
if (!ret) {
context->metric.name_orig = metric_name;
ret = split_metric_name(context, metric_name,
&(context->metric.ns),
Expand Down
13 changes: 9 additions & 4 deletions lib/cmetrics/src/cmt_decode_prometheus.y
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ start:
| START_LABELS labels
| START_SAMPLES samples
| metrics {
if (finish_metric(context, true)) {
if (finish_metric(context, true, NULL)) {
YYABORT;
}
}
Expand All @@ -66,14 +66,18 @@ header:

help:
HELP METRIC_DOC {
parse_metric_name(context, $1);
if (parse_metric_name(context, $1)) {
YYABORT;
}
context->metric.docstring = $2;
}
;

type:
TYPE metric_type {
parse_metric_name(context, $1);
if (parse_metric_name(context, $1)) {
YYABORT;
}
context->metric.type = $2;
}
;
Expand Down Expand Up @@ -104,7 +108,8 @@ sample:
;

sample_data:
'{' labels '}' values
'{' '}' values
| '{' labels '}' values
| values
;

Expand Down
Loading

0 comments on commit ccb6ec5

Please sign in to comment.