From 9458c5ad7fde2d5602b348485dd58157d9c4d8c6 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 26 Aug 2022 14:45:11 -0400 Subject: [PATCH] refactor: http route total counters --- runtime/Makefile | 8 ++- runtime/include/http_route_total.h | 48 ++++++++++++++++ runtime/include/http_router.h | 2 +- runtime/include/http_session.h | 4 +- runtime/include/route.h | 8 +-- runtime/include/route_metrics.h | 55 ------------------- .../src/metrics_server_route_level_metrics.c | 5 ++ 7 files changed, 66 insertions(+), 64 deletions(-) create mode 100644 runtime/include/http_route_total.h delete mode 100644 runtime/include/route_metrics.h diff --git a/runtime/Makefile b/runtime/Makefile index c2f2049..7fee2cb 100644 --- a/runtime/Makefile +++ b/runtime/Makefile @@ -75,11 +75,15 @@ BINARY_NAME=sledgert # page is allocated. This helps understand the relationship to memory allocation and execution time. # CFLAGS += -DLOG_SANDBOX_MEMORY_PROFILE -# This flag dumps totals of incoming requests and outgoing responses, broken out by status code -# family, such as 2XX, 4XX, 5XX. It is useful to debug clients hanging waiting for a response. +# This flag enables HTTP-level counters of incoming requests and outgoing responses, broken out by status code +# family, such as 2XX, 4XX, 5XX. # To log, run `call http_total_log()` while in GDB # CFLAGS += -DHTTP_TOTAL_COUNTERS +# This flag enables per-route counters of incoming requests and outgoing responses, broken out by status code +# family, such as 2XX, 4XX, 5XX. +CFLAGS += -DHTTP_ROUTE_TOTAL_COUNTERS + # This flag tracks the total number of sandboxes in the various states # It is useful to debug if sandboxes are "getting caught" in a particular state # CFLAGS += -DSANDBOX_STATE_TOTALS diff --git a/runtime/include/http_route_total.h b/runtime/include/http_route_total.h new file mode 100644 index 0000000..8f30579 --- /dev/null +++ b/runtime/include/http_route_total.h @@ -0,0 +1,48 @@ +#pragma once + +#include + +#ifdef HTTP_ROUTE_TOTAL_COUNTERS +struct http_route_total { + atomic_ulong total_requests; + atomic_ulong total_2XX; + atomic_ulong total_4XX; + atomic_ulong total_5XX; +}; +#else +struct http_route_total { +}; +#endif + +static inline void +http_route_total_init(struct http_route_total *rm) +{ +#ifdef HTTP_ROUTE_TOTAL_COUNTERS + atomic_init(&rm->total_requests, 0); + atomic_init(&rm->total_2XX, 0); + atomic_init(&rm->total_4XX, 0); + atomic_init(&rm->total_5XX, 0); +#endif +} + +static inline void +http_route_total_increment_request(struct http_route_total *rm) +{ +#ifdef HTTP_ROUTE_TOTAL_COUNTERS + atomic_fetch_add(&rm->total_requests, 1); +#endif +} + +static inline void +http_route_total_increment(struct http_route_total *rm, int status_code) +{ +#ifdef HTTP_ROUTE_TOTAL_COUNTERS + if (status_code >= 200 && status_code <= 299) { + atomic_fetch_add(&rm->total_2XX, 1); + } else if (status_code >= 400 && status_code <= 499) { + atomic_fetch_add(&rm->total_4XX, 1); + } else if (status_code >= 500 && status_code <= 599) { + atomic_fetch_add(&rm->total_5XX, 1); + } +#endif +} diff --git a/runtime/include/http_router.h b/runtime/include/http_router.h index 1291ad9..7039525 100644 --- a/runtime/include/http_router.h +++ b/runtime/include/http_router.h @@ -37,7 +37,7 @@ http_router_add_route(http_router_t *router, struct route_config *config, struct .response_size = config->http_resp_size, .response_content_type = config->http_resp_content_type }; - route_metrics_init(&route.metrics); + http_route_total_init(&route.metrics); /* Admissions Control */ uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz; diff --git a/runtime/include/http_session.h b/runtime/include/http_session.h index ca7e1d1..9a0e092 100644 --- a/runtime/include/http_session.h +++ b/runtime/include/http_session.h @@ -16,7 +16,7 @@ #include "http_parser_settings.h" #include "http_total.h" #include "route.h" -#include "route_metrics.h" +#include "http_route_total.h" #include "tenant.h" #include "vec.h" #include "http_session_perf_log.h" @@ -182,7 +182,7 @@ http_session_set_response_header(struct http_session *session, int 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); } + if (likely(session->route != NULL)) { http_route_total_increment(&session->route->metrics, status_code); } if (status_code == 200) { session->response_header_length = snprintf(session->response_header, diff --git a/runtime/include/route.h b/runtime/include/route.h index 8b01aa6..853c44f 100644 --- a/runtime/include/route.h +++ b/runtime/include/route.h @@ -5,13 +5,13 @@ #include "admissions_info.h" #include "module.h" -#include "route_metrics.h" +#include "http_route_total.h" /* Assumption: entrypoint is always _start. This should be enhanced later */ struct route { - char *route; - struct route_metrics metrics; - struct module *module; + char *route; + struct http_route_total metrics; + struct module *module; /* HTTP State */ uint32_t relative_deadline_us; uint64_t relative_deadline; /* cycles */ diff --git a/runtime/include/route_metrics.h b/runtime/include/route_metrics.h deleted file mode 100644 index d7480ad..0000000 --- a/runtime/include/route_metrics.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include - -struct route_metrics { - atomic_ulong total_requests; - atomic_ulong total_2XX; - atomic_ulong total_4XX; - atomic_ulong total_5XX; -}; - -static inline void -route_metrics_init(struct route_metrics *rm) -{ - atomic_init(&rm->total_requests, 0); - atomic_init(&rm->total_2XX, 0); - atomic_init(&rm->total_4XX, 0); - atomic_init(&rm->total_5XX, 0); -} - -static inline void -route_metrics_increment_request(struct route_metrics *rm) -{ - atomic_fetch_add(&rm->total_requests, 1); -} - -static inline void -route_metrics_increment_2XX(struct route_metrics *rm) -{ - atomic_fetch_add(&rm->total_2XX, 1); -} - -static inline void -route_metrics_increment_4XX(struct route_metrics *rm) -{ - atomic_fetch_add(&rm->total_4XX, 1); -} - -static inline void -route_metrics_increment_5XX(struct route_metrics *rm) -{ - atomic_fetch_add(&rm->total_5XX, 1); -} - -static inline void -route_metrics_increment(struct route_metrics *rm, int status_code) -{ - if (status_code >= 200 && status_code <= 299) { - route_metrics_increment_2XX(rm); - } else if (status_code >= 400 && status_code <= 499) { - route_metrics_increment_4XX(rm); - } else if (status_code >= 500 && status_code <= 599) { - route_metrics_increment_5XX(rm); - } -} diff --git a/runtime/src/metrics_server_route_level_metrics.c b/runtime/src/metrics_server_route_level_metrics.c index bb54d4a..714dd52 100644 --- a/runtime/src/metrics_server_route_level_metrics.c +++ b/runtime/src/metrics_server_route_level_metrics.c @@ -12,6 +12,7 @@ static const int p90_idx = perf_window_capacity * 90 / 100; void render_routes(struct route *route, void *arg_one, void *arg_two) { +#ifdef HTTP_ROUTE_TOTAL_COUNTERS FILE *ostream = (FILE *)arg_one; struct tenant *tenant = (struct tenant *)arg_two; @@ -47,6 +48,8 @@ render_routes(struct route *route, void *arg_one, void *arg_two) fprintf(ostream, "# TYPE %s_%s_latency_p90 gauge\n", tenant->name, route_label); fprintf(ostream, "%s_%s_latency_p90: %lu\n", tenant->name, route_label, latency_p90); #endif + +#endif /* HTTP_ROUTE_TOTAL_COUNTERS */ } void @@ -61,5 +64,7 @@ render_tenant_routers(struct tenant *tenant, void *arg_one, void *arg_two) void metrics_server_route_level_metrics_render(FILE *ostream) { +#ifdef HTTP_ROUTE_TOTAL_COUNTERS tenant_database_foreach(render_tenant_routers, ostream, NULL); +#endif }