Skip to content

Commit

Permalink
fix(astro): Correctly extract request data (#13315)
Browse files Browse the repository at this point in the history
builds on top of #13306, found while working on #13116

This PR ensures that we correctly extract the request data in our Astro
middleware. Previously we didn't convert the `request.headers` object
into a `Record<string, string>` but simply passed a `Headers` instance.
This caused problems with the `requestDataIntegration` which doesn't
handle the instance correctly.
  • Loading branch information
Lms24 authored Aug 12, 2024
1 parent bd87457 commit 2c24a33
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 2c24a33

Please sign in to comment.