Skip to content

feat(otel): add Vercel handler instrumentation for distributed tracing#163

Draft
Schniz wants to merge 1 commit into
mainfrom
schniz/vercel-handler-hook
Draft

feat(otel): add Vercel handler instrumentation for distributed tracing#163
Schniz wants to merge 1 commit into
mainfrom
schniz/vercel-handler-hook

Conversation

@Schniz
Copy link
Copy Markdown

@Schniz Schniz commented Aug 7, 2025

Implements OpenTelemetry instrumentation for Vercel handlers by patching
the Node.js HTTP Server's emit method to automatically extract trace
context from incoming request headers and propagate it through the
request lifecycle.

🤖 Generated with opencode

Implements OpenTelemetry instrumentation for Vercel handlers by patching
the Node.js HTTP Server's emit method to automatically extract trace
context from incoming request headers and propagate it through the
request lifecycle.

🤖 Generated with [opencode](https://opencode.ai)
@Schniz Schniz self-assigned this Aug 7, 2025
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Aug 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
otel-site ❌ Failed (Inspect) Aug 7, 2025 4:07pm

extends InstrumentationConfig {}

export class VercelHandlerInstrumentation implements Instrumentation {
instrumentationName = "@vercel/otel/handlers";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, to be more inline with the class name/file name (also it's more clear what it is):

Suggested change
instrumentationName = "@vercel/otel/handlers";
instrumentationName = "@vercel/otel/vercel-handler";

export class VercelHandlerInstrumentation implements Instrumentation {
instrumentationName = "@vercel/otel/handlers";
instrumentationVersion = "1.0.0";
instrumentationDescription?: string;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe remove?

instrumentationVersion = "1.0.0";
instrumentationDescription?: string;

traceProvider?: TracerProvider;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo i think (tracerProvider):

Suggested change
traceProvider?: TracerProvider;
tracerProvider?: TracerProvider;

instrumentationDescription?: string;

traceProvider?: TracerProvider;
supportedVersions?: string[];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe remove if not used

if (isEnabled() && event === "request") {
const request = args[0];
console.log("instrumenting request", request.headers);
const c = propagation.extract(context.active(), request.headers);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: should we be a bit more defensive here:

  • what if request is undefined or null?
  • what if request.headers is undefined or null?

const next = () => emit.call(this, event, ...args);
if (isEnabled() && event === "request") {
const request = args[0];
console.log("instrumenting request", request.headers);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("instrumenting request", request.headers);

if (isEnabled() && event === "request") {
const request = args[0];
console.log("instrumenting request", request.headers);
const c = propagation.extract(context.active(), request.headers);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to provide a custom header reader here. I don't quite remember the exact request.headers API - is it always a simple Record<string, string>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants