-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nextjs): Re-enable OTEL fetch instrumentation and disable Next.js…
… fetch instrumentation (#11686)
- Loading branch information
Showing
15 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...ications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/check/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { checkHandler } from '../../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const GET = checkHandler; |
10 changes: 10 additions & 0 deletions
10
...t-applications/nextjs-14/app/propagation/test-outgoing-fetch-external-disallowed/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch-external-disallowed/check`).then( | ||
res => res.json(), | ||
); | ||
return NextResponse.json(data); | ||
} |
5 changes: 5 additions & 0 deletions
5
.../e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/check/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { checkHandler } from '../../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const GET = checkHandler; |
8 changes: 8 additions & 0 deletions
8
...ckages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-fetch/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
const data = await fetch(`http://localhost:3030/propagation/test-outgoing-fetch/check`).then(res => res.json()); | ||
return NextResponse.json(data); | ||
} |
5 changes: 5 additions & 0 deletions
5
...lications/nextjs-14/app/propagation/test-outgoing-http-external-disallowed/check/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { checkHandler } from '../../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const GET = checkHandler; |
9 changes: 9 additions & 0 deletions
9
...st-applications/nextjs-14/app/propagation/test-outgoing-http-external-disallowed/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { makeHttpRequest } from '../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
const data = await makeHttpRequest(`http://localhost:3030/propagation/test-outgoing-http-external-disallowed/check`); | ||
return NextResponse.json(data); | ||
} |
5 changes: 5 additions & 0 deletions
5
...s/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-http/check/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { checkHandler } from '../../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const GET = checkHandler; |
9 changes: 9 additions & 0 deletions
9
...ackages/e2e-tests/test-applications/nextjs-14/app/propagation/test-outgoing-http/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { makeHttpRequest } from '../utils'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
const data = await makeHttpRequest(`http://localhost:3030/propagation/test-outgoing-http/check`); | ||
return NextResponse.json(data); | ||
} |
38 changes: 38 additions & 0 deletions
38
dev-packages/e2e-tests/test-applications/nextjs-14/app/propagation/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import http from 'http'; | ||
import { headers } from 'next/headers'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
export function makeHttpRequest(url: string) { | ||
return new Promise(resolve => { | ||
const data: any[] = []; | ||
http | ||
.request(url, httpRes => { | ||
httpRes.on('data', chunk => { | ||
data.push(chunk); | ||
}); | ||
httpRes.on('error', error => { | ||
resolve({ error: error.message, url }); | ||
}); | ||
httpRes.on('end', () => { | ||
try { | ||
const json = JSON.parse(Buffer.concat(data).toString()); | ||
resolve(json); | ||
} catch { | ||
resolve({ data: Buffer.concat(data).toString(), url }); | ||
} | ||
}); | ||
}) | ||
.end(); | ||
}); | ||
} | ||
|
||
export function checkHandler() { | ||
const headerList = headers(); | ||
|
||
const headerObj: Record<string, unknown> = {}; | ||
headerList.forEach((value, key) => { | ||
headerObj[key] = value; | ||
}); | ||
|
||
return NextResponse.json({ headers: headerObj }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
dev-packages/e2e-tests/test-applications/nextjs-14/tests/propagation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/event-proxy-server'; | ||
|
||
test('Propagates trace for outgoing http requests', async ({ baseURL, request }) => { | ||
const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-http/check'; | ||
}); | ||
|
||
const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-http'; | ||
}); | ||
|
||
const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-http`)).json(); | ||
|
||
const inboundTransaction = await inboundTransactionPromise; | ||
const outboundTransaction = await outboundTransactionPromise; | ||
|
||
expect(inboundTransaction.contexts?.trace?.trace_id).toStrictEqual(expect.any(String)); | ||
expect(inboundTransaction.contexts?.trace?.trace_id).toBe(outboundTransaction.contexts?.trace?.trace_id); | ||
|
||
const httpClientSpan = outboundTransaction.spans?.find(span => span.op === 'http.client'); | ||
|
||
expect(httpClientSpan).toBeDefined(); | ||
expect(httpClientSpan?.span_id).toStrictEqual(expect.any(String)); | ||
expect(inboundTransaction.contexts?.trace?.parent_span_id).toBe(httpClientSpan?.span_id); | ||
|
||
expect(headers).toMatchObject({ | ||
baggage: expect.any(String), | ||
'sentry-trace': `${outboundTransaction.contexts?.trace?.trace_id}-${httpClientSpan?.span_id}-1`, | ||
}); | ||
}); | ||
|
||
test('Propagates trace for outgoing fetch requests', async ({ baseURL, request }) => { | ||
const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch/check'; | ||
}); | ||
|
||
const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch'; | ||
}); | ||
|
||
const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-fetch`)).json(); | ||
|
||
const inboundTransaction = await inboundTransactionPromise; | ||
const outboundTransaction = await outboundTransactionPromise; | ||
|
||
expect(inboundTransaction.contexts?.trace?.trace_id).toStrictEqual(expect.any(String)); | ||
expect(inboundTransaction.contexts?.trace?.trace_id).toBe(outboundTransaction.contexts?.trace?.trace_id); | ||
|
||
const httpClientSpan = outboundTransaction.spans?.find( | ||
span => span.op === 'http.client' && span.data?.['sentry.origin'] === 'auto.http.otel.node_fetch', | ||
); | ||
|
||
// Right now we assert that the OTEL span is the last span before propagating | ||
expect(httpClientSpan).toBeDefined(); | ||
expect(httpClientSpan?.span_id).toStrictEqual(expect.any(String)); | ||
expect(inboundTransaction.contexts?.trace?.parent_span_id).toBe(httpClientSpan?.span_id); | ||
|
||
expect(headers).toMatchObject({ | ||
baggage: expect.any(String), | ||
'sentry-trace': `${outboundTransaction.contexts?.trace?.trace_id}-${httpClientSpan?.span_id}-1`, | ||
}); | ||
}); | ||
|
||
test('Does not propagate outgoing http requests not covered by tracePropagationTargets', async ({ | ||
baseURL, | ||
request, | ||
}) => { | ||
const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-http-external-disallowed/check'; | ||
}); | ||
|
||
const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-http-external-disallowed'; | ||
}); | ||
|
||
const { headers } = await (await request.get(`${baseURL}/propagation/test-outgoing-http-external-disallowed`)).json(); | ||
|
||
expect(headers.baggage).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
|
||
const inboundTransaction = await inboundTransactionPromise; | ||
const outboundTransaction = await outboundTransactionPromise; | ||
|
||
expect(typeof outboundTransaction.contexts?.trace?.trace_id).toBe('string'); | ||
expect(inboundTransaction.contexts?.trace?.trace_id).not.toBe(outboundTransaction.contexts?.trace?.trace_id); | ||
}); | ||
|
||
test('Does not propagate outgoing fetch requests not covered by tracePropagationTargets', async ({ | ||
baseURL, | ||
request, | ||
}) => { | ||
const inboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch-external-disallowed/check'; | ||
}); | ||
|
||
const outboundTransactionPromise = waitForTransaction('nextjs-14', transactionEvent => { | ||
return transactionEvent.transaction === 'GET /propagation/test-outgoing-fetch-external-disallowed'; | ||
}); | ||
|
||
const { headers } = await ( | ||
await request.get(`${baseURL}/propagation/test-outgoing-fetch-external-disallowed`) | ||
).json(); | ||
|
||
expect(headers.baggage).toBeUndefined(); | ||
expect(headers['sentry-trace']).toBeUndefined(); | ||
|
||
const inboundTransaction = await inboundTransactionPromise; | ||
const outboundTransaction = await outboundTransactionPromise; | ||
|
||
expect(typeof outboundTransaction.contexts?.trace?.trace_id).toBe('string'); | ||
expect(inboundTransaction.contexts?.trace?.trace_id).not.toBe(outboundTransaction.contexts?.trace?.trace_id); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters