Skip to content

Commit b29d771

Browse files
authored
feat(nuxt): Always add tracing meta tags (#13273)
Making sure tracing without performance works. ref (SvelteKit): #13231
1 parent a55e4d5 commit b29d771

File tree

2 files changed

+21
-61
lines changed

2 files changed

+21
-61
lines changed

packages/nuxt/src/runtime/utils.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { getActiveSpan, getRootSpan, spanToTraceHeader } from '@sentry/core';
2-
import { getDynamicSamplingContextFromSpan } from '@sentry/opentelemetry';
1+
import { getTraceMetaTags } from '@sentry/core';
32
import type { Context } from '@sentry/types';
43
import { dropUndefinedKeys } from '@sentry/utils';
5-
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
64
import type { CapturedErrorContext } from 'nitropack';
75
import type { NuxtRenderHTMLContext } from 'nuxt/app';
86

@@ -37,16 +35,9 @@ export function extractErrorContext(errorContext: CapturedErrorContext): Context
3735
* Exported only for testing
3836
*/
3937
export function addSentryTracingMetaTags(head: NuxtRenderHTMLContext['head']): void {
40-
const activeSpan = getActiveSpan();
41-
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
38+
const metaTags = getTraceMetaTags();
4239

43-
if (rootSpan) {
44-
const traceParentData = spanToTraceHeader(rootSpan);
45-
const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader(
46-
getDynamicSamplingContextFromSpan(rootSpan),
47-
);
48-
49-
head.push(`<meta name="sentry-trace" content="${traceParentData}"/>`);
50-
head.push(`<meta name="baggage" content="${dynamicSamplingContext}"/>`);
40+
if (metaTags) {
41+
head.push(metaTags);
5142
}
5243
}

packages/nuxt/test/runtime/plugins/server.test.ts

+17-48
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,33 @@
1-
import { afterEach, describe, expect, it, vi } from 'vitest';
1+
import { getTraceMetaTags } from '@sentry/core';
2+
import { type Mock, afterEach, describe, expect, it, vi } from 'vitest';
23
import { addSentryTracingMetaTags } from '../../../src/runtime/utils';
34

4-
const mockReturns = vi.hoisted(() => {
5-
return {
6-
traceHeader: 'trace-header',
7-
baggageHeader: 'baggage-header',
8-
};
9-
});
10-
11-
vi.mock('@sentry/core', async () => {
12-
const actual = await vi.importActual('@sentry/core');
13-
14-
return {
15-
...actual,
16-
getActiveSpan: vi.fn().mockReturnValue({ spanId: '123' }),
17-
getRootSpan: vi.fn().mockReturnValue({ spanId: 'root123' }),
18-
spanToTraceHeader: vi.fn(() => mockReturns.traceHeader),
19-
};
20-
});
21-
22-
vi.mock('@sentry/opentelemetry', async () => {
23-
const actual = await vi.importActual('@sentry/opentelemetry');
24-
25-
return {
26-
...actual,
27-
getDynamicSamplingContextFromSpan: vi.fn().mockReturnValue('contextValue'),
28-
};
29-
});
30-
31-
vi.mock('@sentry/utils', async () => {
32-
const actual = await vi.importActual('@sentry/utils');
33-
34-
return {
35-
...actual,
36-
dynamicSamplingContextToSentryBaggageHeader: vi.fn().mockReturnValue(mockReturns.baggageHeader),
37-
};
38-
});
5+
vi.mock('@sentry/core', () => ({
6+
getTraceMetaTags: vi.fn(),
7+
}));
398

409
describe('addSentryTracingMetaTags', () => {
4110
afterEach(() => {
4211
vi.resetAllMocks();
4312
});
4413

45-
it('should add meta tags when there is an active root span', () => {
14+
it('should add meta tags to the head array', () => {
15+
const mockMetaTags = [
16+
'<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>',
17+
'<meta name="baggage" content="sentry-environment=production"/>',
18+
].join('\n');
19+
20+
// return value is mocked here as return values of `getTraceMetaTags` are tested separately (in @sentry/core)
21+
(getTraceMetaTags as Mock).mockReturnValue(mockMetaTags);
22+
4623
const head: string[] = [];
4724
addSentryTracingMetaTags(head);
4825

49-
expect(head).toContain(`<meta name="sentry-trace" content="${mockReturns.traceHeader}"/>`);
50-
expect(head).toContain(`<meta name="baggage" content="${mockReturns.baggageHeader}"/>`);
26+
expect(head).toContain(mockMetaTags);
5127
});
5228

53-
it('should not add meta tags when there is no active root span', () => {
54-
vi.doMock('@sentry/core', async () => {
55-
const actual = await vi.importActual('@sentry/core');
56-
57-
return {
58-
...actual,
59-
getActiveSpan: vi.fn().mockReturnValue(undefined),
60-
};
61-
});
29+
it('should handle empty meta tags', () => {
30+
(getTraceMetaTags as Mock).mockReturnValue('');
6231

6332
const head: string[] = [];
6433
addSentryTracingMetaTags(head);

0 commit comments

Comments
 (0)