Skip to content

Commit 56831df

Browse files
Create instrumentation.mdx
1 parent 12ee74f commit 56831df

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Docs/app/instrumentation.mdx

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
Next.js (/app)
2+
3+
Choose a framework to optimize documentation to:
4+
5+
* Next.js (/app)
6+
* Next.js (/pages)
7+
* Other frameworks
8+
9+
On this page
10+
11+
# Instrumentation
12+
13+
Observability is crucial for understanding and optimizing the behavior and performance of your app. Vercel supports OpenTelemetry instrumentation out of the box, which can be used through the `@vercel/otel` package.
14+
15+
> ## [Getting started](#getting-started)[](#getting-started)
16+
17+
To get started, install the following packages:
18+
19+
Terminal
20+
21+
![](https://7nyt0uhk7sse4zvn.public.blob.vercel-storage.com/docs-assets/static/topics/icons/pnpm.svg)pnpmbunyarnnpm
22+
23+
```bash
24+
>_pnpm i @opentelemetry/api @vercel/otel
25+
```
26+
27+
```
28+
>_yarn add @opentelemetry/api @vercel/otel
29+
```
30+
31+
```
32+
>_npm i @opentelemetry/api @vercel/otel
33+
```
34+
35+
```
36+
>_bun add @opentelemetry/api @vercel/otel
37+
```
38+
39+
Next, create a `instrumentation.ts` (or `.js`) file in the root directory of the project, or, on Next.js [it must be placed](https://nextjs.org/docs/app/guides/open-telemetry#using-vercelotel) in the `src` directory if you are using one. Add the following code to initialize and configure OTel using `@vercel/otel`:
40+
41+
Next.js (/app)Next.js (/pages)Other frameworks
42+
43+
instrumentation.ts
44+
45+
TypeScript
46+
47+
TypeScriptJavaScriptBash
48+
49+
```ts
50+
import { registerOTel } from '@vercel/otel';
51+
52+
export function register() {
53+
registerOTel({ serviceName: 'your-project-name' });
54+
}
55+
// NOTE: You can replace `your-project-name` with the actual name of your project
56+
```
57+
58+
## [Configuring context propagation](#configuring-context-propagation)[](#configuring-context-propagation)
59+
60+
Context propagation connects operations across service boundaries so you can trace a request through your entire system. When your app calls another service, context propagation passes trace metadata (for example,trace IDs, span IDs) along with the request, typically through HTTP headers like `traceparent`. This lets OpenTelemetry link all the spans together into a single, complete trace.
61+
62+
Without context propagation, each service generates isolated spans you can't connect. With it, you see exactly how a request flows through your infrastructure—from the initial API call through databases, queues, and external services.
63+
64+
For more details on how context propagation works, see the [OpenTelemetry context propagation documentation](https://opentelemetry.io/docs/concepts/context-propagation/).
65+
66+
### [For outgoing requests](#for-outgoing-requests)[](#for-outgoing-requests)
67+
68+
You can configure context propagation by configuring the `fetch` option in the `instrumentationConfig` option.
69+
70+
Next.js (/app)Next.js (/pages)Other frameworks
71+
72+
instrumentation.ts
73+
74+
TypeScript
75+
76+
TypeScriptJavaScriptBash
77+
78+
```ts
79+
import { registerOTel } from '@vercel/otel';
80+
81+
export function register() {
82+
registerOTel({
83+
serviceName: `your-project-name`,
84+
instrumentationConfig: {
85+
fetch: {
86+
// This URLs will have the tracing context propagated to them.
87+
propagateContextUrls: [
88+
'your-service-domain.com',
89+
'your-database-domain.com',
90+
],
91+
// This URLs will not have the tracing context propagated to them.
92+
dontPropagateContextUrls: [
93+
'some-third-party-service-domain.com',
94+
],
95+
// This URLs will be ignored and will not be traced.
96+
ignoreUrls: ['my-internal-private-tool.com'],
97+
},
98+
},
99+
});
100+
}
101+
// NOTE: You can replace `your-project-name` with the actual name of your project
102+
```
103+
104+
### [From incoming requests](#from-incoming-requests)[](#from-incoming-requests)
105+
106+
Next.js 13.4+ supports automatic OpenTelemetry context propagation for incoming requests. For other frameworks, that do not support automatic OpenTelemetry context propagation, you can refer to the following code example to manually inject the inbound context into a request handler.
107+
108+
api-handler.ts
109+
110+
```ts
111+
import { propagation, context, trace } from "@opentelemetry/api";
112+
113+
const tracer = trace.getTracer('custom-tracer');
114+
115+
// This function injects the inbound context into the request handler
116+
function injectInboundContext(f: (request: Request) => Promise<Response>): (request: Request) => Promise<Response> {
117+
return (req) => {
118+
const c = propagation.extract(context.active(), Object.fromEntries(req.headers))
119+
return context.with(c, async () => {
120+
return await f(req);
121+
})
122+
}
123+
}
124+
125+
export const GET = injectInboundContext(async (req: Request) => {
126+
const span = tracer.startSpan('your-operation-name');
127+
// The above ^ span will be automatically attached to incoming tracing context (if any)
128+
try {
129+
// Your operation logic here
130+
span.setAttributes({
131+
'custom.attribute': 'value',
132+
});
133+
return new Response('Hello, world!');
134+
} finally {
135+
span.end();
136+
}
137+
});
138+
```
139+
140+
### [Sampling behavior](#sampling-behavior)[](#sampling-behavior)
141+
142+
When requests arrive with a `traceparent` header, Vercel's infrastructure considers the inbound sampling decision alongside its own sampling rules. Both must agree to sample for spans to be emitted.
143+
144+
For a span to be emitted, both the inbound decision (if present) and Vercel's sampling rules must agree to sample:
145+
146+
| Inbound decision | Vercel sampled | Result |
147+
| ---------------- | -------------- | ------- |
148+
| Sampled | Yes | Emitted |
149+
| Sampled | No | Dropped |
150+
| Not sampled | Any | Dropped |
151+
| No decision | Yes | Emitted |
152+
| No decision | No | Dropped |
153+
154+
This ensures consistency in distributed systems: if an upstream service marks a trace as not sampled, Vercel respects that decision. When an upstream marks a trace as sampled, Vercel still applies its own sampling rules to control span volume.
155+
156+
## [Adding custom spans](#adding-custom-spans)[](#adding-custom-spans)
157+
158+
After installing `@vercel/otel`, you can add custom spans to your traces to capture additional visibility into your application. Custom spans let you track specific operations that matter to your business logic, such as processing payments, generating reports, or transforming data, so you can measure their performance and debug issues more effectively.
159+
160+
Use the `@opentelemetry/api` package to instrument specific operations:
161+
162+
custom-span.ts
163+
164+
```ts
165+
import { trace } from '@opentelemetry/api';
166+
167+
const tracer = trace.getTracer('custom-tracer');
168+
169+
async function performOperation() {
170+
const span = tracer.startSpan('operation-name');
171+
try {
172+
// Your operation logic here
173+
span.setAttributes({
174+
'custom.attribute': 'value',
175+
});
176+
} finally {
177+
span.end();
178+
}
179+
}
180+
```
181+
182+
Custom spans from functions using the [Edge runtime](/docs/functions/runtimes/edge) are not supported.
183+
184+
## [OpenTelemetry configuration options](#opentelemetry-configuration-options)[](#opentelemetry-configuration-options)
185+
186+
For the full list of configuration options, see the [@vercel/otel documentation](https://github.com/vercel/otel/blob/main/packages/otel/README.md).
187+
188+
## [Limitations](#limitations)[](#limitations)
189+
190+
* If your app uses manual OpenTelemetry SDK configuration without the usage of `@vercel/otel`, you will not be able to use [Session Tracing](/docs/tracing/session-tracing) or [Trace Drains](/docs/drains/reference/traces).

0 commit comments

Comments
 (0)