Skip to content

Commit 9e8f54d

Browse files
committed
lib: cmetrics: upgrade to v2.1.3
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 86ec64a commit 9e8f54d

18 files changed

Lines changed: 763 additions & 70 deletions

lib/cmetrics/.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
submodules: true
145145

146146
- name: Build on ${{ matrix.os }} with ${{ matrix.compiler }}
147-
uses: uraimo/run-on-arch-action@v3.0.1
147+
uses: uraimo/run-on-arch-action@v3.1.0
148148
with:
149149
arch: aarch64
150150
distro: ubuntu_latest

lib/cmetrics/.github/workflows/packages.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
with:
2323
submodules: true
2424

25-
- uses: uraimo/run-on-arch-action@v3.0.1
25+
- uses: uraimo/run-on-arch-action@v3.1.0
2626
name: Build the ${{matrix.format}} packages
2727
with:
2828
arch: aarch64
@@ -129,7 +129,7 @@ jobs:
129129
artifacts/**/*
130130
131131
- name: Release on tag
132-
uses: softprops/action-gh-release@v2
132+
uses: softprops/action-gh-release@v3
133133
if: startsWith(github.ref, 'refs/tags/')
134134
with:
135135
generate_release_notes: true

lib/cmetrics/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66
# CMetrics Version
77
set(CMT_VERSION_MAJOR 2)
88
set(CMT_VERSION_MINOR 1)
9-
set(CMT_VERSION_PATCH 2)
9+
set(CMT_VERSION_PATCH 3)
1010
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")
1111

1212
# Include helpers

