More cleaner SentryWriter API

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2024-04-11 16:02:39 +02:00
parent f1cb1f80b6
commit 66c6ef0463
3 changed files with 57 additions and 19 deletions

View File

@ -499,7 +499,7 @@ private:
if (sig != SanitizerTrap)
{
if (auto * sentry = SentryWriter::getInstance())
sentry->onFault(sig, error_message, stack_trace.getFramePointers(), stack_trace.getOffset(), stack_trace.getSize());
sentry->onSignal(sig, error_message, stack_trace.getFramePointers(), stack_trace.getOffset(), stack_trace.getSize());
/// Advice the user to send it manually.
if (std::string_view(VERSION_OFFICIAL).contains("official build"))
@ -1030,7 +1030,7 @@ void BaseDaemon::initializeTerminationAndSignalProcessing()
SentryWriter::FramePointers frame_pointers;
for (size_t i = 0; i < trace.size(); ++i)
frame_pointers[i] = trace[i];
sentry->onFault(-code, msg, frame_pointers, /* offset= */ 0, trace.size());
sentry->onException(code, msg, frame_pointers, /* offset= */ 0, trace.size());
}
};
}

View File

@ -147,24 +147,39 @@ SentryWriter::~SentryWriter()
sentry_shutdown();
}
void SentryWriter::onFault(int sig_or_error, const std::string & error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
void SentryWriter::onSignal(int sig, const std::string & error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
{
sendError(Type::SIGNAL, sig, error_message, frame_pointers, offset, size);
}
void SentryWriter::onException(int code, const std::string & error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
{
sendError(Type::EXCEPTION, code, error_message, frame_pointers, offset, size);
}
void SentryWriter::sendError(Type type, int sig_or_error, const std::string & error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
{
auto logger = getLogger("SentryWriter");
if (initialized)
{
sentry_value_t event = sentry_value_new_message_event(SENTRY_LEVEL_FATAL, "fault", error_message.c_str());
if (sig_or_error > 0)
switch (type)
{
int sig = sig_or_error;
sentry_set_tag("signal", strsignal(sig)); // NOLINT(concurrency-mt-unsafe) // not thread-safe but ok in this context
sentry_set_extra("signal_number", sentry_value_new_int32(sig));
}
else
{
/// Can be only LOGICAL_ERROR, but just in case.
int code = -sig_or_error;
sentry_set_tag("exception", DB::ErrorCodes::getName(code).data());
sentry_set_extra("exception_code", sentry_value_new_int32(code));
case SIGNAL:
{
int sig = sig_or_error;
sentry_set_tag("signal", strsignal(sig)); // NOLINT(concurrency-mt-unsafe) // not thread-safe but ok in this context
sentry_set_extra("signal_number", sentry_value_new_int32(sig));
break;
}
case EXCEPTION:
{
int code = sig_or_error;
/// Can be only LOGICAL_ERROR, but just in case.
sentry_set_tag("exception", DB::ErrorCodes::getName(code).data());
sentry_set_extra("exception_code", sentry_value_new_int32(code));
break;
}
}
#if defined(__ELF__) && !defined(OS_FREEBSD)
@ -242,6 +257,8 @@ SentryWriter * SentryWriter::getInstance() { return nullptr; }
SentryWriter::SentryWriter(Poco::Util::LayeredConfiguration &) {}
SentryWriter::~SentryWriter() = default;
void SentryWriter::onFault(int, const std::string &, const FramePointers &, size_t, size_t) {}
void SentryWriter::sendError(Type, int, const std::string &, const FramePointers &, size_t, size_t) {}
void SentryWriter::onSignal(int, const std::string &, const FramePointers &, size_t, size_t) {}
void SentryWriter::onException(int, const std::string &, const FramePointers &, size_t, size_t) {}
#endif

View File

@ -25,10 +25,15 @@ public:
/// @return nullptr if initializeInstance() was not called (i.e. for non-server) or SentryWriter object
static SentryWriter * getInstance();
/// Not signal safe and can't be called from a signal handler
/// @param sig_or_error - signal if >= 0, otherwise exception code
void onFault(
int sig_or_error,
void onSignal(
int sig,
const std::string & error_message,
const FramePointers & frame_pointers,
size_t offset,
size_t size);
void onException(
int code,
const std::string & error_message,
const FramePointers & frame_pointers,
size_t offset,
@ -43,4 +48,20 @@ private:
std::string server_data_path;
explicit SentryWriter(Poco::Util::LayeredConfiguration & config);
enum Type
{
SIGNAL,
EXCEPTION,
};
/// Not signal safe and can't be called from a signal handler
/// @param sig_or_error - signal if >= 0, otherwise exception code
void sendError(
Type type,
int sig_or_error,
const std::string & error_message,
const FramePointers & frame_pointers,
size_t offset,
size_t size);
};