Skip to content

Commit 1e09fd6

Browse files
rlejeune74cuthix
authored andcommitted
feat(GODT-2278): improve sentry logs.
1 parent 48f2c56 commit 1e09fd6

File tree

7 files changed

+56
-20
lines changed

7 files changed

+56
-20
lines changed

internal/frontend/bridge-gui/bridge-gui/AppController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ ProcessMonitor *AppController::bridgeMonitor() const {
7676
//****************************************************************************************************************************************************
7777
void AppController::onFatalError(QString const &function, QString const &message) {
7878
QString const fullMessage = QString("%1(): %2").arg(function, message);
79-
reportSentryException(SENTRY_LEVEL_ERROR, "AppController got notified of a fatal error", "Exception", fullMessage.toLocal8Bit());
79+
auto uuid = reportSentryException(SENTRY_LEVEL_ERROR, "AppController got notified of a fatal error", "Exception", fullMessage.toLocal8Bit());
8080
QMessageBox::critical(nullptr, tr("Error"), message);
8181
restart(true);
82-
log().fatal(fullMessage);
82+
log().fatal(QString("reportID: %1 Captured exception: %2").arg(QByteArray(uuid.bytes, 16).toHex()).arg(fullMessage));
8383
qApp->exit(EXIT_FAILURE);
8484
}
8585

internal/frontend/bridge-gui/bridge-gui/SentryUtils.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,38 @@
1616
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
1717

1818
#include "SentryUtils.h"
19+
#include "Version.h"
20+
#include <bridgepp/BridgeUtils.h>
1921

22+
#include <QByteArray>
23+
#include <QCryptographicHash>
24+
#include <QString>
25+
#include <QSysInfo>
2026

2127
static constexpr const char *LoggerName = "bridge-gui";
2228

29+
QByteArray getProtectedHostname() {
30+
QByteArray hostname = QCryptographicHash::hash(QSysInfo::machineHostName().toUtf8(), QCryptographicHash::Sha256);
31+
return hostname.toHex();
32+
}
33+
34+
void setSentryReportScope() {
35+
sentry_set_tag("OS", bridgepp::goos().toUtf8());
36+
sentry_set_tag("Client", PROJECT_FULL_NAME);
37+
sentry_set_tag("Version", PROJECT_VER);
38+
sentry_set_tag("UserAgent", QString("/ (%1)").arg(bridgepp::goos()).toUtf8());
39+
sentry_set_tag("HostArch", QSysInfo::currentCpuArchitecture().toUtf8());
40+
sentry_set_tag("server_name", getProtectedHostname());
41+
}
2342

24-
void reportSentryEvent(sentry_level_t level, const char *message) {
43+
sentry_uuid_t reportSentryEvent(sentry_level_t level, const char *message) {
2544
auto event = sentry_value_new_message_event(level, LoggerName, message);
26-
sentry_capture_event(event);
45+
return sentry_capture_event(event);
2746
}
2847

2948

30-
void reportSentryException(sentry_level_t level, const char *message, const char *exceptionType, const char *exception) {
49+
sentry_uuid_t reportSentryException(sentry_level_t level, const char *message, const char *exceptionType, const char *exception) {
3150
auto event = sentry_value_new_message_event(level, LoggerName, message);
3251
sentry_event_add_exception(event, sentry_value_new_exception(exceptionType, exception));
33-
sentry_capture_event(event);
52+
return sentry_capture_event(event);
3453
}

internal/frontend/bridge-gui/bridge-gui/SentryUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
#include <sentry.h>
2323

24-
25-
void reportSentryEvent(sentry_level_t level, const char *message);
26-
void reportSentryException(sentry_level_t level, const char *message, const char *exceptionType, const char *exception);
24+
void setSentryReportScope();
25+
sentry_uuid_t reportSentryEvent(sentry_level_t level, const char *message);
26+
sentry_uuid_t reportSentryException(sentry_level_t level, const char *message, const char *exceptionType, const char *exception);
2727

2828
#endif //BRIDGE_GUI_SENTRYUTILS_H

internal/frontend/bridge-gui/bridge-gui/build.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ function check_exit() {
7575

7676
Write-host "Running build for version $bridgeVersion - $buildConfig in $buildDir"
7777

78+
$REVISION_HASH = git rev-parse --short=10 HEAD
7879
git submodule update --init --recursive $vcpkgRoot
7980
. $vcpkgBootstrap -disableMetrics
8081
. $vcpkgExe install sentry-native:x64-windows grpc:x64-windows --clean-after-build
8182
. $vcpkgExe upgrade --no-dry-run
8283
. $cmakeExe -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE="$buildConfig" `
8384
-DBRIDGE_APP_FULL_NAME="$bridgeFullName" `
8485
-DBRIDGE_VENDOR="$bridgeVendor" `
86+
-DBRIDGE_REVISION=$REVISION_HASH `
8587
-DBRIDGE_APP_VERSION="$bridgeVersion" `
8688
-S . -B $buildDir
8789

internal/frontend/bridge-gui/bridge-gui/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ BRIDGE_VENDOR=${BRIDGE_VENDOR:-"Proton AG"}
5555
BUILD_CONFIG=${BRIDGE_GUI_BUILD_CONFIG:-Debug}
5656
BUILD_DIR=$(echo "./cmake-build-${BUILD_CONFIG}" | tr '[:upper:]' '[:lower:]')
5757
VCPKG_ROOT="${BRIDGE_REPO_ROOT}/extern/vcpkg"
58-
58+
BRIDGE_REVISION=$(git rev-parse --short=10 HEAD)
5959
git submodule update --init --recursive ${VCPKG_ROOT}
6060
check_exit "Failed to initialize vcpkg as a submodule."
6161

@@ -93,6 +93,7 @@ cmake \
9393
-DCMAKE_BUILD_TYPE="${BUILD_CONFIG}" \
9494
-DBRIDGE_APP_FULL_NAME="${BRIDGE_APP_FULL_NAME}" \
9595
-DBRIDGE_VENDOR="${BRIDGE_VENDOR}" \
96+
-DBRIDGE_REVISION="${BRIDGE_REVISION}" \
9697
-DBRIDGE_APP_VERSION="${BRIDGE_APP_VERSION}" "${BRIDGE_CMAKE_MACOS_OPTS}" \
9798
-G Ninja \
9899
-S . \

internal/frontend/bridge-gui/bridge-gui/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <bridgepp/Log/Log.h>
2929
#include <bridgepp/ProcessMonitor.h>
3030
#include <sentry.h>
31+
#include <SentryUtils.h>
3132
#include <project_sentry_config.h>
3233

3334

@@ -238,7 +239,8 @@ void focusOtherInstance() {
238239
}
239240
catch (Exception const &e) {
240241
app().log().error(e.qwhat());
241-
reportSentryException(SENTRY_LEVEL_ERROR, "Exception occurred during focusOtherInstance()", "Exception", e.what());
242+
auto uuid = reportSentryException(SENTRY_LEVEL_ERROR, "Exception occurred during focusOtherInstance()", "Exception", e.what());
243+
app().log().fatal(QString("reportID: %1 Captured exception: %2").arg(QByteArray(uuid.bytes, 16).toHex()).arg(e.qwhat()));
242244
}
243245
}
244246

@@ -296,13 +298,13 @@ int main(int argc, char *argv[]) {
296298
const QString sentryCachePath = sentryCacheDir();
297299
sentry_options_set_database_path(sentryOptions, sentryCachePath.toStdString().c_str());
298300
}
299-
sentry_options_set_release(sentryOptions, SentryProductID);
301+
sentry_options_set_release(sentryOptions, QByteArray(PROJECT_REVISION).toHex());
300302
// Enable this for debugging sentry.
301303
// sentry_options_set_debug(sentryOptions, 1);
302304
if (sentry_init(sentryOptions) != 0) {
303305
std::cerr << "Failed to initialize sentry" << std::endl;
304306
}
305-
307+
setSentryReportScope();
306308
auto sentryClose = qScopeGuard([] { sentry_close(); });
307309

308310
// The application instance is needed to display system message boxes. As we may have to do it in the exception handler,
@@ -426,9 +428,9 @@ int main(int argc, char *argv[]) {
426428
return result;
427429
}
428430
catch (Exception const &e) {
429-
reportSentryException(SENTRY_LEVEL_ERROR, "Exception occurred during main", "Exception", e.what());
431+
auto uuid = reportSentryException(SENTRY_LEVEL_ERROR, "Exception occurred during main", "Exception", e.what());
430432
QMessageBox::critical(nullptr, "Error", e.qwhat());
431-
QTextStream(stderr) << e.qwhat() << "\n";
433+
QTextStream(stderr) << "reportID: " << QByteArray(uuid.bytes, 16).toHex() << "Captured exception :" << e.qwhat() << "\n";
432434
return EXIT_FAILURE;
433435
}
434436
}

internal/sentry/reporter.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package sentry
1919

2020
import (
21+
"crypto/sha256"
2122
"errors"
2223
"fmt"
2324
"log"
@@ -62,19 +63,29 @@ type Reporter struct {
6263
appVersion string
6364
identifier Identifier
6465
hostArch string
66+
serverName string
6567
}
6668

6769
type Identifier interface {
6870
GetUserAgent() string
6971
}
7072

73+
func getProtectedHostname() string {
74+
hostname, err := os.Hostname()
75+
if err != nil {
76+
return "Unknown"
77+
}
78+
return fmt.Sprintf("%x", sha256.Sum256([]byte(hostname)))
79+
}
80+
7181
// NewReporter creates new sentry reporter with appName and appVersion to report.
7282
func NewReporter(appName, appVersion string, identifier Identifier) *Reporter {
7383
return &Reporter{
7484
appName: appName,
7585
appVersion: appVersion,
7686
identifier: identifier,
7787
hostArch: getHostArch(),
88+
serverName: getProtectedHostname(),
7889
}
7990
}
8091

@@ -126,11 +137,12 @@ func (r *Reporter) scopedReport(context map[string]interface{}, doReport func())
126137
}
127138

128139
tags := map[string]string{
129-
"OS": runtime.GOOS,
130-
"Client": r.appName,
131-
"Version": r.appVersion,
132-
"UserAgent": r.identifier.GetUserAgent(),
133-
"HostArch": r.hostArch,
140+
"OS": runtime.GOOS,
141+
"Client": r.appName,
142+
"Version": r.appVersion,
143+
"UserAgent": r.identifier.GetUserAgent(),
144+
"HostArch": r.hostArch,
145+
"server_name": r.serverName,
134146
}
135147

136148
sentry.WithScope(func(scope *sentry.Scope) {

0 commit comments

Comments
 (0)