Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Always add tracing meta tags #13273

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine, given the unit under test here is the Nuxt-specific addSentryTracingMetaTags function.

What we should do in the near future is to make sure our e2e tests cover distributed tracing behaviour. For example, we should check that

  • the sever-side request transaction and the client-side pageload transaction is connected by the same trace. This will validate that the trace meta tag injection generally works.
  • potential requests made on the client side to the server (thinking of API endpoint or data loading calls, whatever nuxt offers) are connected to the server side. This is probably easiest testable when making these calls during or directly after a navigation so that we can make sure a client-side root span exists.

I'm also working on a SvelteKit e2e test app that's specifically configured for tracing without performance. We could think about doing something similar for Nuxt but I wouldn't require it for the moment.

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
Loading