lib/cmetrics/include/cmetrics/cmt_encode_prometheus_remote_write.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct cmt_prometheus_metric_metadata {
4242
struct cmt_prometheus_time_series {
4343
uint64_t label_set_hash;
4444
size_t entries_set;
45+
size_t samples_capacity;
4546
Prometheus__TimeSeries data;
4647
struct cfl_list _head;
4748
};

lib/cmetrics/src/cmt_decode_opentelemetry.c

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,25 @@ static struct cmt_map_label *create_label(char *caption, size_t length)
263263
{
264264
struct cmt_map_label *instance;
265265

266+
if (caption == NULL) {
267+
return NULL;
268+
}
269+
266270
instance = calloc(1, sizeof(struct cmt_map_label));
267271

268272
if (instance != NULL) {
269-
if (caption != NULL) {
270-
if (length == 0) {
271-
length = strlen(caption);
272-
}
273+
if (length == 0) {
274+
length = strlen(caption);
275+
}
273276

274-
instance->name = cfl_sds_create_len(caption, length);
277+
instance->name = cfl_sds_create_len(caption, length);
275278

276-
if (instance->name == NULL) {
277-
cmt_errno();
279+
if (instance->name == NULL) {
280+
cmt_errno();
278281

279-
free(instance);
282+
free(instance);
280283

281-
instance = NULL;
282-
}
284+
instance = NULL;
283285
}
284286
}
285287

@@ -315,7 +317,8 @@ static uint64_t compute_metric_hash(struct cmt_map *map, struct cmt_metric *samp
315317
struct cmt_map_label *label_value;
316318
cfl_hash_state_t state;
317319

318-
if (sample == NULL || map == NULL) {
320+
if (sample == NULL || map == NULL ||
321+
map->opts == NULL || map->opts->fqname == NULL) {
319322
return 0;
320323
}
321324

@@ -328,6 +331,9 @@ static uint64_t compute_metric_hash(struct cmt_map *map, struct cmt_metric *samp
328331

329332
cfl_list_foreach(head, &sample->labels) {
330333
label_value = cfl_list_entry(head, struct cmt_map_label, _head);
334+
if (label_value->name == NULL) {
335+
continue;
336+
}
331337
cfl_hash_64bits_update(&state, label_value->name, cfl_sds_len(label_value->name));
332338
}
333339

@@ -599,14 +605,19 @@ static int decode_data_point_labels(struct cmt *cmt,
599605
attribute_index++) {
600606

601607
attribute = attribute_list[attribute_index];
608+
if (attribute == NULL || attribute->key == NULL || attribute->key[0] == '\0') {
609+
result = CMT_DECODE_OPENTELEMETRY_INVALID_ARGUMENT_ERROR;
610+
break;
611+
}
602612

603613
label_found = CMT_FALSE;
604614
label_index = 0;
605615

606616
cfl_list_foreach(label_iterator, &map->label_keys) {
607617
current_label = cfl_list_entry(label_iterator, struct cmt_map_label, _head);
608618

609-
if (strcmp(current_label->name, attribute->key) == 0) {
619+
if (current_label->name != NULL &&
620+
strcmp(current_label->name, attribute->key) == 0) {
610621
label_found = CMT_TRUE;
611622

612623
break;
@@ -636,20 +647,31 @@ static int decode_data_point_labels(struct cmt *cmt,
636647
value_index_list[map_label_index];
637648

638649
if (attribute->value == NULL) {
650+
result = append_new_metric_label_value(metric, "", 0);
639651
continue;
640652
}
641653

642654
if (attribute->value->value_case == OPENTELEMETRY__PROTO__COMMON__V1__ANY_VALUE__VALUE_STRING_VALUE) {
643-
result = append_new_metric_label_value(metric, attribute->value->string_value, 0);
655+
result = append_new_metric_label_value(metric,
656+
attribute->value->string_value != NULL ?
657+
attribute->value->string_value : "",
658+
0);
644659
}
645660
else if (attribute->value->value_case ==
646661
OPENTELEMETRY__PROTO__COMMON__V1__ANY_VALUE__VALUE_STRING_VALUE_STRINDEX) {
647662
result = append_new_metric_label_value(metric, "", 0);
648663
}
649664
else if (attribute->value->value_case == OPENTELEMETRY__PROTO__COMMON__V1__ANY_VALUE__VALUE_BYTES_VALUE) {
650-
result = append_new_metric_label_value(metric,
651-
(char *) attribute->value->bytes_value.data,
652-
attribute->value->bytes_value.len);
665+
if (attribute->value->bytes_value.data == NULL &&
666+
attribute->value->bytes_value.len > 0) {
667+
result = CMT_DECODE_OPENTELEMETRY_INVALID_ARGUMENT_ERROR;
668+
}
669+
else {
670+
result = append_new_metric_label_value(metric,
671+
attribute->value->bytes_value.data != NULL ?
672+
(char *) attribute->value->bytes_value.data : "",
673+
attribute->value->bytes_value.len);
674+
}
653675
}
654676
else if (attribute->value->value_case == OPENTELEMETRY__PROTO__COMMON__V1__ANY_VALUE__VALUE_BOOL_VALUE) {
655677
snprintf(dummy_label_value, sizeof(dummy_label_value) - 1, "%d", attribute->value->bool_value);
@@ -667,9 +689,12 @@ static int decode_data_point_labels(struct cmt *cmt,
667689
result = append_new_metric_label_value(metric, dummy_label_value, 0);
668690
}
669691
else {
670-
result = append_new_metric_label_value(metric, NULL, 0);
692+
result = append_new_metric_label_value(metric, "", 0);
671693
}
672694
}
695+
else {
696+
result = append_new_metric_label_value(metric, "", 0);
697+
}
673698
}
674699

675700
free(value_index_list);

lib/cmetrics/src/cmt_decode_prometheus_remote_write.c

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,23 @@ static char *cmt_metric_name_from_labels(Prometheus__TimeSeries *ts)
4646
int i;
4747
int count;
4848

49+
if (ts == NULL || ts->labels == NULL) {
50+
return NULL;
51+
}
52+
4953
count = ts->n_labels;
5054
for (i = 0; i < count; i++) {
55+
if (ts->labels[i] == NULL ||
56+
ts->labels[i]->name == NULL ||
57+
ts->labels[i]->name[0] == '\0') {
58+
continue;
59+
}
60+
5161
if (strncmp("__name__", ts->labels[i]->name, 8) == 0) {
62+
if (ts->labels[i]->value == NULL) {
63+
return NULL;
64+
}
65+
5266
return strdup(ts->labels[i]->value);
5367
}
5468
}
@@ -60,26 +74,28 @@ static struct cmt_map_label *create_map_label(char *caption, size_t length)
6074
{
6175
struct cmt_map_label *map_label;
6276

77+
if (caption == NULL) {
78+
return NULL;
79+
}
80+
6381
map_label = calloc(1, sizeof(struct cmt_map_label));
6482
if (!map_label) {
6583
return NULL;
6684
}
6785

6886
if (map_label != NULL) {
69-
if (caption != NULL) {
70-
if (length == 0) {
71-
length = strlen(caption);
72-
}
87+
if (length == 0) {
88+
length = strlen(caption);
89+
}
7390

74-
map_label->name = cfl_sds_create_len(caption, length);
91+
map_label->name = cfl_sds_create_len(caption, length);
7592

76-
if (map_label->name == NULL) {
77-
cmt_errno();
93+
if (map_label->name == NULL) {
94+
cmt_errno();
7895

79-
free(map_label);
96+
free(map_label);
8097

81-
map_label = NULL;
82-
}
98+
map_label = NULL;
8399
}
84100
}
85101

@@ -156,14 +172,19 @@ static int decode_labels(struct cmt *cmt,
156172
prom_label_index++) {
157173

158174
label = labels[prom_label_index];
175+
if (label == NULL || label->name == NULL || label->name[0] == '\0') {
176+
result = CMT_DECODE_PROMETHEUS_REMOTE_WRITE_INVALID_ARGUMENT_ERROR;
177+
break;
178+
}
159179

160180
label_found = CMT_FALSE;
161181
label_index = 0;
162182

163183
cfl_list_foreach(label_iterator, &map->label_keys) {
164184
current_label = cfl_list_entry(label_iterator, struct cmt_map_label, _head);
165185

166-
if (strcmp(current_label->name, label->name) == 0) {
186+
if (current_label->name != NULL &&
187+
strcmp(current_label->name, label->name) == 0) {
167188
label_found = CMT_TRUE;
168189

169190
break;
@@ -190,7 +211,12 @@ static int decode_labels(struct cmt *cmt,
190211

191212
if (value_index_list[map_label_index] != NULL) {
192213
label = (Prometheus__Label *) value_index_list[map_label_index];
193-
result = append_new_metric_label_value(metric, label->value, 0);
214+
result = append_new_metric_label_value(metric,
215+
label->value != NULL ? label->value : "",
216+
0);
217+
}
218+
else {
219+
result = append_new_metric_label_value(metric, "", 0);
194220
}
195221
}
196222

@@ -426,6 +452,22 @@ static int decode_histogram_points(struct cmt *cmt,
426452
map->metric_static_set = CMT_TRUE;
427453
}
428454

455+
if (metric->hist_buckets == NULL) {
456+
metric->hist_buckets = calloc(1, sizeof(uint64_t) *
457+
(histogram->buckets->count + 1));
458+
if (metric->hist_buckets == NULL) {
459+
if (static_metric_detected == CMT_FALSE) {
460+
destroy_label_list(&metric->labels);
461+
462+
cfl_list_del(&metric->_head);
463+
464+
free(metric);
465+
}
466+
467+
return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_ALLOCATION_ERROR;
468+
}
469+
}
470+
429471
if (result == CMT_DECODE_PROMETHEUS_REMOTE_WRITE_SUCCESS) {
430472
if (hist->n_negative_spans > 0) {
431473
for (i = 0; i < hist->n_negative_counts; i++) {
@@ -542,17 +584,22 @@ static int decode_metrics_entry(struct cmt *cmt,
542584
ts_count = write->n_timeseries;
543585
for (i = 0; i < ts_count; i++) {
544586
ts = write->timeseries[i];
587+
if (ts == NULL) {
588+
return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_DECODE_ERROR;
589+
}
590+
545591
meta_count = write->n_metadata;
546592
hist_count = ts->n_histograms;
547-
if (meta_count > 0) {
593+
metadata = NULL;
594+
if (meta_count > i) {
548595
metadata = write->metadata[i];
549596
}
550-
if (metadata == NULL) {
551-
type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__GAUGE;
597+
if (hist_count > 0) {
598+
type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__HISTOGRAM;
552599
metric_description = "-";
553600
}
554-
else if (hist_count > 0) {
555-
type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__HISTOGRAM;
601+
else if (metadata == NULL) {
602+
type = PROMETHEUS__METRIC_METADATA__METRIC_TYPE__GAUGE;
556603
metric_description = "-";
557604
}
558605
else {
@@ -635,14 +682,17 @@ static int decode_metrics_entry(struct cmt *cmt,
635682
metric_subsystem,
636683
metric_name,
637684
metric_description,
638-
(struct cmt_histogram_buckets *) cmt,
685+
NULL,
639686
0, NULL);
640687

641688
if (instance == NULL) {
642689
free(metric_name);
643690
return CMT_DECODE_PROMETHEUS_REMOTE_WRITE_ALLOCATION_ERROR;
644691
}
645692

693+
cmt_histogram_buckets_destroy(((struct cmt_histogram *) instance)->buckets);
694+
((struct cmt_histogram *) instance)->buckets = NULL;
695+
646696
result = decode_histogram_entry(cmt, instance, ts);
647697

648698
if (result) {

lib/cmetrics/src/cmt_encode_cloudwatch_emf.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void pack_basic_header(mpack_writer_t *writer, struct cmt *cmt,
7272
mpack_start_array(writer, labels);
7373
cfl_list_foreach(head, &map->label_keys) {
7474
label_k = cfl_list_entry(head, struct cmt_map_label, _head);
75-
mpack_write_cstr(writer, label_k->name);
75+
mpack_write_cstr(writer, label_k->name != NULL ? label_k->name : "");
7676
}
7777

7878
cfl_list_foreach(head, &cmt->static_labels->list) {
@@ -246,13 +246,16 @@ static int pack_metric(mpack_writer_t *writer, struct cmt *cmt,
246246
double val = 0.0;
247247
int c_labels = 0;
248248
int static_labels = 0;
249+
int label_key_count;
250+
int label_index;
249251
struct cfl_list *head;
250252
struct cmt_map_label *label_k;
251253
struct cmt_map_label *label_v;
252254
struct cmt_label *slabel;
253255
struct cmt_opts *opts = map->opts;
254256

255257
c_labels = cfl_list_size(&metric->labels);
258+
label_key_count = map->label_count;
256259
s = 3;
257260

258261
if (c_labels > 0) {
@@ -294,14 +297,20 @@ static int pack_metric(mpack_writer_t *writer, struct cmt *cmt,
294297
pack_basic_header_finish(writer);
295298

296299
/* dimensions */
297-
if (c_labels > 0) {
300+
if (c_labels > 0 && label_key_count > 0) {
298301
label_k = cfl_list_entry_first(&map->label_keys, struct cmt_map_label, _head);
299302

303+
label_index = 0;
300304
cfl_list_foreach(head, &metric->labels) {
305+
if (label_index >= label_key_count) {
306+
break;
307+
}
308+
301309
label_v = cfl_list_entry(head, struct cmt_map_label, _head);
302-
mpack_write_cstr(writer, label_k->name);
303-
mpack_write_cstr(writer, label_v->name);
310+
mpack_write_cstr(writer, label_k->name != NULL ? label_k->name : "");
311+
mpack_write_cstr(writer, label_v->name != NULL ? label_v->name : "");
304312

313+
label_index++;
305314
label_k = cfl_list_entry_next(&label_k->_head, struct cmt_map_label,
306315
_head, &map->label_keys);
307316
}

0 commit comments

Comments
 (0)