diff --git a/tests/filter.c b/tests/filter.c index aa72788..45248fe 100644 --- a/tests/filter.c +++ b/tests/filter.c @@ -97,6 +97,9 @@ struct cmt *generate_filter_test_data() ts = cfl_time_now(); cmt_untyped_set(u, ts, 1.3, 2, (char *[]) {"localhost", "eth0"}); + ts = cfl_time_now(); + cmt_untyped_set(u, ts, 1.8, 2, (char *[]) {"dev", "enp1s0"}); + c = cmt_counter_create(cmt, "cmetrics", "test", "cat_counter", "second counter", 2, (char *[]) {"label1", "label2"}); TEST_CHECK(c != NULL); @@ -129,6 +132,12 @@ struct cmt *generate_filter_test_data() TEST_CHECK(h != NULL); ts = cfl_time_now(); + + for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) { + val = hist_observe_values[i]; + cmt_histogram_observe(h, ts, val, 1, (char *[]) {"another_hist"}); + } + for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) { val = hist_observe_values[i]; cmt_histogram_observe(h, ts, val, 1, (char *[]) {"my_label"}); @@ -151,7 +160,7 @@ struct cmt *generate_filter_test_data() r[4] = 5; r[5] = 6; - /* Create a gauge metric type */ + /* Create a summary metric type */ s = cmt_summary_create(cmt, "spring", "kafka_listener", "seconds", "Kafka Listener Timer", 6, q, @@ -308,7 +317,144 @@ void test_filter() cmt_destroy(cmt6); } +void test_filter_with_label_key_value_pairs() +{ + int ret; + cfl_sds_t text; + struct cmt *cmt; + struct cmt *cmt2; + struct cmt *cmt3; + struct cmt *cmt4; + struct cmt *cmt5; + struct cmt *cmt6; + char *label_key; + char *label_value; + char *tmp = NULL; + + cmt = generate_filter_test_data(); + + text = cmt_encode_text_create(cmt); + printf("[Not filtered] ====>\n%s\n", text); + + cmt_encode_text_destroy(text); + + cmt2 = cmt_create(); + TEST_CHECK(cmt2 != NULL); + + label_key = "label3"; + label_value = "tyu"; + + /* filter with label key-value */ + ret = cmt_filter_with_label_pair(cmt2, cmt, label_key, label_value); + TEST_CHECK(ret == 0); + /* check output (fqname) */ + text = cmt_encode_text_create(cmt2); + printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n", + label_key, label_value, text); + + tmp = strstr(text, "label3=\"tyu\""); + TEST_CHECK(tmp == NULL); + tmp = strstr(text, "label3=\"yyy\""); + TEST_CHECK(tmp != NULL); + + cmt_encode_text_destroy(text); + + cmt3 = cmt_create(); + TEST_CHECK(cmt3 != NULL); + + label_key = "label1"; + label_value = "aaa"; + + /* filter with label key-value */ + ret = cmt_filter_with_label_pair(cmt3, cmt, label_key, label_value); + TEST_CHECK(ret == 0); + /* check output (fqname) */ + text = cmt_encode_text_create(cmt3); + printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n", + label_key, label_value, text); + + tmp = strstr(text, "label1=\"aaa\""); + TEST_CHECK(tmp == NULL); + tmp = strstr(text, "label1=\"ccc\""); + TEST_CHECK(tmp != NULL); + + cmt_encode_text_destroy(text); + + cmt4 = cmt_create(); + TEST_CHECK(cmt4 != NULL); + + label_key = "net"; + label_value = "enp1s0"; + + /* filter with label key-value */ + ret = cmt_filter_with_label_pair(cmt4, cmt, label_key, label_value); + TEST_CHECK(ret == 0); + /* check output (fqname) */ + text = cmt_encode_text_create(cmt4); + printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n", + label_key, label_value, text); + + tmp = strstr(text, "net=\"enp1s0\""); + TEST_CHECK(tmp == NULL); + tmp = strstr(text, "net=\"eth0\""); + TEST_CHECK(tmp != NULL); + + cmt_encode_text_destroy(text); + + cmt5 = cmt_create(); + TEST_CHECK(cmt5 != NULL); + + label_key = "exception"; + label_value = "none"; + + /* filter with label key-value */ + ret = cmt_filter_with_label_pair(cmt5, cmt, label_key, label_value); + TEST_CHECK(ret == 0); + /* check output (fqname) */ + text = cmt_encode_text_create(cmt5); + printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n", + label_key, label_value, text); + + tmp = strstr(text, "exception=\"none\""); + TEST_CHECK(tmp == NULL); + tmp = strstr(text, "net=\"eth0\""); + TEST_CHECK(tmp != NULL); + + cmt_encode_text_destroy(text); + + cmt6 = cmt_create(); + TEST_CHECK(cmt6 != NULL); + + label_key = "my_label"; + label_value = "another_hist"; + + /* filter with label key-value */ + ret = cmt_filter_with_label_pair(cmt6, cmt, label_key, label_value); + TEST_CHECK(ret == 0); + /* check output (fqname) */ + text = cmt_encode_text_create(cmt6); + printf("[label matched with label key-value pair: \"%s\", \"%s\" ] ====>\n%s\n", + label_key, label_value, text); + + tmp = strstr(text, "my_label=\"another_hist\""); + TEST_CHECK(tmp == NULL); + tmp = strstr(text, "my_label=\"my_label\""); + TEST_CHECK(tmp != NULL); + + cmt_encode_text_destroy(text); + + /* destroy contexts */ + cmt_destroy(cmt); + cmt_destroy(cmt2); + cmt_destroy(cmt3); + cmt_destroy(cmt4); + cmt_destroy(cmt5); + cmt_destroy(cmt6); +} + + TEST_LIST = { {"filter", test_filter}, + {"filter_with_label_pair", test_filter_with_label_key_value_pairs}, { 0 } };