refactor: HTTP total counters

master
Sean McBride 2 years ago
parent 89fc0b26fd
commit 2b5957c394

@ -63,38 +63,22 @@
static inline const char *
http_header_build(int status_code)
{
const char *response;
int rc;
switch (status_code) {
case 400:
response = HTTP_RESPONSE_400_BAD_REQUEST;
http_total_increment_4XX();
break;
return HTTP_RESPONSE_400_BAD_REQUEST;
case 404:
response = HTTP_RESPONSE_404_NOT_FOUND;
http_total_increment_4XX();
break;
return HTTP_RESPONSE_404_NOT_FOUND;
case 413:
response = HTTP_RESPONSE_413_PAYLOAD_TOO_LARGE;
http_total_increment_4XX();
break;
return HTTP_RESPONSE_413_PAYLOAD_TOO_LARGE;
case 429:
response = HTTP_RESPONSE_429_TOO_MANY_REQUESTS;
http_total_increment_4XX();
break;
return HTTP_RESPONSE_429_TOO_MANY_REQUESTS;
case 500:
response = HTTP_RESPONSE_500_INTERNAL_SERVER_ERROR;
http_total_increment_5XX();
break;
return HTTP_RESPONSE_500_INTERNAL_SERVER_ERROR;
case 503:
response = HTTP_RESPONSE_503_SERVICE_UNAVAILABLE;
http_total_increment_5XX();
break;
return HTTP_RESPONSE_503_SERVICE_UNAVAILABLE;
default:
panic("%d is not a valid status code\n", status_code);
}
return response;
}
static inline size_t

