Monitoring Hasura GraphQL Engine
Hasura is a powerful GraphQL engine that auto-generates APIs from your database. In production, you need to monitor query performance, error rates, and subscription health.
Built-in Monitoring
Hasura exposes metrics at /v1/metrics (Prometheus format):
`yaml
prometheus.yml
scrape_configs:
- job_name: 'hasura'
static_configs:
- targets: ['hasura:8080'] metrics_path: '/v1/metrics' bearer_token: 'your-admin-secret' `
Key metrics:
hasura_graphql_requests_total
hasura_graphql_execution_time_seconds
hasura_postgres_connections
hasura_active_subscriptions
Request Logging
Enable structured request logging with environment variables:
ash
HASURA_GRAPHQL_ENABLED_LOG_TYPES=startup,http-log,webhook-log,websocket-log,query-log
HASURA_GRAPHQL_LOG_LEVEL=warn
Sample HTTP log output:
json
{
"type": "http-log",
"detail": {
"operation": { "query": "query GetUser...", "operationName": "GetUser" },
"request_id": "abc123",
"response_size": 1234,
"http_info": { "status": 200, "method": "POST" }
}
}
Detecting Slow Queries
Enable the query log and filter by execution time:
`ash
Check slow queries from Hasura logs
docker logs hasura 2>&1 | grep '"execution_time"' | \
awk -F'"execution_time":' '{print }' | \
awk '{if (+0 > 0.5) print "SLOW:", }'
`
Or in your observability stack, alert when hasura_graphql_execution_time_seconds p99 > 500ms.
Health Check
Hasura has a built-in health endpoint:
`ash
curl https://your-hasura.com/healthz
Returns: {"status":"OK"}
`
Monitor this with Vigilmon:
- URL: https://your-hasura.com/healthz
- Expected response body contains: OK
- Alert on failure or latency > 1000ms
Subscription Monitoring
GraphQL subscriptions use WebSocket connections. Monitor:
`javascript
// Client-side subscription health tracking
let subscriptionErrors = 0;
let lastSuccessfulEvent = Date.now();
const subscription = wsClient.subscribe(
{ query: SUBSCRIPTION_QUERY },
{
next: (data) => {
lastSuccessfulEvent = Date.now();
subscriptionErrors = 0;
},
error: (err) => {
subscriptionErrors++;
console.error('Subscription error:', err);
}
}
);
// Alert if no events for > 5 minutes
setInterval(() => {
if (Date.now() - lastSuccessfulEvent > 300000) {
console.warn('Subscription stale - no events in 5 minutes');
}
}, 60000);
`
Webhook Event Trigger Monitoring
Hasura event triggers fire webhooks on database changes. Monitor delivery:
javascript
// Your webhook receiver
app.post('/events/user-created', async (req, res) => {
const start = Date.now();
try {
await processEvent(req.body);
console.log(JSON.stringify({
event: 'hasura_webhook_processed',
trigger: req.body.trigger?.name,
duration_ms: Date.now() - start
}));
res.json({ success: true });
} catch (err) {
console.error('Webhook processing failed:', err.message);
res.status(500).json({ error: err.message });
}
});
Key Takeaways
- Enable Prometheus metrics and the query log
- Monitor /healthz with Vigilmon for external uptime
- Alert on p99 execution time > 500ms
- Track subscription staleness and webhook failure rates
Top comments (0)