DEV Community

Vigilmon
Vigilmon

Posted on

Turso libSQL Monitoring: Query Latency and Edge Health Checks

Turso libSQL Monitoring at the Edge

Turso is optimized for low latency. Queries above 50ms warrant investigation.

Query Instrumentation

import { createClient } from '@libsql/client';
const client = createClient({ url: process.env.TURSO_DATABASE_URL!, authToken: process.env.TURSO_AUTH_TOKEN! });

async function dbQuery(sql: string, args?: any[]) {
  const start = performance.now();
  const result = await client.execute({ sql, args: args ?? [] });
  const duration = performance.now() - start;
  if (duration > 50) {
    console.warn('turso_slow: ' + Math.round(duration) + 'ms - ' + sql.substring(0, 80));
  }
  return result;
}
Enter fullscreen mode Exit fullscreen mode

Health Check

app.get('/health', async (_req, res) => {
  try {
    await client.execute('SELECT 1');
    res.json({ status: 'ok', db: 'turso' });
  } catch (err) {
    res.status(503).json({ status: 'unhealthy' });
  }
});
Enter fullscreen mode Exit fullscreen mode

Monitor with Vigilmon every minute.

Edge Function Logging

export default {
  async fetch(request: Request, env: Env) {
    const start = Date.now();
    const result = await handleRequest(request, env);
    console.log(JSON.stringify({ path: new URL(request.url).pathname, duration_ms: Date.now() - start }));
    return result;
  }
};
Enter fullscreen mode Exit fullscreen mode

Takeaways

  • Alert on queries above 50ms
  • Monitor health endpoint with Vigilmon
  • Log duration on every edge function request

Top comments (0)