Prerequisites
- Node.js 18+
- OpenAI API key
- Phoenix or another OpenTelemetry collector
Installation
Instrumentation Setup
Next.js requires a specific instrumentation pattern using theinstrumentation hook.
Create Root Instrumentation File
Createinstrumentation.ts in the root directory:
Create Node.js Instrumentation
Createinstrumentation-node.ts:
Enable Instrumentation in Next.js Config
Updatenext.config.js:
API Route
Createapp/api/chat/route.ts:
Streaming API Route
For streaming responses, createapp/api/chat/stream/route.ts:
Chat UI Component
Createapp/components/chat.tsx:
Main Page
Updateapp/page.tsx:
Run the Application
Key Features
Automatic Instrumentation
Next.js instrumentation hook automatically initializes tracing before your application code runs.Server-Side Only
Instrumentation only runs in the Node.js runtime (server-side), not in the browser.API Routes
Both standard and streaming API routes are automatically traced.Next Steps
- Add authentication
- Implement rate limiting
- Explore Vercel AI SDK