Skip to content

Commit

Permalink
chore: add sentry tracing to critical parts
Browse files Browse the repository at this point in the history
  • Loading branch information
jely2002 committed Jun 2, 2021
1 parent d9dae48 commit 70bc048
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
6 changes: 5 additions & 1 deletion modules/Analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Sentry = require("@sentry/electron");
const Tracing = require("@sentry/tracing");
const path = require("path");

class Analytics {
Expand All @@ -13,7 +14,10 @@ class Analytics {
dsn: process.env.SENTRY_DSN,
release: "youtube-dl-gui@" + this.app.getVersion(),
sendDefaultPii: true,
environment: process.argv[2] === '--dev' ? "development" : "production"
environment: process.argv[2] === '--dev' ? "development" : "production",
integrations: [new Tracing.Integrations.BrowserTracing()],
tracesSampleRate: process.argv[2] === '--dev' ? 1.0 : 0.01,
autoSessionTracking: true
});
resolve();
});
Expand Down
10 changes: 10 additions & 0 deletions modules/BinaryUpdater.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const axios = require("axios");
const fs = require("fs");
const Sentry = require("@sentry/node");

class BinaryUpdater {

Expand All @@ -10,22 +11,30 @@ class BinaryUpdater {

//Checks for an update and download it if there is.
async checkUpdate() {
const transaction = Sentry.startTransaction({ name: "checkUpdate" });
const span = transaction.startChild({ op: "task" });
console.log("Checking for a new version of ytdl.");
const localVersion = await this.getLocalVersion();
const [remoteUrl, remoteVersion] = await this.getRemoteVersion();
if(remoteVersion === localVersion) {
transaction.setTag("download", "up-to-data");
console.log(`Binaries were already up-to-date! Version: ${localVersion}`);
} else if(localVersion == null) {
transaction.setTag("download", "corrupted");
console.log("Binaries may have been corrupted, downloading binaries to be on the safe side.");
this.win.webContents.send("binaryLock", {lock: true, placeholder: `Re-installing ytdl version ${remoteVersion}...`})
await this.downloadUpdate(remoteUrl, remoteVersion);
} else if(remoteVersion == null) {
transaction.setTag("download", "down");
console.log("Unable to check for new updates, yt-dl.org may be down.");
} else {
console.log(`New version ${remoteVersion} found. Updating...`);
transaction.setTag("download", "update");
this.win.webContents.send("binaryLock", {lock: true, placeholder: `Updating ytdl to version ${remoteVersion}...`})
await this.downloadUpdate(remoteUrl, remoteVersion);
}
span.finish();
transaction.finish();
}

//Returns the currently downloaded version of youtube-dl
Expand Down Expand Up @@ -53,6 +62,7 @@ class BinaryUpdater {

//Returns an array containing the latest available remote version and the download link to it.
async getRemoteVersion() {

const url = (this.paths.platform === "win32") ? "https://yt-dl.org/downloads/latest/youtube-dl.exe" : "https://yt-dl.org/downloads/latest/youtube-dl";
try {
await axios.get(url, {maxRedirects: 0})
Expand Down
18 changes: 18 additions & 0 deletions modules/types/Query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const execa = require('execa');
const UserAgent = require('user-agents');
const Sentry = require("@sentry/node");

class Query {
constructor(environment, identifier) {
Expand Down Expand Up @@ -47,19 +48,27 @@ class Query {
command = this.environment.pythonCommand;
}
if(cb == null) {
const transaction = Sentry.startTransaction({ name: "infoQuery" });
const span = transaction.startChild({ op: "task" });
//Return the data after the query has completed fully.
try {
const {stdout} = await execa(command, args);
span.finish();
transaction.finish();
return stdout
} catch(e) {
if(!this.environment.errorHandler.checkError(e.stderr, this.identifier)) {
if(!this.environment.errorHandler.checkError(e.shortMessage, this.identifier)) {
this.environment.errorHandler.raiseUnhandledError("Unhandled error (execa)", JSON.stringify(e, null, 2), this.identifier);
}
}
span.finish();
transaction.finish();
return "{}";
}
} else {
const transaction = Sentry.startTransaction({ name: "liveQuery" });
const span = transaction.startChild({ op: "task" });
//Return data while the query is running (live)
//Return "done" when the query has finished
return await new Promise((resolve) => {
Expand All @@ -70,16 +79,25 @@ class Query {
});
this.process.stdout.on('close', () => {
if(this.process.killed) {
transaction.setTag("result", "killed");
span.finish();
transaction.finish();
cb("killed");
resolve("killed");
}
transaction.setTag("result", "done");
span.finish();
transaction.finish();
cb("done");
resolve("done");
});
this.process.stderr.on("data", (data) => {
if(this.environment.errorHandler.checkError(data.toString(), this.identifier)) {
cb("killed");
resolve("killed");
transaction.setTag("result", "killed");
span.finish();
transaction.finish();
}
console.error(data.toString())
})
Expand Down
18 changes: 10 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
},
"dependencies": {
"@sentry/electron": "^2.4.1",
"@sentry/node": "^5.27.6",
"@sentry/tracing": "^5.27.6",
"axios": "^0.21.1",
"bootstrap": "^4.5.3",
"bootstrap-icons": "^1.3.0",
Expand Down
6 changes: 5 additions & 1 deletion preload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const Sentry = require("@sentry/electron");
const Tracing = require("@sentry/tracing");
const version = require('./package.json').version;
const { contextBridge, ipcRenderer } = require('electron')

Sentry.init({
dsn: process.env.SENTRY_DSN,
release: "youtube-dl-gui@" + version,
sendDefaultPii: true,
environment: process.argv[2] === '--dev' ? "development" : "production"
integrations: [new Tracing.Integrations.BrowserTracing()],
tracesSampleRate: process.argv[2] === '--dev' ? 1.0 : 0.01,
environment: process.argv[2] === '--dev' ? "development" : "production",
autoSessionTracking: true
});

contextBridge.exposeInMainWorld(
Expand Down

0 comments on commit 70bc048

Please sign in to comment.