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
Vercel Alum
Hi, Bernd! 👋
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:
First, install the OpenTelemetry packages:
1npm install @vercel/otel
You can use it to create spans for your database queries:
1import { trace } from '@vercel/otel';23async function fetchDataFromDB() {4 return trace.span('db.query', async (span) => {5 // Add details about your query6 span.setAttribute('db.system', 'postgresql'); // or whatever DB you use7 span.setAttribute('db.statement', 'SELECT * FROM users');89 // Your actual database query10 const result = await db.query('SELECT * FROM users');1112 // You can record the size of the result13 span.setAttribute('db.result.size', result.length);1415 return result;16 });17}
This will create traces that show up in your Vercel Observability dashboard with timing information similar to the fetch requests.
Thanks. After reading the docs it was not apparent that OTEL traces will show up in the monitoring logs.
(edited)
Hi ,
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.
1import { trace } from "@opentelemetry/api";2
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 can I expect a response?
Vercel Alum
Hi, Bernd!
I actually asked your question in yesterday's live session: