Files
f5f2660cba fix: support root span attributes with a custom server (#85521)
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

Fixes #85520

When there is an active context from a custom server, the handleRequest
span is not marked as a root span. As a result next.js does not track
any root span attributes on the handleRequest span.
Because the custom server does not know what route was matched by next
and next is not tracking the route that matched this makes observability
very challenging for users of a custom server.

This PR changes the isRootSpan logic to treat the handleRequest span as
a root span if there is not already a nextjs root span for the request
instead of whether there is any active span.

---------

Co-authored-by: JJ Kasper <[email protected]>
2025-11-07 15:43:26 -08:00

50 lines
1.2 KiB
TypeScript

import { createServer } from 'http'
import { parse } from 'url'
import next from 'next'
import getPort from 'get-port'
import { trace } from '@opentelemetry/api'
import { register } from './instrumentation-custom-server'
register()
async function main() {
const port = await getPort()
const hostname = 'localhost'
const app = next({
dev: process.env.NODE_ENV === 'development',
hostname,
port,
dir: __dirname,
})
const handle = app.getRequestHandler()
await app.prepare()
const tracer = trace.getTracer('custom-server', '1.0.0')
createServer((req, res) => {
// Create a local parent span to simulate custom server behavior
tracer.startActiveSpan('custom-server-request', async (span) => {
try {
const parsedUrl = parse(req.url!, true)
await handle(req, res, parsedUrl)
span.end()
} catch (err) {
span.recordException(err as Error)
span.end()
res.statusCode = 500
res.end('Internal Server Error')
}
})
}).listen(port, undefined, (err?: Error) => {
if (err) throw err
console.log(`- Local: http://${hostname}:${port}`)
})
}
main().catch((err) => {
console.error(err)
process.exit(1)
})