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 3 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
18 changes: 3 additions & 15 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,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(`<meta name="sentry-trace" content="${traceParentData}"/>`);
head.push(`<meta name="baggage" content="${dynamicSamplingContext}"/>`);
}
const metaTags = getTraceMetaTags();
head.push(metaTags);
}
Copy link
Member

Choose a reason for hiding this comment

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

l: Not sure if this is problematic but I see we did this differently before: If nuxt expects one tag per array item we should split the return value from getTraceMetaTags() at \n since we usually return two tags separated by a \n.

Copy link
Member Author

Choose a reason for hiding this comment

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

I checked this and nuxt puts the html together in the correct way so this should be fine. But I would rather add a function parameter { options.asArray } to return it as array instead of adding \n and splitting it again afterwards.

Copy link
Member Author

Choose a reason for hiding this comment

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

I created a PR for this: #13293

Copy link
Member Author

Choose a reason for hiding this comment

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

As I closed the PR I just use the returned string now. This works with Nuxt as well and we don't have to split the string (slightly better performance).

6 changes: 3 additions & 3 deletions packages/nuxt/test/runtime/plugins/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
vi.resetAllMocks();
});

it('should add meta tags when there is an active root span', () => {
it('should add meta tags', () => {
const head: string[] = [];
addSentryTracingMetaTags(head);

expect(head).toContain(`<meta name="sentry-trace" content="${mockReturns.traceHeader}"/>`);

Check failure on line 49 in packages/nuxt/test/runtime/plugins/server.test.ts

View workflow job for this annotation

GitHub Actions / Node (16) Unit Tests

test/runtime/plugins/server.test.ts > addSentryTracingMetaTags > should add meta tags

AssertionError: expected [ Array(1) ] to include '<meta name="sentry-trace" content="tr…' ❯ test/runtime/plugins/server.test.ts:49:18

Check failure on line 49 in packages/nuxt/test/runtime/plugins/server.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/runtime/plugins/server.test.ts > addSentryTracingMetaTags > should add meta tags

AssertionError: expected [ Array(1) ] to include '<meta name="sentry-trace" content="tr…' ❯ test/runtime/plugins/server.test.ts:49:18

Check failure on line 49 in packages/nuxt/test/runtime/plugins/server.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/runtime/plugins/server.test.ts > addSentryTracingMetaTags > should add meta tags

AssertionError: expected [ Array(1) ] to include '<meta name="sentry-trace" content="tr…' ❯ test/runtime/plugins/server.test.ts:49:18

Check failure on line 49 in packages/nuxt/test/runtime/plugins/server.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/runtime/plugins/server.test.ts > addSentryTracingMetaTags > should add meta tags

AssertionError: expected [ Array(1) ] to include '<meta name="sentry-trace" content="tr…' ❯ test/runtime/plugins/server.test.ts:49:18
expect(head).toContain(`<meta name="baggage" content="${mockReturns.baggageHeader}"/>`);
});

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');

Expand All @@ -63,6 +63,6 @@
const head: string[] = [];
addSentryTracingMetaTags(head);

expect(head).toHaveLength(0);
expect(head).toHaveLength(1);
});
});
Loading