-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add OTEL instrumentation for next-server + OTEL example (#46198)
fixes NEXT-479 ## content This PR adds a `getTracer` API to Next.js that uses the `otel/api` under the hood to provide Next.js level instrumentation through Open Telemetry. This also adds an example `with-opentelemetry` to demonstrate how it can be used, assuming you have a collector. This allows most notably to have `getServerSideProps` and `fetch` calls inside Server Components traced. ## details - we hide most internals spans, if you want to see all of them, use the NEXT_OTEL_VERBOSE=1 env var - if you want to use this, you'll need to rely on the `config.experimental.instrumentationHook` config option to initialise OTEL, like in the example ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
1 parent
65ecd9c
commit 4072955
Showing
32 changed files
with
1,415 additions
and
303 deletions.
There are no files selected for viewing
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,36 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.pnpm-debug.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.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,4 @@ | ||
{ | ||
"typescript.tsdk": "../../node_modules/.pnpm/typescript@4.7.4/node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true | ||
} |
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,30 @@ | ||
# Data fetch example | ||
|
||
Next.js was conceived to make it easy to create universal apps. That's why fetching data | ||
on the server and the client when necessary is so easy with Next.js. | ||
|
||
By using `getStaticProps` Next.js will fetch data at build time from a page, and pre-render the page to static assets. | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/data-fetch) | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/data-fetch&project-name=data-fetch&repository-name=data-fetch) | ||
|
||
## How to use | ||
|
||
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example: | ||
|
||
```bash | ||
npx create-next-app --example data-fetch data-fetch-app | ||
``` | ||
|
||
```bash | ||
yarn create next-app --example data-fetch data-fetch-app | ||
``` | ||
|
||
```bash | ||
pnpm create next-app --example data-fetch data-fetch-app | ||
``` | ||
|
||
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
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,12 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<head> | ||
<title>Next.js with OpenTelemetry</title> | ||
</head> | ||
<body> | ||
<main>{children}</main> | ||
</body> | ||
</html> | ||
) | ||
} |
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,12 @@ | ||
import Link from 'next/link' | ||
import { fetchGithubStars } from '../shared/fetch-github-stars' | ||
|
||
export default async function Page() { | ||
const stars = await fetchGithubStars() | ||
return ( | ||
<> | ||
<p>Next.js has {stars} ⭐️</p> | ||
<Link href="/preact-stars">How about preact?</Link> | ||
</> | ||
) | ||
} |
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,23 @@ | ||
export function register() { | ||
if (process.env.NEXT_RUNTIME === 'nodejs') { | ||
const opentelemetry = require('@opentelemetry/sdk-node') | ||
const { | ||
OTLPTraceExporter, | ||
} = require('@opentelemetry/exporter-trace-otlp-http') | ||
const { Resource } = require('@opentelemetry/resources') | ||
const { | ||
SemanticResourceAttributes, | ||
} = require('@opentelemetry/semantic-conventions') | ||
|
||
const sdk = new opentelemetry.NodeSDK({ | ||
traceExporter: new OTLPTraceExporter({}), | ||
instrumentations: [], | ||
resource: new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: 'my-next-app', | ||
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0', | ||
}), | ||
}) | ||
|
||
sdk.start() | ||
} | ||
} |
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,6 @@ | ||
module.exports = { | ||
experimental: { | ||
instrumentationHook: true, | ||
appDir: true, | ||
}, | ||
} |
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,28 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start", | ||
"start:otel-verbose": "NEXT_OTEL_VERBOSE=1 next start" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/api": "1.4.0", | ||
"@opentelemetry/auto-instrumentations-node": "0.36.3", | ||
"@opentelemetry/exporter-trace-otlp-grpc": "0.35.1", | ||
"@opentelemetry/exporter-trace-otlp-http": "0.35.1", | ||
"@opentelemetry/instrumentation-fetch": "0.35.1", | ||
"@opentelemetry/resources": "1.9.1", | ||
"@opentelemetry/sdk-node": "0.35.1", | ||
"@opentelemetry/sdk-trace-base": "1.9.1", | ||
"@opentelemetry/semantic-conventions": "1.9.1", | ||
"next": "latest", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "18.7.11", | ||
"@types/react": "18.0.17", | ||
"typescript": "4.7.4" | ||
} | ||
} |
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,11 @@ | ||
// hello world api route | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { fetchGithubStars } from '../../shared/fetch-github-stars' | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const stars = await fetchGithubStars() | ||
res.status(200).json({ stars }) | ||
} |
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,20 @@ | ||
import Link from 'next/link' | ||
import { fetchGithubStars } from '../shared/fetch-github-stars' | ||
|
||
export async function getServerSideProps() { | ||
const stars = await fetchGithubStars() | ||
return { | ||
props: { | ||
stars, | ||
}, | ||
} | ||
} | ||
|
||
export default function IndexPage({ stars }) { | ||
return ( | ||
<> | ||
<p>Next.js has {stars} ⭐️</p> | ||
<Link href="/preact-stars">How about preact?</Link> | ||
</> | ||
) | ||
} |
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,15 @@ | ||
import { trace } from '@opentelemetry/api' | ||
|
||
export async function fetchGithubStars() { | ||
const span = trace.getTracer('nextjs-example').startSpan('fetchGithubStars') | ||
return fetch('https://api.github.com/repos/vercel/next.js', { | ||
next: { | ||
revalidate: 0, | ||
}, | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => data.stargazers_count) | ||
.finally(() => { | ||
span.end() | ||
}) | ||
} |
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,26 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"strictNullChecks": true | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
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
Oops, something went wrong.