Skip to content

Commit

Permalink
Add Android support for sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
strseb committed Sep 19, 2022
1 parent 10c86a3 commit a26b64d
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 3 deletions.
2 changes: 1 addition & 1 deletion i18n
Submodule i18n updated 127 files
8 changes: 8 additions & 0 deletions scripts/android/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ rm -rf "android/src/com/adjust" || die "Failed to remove the adjust folder"
cp -a "3rdparty/adjust-android-sdk/Adjust/sdk-core/src/main/java/com/." "android/src/com/" || die "Failed to copy the adjust codebase"
git apply --directory="android/src/" "3rdparty/adjust_https_to_http.diff" || die "Failed to apply the adjust http patch"

print Y "Compile Sentry..."
cmake -B .tmp/sentry_build -S 3rdparty/sentry_native --toolchain ${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake \
-DSENTRY_BUILD_SHARED_LIBS=false \
-DANDROID_ABI=${ARCH}

cmake --build .tmp/sentry_build --parallel
cmake --install .tmp/sentry_build --prefix .tmp/sentry_install --config RelWithDebInfo

printn Y "Computing the version... "
export SHORTVERSION=$(cat version.pri | grep VERSION | grep defined | cut -d= -f2 | tr -d \ ) # Export so gradle can pick it up
export VERSIONCODE=$(date +%s | sed 's/.\{3\}$//' )"0" #Remove the last 3 digits of the timestamp, so we only get every ~16m a new versioncode
Expand Down
2 changes: 2 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ CONSTEXPR(uint32_t, controllerPeriodicStateRecorderMsec, 10800000, 60000, 0)
constexpr const char* SENTRY_DER =
"https://[email protected]/"
"6719480";
constexpr const char* SENTRY_ENVELOPE_INGESTION =
"https://o1396220.ingest.sentry.io/api/6719480/envelope/";

constexpr const char* API_PRODUCTION_URL = "https://vpn.mozilla.org";
constexpr const char* API_STAGING_URL =
Expand Down
14 changes: 14 additions & 0 deletions src/networkrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,20 @@ NetworkRequest* NetworkRequest::createForFxaAuthz(
return r;
}

// static
NetworkRequest* NetworkRequest::createForSentry(Task* parent,
const QByteArray& envelope) {
NetworkRequest* r = new NetworkRequest(parent, 200, false);
QUrl url(Constants::SENTRY_ENVELOPE_INGESTION);
r->m_request.setUrl(url);
r->m_request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-sentry-envelope");
r->m_request.setRawHeader("dsn", Constants::SENTRY_DER);
r->postRequest(envelope);

return r;
}

#ifdef UNIT_TEST
// static
NetworkRequest* NetworkRequest::createForFxaTotpCreation(
Expand Down
3 changes: 3 additions & 0 deletions src/networkrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class NetworkRequest final : public QObject {
const QString& fxaScope,
const QString& fxaAccessType);

static NetworkRequest* createForSentry(Task* parent,
const QByteArray& envelope);

#ifdef UNIT_TEST
static NetworkRequest* createForFxaTotpCreation(
Task* parent, const QByteArray& sessionToken);
Expand Down
16 changes: 16 additions & 0 deletions src/qmake/sentry.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

android{
LIBS += $$PWD/../../.tmp/sentry_install/lib/libsentry.a
LIBS += $$PWD/../../.tmp/sentry_install/lib/libunwindstack.a
INCLUDEPATH += $$PWD/../../.tmp/sentry_install/include
SOURCES += sentry/sentryadapter.cpp
DEFINES += SENTRY_ENABLED

# We need custom transport on android
DEFINES += SENTRY_TRANSPORT_ENABLED
}else{
SOURCES += sentry/dummysentryadapter.cpp
}
3 changes: 2 additions & 1 deletion src/qmake/sources.pri
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ SOURCES += \
rfc/rfc4193.cpp \
rfc/rfc4291.cpp \
rfc/rfc5735.cpp \
sentry/dummysentryadapter.cpp \
serveri18n.cpp \
settingsholder.cpp \
signature.cpp \
Expand All @@ -154,6 +153,7 @@ SOURCES += \
tasks/release/taskrelease.cpp \
tasks/removedevice/taskremovedevice.cpp \
tasks/sendfeedback/tasksendfeedback.cpp \
tasks/sentry/tasksentry.cpp \
tasks/servers/taskservers.cpp \
taskscheduler.cpp \
telemetry.cpp \
Expand Down Expand Up @@ -324,6 +324,7 @@ HEADERS += \
tasks/release/taskrelease.h \
tasks/removedevice/taskremovedevice.h \
tasks/sendfeedback/tasksendfeedback.h \
tasks/sentry/tasksentry.h \
tasks/servers/taskservers.h \
taskscheduler.h \
telemetry.h \
Expand Down
37 changes: 37 additions & 0 deletions src/sentry/sentryadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
#include "logger.h"
#include "settingsholder.h"
#include "mozillavpn.h"
#include "tasks/sentry/tasksentry.h"
#include "taskscheduler.h"

namespace {
SentryAdapter* s_instance = nullptr;
Logger logger(LOG_MAIN, "Sentry");
void* transport_state = 0;

} // namespace

Expand Down Expand Up @@ -58,6 +61,13 @@ void SentryAdapter::init() {
sentryFolder.toLocal8Bit().constData());
sentry_options_set_on_crash(options, &SentryAdapter::onCrash, NULL);

#ifdef SENTRY_TRANSPORT_ENABLED
sentry_transport_t* transport =
sentry_transport_new(&SentryAdapter::transportEnvelope);
sentry_transport_set_state(transport, transport_state);
sentry_options_set_transport(options, transport);
#endif

// Leaving this for convinence, be warned, it's spammy to stdout.
// sentry_options_set_debug(options, 1);

Expand Down Expand Up @@ -99,6 +109,8 @@ sentry_value_t SentryAdapter::onCrash(
sentry_value_t event, // used the same way as in `before_send`
void* closure // user-data that you can provide at configuration time
) {
Q_UNUSED(uctx);
Q_UNUSED(closure);
logger.info() << "Sentry ON CRASH";
// Do contextual clean-up before the crash is sent to sentry's backend
// infrastructure
Expand All @@ -115,3 +127,28 @@ sentry_value_t SentryAdapter::onCrash(
sentry_value_decref(event);
return sentry_value_new_null();
}

// static
void SentryAdapter::transportEnvelope(sentry_envelope_t* envelope,
void* state) {
/*
* Send the event here. If the transport requires state, such as an HTTP
* client object or request queue, it can be specified in the `state`
* parameter when configuring the transport. It will be passed as second
* argument to this function.
* The transport takes ownership of the `envelope`, and must free it once it
* is done.
*/
Q_UNUSED(state);
size_t sentry_buf_size = 0;
char* sentry_buf = sentry_envelope_serialize(envelope, &sentry_buf_size);

// Qt Will copy this.
auto qt_owned_buffer = QByteArray(sentry_buf, sentry_buf_size);
// We can now free the stuff.
sentry_envelope_free(envelope);
sentry_free(sentry_buf);

auto t = new TaskSentry(qt_owned_buffer);
TaskScheduler::scheduleTask(t);
}
2 changes: 1 addition & 1 deletion src/sentry/sentryadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SentryAdapter final : public QObject {
// Called before Sentry will send a crash report
static sentry_value_t onCrash(const sentry_ucontext_t* uctx,
sentry_value_t event, void* closure);

static void transportEnvelope(sentry_envelope_t* envelope, void* state);
#endif

private:
Expand Down
1 change: 1 addition & 0 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include($$PWD/qmake/debug.pri)
include($$PWD/qmake/includes_and_defines.pri)
include($$PWD/qmake/qt.pri)
include($$PWD/qmake/signature.pri)
include($$PWD/qmake/sentry.pri)
include($$PWD/qmake/webextension.pri)
include($$PWD/../glean/glean.pri)
include($$PWD/../nebula/nebula.pri)
Expand Down
38 changes: 38 additions & 0 deletions src/tasks/sentry/tasksentry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "tasksentry.h"
#include "errorhandler.h"
#include "leakdetector.h"
#include "logger.h"
#include "mozillavpn.h"
#include "networkrequest.h"

namespace {
Logger logger(LOG_MAIN, "TaskSentry");
}

TaskSentry::TaskSentry(const QByteArray& envelope) : Task("TaskSentry") {
MVPN_COUNT_CTOR(TaskSentry);
m_envelope = envelope;
}

TaskSentry::~TaskSentry() { MVPN_COUNT_DTOR(TaskSentry); }

void TaskSentry::run() {
NetworkRequest* request = NetworkRequest::createForSentry(this, m_envelope);

connect(request, &NetworkRequest::requestFailed, this,
[this](QNetworkReply::NetworkError error, const QByteArray&) {
Q_UNUSED(error);
logger.error() << "Failed to send envelope";
emit completed();
});
connect(request, &NetworkRequest::requestCompleted, this,
[this](const QByteArray& data) {
Q_UNUSED(data);
logger.debug() << "Sentry sent events";
emit completed();
});
}
26 changes: 26 additions & 0 deletions src/tasks/sentry/tasksentry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef TASKSENTRY_H
#define TASKSENTRY_H

#include "task.h"

#include <QObject>
#include <QByteArray>

class TaskSentry final : public Task {
Q_DISABLE_COPY_MOVE(TaskSentry)

public:
TaskSentry(const QByteArray& envelope);
~TaskSentry();

void run() override;

private:
QByteArray m_envelope;
};

#endif // TASKSENTRY_H

0 comments on commit a26b64d

Please sign in to comment.