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

feat: Upload event attachment #3

Merged
merged 5 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
Binary file added .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ example
*.dylib
*.dSYM
*.mp
*.dmp
*.dmp
.sentryclirc
.venv/
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"optional": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"mpack.c": "cpp"
"mpack.c": "cpp",
"numeric": "cpp",
"random": "cpp"
}
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ example: example.c libsentry.dylib
gcc -g -o example example.c -I ./include -L . -lsentry
build-example: example
clean:
rm -rf example libsentry.dylib completed new pending *.dSYM *.mp *.dmp *.dat
rm -rf example libsentry.dylib crashpad-db *.dSYM *.mp
2 changes: 1 addition & 1 deletion example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(void) {
option.environment = "Production";
option.release = "5fd7a6cd";
option.dist = "12345";
option.database_path = ".";
option.database_path = "crashpad-db";
option.debug = 1;

sentry_init(&option);
Expand Down
16 changes: 11 additions & 5 deletions src/crashpad_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace sentry {
namespace crashpad {
SimpleStringDictionary simple_annotations;

int init(const sentry_options_t *options, const char *minidump_url) {
int init(const sentry_options_t *options,
const char *minidump_url,
const char *event_file) {
if (minidump_url == nullptr) {
return SENTRY_ERROR_NO_MINIDUMP_URL;
}
Expand All @@ -29,15 +31,19 @@ int init(const sentry_options_t *options, const char *minidump_url) {
std::string url(minidump_url);
// Optional annotations passed via --annotations to the handler
std::map<std::string, std::string> annotations;
std::map<std::string, base::FilePath> fileAttachments = {
{SENTRY_EVENT_FILE_NAME, base::FilePath(event_file)}};

// Optional arguments to pass to the handler
std::vector<std::string> arguments;
arguments.push_back("--no-rate-limit");

CrashpadClient client;
bool success = client.StartHandler(handler, database, database, url,
annotations, arguments,
/* restartable */ true,
/* asynchronous_start */ false);
bool success = client.StartHandlerWithAttachments(
handler, database, database, url, annotations, fileAttachments,
arguments,
/* restartable */ true,
/* asynchronous_start */ false);

if (success) {
SENTRY_PRINT_DEBUG("Started client handler.\n");
Expand Down
4 changes: 3 additions & 1 deletion src/crashpad_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace sentry {
namespace crashpad {
int init(const sentry_options_t *options, const char *minidump_url);
int init(const sentry_options_t *options,
const char *minidump_url,
const char *event_file);
int set_annotation(const char *key, const char *value);
int remove_annotation(const char *key);
} // namespace crashpad
Expand Down
2 changes: 2 additions & 0 deletions src/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

const sentry_options_t *sentry__get_options(void);

static const char *SENTRY_EVENT_FILE_NAME = "sentry-event.mp";

#endif
46 changes: 37 additions & 9 deletions src/sentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#elif defined(SENTRY_BREAKPAD)
#include "breakpad_wrapper.hpp"
#endif
#include <sys/stat.h>
#include <map>
#include <sstream>
#include <string>
#include "internal.hpp"
#include "print_macros.hpp"
#include "random"
#include "sentry.h"
#include "vendor/mpack.h"

Expand All @@ -16,8 +19,6 @@ using namespace sentry::crashpad;
using namespace sentry::breakpad;
#endif

static const char *SENTRY_EVENT_FILE_NAME = "sentry-event.mp";

struct SentryDsn {
const char *scheme;
const char *public_key;
Expand All @@ -36,6 +37,8 @@ struct SentryEvent {
std::map<std::string, std::string> user;
std::map<std::string, std::string> tags;
std::map<std::string, std::string> extra;
std::string run_id;
std::string run_path;
};

static SentryEvent sentry_event = {
Expand Down Expand Up @@ -143,7 +146,10 @@ static int minidump_url_from_dsn(char *dsn, std::string &minidump_url_out) {
static void serialize(const SentryEvent *event) {
mpack_writer_t writer;
// TODO: cycle event file
mpack_writer_init_filename(&writer, SENTRY_EVENT_FILE_NAME);
// Path must exist otherwise mpack will fail to write.
auto dest_path = (event->run_path + SENTRY_EVENT_FILE_NAME).c_str();
SENTRY_PRINT_DEBUG_ARGS("Serializing to file: %s\n", dest_path);
mpack_writer_init_filename(&writer, dest_path);
mpack_start_map(&writer, 8);
mpack_write_cstr(&writer, "release");
mpack_write_cstr_or_nil(&writer, event->release);
Expand Down Expand Up @@ -206,6 +212,7 @@ static void serialize(const SentryEvent *event) {

int sentry_init(const sentry_options_t *options) {
sentry_options = options;

if (options->dsn == nullptr) {
SENTRY_PRINT_ERROR("Not DSN specified. Sentry SDK will be disabled.\n");
return SENTRY_ERROR_NO_DSN;
Expand All @@ -219,12 +226,8 @@ int sentry_init(const sentry_options_t *options) {
return err;
}

if (options->debug) {
SENTRY_PRINT_DEBUG_ARGS("Initializing with minidump endpoint: %s\n",
minidump_url.c_str());
}

err = init(options, minidump_url.c_str());
SENTRY_PRINT_DEBUG_ARGS("Initializing with minidump endpoint: %s\n",
minidump_url.c_str());

if (options->environment != nullptr) {
sentry_event.environment = options->environment;
Expand All @@ -238,6 +241,31 @@ int sentry_init(const sentry_options_t *options) {
sentry_event.dist = options->dist;
}

std::random_device seed;
std::default_random_engine engine(seed());
std::uniform_int_distribution<int> uniform_dist(0, INT32_MAX);
std::time_t result = std::time(nullptr);
std::stringstream ss;
ss << result << "-" << uniform_dist(engine);
sentry_event.run_id = ss.str();

/* Make sure run dir exists before serializer needs to write to it */
/* TODO: Write proper x-plat mkdir */
auto run_path = std::string(options->database_path);
mkdir(run_path.c_str(), 0777);
bruno-garcia marked this conversation as resolved.
Show resolved Hide resolved
run_path = run_path + "/" + "sentry-runs/";
mkdir(run_path.c_str(), 0777);
sentry_event.run_path = run_path + sentry_event.run_id + "/";
auto rv = mkdir(sentry_event.run_path.c_str(), 0777);
if (rv != 0 && rv != EEXIST) {
SENTRY_PRINT_ERROR_ARGS("Failed to create sentry_runs directory '%s'\n",
sentry_event.run_path.c_str());
return rv;
}

err = init(options, minidump_url.c_str(),
(sentry_event.run_path + SENTRY_EVENT_FILE_NAME).c_str());

return 0;
}

Expand Down