DEV Community

Vigilmon
Vigilmon

Posted on

How to Monitor Google Cloud SQL with Vigilmon

How to Monitor Google Cloud SQL with Vigilmon

Google Cloud SQL is a fully managed relational database service supporting PostgreSQL, MySQL, and SQL Server. Teams running GCP workloads depend on Cloud SQL for core data persistence — which makes monitoring your Cloud SQL-connected applications critical.

This guide shows you how to build a health check that verifies Cloud SQL connectivity and monitor it with Vigilmon.

Why Monitor Cloud SQL Through Your Application

Cloud SQL has its own monitoring via Cloud Monitoring (formerly Stackdriver). But what you also need is external monitoring of your application's ability to connect to and query Cloud SQL. These are different things:

What Monitored by
Cloud SQL instance up/down Cloud Monitoring
Your app's Cloud SQL connection Vigilmon health check
User-facing query performance Both

Building a Cloud SQL Health Check

Node.js / Express with Cloud SQL Connector

const { Connector } = require('@google-cloud/cloud-sql-connector');
const { Pool } = require('pg');

const connector = new Connector();

async function createPool() {
  const clientOpts = await connector.getOptions({
    instanceConnectionName: process.env.INSTANCE_CONNECTION_NAME,
    ipType: 'PUBLIC',
  });
  return new Pool({
    ...clientOpts,
    database: process.env.DB_NAME,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
  });
}

let pool;

app.get('/health', async (req, res) => {
  try {
    if (!pool) pool = await createPool();
    await pool.query('SELECT 1');
    res.json({ status: 'ok', db: 'cloud-sql', connected: true });
  } catch (err) {
    res.status(503).json({
      status: 'error',
      db: 'cloud-sql',
      connected: false
    });
  }
});
Enter fullscreen mode Exit fullscreen mode

Python / Flask with Cloud SQL Python Connector

from flask import Flask, jsonify
from google.cloud.sql.connector import Connector
import sqlalchemy
import os

app = Flask(__name__)
connector = Connector()

def get_connection():
    return connector.connect(
        os.environ["INSTANCE_CONNECTION_NAME"],
        "pg8000",
        user=os.environ["DB_USER"],
        password=os.environ["DB_PASS"],
        db=os.environ["DB_NAME"],
    )

pool = sqlalchemy.create_engine(
    "postgresql+pg8000://",
    creator=get_connection,
)

@app.route("/health")
def health():
    try:
        with pool.connect() as conn:
            conn.execute(sqlalchemy.text("SELECT 1"))
        return jsonify({"status": "ok", "db": "cloud-sql", "connected": True})
    except Exception:
        return jsonify({"status": "error", "connected": False}), 503
Enter fullscreen mode Exit fullscreen mode

Using Private IP (Cloud Run / GKE)

If your app runs on Cloud Run or GKE and uses Cloud SQL with private IP:

// Private IP connection (no connector needed)
const { Pool } = require('pg');

const pool = new Pool({
  host: process.env.DB_HOST, // Private IP, e.g., 10.x.x.x
  database: process.env.DB_NAME,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  ssl: false // Private IP, no SSL needed
});

app.get('/health', async (req, res) => {
  try {
    await pool.query('SELECT 1');
    res.json({ status: 'ok' });
  } catch {
    res.status(503).json({ status: 'error' });
  }
});
Enter fullscreen mode Exit fullscreen mode

Setting Up Vigilmon

  1. Sign up at vigilmon.online
  2. Add Monitor → enter https://your-cloud-run-service.run.app/health
  3. Interval: 1 minute
  4. Keyword check: "connected":true
  5. Enable multi-region checks
  6. Set up email/Slack/PagerDuty alert

Cloud SQL + Cloud Run Monitoring Best Practices

Connection Pool Monitoring

Cloud SQL has connection limits. Monitor your connection pool:

app.get('/health/detailed', async (req, res) => {
  const stats = pool.totalCount;
  res.json({
    status: 'ok',
    pool: {
      total: pool.totalCount,
      idle: pool.idleCount,
      waiting: pool.waitingCount
    }
  });
});
Enter fullscreen mode Exit fullscreen mode

High waitingCount means your app is waiting for database connections — a sign you need to scale Cloud SQL or optimize queries.

Alerting Thresholds

Metric Normal Alert
Health endpoint response time < 500ms > 2000ms
Health check success rate 100% < 99.5%
SSL cert expiry > 30 days < 14 days

Conclusion

Cloud SQL is reliable — but your application's connectivity to it isn't guaranteed. Vigilmon monitors your Cloud SQL-connected services externally, alerting you the moment the connection breaks.

Start monitoring at vigilmon.online.

Top comments (0)