diff options
| author | Stefan Boberg <[email protected]> | 2021-10-04 17:54:21 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-10-04 17:54:21 +0200 |
| commit | f4c6f75ed3620e777d8194bf59a24b7d6dc4200a (patch) | |
| tree | 7c3f77c7907e091270847602fc76371755f105ab /zenserver/monitoring | |
| parent | filesystem: Added comment for future optimization opportunities in CreateDire... (diff) | |
| download | zen-f4c6f75ed3620e777d8194bf59a24b7d6dc4200a.tar.xz zen-f4c6f75ed3620e777d8194bf59a24b7d6dc4200a.zip | |
stats: Implemented new stats endpoint
Stats are exposed under /stats/{id}, so for example structured cache stats are exposed under /stats/z$
The separate endpoint makes it easier to separate request handling to ensure stats/status endpoints still respond if the regular request queue is somehow saturated or otherwise not behaving
There is also a /status endpoint which is similar and is targeted towards lightweight health monitoring
Diffstat (limited to 'zenserver/monitoring')
| -rw-r--r-- | zenserver/monitoring/httpstats.cpp | 19 | ||||
| -rw-r--r-- | zenserver/monitoring/httpstats.h | 13 | ||||
| -rw-r--r-- | zenserver/monitoring/httpstatus.cpp | 21 | ||||
| -rw-r--r-- | zenserver/monitoring/httpstatus.h | 13 |
4 files changed, 61 insertions, 5 deletions
diff --git a/zenserver/monitoring/httpstats.cpp b/zenserver/monitoring/httpstats.cpp index a82cfda50..de04294d0 100644 --- a/zenserver/monitoring/httpstats.cpp +++ b/zenserver/monitoring/httpstats.cpp @@ -19,14 +19,31 @@ HttpStatsService::BaseUri() const } void +HttpStatsService::RegisterHandler(std::string_view Id, IHttpStatsProvider& Provider) +{ + RwLock::ExclusiveLockScope _(m_Lock); + m_Providers.insert_or_assign(std::string(Id), &Provider); +} + +void HttpStatsService::HandleRequest(HttpServerRequest& Request) { using namespace std::literals; + std::string_view Key = Request.RelativeUri(); + switch (Request.RequestVerb()) { + case HttpVerb::kHead: case HttpVerb::kGet: - return Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv); + if (auto It = m_Providers.find(std::string{Key}); It != end(m_Providers)) + { + return It->second->HandleStatsRequest(Request); + } + + [[fallthrough]]; + default: + return; } } diff --git a/zenserver/monitoring/httpstats.h b/zenserver/monitoring/httpstats.h index 9da48233b..1c3c79dd0 100644 --- a/zenserver/monitoring/httpstats.h +++ b/zenserver/monitoring/httpstats.h @@ -2,11 +2,18 @@ #pragma once -#include <zenhttp/httpserver.h> #include <zencore/logging.h> +#include <zenhttp/httpserver.h> + +#include <map> namespace zen { +struct IHttpStatsProvider +{ + virtual void HandleStatsRequest(HttpServerRequest& Request) = 0; +}; + class HttpStatsService : public HttpService { public: @@ -15,12 +22,16 @@ public: virtual const char* BaseUri() const override; virtual void HandleRequest(HttpServerRequest& Request) override; + void RegisterHandler(std::string_view Id, IHttpStatsProvider& Provider); private: spdlog::logger& m_Log; HttpRequestRouter m_Router; inline spdlog::logger& Log() { return m_Log; } + + RwLock m_Lock; + std::map<std::string, IHttpStatsProvider*> m_Providers; }; } // namespace zen
\ No newline at end of file diff --git a/zenserver/monitoring/httpstatus.cpp b/zenserver/monitoring/httpstatus.cpp index c18bf6c1f..e12662b1c 100644 --- a/zenserver/monitoring/httpstatus.cpp +++ b/zenserver/monitoring/httpstatus.cpp @@ -15,7 +15,14 @@ HttpStatusService::~HttpStatusService() const char* HttpStatusService::BaseUri() const { - return "/statUs/"; + return "/status/"; +} + +void +HttpStatusService::RegisterHandler(std::string_view Id, IHttpStatusProvider& Provider) +{ + RwLock::ExclusiveLockScope _(m_Lock); + m_Providers.insert_or_assign(std::string(Id), &Provider); } void @@ -23,10 +30,20 @@ HttpStatusService::HandleRequest(HttpServerRequest& Request) { using namespace std::literals; + std::string_view Key = Request.RelativeUri(); + switch (Request.RequestVerb()) { + case HttpVerb::kHead: case HttpVerb::kGet: - return Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv); + if (auto It = m_Providers.find(std::string{Key}); It != end(m_Providers)) + { + return It->second->HandleStatusRequest(Request); + } + + [[fallthrough]]; + default: + return; } } diff --git a/zenserver/monitoring/httpstatus.h b/zenserver/monitoring/httpstatus.h index f5c6bd616..8f069f760 100644 --- a/zenserver/monitoring/httpstatus.h +++ b/zenserver/monitoring/httpstatus.h @@ -2,11 +2,18 @@ #pragma once -#include <zenhttp/httpserver.h> #include <zencore/logging.h> +#include <zenhttp/httpserver.h> + +#include <map> namespace zen { +struct IHttpStatusProvider +{ + virtual void HandleStatusRequest(HttpServerRequest& Request) = 0; +}; + class HttpStatusService : public HttpService { public: @@ -15,11 +22,15 @@ public: virtual const char* BaseUri() const override; virtual void HandleRequest(HttpServerRequest& Request) override; + void RegisterHandler(std::string_view Id, IHttpStatusProvider& Provider); private: spdlog::logger& m_Log; HttpRequestRouter m_Router; + RwLock m_Lock; + std::map<std::string, IHttpStatusProvider*> m_Providers; + inline spdlog::logger& Log() { return m_Log; } }; |