Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@ Sentry.init({
environment: 'qa',
tracesSampleRate: 1.0,
tunnel: 'http://localhost:3031/', // proxy server
integrations: [Sentry.browserTracingIntegration()],
integrations: [
Sentry.browserTracingIntegration({
beforeStartSpan: opts => {
if (opts.name.startsWith('/blog/')) {
return {
...opts,
name: window.location.pathname,
};
}
return opts;
},
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Layout from '../../layouts/Layout.astro';
export const prerender = false;
const { slug } = Astro.params;
---

<Layout title="Dynamic SSR page">
<h1>Blog post: {slug}</h1>
</Layout>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Layout from '../layouts/Layout.astro';
<a href="/test-static" title="static page">Static Page</a>
<a href="/server-island">Server Island</a>
<a href="/user-page/myUsername123">Test Parametrized Routes</a>
<a href="/blog/my-post">Route Parametrization override</a>
</ul>
</main>
</Layout>
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,18 @@ test.describe('parametrized vs static paths', () => {
request: { url: expect.stringContaining('/user-page/settings') },
});
});

test('allows for span name override via beforeStartSpan', async ({ page }) => {
const clientPageloadTxnPromise = waitForTransaction('astro-5', txnEvent => {
return txnEvent?.transaction?.startsWith('/blog/') ?? false;
});

await page.goto('/blog/my-post');

const clientPageloadTxn = await clientPageloadTxnPromise;
expect(clientPageloadTxn).toMatchObject({
transaction: '/blog/my-post',
transaction_info: { source: 'custom' },
});
});
});
79 changes: 53 additions & 26 deletions packages/astro/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { browserTracingIntegration as originalBrowserTracingIntegration, WINDOW } from '@sentry/browser';
import {
browserTracingIntegration as originalBrowserTracingIntegration,
startBrowserTracingPageLoadSpan,
WINDOW,
} from '@sentry/browser';
import type { Integration, TransactionSource } from '@sentry/core';
import { debug, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import {
browserPerformanceTimeOrigin,
debug,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';

/**
Expand All @@ -18,35 +27,53 @@ function getMetaContent(metaName: string): string | undefined {
export function browserTracingIntegration(
options: Parameters<typeof originalBrowserTracingIntegration>[0] = {},
): Integration {
const integration = originalBrowserTracingIntegration(options);
const integration = originalBrowserTracingIntegration({ ...options, instrumentPageLoad: false });

return {
...integration,
setup(client) {
// Original integration setup call
integration.setup?.(client);

client.on('afterStartPageLoadSpan', pageLoadSpan => {
const routeNameFromMetaTags = getMetaContent('sentry-route-name');

if (routeNameFromMetaTags) {
let decodedRouteName;
try {
decodedRouteName = decodeURIComponent(routeNameFromMetaTags);
} catch {
// We ignore errors here, e.g. if the value cannot be URL decoded.
return;
}

DEBUG_BUILD && debug.log(`[Tracing] Using route name from Sentry HTML meta-tag: ${decodedRouteName}`);

pageLoadSpan.updateName(decodedRouteName);
pageLoadSpan.setAttributes({
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' as TransactionSource,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.astro',
afterAllSetup(client) {
// Original integration afterAllSetup call
integration.afterAllSetup?.(client);

if (WINDOW.location) {
if (options.instrumentPageLoad != false) {
const origin = browserPerformanceTimeOrigin();

const { name, source } = getPageloadSpanName();

startBrowserTracingPageLoadSpan(client, {
name,
// pageload should always start at timeOrigin (and needs to be in s, not ms)
startTime: origin ? origin / 1000 : undefined,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.astro',
},
});
}
});
}
},
};
}

function getPageloadSpanName(): { name: string; source: TransactionSource } {
try {
const routeNameFromMetaTags = getMetaContent('sentry-route-name');
if (routeNameFromMetaTags) {
const decodedRouteName = decodeURIComponent(routeNameFromMetaTags);

DEBUG_BUILD && debug.log(`[Tracing] Using route name from Sentry HTML meta-tag: ${decodedRouteName}`);

return {
name: decodedRouteName,
source: 'route',
};
}
} catch {
// fail silently if decoding or reading the meta tag fails
}
return {
name: WINDOW.location.pathname,
source: 'url',
};
}
Loading