Skip to content

Commit

Permalink
fix(astro): Correctly extract request data
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Aug 12, 2024
1 parent 38d9689 commit a24cebc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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

0 comments on commit a24cebc

Please sign in to comment.