Skip to content

fix(astro): Correctly extract request data #13315

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

Merged
merged 1 commit 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: 12 additions & 5 deletions packages/astro/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
withIsolationScope,
} from '@sentry/node';
import type { Client, Scope, Span, SpanAttributes } from '@sentry/types';
import { addNonEnumerableProperty, objectify, stripUrlQueryAndFragment } from '@sentry/utils';
import {
addNonEnumerableProperty,
objectify,
stripUrlQueryAndFragment,
winterCGRequestToRequestData,
} from '@sentry/utils';
import type { APIContext, MiddlewareResponseHandler } from 'astro';

type MiddlewareOptions = {
Expand Down Expand Up @@ -86,11 +91,13 @@ async function instrumentRequest(

const isDynamicPageRequest = checkIsDynamicPageRequest(ctx);

const request = ctx.request;

const { method, headers } = isDynamicPageRequest
? ctx.request
: // headers can only be accessed in dynamic routes. Accessing `ctx.request.headers` in a static route
? request
: // headers can only be accessed in dynamic routes. Accessing `request.headers` in a static route
// will make the server log a warning.
{ method: ctx.request.method, headers: undefined };
{ method: request.method, headers: undefined };

return continueTrace(
{
Expand All @@ -101,7 +108,7 @@ async function instrumentRequest(
getCurrentScope().setSDKProcessingMetadata({
// We store the request on the current scope, not isolation scope,
// because we may have multiple requests nested inside each other
request: isDynamicPageRequest ? ctx.request : { method, url: ctx.request.url },
request: isDynamicPageRequest ? winterCGRequestToRequestData(request) : { method, url: request.url },
});

if (options.trackClientIp && isDynamicPageRequest) {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/server/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ describe('sentryMiddleware', () => {
request: {
method: 'GET',
url: '/users',
headers: new Headers({
headers: {
'some-header': 'some-value',
}),
},
},
});
expect(next).toHaveBeenCalledTimes(1);
Expand Down
Loading