Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sentry): add finer grained transactions #400

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions shttps/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,29 @@ namespace shttps {
* @param hd not used
*/
static void default_handler(Connection &conn, LuaServer &lua, void *user_data, void *hd) {

// we start a sentry transaction
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"default_handler",
"execute_handler"
);
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());

conn.status(Connection::NOT_FOUND);
conn.header("Content-Type", "text/text");
conn.setBuffer();

try {
conn << "No handler available" << Connection::flush_data;
} catch (InputFailure iofail) {
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

syslog(LOG_WARNING, "No handler available! Host: %s Uri: %s", conn.host().c_str(), conn.uri().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
}
//=========================================================================

Expand All @@ -131,6 +143,14 @@ namespace shttps {
* @param hd Pointer to string object containing the lua script file name
*/
void script_handler(shttps::Connection &conn, LuaServer &lua, void *user_data, void *hd) {

// we start a sentry transaction
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"script_handler",
"execute_handler"
);
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());

std::vector<std::string> headers = conn.header();
std::string uri = conn.uri();

Expand All @@ -142,6 +162,8 @@ namespace shttps {
conn << "File not found\n";
conn.flush();
syslog(LOG_ERR, "script_handler: %s not readable!", script.c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

Expand All @@ -158,11 +180,13 @@ namespace shttps {
inf.open(script); //open the input file
std::stringstream sstr;
sstr << inf.rdbuf(); //read the file
std::string luacode = sstr.str();//str holds the content of the file
std::string luacode = sstr.str(); //str holds the content of the file

try {
if (lua.executeChunk(luacode, script) < 0) {
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
} catch (Error &err) {
Expand All @@ -173,10 +197,14 @@ namespace shttps {
conn << "Lua Error:\r\n==========\r\n" << err << "\r\n";
conn.flush();
} catch (int i) {
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

syslog(LOG_ERR, "script_handler: error executing lua script: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
conn.flush();
Expand Down Expand Up @@ -210,6 +238,8 @@ namespace shttps {
try {
if (lua.executeChunk(luastr, script) < 0) {
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
} catch (Error &err) {
Expand All @@ -219,23 +249,31 @@ namespace shttps {
conn << "Lua Error:\r\n==========\r\n" << err << "\r\n";
conn.flush();
} catch (InputFailure iofail) {
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

syslog(LOG_ERR, "script_handler: error executing lua chunk: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
}

std::string htmlcode = eluacode.substr(end);
conn << htmlcode;
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
} else {
conn.status(Connection::INTERNAL_SERVER_ERROR);
conn.header("Content-Type", "text/text; charset=utf-8");
conn << "Script has no valid extension: '" << extension << "' !";
conn.flush();
syslog(LOG_ERR, "script_handler: error executing script, unknown extension: %s", extension.c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
}
} catch (InputFailure iofail) {
return; // we have an io error => just return, the thread will exit
Expand All @@ -245,11 +283,17 @@ namespace shttps {
conn.header("Content-Type", "text/text; charset=utf-8");
conn << err;
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
} catch (InputFailure iofail) {
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

syslog(LOG_ERR, "file_handler: internal error: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
}
Expand All @@ -267,6 +311,14 @@ namespace shttps {
* @param hd nullptr or pair (docroot, route)
*/
void file_handler(shttps::Connection &conn, LuaServer &lua, void *user_data, void *hd) {

// we start a sentry transaction
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"file_handler",
"execute_handler"
);
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());

std::vector<std::string> headers = conn.header();
std::string uri = conn.uri();

Expand Down Expand Up @@ -297,6 +349,8 @@ namespace shttps {
conn << "File not found\n";
conn.flush();
syslog(LOG_ERR, "file_handler: %s not readable", infile.c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

Expand All @@ -309,6 +363,8 @@ namespace shttps {
conn << infile << " not aregular file\n";
conn.flush();
syslog(LOG_ERR, "file_handler: %s is not regular file", infile.c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
} else {
Expand All @@ -317,6 +373,8 @@ namespace shttps {
conn << "Could not stat file" << infile << "\n";
conn.flush();
syslog(LOG_ERR, "file_handler: Could not stat %s", infile.c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

Expand Down Expand Up @@ -351,6 +409,8 @@ namespace shttps {
try {
if (lua.executeChunk(luacode, infile) < 0) {
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
} catch (Error &err) {
Expand All @@ -361,10 +421,14 @@ namespace shttps {
conn.flush();
} catch (int i) {
syslog(LOG_ERR, "file_handler: error executing lua chunk!");
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

syslog(LOG_ERR, "file_handler: error executing lua chunk: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}

Expand Down Expand Up @@ -399,6 +463,8 @@ namespace shttps {
try {
if (lua.executeChunk(luastr, infile) < 0) {
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
} catch (Error &err) {
Expand All @@ -407,16 +473,22 @@ namespace shttps {
conn.header("Content-Type", "text/text; charset=utf-8");
conn << "Lua Error:\r\n==========\r\n" << err << "\r\n";
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
} catch (InputFailure iofail) {}

syslog(LOG_ERR, "file_handler: error executing lua chunk: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
}

std::string htmlcode = eluacode.substr(end);
conn << htmlcode;
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
} else {
std::string actual_mimetype = shttps::Parsing::getFileMimetype(infile).first;
//
Expand Down Expand Up @@ -482,20 +554,30 @@ namespace shttps {
conn.sendFile(infile, 8192, start, end);
}
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
}
} catch (InputFailure iofail) {
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return; // we have an io error => just return, the thread will exit
} catch (Error &err) {
try {
conn.status(Connection::INTERNAL_SERVER_ERROR);
conn.header("Content-Type", "text/text; charset=utf-8");
conn << err;
conn.flush();
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
} catch (InputFailure iofail) {}

syslog(LOG_ERR, "file_handler: internal error: %s", err.to_string().c_str());
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
return;
}
// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
}
//=========================================================================

Expand Down Expand Up @@ -749,13 +831,6 @@ namespace shttps {
break; // should never happen!
case SocketControl::PROCESS_REQUEST: {

// we start a sentry transaction
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"socket_request_processor",
"process_request"
);
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());

//
// here we process the request
//
Expand Down Expand Up @@ -794,9 +869,6 @@ namespace shttps {
msg.type = SocketControl::FINISHED_AND_CLOSE;
}
SocketControl::send_control_message(tdata->control_pipe, msg);

// we end the sentry transaction and send it to Sentry
sentry_transaction_finish(tx);
break;
}
case SocketControl::EXIT: {
Expand Down
Loading