Skip to content

Commit

Permalink
feat: Add runtime config support (#254)
Browse files Browse the repository at this point in the history
Co-authored-by: Frédéric Ledarath <frederic.ledarath@creatiwity.net>
Co-authored-by: Rafał Chłodnicki <rchl2k@gmail.com>
  • Loading branch information
3 people committed Jan 21, 2021
1 parent 4a57102 commit 7f8b373
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/content/en/sentry/lazy-loading.md
@@ -1,7 +1,7 @@
---
title: Lazy-Loading (client-side)
description: 'Load Sentry module lazily on the client'
position: 4
position: 6
category: Sentry
---

Expand Down
20 changes: 16 additions & 4 deletions docs/content/en/sentry/options.md
@@ -1,12 +1,18 @@
---
title: Options
description: 'Options can be passed to Sentry using either environment variables'
position: 5
position: 4
category: Sentry
---

Options can be passed using either environment variables or `sentry` section in `nuxt.config.js`.
Normally, setting required DSN information would be enough.
Options can be passed using either:
- environment variables
- `sentry` object in `nuxt.config.js`
- when registering the module: `modules: [['@nuxtjs/sentry', {/*options*/}]]`

The `config`, `serverConfig` and `clientConfig` options can also be configured using [Runtime Config](/sentry/runtime-config).

Normally, just setting DSN would be enough.

### dsn
- Type: `String`
Expand Down Expand Up @@ -108,6 +114,12 @@ Normally, setting required DSN information would be enough.
- Default: `false`
- Whether the Sentry chunk should be preloaded

### runtimeConfigKey
- Type: `String`
- Default: `sentry`
- Specified object in Nuxt config in `publicRuntimeConfig[runtimeConfigKey]` will override some options at runtime. See documentation at https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-runtime-config/
- Used to define the environment at runtime for example

### disabled
- Type: `Boolean`
- Default: `process.env.SENTRY_DISABLED || false`
Expand Down Expand Up @@ -252,4 +264,4 @@ Normally, setting required DSN information would be enough.
### requestHandlerConfig
- Type: `Object`
- Default: `{}`
- Options passed to `requestHandler` in `@sentry/node`. See: https://docs.sentry.io/platforms/node/guides/express/
- Options passed to `requestHandler` in `@sentry/node`. See: https://docs.sentry.io/platforms/node/guides/express/
30 changes: 30 additions & 0 deletions docs/content/en/sentry/runtime-config.md
@@ -0,0 +1,30 @@
---
title: Runtime config
description: "Load Sentry configuration at runtime"
position: 5
category: Sentry
---

Defining options using the [Nuxt Runtime Config](https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-runtime-config/) functionality allows them to be runtime-based rather than build-time based, as is the case by default.

Currently only the `config`, `clientConfig` and `serverConfig` [options](/sentry/options) can be configured using the runtime config.

In the Nuxt configuration file define a `publicRuntimeConfig.sentry` configuration object with settings that will be applied at runtime. For example:

```js [nuxt.config.js]
publicRuntimeConfig: {
sentry: {
config: {
environment: process.env.SENTRY_ENVIRONMENT
},
serverConfig: {
// Any server-specific config
},
clientConfig: {
// Any client-specific config
}
}
}
```

You can customize the key that is used to access settings from `publicRuntimeConfig` by setting [`runtimeConfigKey`](/sentry/options#runtimeconfigkey) in the non-runtime options.
2 changes: 1 addition & 1 deletion docs/content/en/sentry/submitting-releases.md
Expand Up @@ -2,7 +2,7 @@
title: Submitting releases to Sentry
menuTitle: Submitting releases
description: 'Submitting releases to Sentry'
position: 6
position: 7
category: Sentry
---

Expand Down
20 changes: 14 additions & 6 deletions lib/core/hooks.js
@@ -1,5 +1,5 @@
import { resolve, posix } from 'path'
import deepMerge from 'deepmerge'
import merge from 'lodash.merge'
import * as Integrations from '@sentry/integrations'
import * as Sentry from '@sentry/node'
import WebpackPlugin from '@sentry/webpack-plugin'
Expand Down Expand Up @@ -47,8 +47,8 @@ export async function buildHook (moduleContainer, options, logger) {
}
}

options.serverConfig = deepMerge.all([options.config, options.serverConfig])
options.clientConfig = deepMerge.all([options.config, options.clientConfig])
options.serverConfig = merge({}, options.config, options.serverConfig)
options.clientConfig = merge({}, options.config, options.clientConfig)

const apiMethods = await getBrowserApiMethods()

Expand All @@ -64,7 +64,7 @@ export async function buildHook (moduleContainer, options, logger) {
}

options.lazy = /** @type {Required<import('../../types/sentry').LazyConfiguration>} */(
Object.assign({}, defaultLazyOptions, options.lazy)
merge({}, defaultLazyOptions, options.lazy)
)

if (!options.lazy.injectMock) {
Expand All @@ -87,7 +87,7 @@ export async function buildHook (moduleContainer, options, logger) {
}
}
if (options.tracing) {
options.tracing = deepMerge.all([{
options.tracing = merge({
tracesSampleRate: 1.0,
vueOptions: {
tracing: true,
Expand All @@ -98,7 +98,7 @@ export async function buildHook (moduleContainer, options, logger) {
}
},
browserOptions: {}
}, typeof options.tracing === 'boolean' ? {} : options.tracing])
}, typeof options.tracing === 'boolean' ? {} : options.tracing)
options.clientConfig.tracesSampleRate = options.tracing.tracesSampleRate
}

Expand All @@ -119,6 +119,7 @@ export async function buildHook (moduleContainer, options, logger) {
SENTRY_DEFAULT_INTEGRATIONS,
SENTRY_BROWSER_INTEGRATIONS,
dev: moduleContainer.options.dev,
runtimeConfigKey: options.runtimeConfigKey,
config: {
dsn: options.dsn,
...options.clientConfig
Expand All @@ -145,6 +146,7 @@ export async function buildHook (moduleContainer, options, logger) {
mode: 'server',
options: {
dev: moduleContainer.options.dev,
runtimeConfigKey: options.runtimeConfigKey,
lazy: options.lazy,
apiMethods,
logMockCalls: options.logMockCalls // for mocked only
Expand Down Expand Up @@ -249,6 +251,12 @@ export async function initializeServerSentry (moduleContainer, options) {
// Ignored
}

const { publicRuntimeConfig } = moduleContainer.options
const { runtimeConfigKey } = options
if (publicRuntimeConfig && runtimeConfigKey && publicRuntimeConfig[runtimeConfigKey]) {
merge(options.serverConfig, publicRuntimeConfig[runtimeConfigKey].config, publicRuntimeConfig[runtimeConfigKey].serverConfig)
}

if (canInitialize(options)) {
Sentry.init({
dsn: options.dsn,
Expand Down
5 changes: 3 additions & 2 deletions lib/module.js
@@ -1,5 +1,5 @@
import consola from 'consola'
import deepMerge from 'deepmerge'
import merge from 'lodash.merge'
import { Handlers as SentryHandlers, captureException, withScope } from '@sentry/node'
import { buildHook, initializeServerSentry, webpackConfigHook } from './core/hooks'
import { boolToText, canInitialize, clientSentryEnabled, envToBool, serverSentryEnabled } from './core/utils'
Expand All @@ -14,6 +14,7 @@ export default function SentryModule (moduleOptions) {
dsn: process.env.SENTRY_DSN || '',
disabled: envToBool(process.env.SENTRY_DISABLED) || false,
initialize: envToBool(process.env.SENTRY_INITIALIZE) || true,
runtimeConfigKey: 'sentry',
disableClientSide: envToBool(process.env.SENTRY_DISABLE_CLIENT_SIDE) || false,
disableServerSide: envToBool(process.env.SENTRY_DISABLE_SERVER_SIDE) || false,
publishRelease: envToBool(process.env.SENTRY_PUBLISH_RELEASE) || false,
Expand Down Expand Up @@ -55,7 +56,7 @@ export default function SentryModule (moduleOptions) {

const topLevelOptions = this.options.sentry || {}
const options = /** @type {Required<import('../types/sentry').ModuleConfiguration>} */(
deepMerge.all([defaults, topLevelOptions, moduleOptions])
merge({}, defaults, topLevelOptions, moduleOptions)
)

if (serverSentryEnabled(options)) {
Expand Down
7 changes: 7 additions & 0 deletions lib/plugin.client.js
@@ -1,4 +1,5 @@
import VueLib from 'vue'
import merge from 'lodash.merge'
import * as Sentry from '@sentry/browser'
<%
if (options.initialize) {
Expand All @@ -18,6 +19,12 @@ export default function (ctx, inject) {
<% if (options.initialize) { %>
/* eslint-disable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
const config = <%= serialize(options.config) %>

const runtimeConfigKey = <%= serialize(options.runtimeConfigKey) %>
if (ctx.$config && runtimeConfigKey && ctx.$config[runtimeConfigKey]) {
merge(config, ctx.$config[runtimeConfigKey].config, ctx.$config[runtimeConfigKey].clientConfig)
}

config.integrations = [
<%= Object.entries(options.integrations).map(([name, integration]) => {
if (name === 'Vue') {
Expand Down
7 changes: 7 additions & 0 deletions lib/plugin.lazy.js
Expand Up @@ -123,6 +123,13 @@ async function loadSentry (ctx, inject) {
%>
/* eslint-disable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
const config = <%= serialize(options.config) %>

const runtimeConfigKey = <%= serialize(options.runtimeConfigKey) %>
if (ctx.$config && runtimeConfigKey && ctx.$config[runtimeConfigKey]) {
const merge = await import(/* <%= magicComments.join(', ') %> */ 'lodash.merge')
merge(config, ctx.$config[runtimeConfigKey].config, ctx.$config[runtimeConfigKey].clientConfig)
}

config.integrations = [
<%= Object.entries(options.integrations).map(([name, integration]) => {
if (name === 'Vue') {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -48,7 +48,7 @@
"@sentry/node": "^5.29.2",
"@sentry/webpack-plugin": "^1.14.0",
"consola": "^2.15.0",
"deepmerge": "^4.2.2"
"lodash.merge": "^4.6.2"
},
"devDependencies": {
"@babel/core": "^7.12.10",
Expand All @@ -58,8 +58,8 @@
"@nuxtjs/module-test-utils": "^1.6.3",
"@release-it/conventional-changelog": "^2.0.0",
"@types/consola": "^2.2.5",
"@types/deepmerge": "^2.2.0",
"@types/jest": "^26.0.19",
"@types/lodash.merge": "^4.6.6",
"@types/node": "^14.14.14",
"@types/request-promise-native": "^1.0.17",
"babel-core": "^7.0.0-bridge.0",
Expand Down
6 changes: 6 additions & 0 deletions types/extend.d.ts
Expand Up @@ -29,6 +29,12 @@ declare module '@nuxt/types' {
}
}

declare module '@nuxt/types/config/runtime' {
interface NuxtRuntimeConfig {
sentry?: ModuleConfiguration
}
}

// add types for Vuex Store
declare module 'vuex/types' {
interface Store<S> {
Expand Down
1 change: 1 addition & 0 deletions types/sentry.d.ts
Expand Up @@ -64,6 +64,7 @@ export interface ModuleConfiguration {
logMockCalls?: boolean
publishRelease?: boolean
repo?: string
runtimeConfigKey?: string
serverConfig?: SentryOptions
serverIntegrations?: IntegrationsConfiguration
sourceMapStyle?: WebpackOptions.Devtool
Expand Down
26 changes: 18 additions & 8 deletions yarn.lock
Expand Up @@ -2274,13 +2274,6 @@
dependencies:
consola "*"

"@types/deepmerge@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@types/deepmerge/-/deepmerge-2.2.0.tgz#6f63896c217f3164782f52d858d9f3a927139f64"
integrity sha512-FEQYDHh6+Q+QXKSrIY46m+/lAmAj/bk4KpLaam+hArmzaVpMBHLcfwOH2Q2UOkWM7XsdY9PmZpGyPAjh/JRGhQ==
dependencies:
deepmerge "*"

"@types/etag@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@types/etag/-/etag-1.8.0.tgz#37f0b1f3ea46da7ae319bbedb607e375b4c99f7e"
Expand Down Expand Up @@ -2394,6 +2387,18 @@
resolved "https://registry.yarnpkg.com/@types/less/-/less-3.0.1.tgz#625694093c72f8356c4042754e222407e50d6b08"
integrity sha512-dBp05MtWN/w1fGVjj5LVrDw6VrdYllpWczbUkCsrzBj08IHsSyRLOFvUrCFqZFVR+nsqkrRLNg6oOlvqMLPaSA==

"@types/lodash.merge@^4.6.6":
version "4.6.6"
resolved "https://registry.yarnpkg.com/@types/lodash.merge/-/lodash.merge-4.6.6.tgz#b84b403c1d31bc42d51772d1cd5557fa008cd3d6"
integrity sha512-IB90krzMf7YpfgP3u/EvZEdXVvm4e3gJbUvh5ieuI+o+XqiNEt6fCzqNRaiLlPVScLI59RxIGZMQ3+Ko/DJ8vQ==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==

"@types/memory-fs@*":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@types/memory-fs/-/memory-fs-0.3.2.tgz#5d4753f9b390cb077c8c8af97bc96463399ceccd"
Expand Down Expand Up @@ -5071,7 +5076,7 @@ deep-is@^0.1.3, deep-is@~0.1.3:
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=

deepmerge@*, deepmerge@^4.2.2:
deepmerge@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==
Expand Down Expand Up @@ -8324,6 +8329,11 @@ lodash.memoize@^4.1.2:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down

0 comments on commit 7f8b373

Please sign in to comment.