From f05848c1dae13e863458ce2463d5de25cffee33b Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 8 Aug 2024 09:50:27 +0200 Subject: [PATCH 1/2] feat(nuxt): Always add tracing meta tags --- packages/nuxt/src/runtime/utils.ts | 18 +++--------------- .../nuxt/test/runtime/plugins/server.test.ts | 6 +++--- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index b7c038ddd505..c715700ba88c 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,6 @@ 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; - - if (rootSpan) { - const traceParentData = spanToTraceHeader(rootSpan); - const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader( - getDynamicSamplingContextFromSpan(rootSpan), - ); - - head.push(``); - head.push(``); - } + const metaTags = getTraceMetaTags(); + 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..4abd0d32fd6f 100644 --- a/packages/nuxt/test/runtime/plugins/server.test.ts +++ b/packages/nuxt/test/runtime/plugins/server.test.ts @@ -42,7 +42,7 @@ describe('addSentryTracingMetaTags', () => { vi.resetAllMocks(); }); - it('should add meta tags when there is an active root span', () => { + it('should add meta tags', () => { const head: string[] = []; addSentryTracingMetaTags(head); @@ -50,7 +50,7 @@ describe('addSentryTracingMetaTags', () => { expect(head).toContain(``); }); - it('should not add meta tags when there is no active root span', () => { + it('should also add meta tags when there is no active root span', () => { vi.doMock('@sentry/core', async () => { const actual = await vi.importActual('@sentry/core'); @@ -63,6 +63,6 @@ describe('addSentryTracingMetaTags', () => { const head: string[] = []; addSentryTracingMetaTags(head); - expect(head).toHaveLength(0); + expect(head).toHaveLength(1); }); }); From 346a3ad15ec078e826c058fb22f1afb27e370dc8 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 12 Aug 2024 11:03:24 +0200 Subject: [PATCH 2/2] fix test --- packages/nuxt/src/runtime/utils.ts | 5 +- .../nuxt/test/runtime/plugins/server.test.ts | 67 +++++-------------- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/packages/nuxt/src/runtime/utils.ts b/packages/nuxt/src/runtime/utils.ts index c715700ba88c..585387f59003 100644 --- a/packages/nuxt/src/runtime/utils.ts +++ b/packages/nuxt/src/runtime/utils.ts @@ -36,5 +36,8 @@ export function extractErrorContext(errorContext: CapturedErrorContext): Context */ export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head']): void { const metaTags = getTraceMetaTags(); - head.push(metaTags); + + 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 4abd0d32fd6f..5750f0f9495f 100644 --- a/packages/nuxt/test/runtime/plugins/server.test.ts +++ b/packages/nuxt/test/runtime/plugins/server.test.ts @@ -1,68 +1,37 @@ -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', () => { + 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 also 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); - expect(head).toHaveLength(1); + expect(head).toHaveLength(0); }); });