Skip to content

Commit 91cd04a

Browse files
committed
refactor util_health
1 parent cf1ccff commit 91cd04a

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

axiom/tests/test_health.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ static void test_health(void) {
2323
nrh_set_start_time();
2424

2525
nr_unlink("health-bc21b5891f5e44fc9272caef924611a8.yml");
26+
nr_unlink("health-ffffffffffffffffffffffffffffffff.yml");
2627

2728
location = nrh_get_health_location("/should/not/exist");
2829
tlib_pass_if_true("initialization to bad path fails", NULL == location,
2930
"location=%s", NULL == location ? "NULL" : location);
3031
nr_free(location);
3132

32-
nrh_set_health_filename();
33+
rv = nrh_set_uuid("bc21b5891f5e44fc9272caef924611a");
34+
tlib_pass_if_true("set uuid with invalid length uuid fails", NR_FAILURE == rv,
35+
"rv=%d", (int)rv);
36+
37+
rv = nrh_set_uuid("ffffffffffffffffffffffffffffffff");
38+
tlib_pass_if_true("set uuid succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
3339

3440
location = nrh_get_health_location("file://./");
3541
tlib_pass_if_true("initialization to good path succeeds", NULL != location,
@@ -41,7 +47,7 @@ static void test_health(void) {
4147
tlib_pass_if_true("health file write succeeds", NR_SUCCESS == rv, "rv=%d",
4248
(int)rv);
4349

44-
tlib_pass_if_exists("./health-bc21b5891f5e44fc9272caef924611a8.yml");
50+
tlib_pass_if_exists("./health-ffffffffffffffffffffffffffffffff.yml");
4551

4652
rv = nrh_set_last_error(NRH_MISSING_APPNAME);
4753

axiom/util_health.c

+18-30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "util_logging.h"
2020

2121
#define BILLION (1000000000L)
22+
#define UUID_LEN 32 // 128 bits, 32 hex characters
2223

2324
typedef struct _nrh_status_codes_t {
2425
const char* code;
@@ -44,11 +45,16 @@ static nrh_status_codes_t health_statuses[NRH_MAX_STATUS] = {
4445

4546
static struct timespec start_time = {0, 0};
4647
static nrhealth_t last_error_code = NRH_HEALTHY;
47-
static char health_filename[] = "health-bc21b5891f5e44fc9272caef924611a8.yml";
48+
static char health_uuid[] = "bc21b5891f5e44fc9272caef924611a8";
4849

49-
static char* nrh_get_uuid(void) {
50-
// TODO: UUID generation logic
51-
return nr_strdup("bc21b5891f5e44fc9272caef924611a8");
50+
nr_status_t nrh_set_uuid(char* uuid) {
51+
if (UUID_LEN != nr_strlen(uuid)) {
52+
return NR_FAILURE;
53+
}
54+
55+
nr_strlcpy(&health_uuid[0], uuid, UUID_LEN + 1);
56+
57+
return NR_SUCCESS;
5258
}
5359

5460
char* nrh_strip_scheme_prefix(char* uri) {
@@ -78,31 +84,7 @@ char* nrh_strip_scheme_prefix(char* uri) {
7884
}
7985

8086
char* nrh_get_health_filename(void) {
81-
return health_filename;
82-
}
83-
84-
nr_status_t nrh_set_health_filename(void) {
85-
char* uuid = NULL;
86-
char* fname = NULL;
87-
88-
uuid = nrh_get_uuid();
89-
90-
if (NULL == uuid) {
91-
return NR_FAILURE;
92-
}
93-
94-
fname = nr_formatf("health-%s.yml", uuid);
95-
96-
if (nr_strlen(fname) != nr_strlen(health_filename)) {
97-
nr_free(uuid);
98-
return NR_FAILURE;
99-
}
100-
101-
nr_strcpy(&health_filename[0], fname);
102-
103-
nr_free(uuid);
104-
nr_free(fname);
105-
return NR_SUCCESS;
87+
return nr_formatf("health-%s.yml", health_uuid);
10688
}
10789

10890
char* nrh_get_health_location(char* uri) {
@@ -134,12 +116,18 @@ char* nrh_get_health_location(char* uri) {
134116

135117
char* nrh_get_health_filepath(char* filedir) {
136118
char* filepath = NULL;
119+
char* filename = NULL;
137120

138121
if (NULL == filedir) {
139122
return NULL;
140123
}
141124

142-
filepath = nr_formatf("%s/%s", filedir, health_filename);
125+
filename = nrh_get_health_filename();
126+
if (NULL == filename) {
127+
return NULL;
128+
}
129+
130+
filepath = nr_formatf("%s/%s", filedir, filename);
143131

144132
return filepath;
145133
}

axiom/util_health.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ typedef enum _nrhealth_t {
2929
NRH_MAX_STATUS
3030
} nrhealth_t;
3131

32+
/* utility */
3233
extern char* nrh_strip_scheme_prefix(char* uri);
34+
extern nr_status_t nrh_write_health(char* uri);
35+
extern char* nrh_generate_uuid(void);
36+
37+
/* getters */
3338
extern char* nrh_get_health_location(char* uri);
3439
extern char* nrh_get_health_filepath(char* filedir);
3540
extern char* nrh_get_health_filename(void);
36-
extern nr_status_t nrh_set_health_filename(void);
37-
extern nr_status_t nrh_set_start_time(void);
3841
extern long long nrh_get_start_time_ns(void);
3942
extern long long nrh_get_current_time_ns(void);
40-
extern nr_status_t nrh_set_last_error(nrhealth_t code);
4143
extern nrhealth_t nrh_get_last_error(void);
42-
extern nr_status_t nrh_write_health(char* uri);
44+
45+
/* setters */
46+
extern nr_status_t nrh_set_start_time(void);
47+
extern nr_status_t nrh_set_last_error(nrhealth_t code);
48+
extern nr_status_t nrh_set_uuid(char* uuid);
4349

4450
#endif /* UTIL_HEALTH_HDR */

0 commit comments

Comments
 (0)