Skip to content

Commit

Permalink
feat: refactor, integrate sentry with posthog
Browse files Browse the repository at this point in the history
  • Loading branch information
Weihua Lu authored and tiensonqin committed May 27, 2021
1 parent 8793af6 commit bf61ea1
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -60,6 +60,7 @@
"dependencies": {
"@excalidraw/excalidraw": "^0.4.2",
"@kanru/rage-wasm": "^0.2.1",
"@sentry/browser": "^6.4.1",
"@tippyjs/react": "^4.2.5",
"chokidar": "^3.5.1",
"chrono-node": "^2.2.4",
Expand Down
1 change: 0 additions & 1 deletion resources/js/sentry.min.js

This file was deleted.

14 changes: 5 additions & 9 deletions src/main/frontend/components/settings.cljs
Expand Up @@ -12,7 +12,7 @@
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user-handler]
[frontend.modules.instrumentation.posthog :as posthog]
[frontend.modules.instrumentation.core :as instrument]
[frontend.state :as state]
[frontend.ui :as ui]
[frontend.util :as util]
Expand Down Expand Up @@ -145,11 +145,6 @@
:width 500
:height 500}]])

(defn set-sentry-disabled!
[value]
(posthog/opt-out value)
(state/set-sentry-disabled! value))

(rum/defcs settings < rum/reactive
[]
(let [preferred-format (state/get-preferred-format)
Expand All @@ -160,7 +155,7 @@
current-repo (state/get-current-repo)
enable-journals? (state/enable-journals? current-repo)
enable-encryption? (state/enable-encryption? current-repo)
sentry-disabled? (state/sub :sentry/disabled?)
instrument-disabled? (state/sub :instrument/disabled?)
logical-outdenting? (state/logical-outdenting?)
enable-tooltip? (state/enable-tooltip?)
enable-git-auto-push? (state/enable-git-auto-push? current-repo)
Expand Down Expand Up @@ -380,8 +375,9 @@
[:p "Logseq will never collect your local graph database or sell your data."]
(toggle "usage-diagnostics"
(t :settings-page/disable-sentry)
(not sentry-disabled?)
(fn [] (set-sentry-disabled! (not sentry-disabled?))))]
(not instrument-disabled?)
(fn [] (instrument/disable-instrument
(not instrument-disabled?))))]

[:hr]

Expand Down
17 changes: 3 additions & 14 deletions src/main/frontend/handler.cljs
Expand Up @@ -15,7 +15,7 @@
[frontend.handler.repo :as repo-handler]
[frontend.handler.ui :as ui-handler]
[frontend.idb :as idb]
[frontend.modules.instrumentation.posthog :as posthog]
[frontend.modules.instrumentation.core :as instrument]
[frontend.modules.shortcut.core :as shortcut]
[frontend.search :as search]
[frontend.search.db :as search-db]
Expand Down Expand Up @@ -143,21 +143,11 @@
[{:url config/local-repo
:example? true}]))))

(defn init-sentry!
[]
(when-not (state/sentry-disabled?)
(let [cfg
{:dsn "https://636e9174ffa148c98d2b9d3369661683@o416451.ingest.sentry.io/5311485"
:release (util/format "logseq@%s" version/version)
:environment (if config/dev? "development" "production")
:tracesSampleRate 1.0}]
(.init js/window.Sentry (clj->js cfg)))))

(defn on-load-events
[]
(set! js/window.onload
(fn []
(when-not config/dev? (init-sentry!)))))
(instrument/init))))

(defn clear-cache!
[]
Expand Down Expand Up @@ -198,8 +188,7 @@
(file-handler/run-writes-chan!)
(shortcut/install-shortcuts!)
(when (util/electron?)
(el/listen!))
(posthog/init)))
(el/listen!))))

(defn stop! []
(prn "stop!"))
18 changes: 18 additions & 0 deletions src/main/frontend/modules/instrumentation/core.cljs
@@ -0,0 +1,18 @@
(ns frontend.modules.instrumentation.core
(:require [frontend.modules.instrumentation.posthog :as posthog]
[frontend.modules.instrumentation.sentry :as sentry]
[frontend.state :as state]
[frontend.storage :as storage]))

(defn init
[]
(when-not (:instrument/disabled? @state/state)
(posthog/init)
(sentry/init)))

(defn disable-instrument [disable?]
(state/set-state! :instrument/disabled? disable?)
(storage/set "instrument-disabled" disable?)
(posthog/opt-out disable?)
(when-not disable?
(sentry/init)))
4 changes: 3 additions & 1 deletion src/main/frontend/modules/instrumentation/posthog.cljs
Expand Up @@ -33,4 +33,6 @@
(defn opt-out [opt-out?]
(if opt-out?
(posthog/opt_out_capturing)
(posthog/opt_in_capturing)))
(do
(init)
(posthog/opt_in_capturing))))
17 changes: 17 additions & 0 deletions src/main/frontend/modules/instrumentation/sentry.cljs
@@ -0,0 +1,17 @@
(ns frontend.modules.instrumentation.sentry
(:require [frontend.version :refer [version]]
[frontend.util :as util]
[frontend.config :as cfg]
["@sentry/browser" :as Sentry]
["posthog-js" :as posthog]))

(def config
{:dsn "https://636e9174ffa148c98d2b9d3369661683@o416451.ingest.sentry.io/5311485"
:release (util/format "logseq@%s" version)
:environment (if cfg/dev? "development" "production")
:integrations [(new posthog/SentryIntegration posthog "logseq" 5311485)]
:debug cfg/dev?
:tracesSampleRate 1.0})

(defn init []
(Sentry/init (clj->js config)))
11 changes: 1 addition & 10 deletions src/main/frontend/state.cljs
Expand Up @@ -29,7 +29,7 @@
:repo/changed-files nil
:nfs/user-granted? {}
:nfs/refreshing? nil
:sentry/disabled? (storage/get "sentry-disabled")
:instrument/disabled? (storage/get "instrument-disabled")
;; TODO: how to detect the network reliably?
:network/online? true
:indexeddb/support? true
Expand Down Expand Up @@ -1224,15 +1224,6 @@
[value]
(set-state! :block/component-editing-mode? value))

(defn sentry-disabled?
[]
(:sentry/disabled? @state))

(defn set-sentry-disabled!
[value]
(set-state! :sentry/disabled? value)
(storage/set "sentry-disabled" value))

(defn logical-outdenting?
[]
(:editor/logical-outdenting?
Expand Down
57 changes: 57 additions & 0 deletions yarn.lock
Expand Up @@ -242,6 +242,58 @@
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.9.2.tgz#adea7b6953cbb34651766b0548468e743c6a2353"
integrity sha512-VZMYa7+fXHdwIq1TDhSXoVmSPEGM/aa+6Aiq3nVVJ9bXr24zScr+NlKFKC3iPljA7ho/GAZr+d2jOf5GIRC30Q==

"@sentry/browser@^6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.4.1.tgz#b6c62736caaade7fdf6638513d9aad138abde2ac"
integrity sha512-3cDud6GWutnJqcnheIq0lPNTsUJbrRLevQ+g1YfawVXFUxfmmY2bOsGd/Mxq17LxYeBHgKTitXv3DU1bsQ+WBQ==
dependencies:
"@sentry/core" "6.4.1"
"@sentry/types" "6.4.1"
"@sentry/utils" "6.4.1"
tslib "^1.9.3"

"@sentry/core@6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.4.1.tgz#789b0071996de5c1a20673f879408926aa3b4fa6"
integrity sha512-Lx13oTiP+Tjvm5VxulcCszNVd2S1wY4viSnr+ygq62ySVERR+t7uOZDSARZ0rZ259GwW6nkbMh9dDmD0d6VCGQ==
dependencies:
"@sentry/hub" "6.4.1"
"@sentry/minimal" "6.4.1"
"@sentry/types" "6.4.1"
"@sentry/utils" "6.4.1"
tslib "^1.9.3"

"@sentry/hub@6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-6.4.1.tgz#fa9c05ca32674e2e8477120b71084a1c91a5e023"
integrity sha512-7IZRP5buDE6s/c3vWzzPR/ySE+8GUuHPgTEPiDCPOCWwUN11zXDafJDKkJqY3muJfebUKmC/JG67RyBx+XlnlQ==
dependencies:
"@sentry/types" "6.4.1"
"@sentry/utils" "6.4.1"
tslib "^1.9.3"

"@sentry/minimal@6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.4.1.tgz#d3f968c060c3d3cc997071756659e24047b5dd97"
integrity sha512-4x/PRbDZACCKJqjta9EkhiIMyGMf7VgBX13EEWEDVWLP7ymFukBuTr4ap/Tz9429kB/yXZuDGGMIZp/G618H3g==
dependencies:
"@sentry/hub" "6.4.1"
"@sentry/types" "6.4.1"
tslib "^1.9.3"

"@sentry/types@6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.4.1.tgz#7c0a4355a1d04321b901197723a8f55c263226e9"
integrity sha512-sTu/GaLsLYk1AkAqpkMT4+4q665LtZjhV0hkgiTD4N3zPl5uSf1pCIzxPRYjOpe7NEANmWv8U4PaGKGtc2eMfA==

"@sentry/utils@6.4.1":
version "6.4.1"
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-6.4.1.tgz#55fa7da58898773cbd538e4895fc2e4ec695ecab"
integrity sha512-xJ1uVa5fvg23pXQfulvCIBb9pQ3p1awyd1PapK2AYi+wKjTuYl4B9edmhjRREEQEExznl/d2OVm78fRXLq7M9Q==
dependencies:
"@sentry/types" "6.4.1"
tslib "^1.9.3"

"@sindresorhus/is@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz"
Expand Down Expand Up @@ -6248,6 +6300,11 @@ ts-essentials@^2.0.3:
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz"
integrity sha512-3IVX4nI6B5cc31/GFFE+i8ey/N2eA0CZDbo6n0yrz0zDX8ZJ8djmU1p+XRz7G3is0F3bB3pu2pAroFdAWQKU3w==

tslib@^1.9.3:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tty-browserify@0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz"
Expand Down

0 comments on commit bf61ea1

Please sign in to comment.