Skip to content

Commit c6d0684

Browse files
authored
Merge branch 'develop' into timfish/test/fix-test-runner
2 parents f228471 + ab28544 commit c6d0684

File tree

6 files changed

+477
-100
lines changed

6 files changed

+477
-100
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as util from 'node:util';
22
import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core';
3-
import type { IntegrationFn } from '@sentry/types';
4-
import { addConsoleInstrumentationHandler, severityLevelFromString } from '@sentry/utils';
3+
import { addConsoleInstrumentationHandler, severityLevelFromString, truncate } from '@sentry/utils';
54

65
const INTEGRATION_NAME = 'Console';
76

8-
const _consoleIntegration = (() => {
7+
/**
8+
* Capture console logs as breadcrumbs.
9+
*/
10+
export const consoleIntegration = defineIntegration(() => {
911
return {
1012
name: INTEGRATION_NAME,
1113
setup(client) {
@@ -18,7 +20,7 @@ const _consoleIntegration = (() => {
1820
{
1921
category: 'console',
2022
level: severityLevelFromString(level),
21-
message: util.format.apply(undefined, args),
23+
message: truncate(util.format.apply(undefined, args), 2048), // 2KB
2224
},
2325
{
2426
input: [...args],
@@ -28,9 +30,4 @@ const _consoleIntegration = (() => {
2830
});
2931
},
3032
};
31-
}) satisfies IntegrationFn;
32-
33-
/**
34-
* Capture console logs as breadcrumbs.
35-
*/
36-
export const consoleIntegration = defineIntegration(_consoleIntegration);
33+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as SentryCore from '@sentry/core';
2+
import { resetInstrumentationHandlers } from '@sentry/utils';
3+
import { getClient } from '../../src';
4+
import type { NodeClient } from '../../src';
5+
import { consoleIntegration } from '../../src/integrations/console';
6+
7+
const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb');
8+
9+
jest.spyOn(console, 'log').mockImplementation(() => {
10+
// noop so that we don't spam the logs
11+
});
12+
13+
afterEach(() => {
14+
jest.clearAllMocks();
15+
resetInstrumentationHandlers();
16+
});
17+
18+
describe('Console integration', () => {
19+
it('should add a breadcrumb on console.log', () => {
20+
consoleIntegration().setup?.(getClient() as NodeClient);
21+
22+
// eslint-disable-next-line no-console
23+
console.log('test');
24+
25+
expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
26+
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
27+
{
28+
category: 'console',
29+
level: 'log',
30+
message: 'test',
31+
},
32+
{
33+
input: ['test'],
34+
level: 'log',
35+
},
36+
);
37+
});
38+
39+
it('should truncate breadcrumbs with more than 2 KB message size', () => {
40+
consoleIntegration().setup?.(getClient() as NodeClient);
41+
42+
const longMsg = 'A'.repeat(10_000);
43+
44+
// eslint-disable-next-line no-console
45+
console.log(longMsg);
46+
47+
expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1);
48+
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
49+
{
50+
category: 'console',
51+
level: 'log',
52+
message: `${'A'.repeat(2048)}...`,
53+
},
54+
{
55+
input: [longMsg],
56+
level: 'log',
57+
},
58+
);
59+
});
60+
});

packages/nuxt/README.md

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
</a>
55
</p>
66

7-
# Official Sentry SDK for Nuxt (EXPERIMENTAL)
7+
# Official Sentry SDK for Nuxt (BETA)
88

99
[![npm version](https://img.shields.io/npm/v/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)
1010
[![npm dm](https://img.shields.io/npm/dm/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)
1111
[![npm dt](https://img.shields.io/npm/dt/@sentry/nuxt.svg)](https://www.npmjs.com/package/@sentry/nuxt)
1212

13-
**This SDK is under active development! Feel free to already try it but expect breaking changes**
13+
This SDK is in **Beta**. The API is stable but updates may include minor changes in behavior. Please reach out on
14+
[GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns. This
15+
SDK is for [Nuxt](https://nuxt.com/). If you're using [Vue](https://vuejs.org/) see our
16+
[Vue SDK here](https://github.com/getsentry/sentry-javascript/tree/develop/packages/vue).
1417

1518
## Links
1619

17-
todo: link official SDK docs
18-
19-
- [Official Browser SDK Docs](https://docs.sentry.io/platforms/javascript/)
20-
- [Official Node SDK Docs](https://docs.sentry.io/platforms/node/)
20+
- [Official Nuxt SDK Docs](https://docs.sentry.io/platforms/javascript/guides/nuxt/)
2121

2222
## Compatibility
2323

@@ -28,43 +28,12 @@ The minimum supported version of Nuxt is `3.0.0`.
2828
This package is a wrapper around `@sentry/node` for the server and `@sentry/vue` for the client side, with added
2929
functionality related to Nuxt.
3030

31-
**What is working:**
32-
33-
- Error Reporting
34-
- Vue
35-
- Node
36-
- Nitro
37-
38-
**What is partly working:**
39-
40-
- Source Maps
41-
- Connected Tracing (Frontend & Backend)
42-
- Tracing by setting `tracesSampleRate`
43-
- UI (Vue) traces
44-
- HTTP (Node) traces
45-
46-
**Known Issues:**
47-
48-
- When adding `sentry.server.config.(ts/js)`, you get an error like this:
49-
"`Failed to register ESM hook (import-in-the-middle/hook.mjs)`". You can add a resolution for `@vercel/nft` to fix
50-
this. This will add the `hook.mjs` file to your build output
51-
([issue here](https://github.com/unjs/nitro/issues/2703)).
52-
```json
53-
"resolutions": {
54-
"@vercel/nft": "^0.27.4"
55-
}
56-
```
57-
58-
## Automatic Setup
31+
**Limitations:**
5932

60-
todo: add wizard instructions
61-
62-
Take a look at the sections below if you want to customize your SDK configuration.
33+
- Server monitoring is not available during development mode (`nuxt dev`)
6334

6435
## Manual Setup
6536

66-
If the setup through the wizard doesn't work for you, you can also set up the SDK manually.
67-
6837
### 1. Prerequisites & Installation
6938

7039
1. Install the Sentry Nuxt SDK:
@@ -92,7 +61,7 @@ export default defineNuxtConfig({
9261

9362
### 3. Client-side setup
9463

95-
Add a `sentry.client.config.(js|ts)` file to the root of your project:
64+
Add a `sentry.client.config.ts` file to the root of your project:
9665

9766
```javascript
9867
import { useRuntimeConfig } from '#imports';
@@ -106,7 +75,7 @@ Sentry.init({
10675

10776
### 4. Server-side setup
10877

109-
Add an `sentry.client.config.(js|ts)` file to the root of your project:
78+
Add an `sentry.client.config.ts` file to the root of your project:
11079

11180
```javascript
11281
import * as Sentry from '@sentry/nuxt';
@@ -119,11 +88,12 @@ if (process.env.SENTRY_DSN) {
11988
}
12089
```
12190

122-
The Nuxt runtime config does not work in the Sentry server to technical reasons (it has to be loaded before Nuxt is
123-
loaded). To be able to use `process.env` you either have to add `--env-file=.env` to your node command
91+
Using `useRuntimeConfig` does not work in the Sentry server config file due to technical reasons (the file has to be
92+
loaded before Nuxt is loaded). To be able to use `process.env` you either have to add `--env-file=.env` to your node
93+
command
12494

12595
```bash
126-
node --env-file=.env --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs
96+
node --env-file=.env .output/server/index.mjs
12797
```
12898

12999
or use the `dotenv` package:
@@ -139,28 +109,18 @@ Sentry.init({
139109
});
140110
```
141111

142-
Add an import flag to the Node options of your `node` command (not `nuxt preview`), so the file loads before any other
143-
imports (keep in mind the `.mjs` file ending):
144-
145-
```json
146-
{
147-
"scripts": {
148-
"start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs"
149-
}
150-
}
151-
```
152-
153112
## Uploading Source Maps
154113

155-
To upload source maps, you can use the `sourceMapsUploadOptions` option inside the `sentry` options of your
156-
`nuxt.config.ts`:
114+
To upload source maps, you have to enable client source maps in your `nuxt.config.ts`. Then, you add your project
115+
settings to the `sentry.sourceMapsUploadOptions` of your `nuxt.config.ts`:
157116

158117
```javascript
159118
// nuxt.config.ts
160119
export default defineNuxtConfig({
120+
sourcemap: { client: true },
121+
161122
modules: ['@sentry/nuxt/module'],
162123
sentry: {
163-
debug: true,
164124
sourceMapsUploadOptions: {
165125
org: 'your-org-slug',
166126
project: 'your-project-slug',
@@ -169,3 +129,24 @@ export default defineNuxtConfig({
169129
},
170130
});
171131
```
132+
133+
## Troubleshooting
134+
135+
When adding `sentry.server.config.ts`, you might get an error like this:
136+
"`Failed to register ESM hook import-in-the-middle/hook.mjs`". You can add an override (npm/pnpm) or a resolution (yarn)
137+
for `@vercel/nft` to fix this. This will add the `hook.mjs` file to your build output
138+
([Nitro issue here](https://github.com/unjs/nitro/issues/2703)).
139+
140+
```json
141+
"overrides": {
142+
"@vercel/nft": "^0.27.4"
143+
}
144+
```
145+
146+
or in `yarn`:
147+
148+
```json
149+
"resolutions": {
150+
"@vercel/nft": "^0.27.4"
151+
}
152+
```

packages/nuxt/src/module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ export default defineNuxtModule<ModuleOptions>({
7474

7575
nuxt.hooks.hook('nitro:init', nitro => {
7676
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
77+
if (nitro.options.dev) {
78+
consoleSandbox(() => {
79+
// eslint-disable-next-line no-console
80+
console.log(
81+
'[Sentry] Your application is running in development mode. Note: @sentry/nuxt is in beta and may not work as expected on the server-side (Nitro). Errors are reported, but tracing does not work.',
82+
);
83+
});
84+
}
85+
7786
if (moduleOptions.dynamicImportForServerEntry === false) {
7887
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);
7988

0 commit comments

Comments
 (0)