From b4a31db46b9b4ac9214fbeb1e0c519b21cb74a60 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 14 Apr 2025 13:37:16 +0200 Subject: [PATCH 1/5] feat(nuxt): Log when adding HTML trace meta tags --- packages/nuxt/src/runtime/plugins/sentry.server.ts | 3 ++- packages/nuxt/src/runtime/utils.ts | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/runtime/plugins/sentry.server.ts b/packages/nuxt/src/runtime/plugins/sentry.server.ts index f65ac64b9982..120daf05a2a4 100644 --- a/packages/nuxt/src/runtime/plugins/sentry.server.ts +++ b/packages/nuxt/src/runtime/plugins/sentry.server.ts @@ -57,7 +57,8 @@ export default defineNitroPlugin(nitroApp => { // @ts-expect-error - 'render:html' is a valid hook name in the Nuxt context nitroApp.hooks.hook('render:html', (html: NuxtRenderHTMLContext) => { - addSentryTracingMetaTags(html.head); + const sentryClient = SentryNode.getClient(); + addSentryTracingMetaTags(html.head, sentryClient?.getOptions().debug); }); }); diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index c6eb59807764..5f56417a2f45 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -1,4 +1,5 @@ import type { ClientOptions, Context } from '@sentry/core'; +import { consoleSandbox } from '@sentry/core'; import { captureException, getClient, getTraceMetaTags } from '@sentry/core'; import type { VueOptions } from '@sentry/vue/src/types'; import type { CapturedErrorContext } from 'nitropack'; @@ -33,10 +34,16 @@ export function extractErrorContext(errorContext: CapturedErrorContext | undefin * * Exported only for testing */ -export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head']): void { +export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head'], debug?: boolean): void { const metaTags = getTraceMetaTags(); if (metaTags) { + if (debug) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log('[Sentry] Adding Sentry tracing meta tags to HTML page:', metaTags); + }); + } head.push(metaTags); } } From fb245866b64338c7e5c7e177d4c741265b51d709 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 14 Apr 2025 14:21:20 +0200 Subject: [PATCH 2/5] use logger --- packages/nuxt/src/runtime/utils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index 5f56417a2f45..9ad20e4d49ae 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -1,5 +1,5 @@ import type { ClientOptions, Context } from '@sentry/core'; -import { consoleSandbox } from '@sentry/core'; +import { logger } from '@sentry/core'; import { captureException, getClient, getTraceMetaTags } from '@sentry/core'; import type { VueOptions } from '@sentry/vue/src/types'; import type { CapturedErrorContext } from 'nitropack'; @@ -39,10 +39,7 @@ export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head'], de if (metaTags) { if (debug) { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log('[Sentry] Adding Sentry tracing meta tags to HTML page:', metaTags); - }); + logger.log('[Sentry] Adding Sentry tracing meta tags to HTML page:', metaTags); } head.push(metaTags); } From c523b219aeb1933737275396e28b24af231d2f10 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 14 Apr 2025 14:37:39 +0200 Subject: [PATCH 3/5] adapt logger message --- packages/nuxt/src/runtime/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index 9ad20e4d49ae..4f7577a2db86 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -39,7 +39,7 @@ export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head'], de if (metaTags) { if (debug) { - logger.log('[Sentry] Adding Sentry tracing meta tags to HTML page:', metaTags); + logger.log('Adding Sentry tracing meta tags to HTML page:', metaTags); } head.push(metaTags); } From 5136d1d4d58ffd86ca0a4a9dedb5adad88c06bc8 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 14 Apr 2025 16:57:53 +0200 Subject: [PATCH 4/5] remove debug check --- packages/nuxt/src/runtime/plugins/sentry.server.ts | 3 +-- packages/nuxt/src/runtime/utils.ts | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/src/runtime/plugins/sentry.server.ts b/packages/nuxt/src/runtime/plugins/sentry.server.ts index 120daf05a2a4..f65ac64b9982 100644 --- a/packages/nuxt/src/runtime/plugins/sentry.server.ts +++ b/packages/nuxt/src/runtime/plugins/sentry.server.ts @@ -57,8 +57,7 @@ export default defineNitroPlugin(nitroApp => { // @ts-expect-error - 'render:html' is a valid hook name in the Nuxt context nitroApp.hooks.hook('render:html', (html: NuxtRenderHTMLContext) => { - const sentryClient = SentryNode.getClient(); - addSentryTracingMetaTags(html.head, sentryClient?.getOptions().debug); + addSentryTracingMetaTags(html.head); }); }); diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index 4f7577a2db86..fb247504f78a 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -34,13 +34,11 @@ export function extractErrorContext(errorContext: CapturedErrorContext | undefin * * Exported only for testing */ -export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head'], debug?: boolean): void { +export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head']): void { const metaTags = getTraceMetaTags(); if (metaTags) { - if (debug) { - logger.log('Adding Sentry tracing meta tags to HTML page:', metaTags); - } + logger.log('Adding Sentry tracing meta tags to HTML page:', metaTags); head.push(metaTags); } } From e0aa805a763f3e531fbf1a6b3aad78b8f5b39dac Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Tue, 15 Apr 2025 13:28:41 +0200 Subject: [PATCH 5/5] import original from core package --- packages/nuxt/test/runtime/plugins/server.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/test/runtime/plugins/server.test.ts b/packages/nuxt/test/runtime/plugins/server.test.ts index 5750f0f9495f..2190e4ed5ef3 100644 --- a/packages/nuxt/test/runtime/plugins/server.test.ts +++ b/packages/nuxt/test/runtime/plugins/server.test.ts @@ -2,9 +2,13 @@ import { getTraceMetaTags } from '@sentry/core'; import { type Mock, afterEach, describe, expect, it, vi } from 'vitest'; import { addSentryTracingMetaTags } from '../../../src/runtime/utils'; -vi.mock('@sentry/core', () => ({ - getTraceMetaTags: vi.fn(), -})); +vi.mock(import('@sentry/core'), async importOriginal => { + const mod = await importOriginal(); + return { + ...mod, + getTraceMetaTags: vi.fn(), + }; +}); describe('addSentryTracingMetaTags', () => { afterEach(() => {