Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
import { useFetch, useLazyAsyncData } from '#imports';

// On the server, each handler runs while the page renders, and the API route runs in the same process.
const { data: keyed } = await useFetch('/api/data-fetching', { key: 'visits' });
const { data: keyless } = await useFetch('/api/data-fetching');
const { data: orderCount } = useLazyAsyncData('order-count', () => Promise.resolve(3));
</script>

<template>
<p id="keyed">{{ keyed?.success }}</p>
<p id="keyless">{{ keyless?.success }}</p>
<p id="order-count">{{ orderCount }}</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineEventHandler, useDatabase, useStorage } from '#imports';

// The page `/data-fetching` fetches this route while it renders on the server.
export default defineEventHandler(async () => {
await useStorage('test-storage').setItem('data-fetching:visits', 1);
await useDatabase().exec('CREATE TABLE IF NOT EXISTS visits (id INTEGER PRIMARY KEY, page TEXT)');

return { success: true };
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SENTRY_DESCRIPTION, URL_PATH } from '@sentry/conventions/attributes';
import { FUNCTION } from '@sentry/conventions/op';
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('sends a span for each data composable as a child of the render span', async ({ request }) => {
const transactionPromise = waitForTransaction('nuxt-4', transactionEvent => {
return transactionEvent.contexts?.trace?.data?.[URL_PATH] === '/data-fetching';
});

await request.get('/data-fetching');

const transaction = await transactionPromise;
const renderSpan = transaction.spans?.find(span => span.origin === 'auto.function.nuxt.render');
const dataSpans = (transaction.spans ?? []).filter(span => span.origin === 'auto.function.nuxt.data');

expect(dataSpans.map(span => span.description).sort()).toEqual(['useAsyncData', 'useAsyncData', 'useLazyAsyncData']);
expect(dataSpans.map(span => span.op)).toEqual([FUNCTION, FUNCTION, FUNCTION]);
// Relay replaces the description of a transaction span with the span name, so the SDK sends no key.
expect(dataSpans.map(span => span.data[SENTRY_DESCRIPTION])).toEqual([undefined, undefined, undefined]);
expect(dataSpans.map(span => span.parent_span_id)).toEqual(Array(3).fill(renderSpan?.span_id));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import type { APIRequestContext } from '@playwright/test';
import { CODE_FUNCTION_NAME, SENTRY_DESCRIPTION, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes';
import { CACHE_PUT, DB_QUERY, FUNCTION, MIDDLEWARE } from '@sentry/conventions/op';
import { expect, test } from '@playwright/test';
import { collectStreamedSpans, getSpanOp } from '@sentry-internal/test-utils';
import type { SerializedStreamedSpan } from '@sentry-internal/test-utils';
import { isViteServer, NITRO_ONLY } from './isViteServer';
import { DB_ORIGIN } from './runtime';

const DATA_ORIGIN = 'auto.function.nuxt.data';
const STORAGE_ORIGIN = 'auto.cache.nuxt';

const hasOrigin = (origin: string) => (span: SerializedStreamedSpan) =>
span.attributes[SENTRY_ORIGIN]?.value === origin;

/**
* Requests the page and returns the spans of that request. On Cloudflare, the spans of one request can arrive in more
* than one envelope and in any order, so each test names the spans that it waits for.
*/
async function requestPage(
request: APIRequestContext,
hasSpansUnderTest: (spans: SerializedStreamedSpan[]) => boolean,
): Promise<SerializedStreamedSpan[]> {
const spansPromise = collectStreamedSpans(
'nuxt-4',
spans =>
spans.some(span => span.is_segment && span.attributes[URL_PATH]?.value === '/data-fetching') &&
hasSpansUnderTest(spans),
);

await request.get('/data-fetching');

return spansPromise;
}

/** On the Vite server, the fetch of an API route renders a page inside the data span, so the name alone is not enough. */
function getPageRenderSpan(spans: SerializedStreamedSpan[]): SerializedStreamedSpan | undefined {
const rootSpan = spans.find(span => span.is_segment);

return spans.find(span => span.name === 'renderPage' && span.parent_span_id === rootSpan?.span_id);
}

/** The spans of the two `useFetch` calls of the page. */
function getFetchSpans(spans: SerializedStreamedSpan[]): SerializedStreamedSpan[] {
return spans.filter(span => hasOrigin(DATA_ORIGIN)(span) && span.name === 'useAsyncData');
}

const hasBothFetchSpans = (spans: SerializedStreamedSpan[]) => getFetchSpans(spans).length >= 2;
const getSpanIds = (spans: SerializedStreamedSpan[]) => spans.map(span => span.span_id).sort();
const getParentSpanIds = (spans: SerializedStreamedSpan[]) => spans.map(span => span.parent_span_id).sort();

test('sends a span for `useLazyAsyncData` as a child of the render span', async ({ request }) => {
const spans = await requestPage(
request,
spans => !!getPageRenderSpan(spans) && spans.some(span => span.name === 'useLazyAsyncData'),
);

const lazySpans = spans.filter(span => span.name === 'useLazyAsyncData');

expect(lazySpans).toHaveLength(1);
expect(lazySpans[0]?.status).toBe('ok');
expect(lazySpans[0]?.parent_span_id).toBe(getPageRenderSpan(spans)?.span_id);
expect(getSpanOp(lazySpans[0]!)).toBe(FUNCTION);
expect(lazySpans[0]?.attributes[SENTRY_ORIGIN]).toEqual({ type: 'string', value: DATA_ORIGIN });
expect(lazySpans[0]?.attributes[CODE_FUNCTION_NAME]).toEqual({ type: 'string', value: 'useLazyAsyncData' });
expect(lazySpans[0]?.attributes[SENTRY_DESCRIPTION]).toEqual({ type: 'string', value: 'order-count' });
});

// A production build of Nuxt sends the name of the composable only for `useLazyAsyncData`.
test('names the span of a `useFetch` call `useAsyncData`', async ({ request }) => {
const spans = await requestPage(request, spans => spans.filter(hasOrigin(DATA_ORIGIN)).length >= 3);

const dataSpans = spans.filter(hasOrigin(DATA_ORIGIN));

expect(dataSpans.map(span => span.name).sort()).toEqual(['useAsyncData', 'useAsyncData', 'useLazyAsyncData']);
expect(dataSpans.map(span => span.attributes[CODE_FUNCTION_NAME]?.value).sort()).toEqual([
'useAsyncData',
'useAsyncData',
'useLazyAsyncData',
]);
});

test('adds the key as the description only for a `useFetch` call with a key', async ({ request }) => {
const spans = await requestPage(request, hasBothFetchSpans);

const descriptions = getFetchSpans(spans).map(span => span.attributes[SENTRY_DESCRIPTION]?.value);

expect(descriptions).toHaveLength(2);
expect(descriptions).toContain('visits');
expect(descriptions).toContain(undefined);
});

test('makes each `useFetch` span a child of the render span', async ({ request }) => {
const spans = await requestPage(request, spans => !!getPageRenderSpan(spans) && hasBothFetchSpans(spans));

const renderSpanId = getPageRenderSpan(spans)?.span_id;

expect(getParentSpanIds(getFetchSpans(spans))).toEqual([renderSpanId, renderSpanId]);
});

test.describe('spans of the API route that a `useFetch` call fetches on the server', () => {
test.skip(isViteServer, NITRO_ONLY);

test('makes the storage span a child of the `useFetch` span', async ({ request }) => {
const spans = await requestPage(
request,
spans => hasBothFetchSpans(spans) && spans.filter(hasOrigin(STORAGE_ORIGIN)).length >= 2,
);

const storageSpans = spans.filter(hasOrigin(STORAGE_ORIGIN));

expect(getParentSpanIds(storageSpans)).toEqual(getSpanIds(getFetchSpans(spans)));
expect(storageSpans.map(span => span.name)).toEqual(['cache.put', 'cache.put']);
expect(storageSpans.map(getSpanOp)).toEqual([CACHE_PUT, CACHE_PUT]);
});

test('makes the database span a child of the `useFetch` span', async ({ request }) => {
const spans = await requestPage(
request,
spans => hasBothFetchSpans(spans) && spans.filter(hasOrigin(DB_ORIGIN)).length >= 2,
);

const dbSpans = spans.filter(hasOrigin(DB_ORIGIN));

expect(getParentSpanIds(dbSpans)).toEqual(getSpanIds(getFetchSpans(spans)));
expect(dbSpans.map(span => span.name)).toEqual(['CREATE TABLE visits', 'CREATE TABLE visits']);
expect(dbSpans.map(getSpanOp)).toEqual([DB_QUERY, DB_QUERY]);
});

// The server middleware runs for the page request and for each fetch of the API route.
test('makes the server middleware spans children of the `useFetch` span', async ({ request }) => {
const getMiddlewareNames = (spans: SerializedStreamedSpan[], parentSpanId: string | undefined) =>
spans
.filter(span => getSpanOp(span) === MIDDLEWARE && span.parent_span_id === parentSpanId)
.map(span => `${span.name} (${span.attributes[SENTRY_ORIGIN]?.value})`)
.sort();
const getMiddlewareNamesOfPage = (spans: SerializedStreamedSpan[]) =>
getMiddlewareNames(spans, spans.find(span => span.is_segment)?.span_id);
const getMiddlewareNamesOfFetches = (spans: SerializedStreamedSpan[]) =>
getFetchSpans(spans).map(fetchSpan => getMiddlewareNames(spans, fetchSpan.span_id));
const spans = await requestPage(
request,
spans =>
hasBothFetchSpans(spans) &&
getMiddlewareNamesOfFetches(spans).every(names => names.length >= getMiddlewareNamesOfPage(spans).length),
);

const namesOfPage = getMiddlewareNamesOfPage(spans);

expect(namesOfPage.length).toBeGreaterThan(0);
expect(namesOfPage.every(name => name.endsWith('(auto.middleware.nuxt)'))).toBe(true);
expect(getMiddlewareNamesOfFetches(spans)).toEqual([namesOfPage, namesOfPage]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
import { useFetch, useLazyAsyncData } from '#imports';

// On the server, each handler runs while the page renders, and the API route runs in the same process.
const { data: keyed } = await useFetch('/api/data-fetching', { key: 'visits' });
const { data: keyless } = await useFetch('/api/data-fetching');
const { data: orderCount } = useLazyAsyncData('order-count', () => Promise.resolve(3));
</script>

<template>
<p id="keyed">{{ keyed?.success }}</p>
<p id="keyless">{{ keyless?.success }}</p>
<p id="order-count">{{ orderCount }}</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineHandler } from 'nitro';
import { useDatabase } from 'nitro/database';
import { useStorage } from 'nitro/storage';

// The page `/data-fetching` fetches this route while it renders on the server.
export default defineHandler(async () => {
await useStorage('test-storage').setItem('data-fetching:visits', 1);
await useDatabase().exec('CREATE TABLE IF NOT EXISTS visits (id INTEGER PRIMARY KEY, page TEXT)');

return { success: true };
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import type { APIRequestContext } from '@playwright/test';
import { CODE_FUNCTION_NAME, SENTRY_DESCRIPTION, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes';
import { CACHE_PUT, DB_QUERY, FUNCTION, MIDDLEWARE } from '@sentry/conventions/op';
import { expect, test } from '@playwright/test';
import { collectStreamedSpans, getSpanOp } from '@sentry-internal/test-utils';
import type { SerializedStreamedSpan } from '@sentry-internal/test-utils';
import { DB_ORIGIN } from './runtime';

const DATA_ORIGIN = 'auto.function.nuxt.data';
const STORAGE_ORIGIN = 'auto.cache.nuxt';

const hasOrigin = (origin: string) => (span: SerializedStreamedSpan) =>
span.attributes[SENTRY_ORIGIN]?.value === origin;

/**
* Requests the page and returns the spans of that request. On Cloudflare, the spans of one request can arrive in more
* than one envelope and in any order, so each test names the spans that it waits for.
*/
async function requestPage(
request: APIRequestContext,
hasSpansUnderTest: (spans: SerializedStreamedSpan[]) => boolean,
): Promise<SerializedStreamedSpan[]> {
const spansPromise = collectStreamedSpans(
'nuxt-5',
spans =>
spans.some(span => span.is_segment && span.attributes[URL_PATH]?.value === '/data-fetching') &&
hasSpansUnderTest(spans),
);

await request.get('/data-fetching');

return spansPromise;
}

function getPageRenderSpan(spans: SerializedStreamedSpan[]): SerializedStreamedSpan | undefined {
const rootSpan = spans.find(span => span.is_segment);

return spans.find(span => span.name === 'renderPage' && span.parent_span_id === rootSpan?.span_id);
}

/** The spans of the two `useFetch` calls of the page. */
function getFetchSpans(spans: SerializedStreamedSpan[]): SerializedStreamedSpan[] {
return spans.filter(span => hasOrigin(DATA_ORIGIN)(span) && span.name === 'useAsyncData');
}

const hasBothFetchSpans = (spans: SerializedStreamedSpan[]) => getFetchSpans(spans).length >= 2;
const getSpanIds = (spans: SerializedStreamedSpan[]) => spans.map(span => span.span_id).sort();
const getParentSpanIds = (spans: SerializedStreamedSpan[]) => spans.map(span => span.parent_span_id).sort();

test('sends a span for `useLazyAsyncData` as a child of the render span', async ({ request }) => {
const spans = await requestPage(
request,
spans => !!getPageRenderSpan(spans) && spans.some(span => span.name === 'useLazyAsyncData'),
);

const lazySpans = spans.filter(span => span.name === 'useLazyAsyncData');

expect(lazySpans).toHaveLength(1);
expect(lazySpans[0]?.status).toBe('ok');
expect(lazySpans[0]?.parent_span_id).toBe(getPageRenderSpan(spans)?.span_id);
expect(getSpanOp(lazySpans[0]!)).toBe(FUNCTION);
expect(lazySpans[0]?.attributes[SENTRY_ORIGIN]).toEqual({ type: 'string', value: DATA_ORIGIN });
expect(lazySpans[0]?.attributes[CODE_FUNCTION_NAME]).toEqual({ type: 'string', value: 'useLazyAsyncData' });
expect(lazySpans[0]?.attributes[SENTRY_DESCRIPTION]).toEqual({ type: 'string', value: 'order-count' });
});

// A production build of Nuxt sends the name of the composable only for `useLazyAsyncData`.
test('names the span of a `useFetch` call `useAsyncData`', async ({ request }) => {
const spans = await requestPage(request, spans => spans.filter(hasOrigin(DATA_ORIGIN)).length >= 3);

const dataSpans = spans.filter(hasOrigin(DATA_ORIGIN));

expect(dataSpans.map(span => span.name).sort()).toEqual(['useAsyncData', 'useAsyncData', 'useLazyAsyncData']);
expect(dataSpans.map(span => span.attributes[CODE_FUNCTION_NAME]?.value).sort()).toEqual([
'useAsyncData',
'useAsyncData',
'useLazyAsyncData',
]);
});

test('adds the key as the description only for a `useFetch` call with a key', async ({ request }) => {
const spans = await requestPage(request, hasBothFetchSpans);

const descriptions = getFetchSpans(spans).map(span => span.attributes[SENTRY_DESCRIPTION]?.value);

expect(descriptions).toHaveLength(2);
expect(descriptions).toContain('visits');
expect(descriptions).toContain(undefined);
});

test('makes each `useFetch` span a child of the render span', async ({ request }) => {
const spans = await requestPage(request, spans => !!getPageRenderSpan(spans) && hasBothFetchSpans(spans));

const renderSpanId = getPageRenderSpan(spans)?.span_id;

expect(getParentSpanIds(getFetchSpans(spans))).toEqual([renderSpanId, renderSpanId]);
});

test.describe('spans of the API route that a `useFetch` call fetches on the server', () => {
test('makes the storage span a child of the `useFetch` span', async ({ request }) => {
const spans = await requestPage(
request,
spans => hasBothFetchSpans(spans) && spans.filter(hasOrigin(STORAGE_ORIGIN)).length >= 2,
);

const storageSpans = spans.filter(hasOrigin(STORAGE_ORIGIN));

expect(getParentSpanIds(storageSpans)).toEqual(getSpanIds(getFetchSpans(spans)));
expect(storageSpans.map(span => span.name)).toEqual(['cache.put', 'cache.put']);
expect(storageSpans.map(getSpanOp)).toEqual([CACHE_PUT, CACHE_PUT]);
});

test('makes the database span a child of the `useFetch` span', async ({ request }) => {
const spans = await requestPage(
request,
spans => hasBothFetchSpans(spans) && spans.filter(hasOrigin(DB_ORIGIN)).length >= 2,
);

const dbSpans = spans.filter(hasOrigin(DB_ORIGIN));

expect(getParentSpanIds(dbSpans)).toEqual(getSpanIds(getFetchSpans(spans)));
expect(dbSpans.map(span => span.name)).toEqual(['CREATE TABLE visits', 'CREATE TABLE visits']);
expect(dbSpans.map(getSpanOp)).toEqual([DB_QUERY, DB_QUERY]);
});

// The server middleware runs for the page request and for each fetch of the API route.
test('makes the server middleware spans children of the `useFetch` span', async ({ request }) => {
const getMiddlewareNames = (spans: SerializedStreamedSpan[], parentSpanId: string | undefined) =>
spans
.filter(span => getSpanOp(span) === MIDDLEWARE && span.parent_span_id === parentSpanId)
.map(span => `${span.name} (${span.attributes[SENTRY_ORIGIN]?.value})`)
.sort();
const getMiddlewareNamesOfPage = (spans: SerializedStreamedSpan[]) =>
getMiddlewareNames(spans, spans.find(span => span.is_segment)?.span_id);
const getMiddlewareNamesOfFetches = (spans: SerializedStreamedSpan[]) =>
getFetchSpans(spans).map(fetchSpan => getMiddlewareNames(spans, fetchSpan.span_id));
const spans = await requestPage(
request,
spans =>
hasBothFetchSpans(spans) &&
getMiddlewareNamesOfFetches(spans).every(names => names.length >= getMiddlewareNamesOfPage(spans).length),
);

const namesOfPage = getMiddlewareNamesOfPage(spans);

expect(namesOfPage.length).toBeGreaterThan(0);
expect(namesOfPage.every(name => name.endsWith('(auto.middleware.nuxt)'))).toBe(true);
expect(getMiddlewareNamesOfFetches(spans)).toEqual([namesOfPage, namesOfPage]);
});
});
Loading
Loading