@ -179,7 +179,7 @@ http_session_set_response_header(struct http_session *session, int status_code,
{
assert(session != NULL);
assert(status_code >= 200 && status_code <= 599);
http_total_increment(status_code);
http_total_increment_response(status_code);
/* We might not have actually matched a route */
if (likely(session->route != NULL)) { route_metrics_increment(&session->route->metrics, status_code); }

@ -9,10 +9,9 @@
* behind a compiler flag. 2XX and 4XX can be incremented by worker cores, so they are behind a flag because
* of concerns about contention
*/
#ifdef HTTP_TOTAL_COUNTERS
extern _Atomic uint32_t http_total_requests;
extern _Atomic uint32_t http_total_5XX;
#ifdef LOG_TOTAL_REQS_RESPS
extern _Atomic uint32_t http_total_2XX;
extern _Atomic uint32_t http_total_4XX;
#endif
@ -20,54 +19,32 @@ extern _Atomic uint32_t http_total_4XX;
static inline void
http_total_init()
{
#ifdef HTTP_TOTAL_COUNTERS
atomic_init(&http_total_requests, 0);
atomic_init(&http_total_5XX, 0);
#ifdef LOG_TOTAL_REQS_RESPS
atomic_init(&http_total_2XX, 0);
atomic_init(&http_total_4XX, 0);
atomic_init(&http_total_5XX, 0);
#endif
}
static inline void
http_total_increment_request()
{
#ifdef HTTP_TOTAL_COUNTERS
atomic_fetch_add(&http_total_requests, 1);
}
static inline void
http_total_increment_2XX()
{
#ifdef LOG_TOTAL_REQS_RESPS
atomic_fetch_add(&http_total_2XX, 1);
#endif
}
static inline void
http_total_increment_4XX()
{
#ifdef LOG_TOTAL_REQS_RESPS
atomic_fetch_add(&http_total_4XX, 1);
#endif
}
static inline void
http_total_increment_5XX()
{
atomic_fetch_add(&http_total_5XX, 1);
}
static inline void
http_total_increment(int status_code)
http_total_increment_response(int status_code)
{
#ifdef LOG_TOTAL_REQS_RESPS
#ifdef HTTP_TOTAL_COUNTERS
if (status_code >= 200 && status_code <= 299) {
http_total_increment_2XX();
atomic_fetch_add(&http_total_2XX, 1);
} else if (status_code >= 400 && status_code <= 499) {
http_total_increment_4XX();
atomic_fetch_add(&http_total_4XX, 1);
} else if (status_code >= 500 && status_code <= 599) {
http_total_increment_5XX();
atomic_fetch_add(&http_total_5XX, 1);
}
#else
if (status_code >= 500 && status_code <= 599) { http_total_increment_5XX(); }
#endif
}

@ -5,31 +5,26 @@
/* 2XX + 4XX should equal sandboxes */
/* Listener Core Bookkeeping */
#ifdef HTTP_TOTAL_COUNTERS
_Atomic uint32_t http_total_requests = 0;
_Atomic uint32_t http_total_5XX = 0;
#ifdef LOG_TOTAL_REQS_RESPS
_Atomic uint32_t http_total_2XX = 0;
_Atomic uint32_t http_total_4XX = 0;
_Atomic uint32_t http_total_2XX = 0;
_Atomic uint32_t http_total_4XX = 0;
#endif
/* Primarily intended to be called via GDB */
void
http_total_log()
{
uint32_t total_reqs = atomic_load(&http_total_requests);
uint32_t total_5XX = atomic_load(&http_total_5XX);
#ifdef LOG_TOTAL_REQS_RESPS
uint32_t total_2XX = atomic_load(&http_total_2XX);
uint32_t total_4XX = atomic_load(&http_total_4XX);
int64_t total_responses = total_2XX + total_4XX + total_5XX;
int64_t outstanding_requests = (int64_t)total_reqs - total_responses;
#ifdef HTTP_TOTAL_COUNTERS
uint32_t total_reqs = atomic_load(&http_total_requests);
uint32_t total_2XX = atomic_load(&http_total_2XX);
uint32_t total_4XX = atomic_load(&http_total_4XX);
uint32_t total_5XX = atomic_load(&http_total_5XX);
int64_t total_responses = total_2XX + total_4XX + total_5XX;
int64_t outstanding_requests = (int64_t)total_reqs - total_responses;
debuglog("Requests: %u (%ld outstanding)\n\tResponses: %ld\n\t\t2XX: %u\n\t\t4XX: %u\n\t\t5XX: %u\n",
total_reqs, outstanding_requests, total_responses, total_2XX, total_4XX, total_5XX);
#else
debuglog("Requests: %u\n\tResponses:\n\t\t\t5XX: %u\n", total_reqs, total_5XX);
#endif
};

@ -319,10 +319,10 @@ log_compiletime_config()
pretty_print_key_disabled("Log State Changes");
#endif
#ifdef LOG_TOTAL_REQS_RESPS
pretty_print_key_enabled("Log Total Reqs/Resps");
#ifdef HTTP_TOTAL_COUNTERS
pretty_print_key_enabled("HTTP Total Counters");
#else
pretty_print_key_disabled("Log Total Reqs/Resps");
pretty_print_key_disabled("HTTP Total Counters");
#endif
#ifdef SANDBOX_STATE_TOTALS

@ -86,12 +86,11 @@ metrics_server_handler(void *arg)
FILE *ostream = open_memstream(&ostream_base, &ostream_size);
assert(ostream != NULL);
#ifdef HTTP_TOTAL_COUNTERS
uint32_t total_reqs = atomic_load(&http_total_requests);
uint32_t total_5XX = atomic_load(&http_total_5XX);
#ifdef LOG_TOTAL_REQS_RESPS
uint32_t total_2XX = atomic_load(&http_total_2XX);
uint32_t total_4XX = atomic_load(&http_total_4XX);
uint32_t total_2XX = atomic_load(&http_total_2XX);
uint32_t total_4XX = atomic_load(&http_total_4XX);
#endif
uint32_t total_sandboxes = atomic_load(&sandbox_total);
@ -118,23 +117,25 @@ metrics_server_handler(void *arg)
fprintf(ostream, "HTTP/1.1 200 OK\r\n\r\n");
fprintf(ostream, "# TYPE total_requests counter\n");
fprintf(ostream, "total_requests: %d\n", total_reqs);
#ifdef ADMISSIONS_CONTROL
fprintf(ostream, "# TYPE work_admitted_percentile gauge\n");
fprintf(ostream, "work_admitted_percentile: %f\n", work_admitted_percentile);
#endif
fprintf(ostream, "# TYPE total_5XX counter\n");
fprintf(ostream, "total_5XX: %d\n", total_5XX);
#ifdef LOG_TOTAL_REQS_RESPS
#ifdef HTTP_TOTAL_COUNTERS
fprintf(ostream, "# TYPE total_requests counter\n");
fprintf(ostream, "total_requests: %d\n", total_reqs);
fprintf(ostream, "# TYPE total_2XX counter\n");
fprintf(ostream, "total_2XX: %d\n", total_2XX);
fprintf(ostream, "# TYPE total_4XX counter\n");
fprintf(ostream, "total_4XX: %d\n", total_4XX);
fprintf(ostream, "# TYPE total_5XX counter\n");
fprintf(ostream, "total_5XX: %d\n", total_5XX);
#endif
// This global is padded by 1 for error handling, so decrement here for true value

Loading…
Cancel
Save