diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index b7c038ddd505..585387f59003 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -1,8 +1,6 @@ -import { getActiveSpan, getRootSpan, spanToTraceHeader } from '@sentry/core'; -import { getDynamicSamplingContextFromSpan } from '@sentry/opentelemetry'; +import { getTraceMetaTags } from '@sentry/core'; import type { Context } from '@sentry/types'; import { dropUndefinedKeys } from '@sentry/utils'; -import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils'; import type { CapturedErrorContext } from 'nitropack'; import type { NuxtRenderHTMLContext } from 'nuxt/app'; @@ -37,16 +35,9 @@ export function extractErrorContext(errorContext: CapturedErrorContext): Context * Exported only for testing */ export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head']): void { - const activeSpan = getActiveSpan(); - const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined; + const metaTags = getTraceMetaTags(); - if (rootSpan) { - const traceParentData = spanToTraceHeader(rootSpan); - const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader( - getDynamicSamplingContextFromSpan(rootSpan), - ); - - head.push(``); - head.push(``); + if (metaTags) { + head.push(metaTags); } } diff --git a/packages/nuxt/test/runtime/plugins/server.test.ts b/packages/nuxt/test/runtime/plugins/server.test.ts index 518b20026cbd..5750f0f9495f 100644 --- a/packages/nuxt/test/runtime/plugins/server.test.ts +++ b/packages/nuxt/test/runtime/plugins/server.test.ts @@ -1,64 +1,33 @@ -import { afterEach, describe, expect, it, vi } from 'vitest'; +import { getTraceMetaTags } from '@sentry/core'; +import { type Mock, afterEach, describe, expect, it, vi } from 'vitest'; import { addSentryTracingMetaTags } from '../../../src/runtime/utils'; -const mockReturns = vi.hoisted(() => { - return { - traceHeader: 'trace-header', - baggageHeader: 'baggage-header', - }; -}); - -vi.mock('@sentry/core', async () => { - const actual = await vi.importActual('@sentry/core'); - - return { - ...actual, - getActiveSpan: vi.fn().mockReturnValue({ spanId: '123' }), - getRootSpan: vi.fn().mockReturnValue({ spanId: 'root123' }), - spanToTraceHeader: vi.fn(() => mockReturns.traceHeader), - }; -}); - -vi.mock('@sentry/opentelemetry', async () => { - const actual = await vi.importActual('@sentry/opentelemetry'); - - return { - ...actual, - getDynamicSamplingContextFromSpan: vi.fn().mockReturnValue('contextValue'), - }; -}); - -vi.mock('@sentry/utils', async () => { - const actual = await vi.importActual('@sentry/utils'); - - return { - ...actual, - dynamicSamplingContextToSentryBaggageHeader: vi.fn().mockReturnValue(mockReturns.baggageHeader), - }; -}); +vi.mock('@sentry/core', () => ({ + getTraceMetaTags: vi.fn(), +})); describe('addSentryTracingMetaTags', () => { afterEach(() => { vi.resetAllMocks(); }); - it('should add meta tags when there is an active root span', () => { + it('should add meta tags to the head array', () => { + const mockMetaTags = [ + '', + '', + ].join('\n'); + + // return value is mocked here as return values of `getTraceMetaTags` are tested separately (in @sentry/core) + (getTraceMetaTags as Mock).mockReturnValue(mockMetaTags); + const head: string[] = []; addSentryTracingMetaTags(head); - expect(head).toContain(``); - expect(head).toContain(``); + expect(head).toContain(mockMetaTags); }); - it('should not add meta tags when there is no active root span', () => { - vi.doMock('@sentry/core', async () => { - const actual = await vi.importActual('@sentry/core'); - - return { - ...actual, - getActiveSpan: vi.fn().mockReturnValue(undefined), - }; - }); + it('should handle empty meta tags', () => { + (getTraceMetaTags as Mock).mockReturnValue(''); const head: string[] = []; addSentryTracingMetaTags(head);