Sentry: sort by response code

This commit is contained in:
Lion Kortlepel 2021-08-11 23:13:23 +02:00 committed by Lion
parent 683e13a4a0
commit 4b92532203
5 changed files with 15 additions and 8 deletions

View File

@ -61,12 +61,14 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch
// In release build, these macros turn into NOPs. The compiler will optimize these out. // In release build, these macros turn into NOPs. The compiler will optimize these out.
#define Assert(cond) \ #define Assert(cond) \
do { \ do { \
bool result = (cond); \
if (!result) { \ if (!result) { \
Sentry.LogAssert(#cond, _file_basename, _line, __func__); \ Sentry.LogAssert(#cond, _file_basename, _line, __func__); \
} \ } \
} while (false) } while (false)
#define AssertNotReachable() \ #define AssertNotReachable() \
do { \ do { \
bool result = (cond); \
if (!result) { \ if (!result) { \
Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \ Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \
} \ } \

View File

@ -5,5 +5,5 @@
namespace Http { namespace Http {
std::string GET(const std::string& host, int port, const std::string& target); std::string GET(const std::string& host, int port, const std::string& target);
std::string POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json); std::string POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json, int* status = nullptr);
} }

View File

@ -54,7 +54,7 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ
} }
} }
std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json) { std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json, int* status) {
try { try {
net::io_context io; net::io_context io;
@ -136,6 +136,9 @@ std::string Http::POST(const std::string& host, const std::string& target, const
std::unordered_map<std::string, std::string> response_data; std::unordered_map<std::string, std::string> response_data;
response_data["reponse-code"] = std::to_string(response.result_int()); response_data["reponse-code"] = std::to_string(response.result_int());
if (status) {
*status = response.result_int();
}
for (const auto& header : response.base()) { for (const auto& header : response.base()) {
// need to do explicit casts to convert string_view to string // need to do explicit casts to convert string_view to string
// since string_view may not be null-terminated (and in fact isn't, here) // since string_view may not be null-terminated (and in fact isn't, here)

View File

@ -36,7 +36,8 @@ void THeartbeatThread::operator()() {
Body += "&pps=" + Application::PPS(); Body += "&pps=" + Application::PPS();
auto Target = "/heartbeat"; auto Target = "/heartbeat";
T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false); int ResponseCode = -1;
T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode);
if (T.substr(0, 2) != "20") { if (T.substr(0, 2) != "20") {
auto SentryReportError = [&](const std::string& transaction) { auto SentryReportError = [&](const std::string& transaction) {
@ -47,7 +48,7 @@ void THeartbeatThread::operator()() {
{ { "response-body", T }, { { "response-body", T },
{ "request-body", Body } }); { "request-body", Body } });
Sentry.SetTransaction(transaction); Sentry.SetTransaction(transaction);
Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format"); Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format (" + std::to_string(ResponseCode) + ")");
} }
}; };
SentryReportError(Application::GetBackendHostname() + Target); SentryReportError(Application::GetBackendHostname() + Target);

View File

@ -283,8 +283,9 @@ void TNetwork::Authentication(SOCKET TCPSock) {
auto RequestString = R"({"key":")" + Rc + "\"}"; auto RequestString = R"({"key":")" + Rc + "\"}";
auto Target = "/pkToUser"; auto Target = "/pkToUser";
int ResponseCode = -1;
if (!Rc.empty()) { if (!Rc.empty()) {
Rc = Http::POST(Application::GetBackendUrlForAuth(), Target, {}, RequestString, true); Rc = Http::POST(Application::GetBackendUrlForAuth(), Target, {}, RequestString, true, &ResponseCode);
} }
json::Document AuthResponse; json::Document AuthResponse;
@ -302,7 +303,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
{ { "response-body", Rc }, { { "response-body", Rc },
{ "key", RequestString } }); { "key", RequestString } });
Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target);
Sentry.Log(SENTRY_LEVEL_ERROR, "default", "auth: wrong backend response format"); Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format (" + std::to_string(ResponseCode) + ")");
return; return;
} else if (Rc == "0") { } else if (Rc == "0") {
auto Lock = Sentry.CreateExclusiveContext(); auto Lock = Sentry.CreateExclusiveContext();
@ -310,7 +311,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
{ { "response-body", Rc }, { { "response-body", Rc },
{ "key", RequestString } }); { "key", RequestString } });
Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target);
Sentry.Log(SENTRY_LEVEL_INFO, "default", "backend returned 0 instead of json"); Sentry.Log(SENTRY_LEVEL_INFO, "default", "backend returned 0 instead of json (" + std::to_string(ResponseCode) + ")");
} }
if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString() if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString()