Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jul 4, 2024
1 parent 0324a00 commit 8c2fe86
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async function Page() {
}

export async function generateMetadata() {
(await fetch('http://example.com/')).text();
(await fetch('http://example.com/', { cache: 'no-store' })).text();

return {
title: 'my title',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('all server component transactions should be attached to the pageload request span', async ({ page }) => {
test('App router transactions should be attached to the pageload request span', async ({ page }) => {
const serverTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === 'GET /pageload-tracing';
});
Expand Down
15 changes: 13 additions & 2 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,18 @@ export function init(options: NodeOptions): NodeClient | undefined {
applySdkMetadata(opts, 'nextjs', ['nextjs', 'node']);

const client = nodeInit(opts);

// If we encounter a span emitted by Next.js, we do not want to sample it
// The reason for this is that the data quality of the spans varies, it is different per version of Next,
// and we need to keep our manual instrumentation around for the edge runtime anyhow.
// BUT we only do this if we don't have a parent span with a sampling decision yet (or if the parent is remote)
client?.on('beforeSampling', ({ spanAttributes, spanName, parentSampled, parentContext }, samplingDecision) => {
// We "whitelist" the "BaseServer.handleRequest" span, since that one is responsible for App Router requests, which are actually useful for us.
// HOWEVER, that span is not only responsible for App Router requests, which is why we additionally filter for certain transactions in an
// event processor further below.
if (spanAttributes['next.span_type'] === 'BaseServer.handleRequest') {
return;
}

if (
(spanAttributes['next.span_type'] || NEXTJS_SPAN_NAME_PREFIXES.some(prefix => spanName.startsWith(prefix))) &&
(parentSampled === undefined || parentContext?.isRemote)
Expand All @@ -144,7 +154,8 @@ export function init(options: NodeOptions): NodeClient | undefined {

// We only want to use our HTTP integration/instrumentation for app router requests, which are marked with the `sentry.rsc` attribute.
if (
event.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.http.otel.http' &&
(event.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.http.otel.http' ||
event.contexts?.trace?.data?.['next.span_type'] === 'BaseServer.handleRequest') &&
event.contexts?.trace?.data?.['sentry.rsc'] !== true
) {
return null;
Expand Down

0 comments on commit 8c2fe86

Please sign in to comment.