Queries in Vercel Observability

Hey,

I think the observability insights in the logs are pretty cool, but our application is getting the data in RSCs 99% from db queries and not from fetches. Having a similar UI with query duration for queries would be great.

Is there an Observability API that I can use to add traces?

Thanks

Hi, Bernd! :waving_hand:

Yes, you can add custom traces for your database queries using Vercel’s OpenTelemetry integration.

Vercel’s Observability platform supports custom instrumentation through OpenTelemetry, which allows you to add traces for your database queries similar to how fetch requests are automatically traced.

Here’s how you can get started:

  1. First, install the OpenTelemetry packages:
npm install @vercel/otel
  1. You can use it to create spans for your database queries:
import { trace } from '@vercel/otel';

async function fetchDataFromDB() {
  return trace.span('db.query', async (span) => {
    // Add details about your query
    span.setAttribute('db.system', 'postgresql'); // or whatever DB you use
    span.setAttribute('db.statement', 'SELECT * FROM users');
    
    // Your actual database query
    const result = await db.query('SELECT * FROM users');
    
    // You can record the size of the result
    span.setAttribute('db.result.size', result.length);
    
    return result;
  });
}
  1. This will create traces that show up in your Vercel Observability dashboard with timing information similar to the fetch requests.

Resources

Let us know how you get on!

Thanks. After reading the docs it was not apparent that OTEL traces will show up in the monitoring logs.

Hi @pawlean ,

We are now an Observability customer.

First of all. Your example is not working. You have posted a made up API method.

After digging through the docs I’ve added

registerOTel({ serviceName: "app-frontend" }); to instrumentation.ts

and then added spans for my db calls.

import { trace } from "@opentelemetry/api";

I cannot submit the rest of my post because I get this error:

Long story short: The span is not showing up in the Vercel logs. Please provide some link to documentation or code from an actual API that can be used to track performance of custom code to show up in the log details. I’m not interested to send spans to New Relic or Datadog.

Hi @pawlean can I expect a response?

Hi, Bernd!

I actually asked your question in yesterday’s live session:

Does this help?

The question was aimed at if it’s possible to show custom traces in the logs UI like here: