Unverified Commit 1e47bc23 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Finish up otel example with working docker and small guide in readme (#46819)

Finish up OTEL example with HTTP (we don't recommend grpc because that
package is needlessly large).

Also added a link to a simple repo with otel collector and few backends:
https://github.com/vercel/opentelemetry-collector-dev-setup

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 6706a2a6
...@@ -58,7 +58,6 @@ ...@@ -58,7 +58,6 @@
"*.ts": "$(capture).test.ts, $(capture).test.tsx", "*.ts": "$(capture).test.ts, $(capture).test.tsx",
"*.tsx": "$(capture).test.ts, $(capture).test.tsx" "*.tsx": "$(capture).test.ts, $(capture).test.tsx"
}, },
// Allow to find the cargo project for rust-analyzer. // Allow to find the cargo project for rust-analyzer.
"rust-analyzer.linkedProjects": ["packages/next-swc/Cargo.toml"], "rust-analyzer.linkedProjects": ["packages/next-swc/Cargo.toml"],
// Compile rust-analyzer in a separate directory to avoid conflicts with the main project. // Compile rust-analyzer in a separate directory to avoid conflicts with the main project.
...@@ -68,5 +67,6 @@ ...@@ -68,5 +67,6 @@
"rust-analyzer.server.extraEnv": { "rust-analyzer.server.extraEnv": {
"CARGO_TARGET_DIR": "target/rust-analyzer", "CARGO_TARGET_DIR": "target/rust-analyzer",
"RUST_BACKTRACE": "0" "RUST_BACKTRACE": "0"
} },
"cSpell.words": ["opentelemetry", "zipkin"]
} }
# Data fetch example # OpenTelemetry example
Next.js was conceived to make it easy to create universal apps. That's why fetching data [OpenTelemetry](https://opentelemetry.io/) is a collection of tools for easier instrumentation of you applications.
on the server and the client when necessary is so easy with Next.js. You can use it to setup instrumentation for Next.js with the `instrumentation.ts` file.
By using `getStaticProps` Next.js will fetch data at build time from a page, and pre-render the page to static assets. In order to collect and inspect traces generated by OpenTelemetry you need a running OpenTelemetry collector and a backend.
You can easily deploy this example on Vercel. It manages OpenTelemetry collector for you, read the [docs](https://vercel.com/docs/concepts/observability/otel-overview/quickstart) for more details. Or you can follow official OpenTelemetry [documentation](https://opentelemetry.io/docs/collector/getting-started/) if you want to use it in your self-hosted Next.js app.
But you need to run your own collector if you want to test OpenTelemetry locally. We recommend cloning pre-made [dev setup](https://github.com/vercel/opentelemetry-collector-dev-setup). It contains pre-configured docker images for Collector, ZipKin, Jaeger and Prometheus.
## Deploy your own ## 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 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) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-opentelemetry&project-name=with-opentelemetry&repository-name=with-opentelemetry)
## How to use ## 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: 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 ```bash
npx create-next-app --example data-fetch data-fetch-app npx create-next-app --example with-opentelemetry with-opentelemetry-app
``` ```
```bash ```bash
yarn create next-app --example data-fetch data-fetch-app yarn create next-app --example with-opentelemetry with-opentelemetry-app
``` ```
```bash ```bash
pnpm create next-app --example data-fetch data-fetch-app pnpm create next-app --example with-opentelemetry with-opentelemetry-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)). 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)).
import { Resource } from '@opentelemetry/resources'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import {
NodeTracerProvider,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-node'
// You can use gRPC exporter instead
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
// Next.js expects you to use to register TraceProvider. It won't work if you use NodeSDK.
// We use registered provider to create traces inside of Next.js internals.
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'next-app',
}),
})
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter({})))
// Make sure to register you provider
provider.register()
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()
}
}
export function register() {
// We need to make sure that we import these files only in Node.js environment.
// OpenTelemetry is **not** supported on Edge or Client side at the moment.
if (process.env.NEXT_RUNTIME === 'nodejs') {
require('./instrumentation-node')
}
}
...@@ -8,12 +8,8 @@ ...@@ -8,12 +8,8 @@
}, },
"dependencies": { "dependencies": {
"@opentelemetry/api": "1.4.0", "@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/exporter-trace-otlp-http": "0.35.1",
"@opentelemetry/instrumentation-fetch": "0.35.1",
"@opentelemetry/resources": "1.9.1", "@opentelemetry/resources": "1.9.1",
"@opentelemetry/sdk-node": "0.35.1",
"@opentelemetry/sdk-trace-base": "1.9.1", "@opentelemetry/sdk-trace-base": "1.9.1",
"@opentelemetry/semantic-conventions": "1.9.1", "@opentelemetry/semantic-conventions": "1.9.1",
"next": "latest", "next": "latest",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment