Vercel Observability tutorial: debug a slow or failing Function

By

Tagged: Services

Debug a slow or failing Vercel Function with structured runtime logs, route metrics, CLI filters, request traces, and a verified fix.

When someone tells you “the app is slow” or “the API returned 500”, you need a fast way to narrow that down to the exact request, route, Function, and deployment behind it. That’s what Vercel Observability is built for.

Let’s walk through a real debugging loop together. We’ll build a small Next.js Route Handler with two bugs baked in on purpose: one request crashes with a 500, and another drags on for 1.5 seconds.

We’ll track down both issues using Runtime Logs, the Vercel CLI, route metrics, and Session Tracing, then ship a fix and confirm it worked in the telemetry.

Create a route with two problems

Start by spinning up a new Next.js app:

npx create-next-app@latest observability-demo
cd observability-demo

Now add a Route Handler at app/api/report/route.ts:

function wait(milliseconds: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, milliseconds)
  })
}

export async function GET(request: Request) {
  const startedAt = Date.now()
  const errorId = crypto.randomUUID()
  const mode = new URL(request.url).searchParams.get('mode')

  console.log(JSON.stringify({
    event: 'report.started',
    errorId,
    mode,
  }))

  try {
    if (mode === 'fail') {
      throw new Error('The billing profile is missing')
    }

    if (mode === 'slow') {
      await wait(1500)
    }

    console.log(JSON.stringify({
      event: 'report.completed',
      errorId,
      mode,
      durationMs: Date.now() - startedAt,
    }))

    return Response.json({
      status: 'ready',
      errorId,
    })
  } catch (error) {
    console.error(JSON.stringify({
      event: 'report.failed',
      errorId,
      mode,
      message: (error as Error).message,
      durationMs: Date.now() - startedAt,
    }))

    return Response.json(
      {
        error: 'Could not create the report',
        errorId,
      },
      { status: 500 },
    )
  }
}

Passing the fail mode makes the handler throw an error, while slow stalls for 1.5 seconds before responding.

Notice that the route also writes structured JSON logs with an event name and an errorId. That errorId ties the server-side log entry straight back to the response the user received.

Keep sensitive data out of your logs when you do this in a real project. Don’t log passwords, access tokens, full request bodies, or personal data. Just log the small bit of context you need to reproduce the bug and figure out what broke.

Fire up the app locally first to see it in action:

npm run dev

Then call each mode:

curl "http://localhost:3000/api/report"
curl "http://localhost:3000/api/report?mode=slow"
curl "http://localhost:3000/api/report?mode=fail"

The slow call succeeds after about 1.5 seconds, and the last request comes back with status 500.

Deploy a Preview

Next, push up a Preview deployment:

npx vercel

Grab the Preview URL the CLI prints and store it in a shell variable so we can reuse it:

PREVIEW_URL="https://your-preview-url.vercel.app"

Now trigger the three requests against that Preview deployment:

curl "$PREVIEW_URL/api/report"
curl "$PREVIEW_URL/api/report?mode=slow"
curl "$PREVIEW_URL/api/report?mode=fail"

I like doing this on a Preview deployment first so we keep all our test errors away from production traffic. Everything we’re about to use works the same way on production deployments too.

Find the 500 in Runtime Logs

Open your project in the Vercel dashboard and head to Logs.

Filter the list like this:

Click the matching request. The details panel gives you the request method, path, status, region, deployment, Function duration, memory usage, outgoing requests, and log messages, along with a Vercel Request ID.

Under log messages, find report.failed. Its errorId matches the value our API sent back in the response.

Having both IDs helps when someone reports a bug:

One thing that trips people up at first is mixing up Build logs and Runtime Logs. Build logs only tell you why a deployment couldn’t be built, while Runtime Logs record what happened when someone called your deployed Function.

Runtime Logs are available on every Vercel plan. Vercel keeps them for one hour on Hobby, one day on Pro, and longer on plans or add-ons with extended retention.

Search the same logs from the CLI

I use the dashboard when I’m poking around and don’t know what I’m looking for yet, and switch to the CLI as soon as I know the exact filter I want.

To list recent Preview requests that returned 500, run:

npx vercel logs \
  --environment preview \
  --status-code 500 \
  --since 30m \
  --expand

You can also search inside the log messages for our report.failed event:

npx vercel logs \
  --environment preview \
  --query "report.failed" \
  --since 30m \
  --expand

Once you have the platform Request ID from that output, pull up just that single request:

npx vercel logs --request-id req_xxxxx --expand

If you want to parse the output in a script, ask for JSON format:

npx vercel logs \
  --environment preview \
  --status-code 500 \
  --since 30m \
  --json

You can also stream live logs in your terminal while you reproduce a bug:

npx vercel logs --follow

Check the vercel logs reference for all the current flags and examples.

Find the slow route in Observability

Now head over to Observability in your project dashboard.

Pick a time range that covers the requests you just sent, then open the Vercel Functions view. Sort the routes by duration and open /api/report.

From the route view, you can inspect a few connected signals in one place:

Because our test app is small, you’ll only see a few data points here. During a real incident, start with a time window around the report, zoom into the spike, and compare the failing route against its usual behavior.

Every plan includes the main Observability dashboard, though detailed latency breakdowns, longer retention, and advanced metrics require Observability Plus.

Use Query when you need to group traffic

The Observability dashboard gives you pre-built views. When you need to slice the telemetry a specific way, switch to Query.

Say you want to know which Function threw the most 500 responses during an incident. You can build a query for:

Then switch the filter to /api/report and group by deployment instead. That immediately shows you whether the failures started with one specific release.

Query is only on Pro and Enterprise plans, and you don’t need it for the workflow in this guide. If you’re on Hobby, Logs and the standard Observability views will still get you to the exact same request.

Trace one slow request

Metrics show you that a route is slow, while a trace breaks down where one request spent its time.

Session Tracing works on Preview and Production deployments across all plans, as long as the Vercel Toolbar is enabled. It’s turned on by default for Preview deployments, and if you don’t see it, go to your project settings, open General, find Vercel Toolbar, and turn it on for Preview.

Open your Preview deployment in the browser and use the Vercel Toolbar:

  1. Open Tracing.
  2. Select Start Tracing Session.
  3. Visit /api/report?mode=slow.
  4. Open Tracing again and choose View Session Traces.

That opens Logs in the dashboard, already filtered to your session. Click the slow request to inspect its spans.

Out of the box, Vercel maps the request as it moves through its infrastructure and into your Function, breaking out framework, middleware, cache, and Function spans so you can spot the bottleneck. If you need finer detail inside your own code, you can add custom instrumentation spans too.

If you only care about a single page load instead of a full session, choose Run Page Trace.

Keep in mind that Session Tracing only works on deployed environments, not on localhost.

Fix the Function

We have two separate bugs to clean up in our handler.

First, a missing billing profile is an expected validation case, so it shouldn’t crash the Function with a 500. Return a useful client error instead:

if (mode === 'fail') {
  console.warn(JSON.stringify({
    event: 'report.rejected',
    errorId,
    reason: 'missing_billing_profile',
  }))

  return Response.json(
    {
      error: 'Add a billing profile before creating a report',
      errorId,
    },
    { status: 422 },
  )
}

Second, remove the artificial delay:

if (mode === 'slow') {
  // Run the real report work here without the artificial wait.
}

When a real Function is slow, resist the urge to immediately bump up its timeout. Open the request details first and look for what’s eating the time:

When operations don’t depend on each other, you can run them concurrently:

const [customer, invoices] = await Promise.all([
  getCustomer(),
  getInvoices(),
])

Measure before and after the change. Parallel work is only safe when those operations truly don’t depend on each other.

Deploy and verify the fix

Push another Preview deployment with both fixes in place:

npx vercel

Hit the same two problematic URLs against the new deployment. Now the failing request returns 422 instead of 500, and the slow endpoint finishes right away without that 1.5-second wait.

Open Logs again, filter to the new deployment, and check that:

Then head back to Observability and compare the old deployment with the new one under the same traffic so you can see the error drop to zero and the duration come down.

A practical incident workflow

Whenever a Vercel Function starts failing or slowing down, here’s the order I follow:

  1. Reproduce the problem on the right deployment.
  2. Filter Runtime Logs by environment, route, status, and time.
  3. Open a matching request and grab its Request ID.
  4. Read your structured application logs and inspect outgoing requests.
  5. Check route metrics to see if it’s a one-off or part of a larger pattern.
  6. Trace one representative request when timing still isn’t clear.
  7. Deploy a focused fix.
  8. Repeat the same requests and compare the new telemetry.

If you’re new to Vercel, start with the Vercel deployment documentation. Understanding how projects, deployments, Preview, and Production fit together makes everything in Observability click much faster.

Want me to talk about your product? You can sponsor this site.

Related posts about services: