Skip to content

Commit caaaff5

Browse files
authored
refactor(renderer): extract sentry and client config into dedicated modules (#1722)
* refactor(renderer): extract sentry and client config into dedicated modules * fix: adjust isEnabled calling
1 parent bf04524 commit caaaff5

3 files changed

Lines changed: 74 additions & 64 deletions

File tree

renderer/src/lib/client-config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { client } from '@common/api/generated/client.gen'
2+
import log from 'electron-log/renderer'
3+
4+
export async function configureClient() {
5+
try {
6+
const port = await window.electronAPI.getToolhivePort()
7+
const telemetryHeaders = await window.electronAPI.getTelemetryHeaders()
8+
const baseUrl = `http://localhost:${port}`
9+
10+
client.setConfig({
11+
baseUrl,
12+
headers: telemetryHeaders,
13+
})
14+
} catch (e) {
15+
log.error('Failed to get ToolHive port from main process: ', e)
16+
throw e
17+
}
18+
}

renderer/src/lib/sentry.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as Sentry from '@sentry/electron/renderer'
2+
3+
export function initSentry() {
4+
Sentry.init({
5+
// Adds request headers and IP for users, for more info visit:
6+
// https://docs.sentry.io/platforms/javascript/guides/electron/configuration/options/#sendDefaultPii
7+
sendDefaultPii: true,
8+
integrations: [
9+
Sentry.browserTracingIntegration(),
10+
Sentry.replayIntegration(),
11+
],
12+
// Set tracesSampleRate to 1.0 to capture 100%
13+
// of transactions for performance monitoring.
14+
// We recommend adjusting this value in production
15+
// Learn more at
16+
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
17+
tracesSampleRate: 1.0,
18+
// Capture Replay for 10% of all sessions,
19+
// plus for 100% of sessions with an error
20+
// Learn more at
21+
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
22+
replaysSessionSampleRate: 0.1,
23+
replaysOnErrorSampleRate: 1.0,
24+
// It will send errors, exceptions and captured messages to Sentry only if the user has enabled telemetry
25+
beforeSend: async (event) =>
26+
(await window.electronAPI.sentry.isEnabled()) ? event : null,
27+
// It will send transactions to Sentry only if the user has enabled telemetry
28+
beforeSendTransaction: async (transaction) => {
29+
if (!(await window.electronAPI.sentry.isEnabled())) {
30+
return null
31+
}
32+
if (!transaction?.contexts?.trace) return null
33+
34+
const instanceId = await window.electronAPI.getInstanceId()
35+
const trace = transaction.contexts.trace
36+
37+
return {
38+
...transaction,
39+
contexts: {
40+
...transaction.contexts,
41+
trace: {
42+
...trace,
43+
data: {
44+
...transaction.contexts.trace.data,
45+
'custom.user_id': instanceId,
46+
},
47+
},
48+
},
49+
}
50+
},
51+
})
52+
}

renderer/src/renderer.tsx

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { client } from '@common/api/generated/client.gen'
21
import { StrictMode } from 'react'
32
import ReactDOM from 'react-dom/client'
43
import {
@@ -9,7 +8,6 @@ import {
98
import { routeTree } from './route-tree.gen'
109
import { QueryClientProvider } from '@tanstack/react-query'
1110
import { TooltipProvider } from '@radix-ui/react-tooltip'
12-
import * as Sentry from '@sentry/electron/renderer'
1311
import { ThemeProvider } from './common/components/theme/theme-provider'
1412
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
1513
import log from 'electron-log/renderer'
@@ -18,79 +16,21 @@ import './index.css'
1816
import { PromptProvider } from './common/contexts/prompt/provider'
1917
import { trackPageView } from './common/lib/analytics'
2018
import { queryClient } from './common/lib/query-client'
19+
import { initSentry } from './lib/sentry'
20+
import { configureClient } from './lib/client-config'
2121
// Import feature flags to bind them to window for developer tools access
2222
import './common/lib/feature-flags'
2323
// Import OS design devtools to bind OsDesign.setMac/setWindows/reset to window
2424
import './common/lib/os-design'
2525

26-
// Sentry setup
27-
Sentry.init({
28-
// Adds request headers and IP for users, for more info visit:
29-
// https://docs.sentry.io/platforms/javascript/guides/electron/configuration/options/#sendDefaultPii
30-
sendDefaultPii: true,
31-
integrations: [
32-
Sentry.browserTracingIntegration(),
33-
Sentry.replayIntegration(),
34-
],
35-
// Set tracesSampleRate to 1.0 to capture 100%
36-
// of transactions for performance monitoring.
37-
// We recommend adjusting this value in production
38-
// Learn more at
39-
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
40-
tracesSampleRate: 1.0,
41-
// Capture Replay for 10% of all sessions,
42-
// plus for 100% of sessions with an error
43-
// Learn more at
44-
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
45-
replaysSessionSampleRate: 0.1,
46-
replaysOnErrorSampleRate: 1.0,
47-
// It will send errors, exceptions and captured messages to Sentry only if the user has enabled telemetry
48-
beforeSend: async (event) =>
49-
(await window.electronAPI.sentry.isEnabled) ? event : null,
50-
// It will send transactions to Sentry only if the user has enabled telemetry
51-
beforeSendTransaction: async (transaction) => {
52-
if (!(await window.electronAPI.sentry.isEnabled)) {
53-
return null
54-
}
55-
if (!transaction?.contexts?.trace) return null
56-
57-
const instanceId = await window.electronAPI.getInstanceId()
58-
const trace = transaction.contexts.trace
59-
60-
return {
61-
...transaction,
62-
contexts: {
63-
...transaction.contexts,
64-
trace: {
65-
...trace,
66-
data: {
67-
...transaction.contexts.trace.data,
68-
'custom.user_id': instanceId,
69-
},
70-
},
71-
},
72-
}
73-
},
74-
})
26+
initSentry()
7527

7628
if (!window.electronAPI || !window.electronAPI.getToolhivePort) {
7729
log.error('ToolHive port API not available in renderer')
7830
}
7931

8032
;(async () => {
81-
try {
82-
const port = await window.electronAPI.getToolhivePort()
83-
const telemetryHeaders = await window.electronAPI.getTelemetryHeaders()
84-
const baseUrl = `http://localhost:${port}`
85-
86-
client.setConfig({
87-
baseUrl,
88-
headers: telemetryHeaders,
89-
})
90-
} catch (e) {
91-
log.error('Failed to get ToolHive port from main process: ', e)
92-
throw e
93-
}
33+
await configureClient()
9434

9535
// One-time migration: sync the old localStorage quit-confirmation
9636
// preference into the main-process electron-store so existing users

0 commit comments

Comments
 (0)