Skip to content

Commit

Permalink
feat(nuxt): Always add tracing meta tags (#13273)
Browse files Browse the repository at this point in the history
Making sure tracing without performance works.

ref (SvelteKit):
#13231
  • Loading branch information
s1gr1d authored Aug 12, 2024
1 parent a55e4d5 commit b29d771
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 61 deletions.
17 changes: 4 additions & 13 deletions packages/nuxt/src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(`<meta name="sentry-trace" content="${traceParentData}"/>`);
head.push(`<meta name="baggage" content="${dynamicSamplingContext}"/>`);
if (metaTags) {
head.push(metaTags);
}
}
65 changes: 17 additions & 48 deletions packages/nuxt/test/runtime/plugins/server.test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
'<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>',
'<meta name="baggage" content="sentry-environment=production"/>',
].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(`<meta name="sentry-trace" content="${mockReturns.traceHeader}"/>`);
expect(head).toContain(`<meta name="baggage" content="${mockReturns.baggageHeader}"/>`);
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);
Expand Down

0 comments on commit b29d771

Please sign in to comment.