-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathonCreateGlobalContext.server.ts
More file actions
30 lines (25 loc) · 1.12 KB
/
onCreateGlobalContext.server.ts
File metadata and controls
30 lines (25 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export { onCreateGlobalContext }
import * as SentryNode from '@sentry/node'
import type { GlobalContextServer } from 'vike/types'
import type { SentryOptions } from '../types.js'
import type { SentryNodeOptions } from '../types.js'
import { resolveDsn } from '../utils/resolveDsn.js'
import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js'
async function onCreateGlobalContext(globalContext: GlobalContextServer): Promise<void> {
const sentryConfigs = globalContext.config.sentry || []
const serverConfig: SentryOptions = {}
for (const curr of [...sentryConfigs].reverse()) {
const resolvedConfig = typeof curr === 'function' ? await curr() : curr
Object.assign(serverConfig, resolvedConfig)
}
if (!SentryNode.getClient()) {
SentryNode.init(resolveSentryServerSettings(serverConfig))
}
}
const resolveSentryServerSettings = (serverConfig: SentryNodeOptions) =>
({
environment: serverConfig.environment || import.meta.env.MODE || 'production',
dsn: resolveDsn(serverConfig.dsn),
traceSampleRate: serverConfig.tracesSampleRate ?? TRACE_DEFAULT_SAMPLE_RATE,
...serverConfig,
}) as SentryNodeOptions