Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor util_health
  • Loading branch information
bduranleau-nr committed Apr 3, 2025
commit 91cd04af60c11d7da3f6bb5752b6aa254b330ba4
10 changes: 8 additions & 2 deletions axiom/tests/test_health.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ static void test_health(void) {
nrh_set_start_time();

nr_unlink("health-bc21b5891f5e44fc9272caef924611a8.yml");
nr_unlink("health-ffffffffffffffffffffffffffffffff.yml");

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

nrh_set_health_filename();
rv = nrh_set_uuid("bc21b5891f5e44fc9272caef924611a");
tlib_pass_if_true("set uuid with invalid length uuid fails", NR_FAILURE == rv,
"rv=%d", (int)rv);

rv = nrh_set_uuid("ffffffffffffffffffffffffffffffff");
tlib_pass_if_true("set uuid succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);

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

tlib_pass_if_exists("./health-bc21b5891f5e44fc9272caef924611a8.yml");
tlib_pass_if_exists("./health-ffffffffffffffffffffffffffffffff.yml");

rv = nrh_set_last_error(NRH_MISSING_APPNAME);

Expand Down
48 changes: 18 additions & 30 deletions axiom/util_health.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "util_logging.h"

#define BILLION (1000000000L)
#define UUID_LEN 32 // 128 bits, 32 hex characters

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

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

static char* nrh_get_uuid(void) {
// TODO: UUID generation logic
return nr_strdup("bc21b5891f5e44fc9272caef924611a8");
nr_status_t nrh_set_uuid(char* uuid) {
if (UUID_LEN != nr_strlen(uuid)) {
return NR_FAILURE;
}

nr_strlcpy(&health_uuid[0], uuid, UUID_LEN + 1);

return NR_SUCCESS;
}

char* nrh_strip_scheme_prefix(char* uri) {
Expand Down Expand Up @@ -78,31 +84,7 @@ char* nrh_strip_scheme_prefix(char* uri) {
}

char* nrh_get_health_filename(void) {
return health_filename;
}

nr_status_t nrh_set_health_filename(void) {
char* uuid = NULL;
char* fname = NULL;

uuid = nrh_get_uuid();

if (NULL == uuid) {
return NR_FAILURE;
}

fname = nr_formatf("health-%s.yml", uuid);

if (nr_strlen(fname) != nr_strlen(health_filename)) {
nr_free(uuid);
return NR_FAILURE;
}

nr_strcpy(&health_filename[0], fname);

nr_free(uuid);
nr_free(fname);
return NR_SUCCESS;
return nr_formatf("health-%s.yml", health_uuid);
}

char* nrh_get_health_location(char* uri) {
Expand Down Expand Up @@ -134,12 +116,18 @@ char* nrh_get_health_location(char* uri) {

char* nrh_get_health_filepath(char* filedir) {
char* filepath = NULL;
char* filename = NULL;

if (NULL == filedir) {
return NULL;
}

filepath = nr_formatf("%s/%s", filedir, health_filename);
filename = nrh_get_health_filename();
if (NULL == filename) {
return NULL;
}

filepath = nr_formatf("%s/%s", filedir, filename);

return filepath;
}
Expand Down
14 changes: 10 additions & 4 deletions axiom/util_health.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ typedef enum _nrhealth_t {
NRH_MAX_STATUS
} nrhealth_t;

/* utility */
extern char* nrh_strip_scheme_prefix(char* uri);
extern nr_status_t nrh_write_health(char* uri);
extern char* nrh_generate_uuid(void);

/* getters */
extern char* nrh_get_health_location(char* uri);
extern char* nrh_get_health_filepath(char* filedir);
extern char* nrh_get_health_filename(void);
extern nr_status_t nrh_set_health_filename(void);
extern nr_status_t nrh_set_start_time(void);
extern long long nrh_get_start_time_ns(void);
extern long long nrh_get_current_time_ns(void);
extern nr_status_t nrh_set_last_error(nrhealth_t code);
extern nrhealth_t nrh_get_last_error(void);
extern nr_status_t nrh_write_health(char* uri);

/* setters */
extern nr_status_t nrh_set_start_time(void);
extern nr_status_t nrh_set_last_error(nrhealth_t code);
extern nr_status_t nrh_set_uuid(char* uuid);

#endif /* UTIL_HEALTH_HDR */
Loading