This example demonstrates OpenTelemetry (OTEL) tracing with Gentrace in a Next.js application.
-
Copy
.env.local.exampleto.env.localand add your API keys:cp .env.local.example .env.local
-
Configure the following environment variables:
OPENAI_API_KEY: Your OpenAI API key from platform.openai.com/api-keysGENTRACE_API_KEY: Your Gentrace API key from gentrace.ai/s/api-keysGENTRACE_PIPELINE_ID: Pipeline ID from your Gentrace dashboard
-
Install dependencies:
npm install
The instrumentation.ts file registers OpenTelemetry with Vercel's OTEL package, sending traces to Gentrace:
import { registerOTel } from "@vercel/otel";
import { OTLPHttpJsonTraceExporter } from "@vercel/otel";
export function register() {
registerOTel({
serviceName: "haiku-app",
traceExporter: new OTLPHttpJsonTraceExporter({
url: "https://gentrace.ai/api/otel/v1/traces",
headers: {
authorization: `Bearer ${process.env.GENTRACE_API_KEY}`,
},
}),
});
}In app/api/haiku/route.ts, Gentrace is initialized with otelSetup: false:
init({
apiKey: process.env.GENTRACE_API_KEY!,
baseURL: "https://gentrace.ai/api",
otelSetup: false,
});Why otelSetup: false? This prevents Gentrace SDK from setting up its own OTEL configuration since we're already using Vercel's OTEL setup in instrumentation.ts. This avoids conflicts between the two OTEL configurations.
When running the development server, you may encounter:
Module not found: Can't resolve '@opentelemetry/exporter-jaeger'
Cause: The @opentelemetry/sdk-node package (bundled with gentrace@0.11.0) contains code that dynamically requires the Jaeger exporter module. Next.js attempts to resolve this module at build time even though it's only used when OTEL_TRACES_EXPORTER=jaeger is explicitly set.
Solution: Install the Jaeger exporter as a dependency:
npm install @opentelemetry/exporter-jaegerThis resolves the build-time module resolution without affecting runtime behavior, as the Jaeger exporter is only activated when explicitly configured.