Skip to content

Commit

Permalink
Resolving ISSUE-34 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
miroswan authored Dec 8, 2020
1 parent 72e72d4 commit 23b260f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion prom/src/prom_metric_formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int prom_metric_formatter_load_sample(prom_metric_formatter_t *self, prom_metric
if (r) return r;

char buffer[50];
sprintf(buffer, "%f", sample->r_value);
sprintf(buffer, "%.17g", sample->r_value);
r = prom_string_builder_add_str(self->string_builder, buffer);
if (r) return r;

Expand Down
5 changes: 4 additions & 1 deletion prom/src/prom_metric_sample_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ static void prom_metric_sample_histogram_free_str_generic(void *gen) {

char *prom_metric_sample_histogram_bucket_to_str(double bucket) {
char *buf = (char *)prom_malloc(sizeof(char) * 50);
sprintf(buf, "%f", bucket);
sprintf(buf, "%g", bucket);
if (!strchr(buf, '.')) {
strcat(buf, ".0");
}
return buf;
}
33 changes: 23 additions & 10 deletions prom/test/prom_collector_registry_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,29 @@ void test_prom_collector_registry_bridge(void) {

const char *result = prom_collector_registry_bridge(PROM_COLLECTOR_REGISTRY_DEFAULT);

const char *expected =
"# HELP test_counter counter under test\n# TYPE test_counter counter\ntest_counter{label=\"foo\"} 1.000000\n\n# "
"HELP test_gauge gauge under test\n# TYPE test_gauge gauge\ntest_gauge{label=\"foo\"} 2.000000\n\n# HELP "
"test_histogram histogram under test\n# TYPE test_histogram histogram\ntest_histogram{le=\"5.000000\"} "
"1.000000\ntest_histogram{le=\"10.000000\"} 2.000000\ntest_histogram{le=\"+Inf\"} 2.000000\ntest_histogram_count "
"2.000000\ntest_histogram_sum 10.000000\n\n# HELP process_max_fds Maximum number of open file descriptors.\n# "
"TYPE process_max_fds gauge\nprocess_max_fds 1048576.000000\n\n# HELP process_virtual_memory_max_bytes Maximum "
"amount of virtual memory available in bytes.\n# TYPE process_virtual_memory_max_bytes "
"gauge\nprocess_virtual_memory_max_bytes -1.000000\n\n";
TEST_ASSERT_NOT_NULL(strstr(result, expected));
const char *expected[] = {
"# HELP test_counter counter under test",
"# TYPE test_counter counter",
"test_counter{label=\"foo\"}",
"HELP test_gauge gauge under test",
"# TYPE test_gauge gauge",
"test_gauge{label=\"foo\"}",
"# HELP test_histogram histogram under test",
"# TYPE test_histogram histogram\ntest_histogram{le=\"5.0\"}",
"test_histogram{le=\"10.0\"}",
"test_histogram{le=\"+Inf\"}",
"test_histogram_count",
"test_histogram_sum",
"# HELP process_max_fds Maximum number of open file descriptors.",
"# TYPE process_max_fds gauge",
"process_max_fds",
"# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.",
"# TYPE process_virtual_memory_max_bytes"};

for (int i = 0; i < 17; i++) {
TEST_ASSERT_NOT_NULL(strstr(result, expected[i]));
}

free((char *)result);
result = NULL;

Expand Down
6 changes: 3 additions & 3 deletions prom/test/prom_histogram_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ void test_prom_histogram(void) {
char *bucket_key = prom_metric_sample_histogram_bucket_to_str(5.0);
const char *l_value = prom_map_get(h_sample->l_values, bucket_key);
prom_metric_sample_t *sample = (prom_metric_sample_t *)prom_map_get(h_sample->samples, l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"5.000000\"}", sample->l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"5.0\"}", sample->l_value);
TEST_ASSERT_EQUAL_DOUBLE(1.0, sample->r_value);
free((char *)bucket_key);
bucket_key = NULL;

bucket_key = prom_metric_sample_histogram_bucket_to_str(10.0);
l_value = prom_map_get(h_sample->l_values, bucket_key);
sample = (prom_metric_sample_t *)prom_map_get(h_sample->samples, l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"10.000000\"}", sample->l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"10.0\"}", sample->l_value);
TEST_ASSERT_EQUAL_DOUBLE(2.0, sample->r_value);
free((char *)bucket_key);
bucket_key = NULL;

bucket_key = prom_metric_sample_histogram_bucket_to_str(15.0);
l_value = prom_map_get(h_sample->l_values, bucket_key);
sample = (prom_metric_sample_t *)prom_map_get(h_sample->samples, l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"15.000000\"}", sample->l_value);
TEST_ASSERT_EQUAL_STRING("test_histogram{le=\"15.0\"}", sample->l_value);
TEST_ASSERT_EQUAL_DOUBLE(3.0, sample->r_value);
free((char *)bucket_key);
bucket_key = NULL;
Expand Down
42 changes: 28 additions & 14 deletions prom/test/prom_metric_formatter_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void test_prom_metric_formatter_load_l_value(void) {
prom_metric_formatter_load_l_value(mf, "test", NULL, 3, keys, values);
char *actual = prom_metric_formatter_dump(mf);
char *expected = "test{foo=\"one\",bar=\"two\",bing=\"three\"}";
TEST_ASSERT_EQUAL_STRING(expected, actual);
TEST_ASSERT_NOT_NULL(strstr(actual, expected));

free(actual);
actual = NULL;
Expand All @@ -37,8 +37,8 @@ void test_prom_metric_formatter_load_sample(void) {
prom_metric_sample_t *sample = prom_metric_sample_new(PROM_COUNTER, l_value, 22.2);
prom_metric_formatter_load_sample(mf, sample);
char *actual = prom_metric_formatter_dump(mf);
char *expected = "test{foo=\"one\",bar=\"two\",bing=\"three\"} 22.200000\n";
TEST_ASSERT_EQUAL_STRING(expected, actual);
char *expected = "test{foo=\"one\",bar=\"two\",bing=\"three\"}";
TEST_ASSERT_NOT_NULL(strstr(actual, expected));

free(actual);
actual = NULL;
Expand All @@ -60,10 +60,13 @@ void test_prom_metric_formatter_load_metric(void) {
prom_metric_formatter_load_metric(mf, m);
const char *result = prom_metric_formatter_dump(mf);

TEST_ASSERT_EQUAL_STRING(
"# HELP test_counter counter under test\n# TYPE test_counter counter\ntest_counter{foo=\"f\",bar=\"b\"} "
"2.300000\ntest_counter{foo=\"o\",bar=\"r\"} 4.600000\n\n",
result);
char *substr =
"# HELP test_counter counter under test\n# TYPE test_counter counter\ntest_counter{foo=\"f\",bar=\"b\"}";

TEST_ASSERT_NOT_NULL(strstr(result, substr));

substr = "\ntest_counter{foo=\"o\",bar=\"r\"}";
TEST_ASSERT_NOT_NULL(strstr(result, substr));

free((char *)result);
result = NULL;
Expand Down Expand Up @@ -91,13 +94,24 @@ void test_prom_metric_formatter_load_metrics(void) {
prom_metric_formatter_load_metrics(mf, PROM_COLLECTOR_REGISTRY_DEFAULT->collectors);

const char *result = prom_metric_formatter_dump(mf);
const char *expected =
"# HELP test_counter_a counter under test\n# TYPE test_counter_a counter\ntest_counter_a 2.300000\n\n# HELP "
"test_counter_b counter under test\n# TYPE test_counter_b counter\ntest_counter_b 4.600000\n\n# HELP "
"process_max_fds Maximum number of open file descriptors.\n# TYPE process_max_fds gauge\nprocess_max_fds "
"1048576.000000\n\n# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in "
"bytes.\n# TYPE process_virtual_memory_max_bytes gauge\nprocess_virtual_memory_max_bytes -1.000000\n\n";
TEST_ASSERT_NOT_NULL(strstr(result, expected));
const char *expected[] = {
"# HELP test_counter_a counter under test",
"# TYPE test_counter_a counter",
"test_counter_a",
"# HELP test_counter_b counter under test",
"# TYPE test_counter_b counter",
"test_counter_b",
"# HELP process_max_fds Maximum number of open file descriptors.",
"# TYPE process_max_fds gauge",
"process_max_fds 1048576",
"# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.",
"# TYPE process_virtual_memory_max_bytes gauge",
"process_virtual_memory_max_bytes -1"};

for (int i = 0; i < 12; i++) {
TEST_ASSERT_NOT_NULL(strstr(result, expected[i]));
}

free((char *)result);
result = NULL;

Expand Down
1 change: 0 additions & 1 deletion promtest/test/promtest_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ static int promtest_parse_counter_output(const char *output, char **value) {
TEST_FAIL_MESSAGE("failed to get metric sample");
}
*value = (char *)json_object_get_string(sample, "value");
break;
}
if (strlen(*value) == 0) {
return 1;
Expand Down
1 change: 0 additions & 1 deletion promtest/test/promtest_gauge.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ static int promtest_parse_gauge_output(const char *output, char **value) {
TEST_FAIL_MESSAGE("failed to get metric sample");
}
*value = (char *)json_object_get_string(sample, "value");
break;
}
if (strlen(*value) == 0) {
return 1;
Expand Down

0 comments on commit 23b260f

Please sign in to comment.