ClickHouse/base/daemon/SentryWriter.cpp

196 lines
6.7 KiB
C++
Raw Normal View History

#include <daemon/SentryWriter.h>
2020-05-29 19:48:32 +00:00
#include <Poco/File.h>
#include <Poco/Util/Application.h>
#include <common/getFQDNOrHostName.h>
2020-05-29 19:48:32 +00:00
#include <common/logger_useful.h>
#if !defined(ARCADIA_BUILD)
2020-05-29 21:36:47 +00:00
# include "Common/config_version.h"
# include <Common/config.h>
#endif
2020-05-27 20:15:33 +00:00
#if USE_SENTRY
2020-05-30 10:54:57 +00:00
# include <sentry.h> // Y_IGNORE
2020-05-27 20:15:33 +00:00
#endif
2020-05-29 21:36:47 +00:00
namespace
{
static bool initialized = false;
2020-05-30 08:02:13 +00:00
static bool anonymize = false;
2020-05-27 20:15:33 +00:00
2020-05-29 21:36:47 +00:00
void setExtras()
{
2020-05-27 20:15:33 +00:00
#if USE_SENTRY
2020-05-30 08:02:13 +00:00
if (!anonymize)
{
sentry_set_extra("server_name", sentry_value_new_string(getFQDNOrHostName().c_str()));
}
sentry_set_tag("version", VERSION_STRING_SHORT);
2020-05-29 21:36:47 +00:00
sentry_set_extra("version_githash", sentry_value_new_string(VERSION_GITHASH));
sentry_set_extra("version_describe", sentry_value_new_string(VERSION_DESCRIBE));
sentry_set_extra("version_integer", sentry_value_new_int32(VERSION_INTEGER));
sentry_set_extra("version_revision", sentry_value_new_int32(VERSION_REVISION));
sentry_set_extra("version_major", sentry_value_new_int32(VERSION_MAJOR));
sentry_set_extra("version_minor", sentry_value_new_int32(VERSION_MINOR));
sentry_set_extra("version_patch", sentry_value_new_int32(VERSION_PATCH));
2020-05-27 20:15:33 +00:00
#endif
2020-05-29 21:36:47 +00:00
}
}
2020-05-29 21:36:47 +00:00
void SentryWriter::initialize(Poco::Util::LayeredConfiguration & config)
{
2020-05-27 20:15:33 +00:00
#if USE_SENTRY
bool enabled = false;
bool debug = config.getBool("send_crash_reports.debug", false);
2020-06-01 14:15:14 +00:00
auto logger = &Poco::Logger::get("SentryWriter");
2020-05-27 20:15:33 +00:00
if (config.getBool("send_crash_reports.enabled", false))
{
if (debug || (strlen(VERSION_OFFICIAL) > 0))
2020-05-27 20:15:33 +00:00
{
enabled = true;
}
}
2020-05-27 20:15:33 +00:00
if (enabled)
{
2020-05-29 21:36:47 +00:00
const std::string & endpoint
= config.getString("send_crash_reports.endpoint", "https://6f33034cfe684dd7a3ab9875e57b1c8d@o388870.ingest.sentry.io/5226277");
const std::string & temp_folder_path
= config.getString("send_crash_reports.tmp_path", config.getString("tmp_path", Poco::Path::temp()) + "sentry/");
2020-05-29 19:48:32 +00:00
Poco::File(temp_folder_path).createDirectories();
2020-05-27 20:15:33 +00:00
sentry_options_t * options = sentry_options_new();
sentry_options_set_release(options, VERSION_STRING);
if (debug)
{
sentry_options_set_debug(options, 1);
}
2020-05-27 20:15:33 +00:00
sentry_options_set_dsn(options, endpoint.c_str());
2020-05-29 19:48:32 +00:00
sentry_options_set_database_path(options, temp_folder_path.c_str());
2020-05-29 21:36:47 +00:00
if (strstr(VERSION_DESCRIBE, "-stable") || strstr(VERSION_DESCRIBE, "-lts"))
{
2020-05-27 20:15:33 +00:00
sentry_options_set_environment(options, "prod");
2020-05-29 21:36:47 +00:00
}
else
{
2020-05-27 20:15:33 +00:00
sentry_options_set_environment(options, "test");
}
2020-05-30 08:02:13 +00:00
2020-05-30 08:24:21 +00:00
const std::string & http_proxy = config.getString("send_crash_reports.http_proxy", "");
if (!http_proxy.empty())
{
sentry_options_set_http_proxy(options, http_proxy.c_str());
}
2020-05-29 19:48:32 +00:00
int init_status = sentry_init(options);
if (!init_status)
{
initialized = true;
2020-05-30 08:02:13 +00:00
anonymize = config.getBool("send_crash_reports.anonymize", false);
const std::string& anonymize_status = anonymize ? " (anonymized)" : "";
2020-05-29 19:48:32 +00:00
LOG_INFO(
2020-06-01 14:15:14 +00:00
logger,
2020-05-30 08:02:13 +00:00
"Sending crash reports is initialized with {} endpoint and {} temp folder{}",
2020-05-29 19:48:32 +00:00
endpoint,
2020-05-30 08:02:13 +00:00
temp_folder_path,
anonymize_status);
2020-05-29 19:48:32 +00:00
}
else
{
2020-06-01 14:15:14 +00:00
LOG_WARNING(logger, "Sending crash reports failed to initialized with {} status", init_status);
2020-05-29 19:48:32 +00:00
}
}
else
{
2020-06-01 14:15:14 +00:00
LOG_INFO(logger, "Sending crash reports is disabled");
2020-05-27 20:15:33 +00:00
}
#endif
}
2020-05-29 21:36:47 +00:00
void SentryWriter::shutdown()
{
2020-05-27 20:15:33 +00:00
#if USE_SENTRY
2020-05-29 21:36:47 +00:00
if (initialized)
{
2020-05-27 20:15:33 +00:00
sentry_shutdown();
}
#endif
}
2020-05-29 21:36:47 +00:00
void SentryWriter::onFault(int sig, const siginfo_t & info, const ucontext_t & context, const StackTrace & stack_trace)
{
2020-05-27 20:15:33 +00:00
#if USE_SENTRY
2020-06-01 14:15:14 +00:00
auto logger = &Poco::Logger::get("SentryWriter");
2020-05-27 20:15:33 +00:00
if (initialized)
{
2020-05-27 20:15:33 +00:00
const std::string & error_message = signalToErrorMessage(sig, info, context);
sentry_value_t event = sentry_value_new_message_event(SENTRY_LEVEL_FATAL, "fault", error_message.c_str());
sentry_set_tag("signal", strsignal(sig));
sentry_set_extra("signal_number", sentry_value_new_int32(sig));
setExtras();
/// Prepare data for https://develop.sentry.dev/sdk/event-payloads/stacktrace/
sentry_value_t frames = sentry_value_new_list();
size_t stack_size = stack_trace.getSize();
if (stack_size > 0)
{
2020-05-27 20:15:33 +00:00
size_t offset = stack_trace.getOffset();
if (stack_size == 1)
{
2020-05-27 20:15:33 +00:00
offset = 1;
}
2020-05-27 20:15:33 +00:00
char instruction_addr[100];
for (size_t i = stack_size - 1; i >= offset; --i)
{
2020-05-27 20:15:33 +00:00
const StackTrace::Frame & current_frame = stack_trace.getFrames().value()[i];
sentry_value_t frame = sentry_value_new_object();
unsigned long long frame_ptr = reinterpret_cast<unsigned long long>(current_frame.virtual_addr);
snprintf(instruction_addr, sizeof(instruction_addr), "0x%llx", frame_ptr);
sentry_value_set_by_key(frame, "instruction_addr", sentry_value_new_string(instruction_addr));
if (current_frame.symbol.has_value())
{
sentry_value_set_by_key(frame, "function", sentry_value_new_string(current_frame.symbol.value().c_str()));
}
if (current_frame.file.has_value())
{
sentry_value_set_by_key(frame, "filename", sentry_value_new_string(current_frame.file.value().c_str()));
}
if (current_frame.line.has_value())
{
sentry_value_set_by_key(frame, "lineno", sentry_value_new_int32(current_frame.line.value()));
}
sentry_value_append(frames, frame);
}
}
2020-05-27 20:15:33 +00:00
/// Prepare data for https://develop.sentry.dev/sdk/event-payloads/threads/
sentry_value_t stacktrace = sentry_value_new_object();
sentry_value_set_by_key(stacktrace, "frames", frames);
2020-05-27 20:15:33 +00:00
sentry_value_t thread = sentry_value_new_object();
sentry_value_set_by_key(thread, "stacktrace", stacktrace);
2020-05-27 20:15:33 +00:00
sentry_value_t values = sentry_value_new_list();
sentry_value_append(values, thread);
2020-05-27 20:15:33 +00:00
sentry_value_t threads = sentry_value_new_object();
sentry_value_set_by_key(threads, "values", values);
2020-05-27 20:15:33 +00:00
sentry_value_set_by_key(event, "threads", threads);
2020-06-01 14:15:14 +00:00
LOG_INFO(logger, "Sending crash report");
2020-05-27 20:15:33 +00:00
sentry_capture_event(event);
shutdown();
}
2020-05-29 19:48:32 +00:00
else
{
2020-06-01 14:15:14 +00:00
LOG_INFO(logger, "Not sending crash report");
2020-05-29 19:48:32 +00:00
}
2020-05-27 20:15:33 +00:00
#endif
2020-05-30 08:02:13 +00:00
}