Skip to content

Commit c57fb0c

Browse files
committed
refactor health file logic
1 parent 01e62cf commit c57fb0c

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

axiom/util_health.c

+18-32
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ static nrh_status_codes_t health_statuses[NRH_MAX_STATUS] = {
4242
};
4343
// clang-format on
4444

45-
static int healthfile_fd = -1;
4645
static struct timespec start_time = {0, 0};
4746
static nrhealth_t last_error_code = NRH_HEALTHY;
4847
static char health_filename[] = "health-bc21b5891f5e44fc9272caef924611a8.yml";
@@ -145,15 +144,6 @@ char* nrh_get_health_filepath(char* filedir) {
145144
return filepath;
146145
}
147146

148-
void nrh_close_health_file(void) {
149-
if (-1 == healthfile_fd) {
150-
return;
151-
}
152-
153-
nr_close(healthfile_fd);
154-
healthfile_fd = -1;
155-
}
156-
157147
nr_status_t nrh_set_start_time(void) {
158148
clock_gettime(CLOCK_REALTIME, &start_time);
159149

@@ -176,10 +166,6 @@ long long nrh_get_current_time_ns(void) {
176166
return (long long)(ts.tv_sec * BILLION + ts.tv_nsec);
177167
}
178168

179-
int nrh_get_healthfile_fd(void) {
180-
return healthfile_fd;
181-
}
182-
183169
nr_status_t nrh_set_last_error(nrhealth_t status) {
184170
if (status < NRH_HEALTHY || status >= NRH_MAX_STATUS) {
185171
return NR_FAILURE;
@@ -198,32 +184,32 @@ nrhealth_t nrh_get_last_error(void) {
198184
return last_error_code;
199185
}
200186

201-
#ifndef HEALTH_STATUS_LINE
202-
#define HEALTH_STATUS_LINE(field, value) \
203-
nr_write(healthfile_fd, nr_formatf("%s: %s\n", field, value), \
204-
nr_strlen(nr_formatf("%s: %s\n", field, value)));
205-
206-
nr_status_t nrh_write_health(void) {
187+
nr_status_t nrh_write_health(char* uri) {
207188
nrhealth_t status = last_error_code;
189+
FILE* fp = NULL;
190+
char* filepath = NULL;
208191

209-
if (-1 == healthfile_fd) {
210-
// healthfile not initialized
192+
if (NULL == uri) {
193+
// invalid uri
211194
return NR_FAILURE;
212195
}
213196

214-
HEALTH_STATUS_LINE("healthy", NRH_HEALTHY == status ? "true" : "false");
215-
216-
HEALTH_STATUS_LINE("status", health_statuses[status].description);
197+
filepath = nrh_get_health_filepath(uri);
217198

218-
HEALTH_STATUS_LINE("last_error_code", health_statuses[status].code);
199+
fp = fopen(filepath, "w");
200+
if (NULL == fp) {
201+
// unable to open healthfile
202+
return NR_FAILURE;
203+
}
219204

220-
HEALTH_STATUS_LINE("status_time_unix_nano",
221-
nr_formatf("%lld", nrh_get_current_time_ns()));
205+
fprintf(fp, "healthy: %s\n", NRH_HEALTHY == status ? "true" : "false");
206+
fprintf(fp, "status: %s\n", health_statuses[status].description);
207+
fprintf(fp, "last_error_code: %s\n", health_statuses[status].code);
208+
fprintf(fp, "status_time_unix_nano: %lld\n", nrh_get_current_time_ns());
209+
fprintf(fp, "status_time_unix_nano: %lld\n", nrh_get_start_time_ns());
222210

223-
HEALTH_STATUS_LINE("start_time_unix_nano",
224-
nr_formatf("%lld", nrh_get_start_time_ns()));
211+
fclose(fp);
212+
nr_free(filepath);
225213

226214
return NR_SUCCESS;
227215
}
228-
#undef HEALTH_STATUS_LINE
229-
#endif

axiom/util_health.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ extern char* nrh_get_health_location(char* uri);
3434
extern char* nrh_get_health_filepath(char* filedir);
3535
extern char* nrh_get_health_filename(void);
3636
extern nr_status_t nrh_set_health_filename(void);
37-
extern void nrh_close_health_file(void);
3837
extern nr_status_t nrh_set_start_time(void);
3938
extern long long nrh_get_start_time_ns(void);
4039
extern long long nrh_get_current_time_ns(void);
41-
extern int nrh_get_healthfile_fd(void);
4240
extern nr_status_t nrh_set_last_error(nrhealth_t code);
4341
extern nrhealth_t nrh_get_last_error(void);
44-
extern nr_status_t nrh_write_health(void);
42+
extern nr_status_t nrh_write_health(char* uri);
4543

4644
#endif /* UTIL_HEALTH_HDR */

0 commit comments

Comments
 (0)