How to Monitor Your Go API with Vigilmon
Go is beloved for its performance and simplicity, but even a well-written Go API can become unreachable due to infrastructure failures, network issues, or downstream dependency outages.
This guide shows how to build production-grade health check endpoints in Go and connect them to Vigilmon for external uptime monitoring.
Why Go APIs Need External Monitoring
External monitoring catches failures that Go internal logging cannot:
- Process dying from a panic and not restarting
- OOM kills from the kernel
- Network infrastructure failures
- DNS resolution failures
- SSL certificate expiry
- Upstream database or cache unavailability
Step 1: Health Check with net/http
package main
import (
\"database/sql\"
\"encoding/json\"
\"net/http\"
\"time\"
\"context\"
)
type HealthResponse struct {
Status string `json:\"status\"`
Checks map[string]string `json:\"checks\"`
}
func healthHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
checks := make(map[string]string)
allHealthy := true
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
checks[\"database\"] = \"error\"
allHealthy = false
} else {
checks[\"database\"] = \"ok\"
}
statusCode := http.StatusOK
status := \"healthy\"
if !allHealthy {
status = \"unhealthy\"
statusCode = http.StatusServiceUnavailable
}
w.Header().Set(\"Content-Type\", \"application/json\")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(HealthResponse{Status: status, Checks: checks})
}
}
func main() {
db, _ := sql.Open(\"postgres\", \"postgresql://...\")
mux := http.NewServeMux()
mux.HandleFunc(\"/health\", healthHandler(db))
http.ListenAndServe(\":8080\", mux)
}
Step 2: Gin Framework
r.GET(\"/health\", func(c *gin.Context) {
sqlDB, err := db.DB()
if err != nil || sqlDB.Ping() != nil {
c.JSON(503, gin.H{\"status\": \"unhealthy\"})
return
}
c.JSON(200, gin.H{\"status\": \"healthy\"})
})
Step 3: Connect Vigilmon
- Sign up at vigilmon.online
- Add Monitor -> HTTP Monitor
- URL:
https://your-go-api.com/health - Interval: 1 minute
- Expected status: 200
- Configure email/Slack alerts
Also add an SSL Certificate Monitor for your domain.
Failure Coverage
| Failure | Go Logs | Vigilmon |
|---|---|---|
| Panic crash | Last log only | Yes - immediate alert |
| OOM kill | Silent | Yes - immediate alert |
| DB exhaustion | Errors | Yes - health endpoint |
| DNS failure | Silent | Yes - external probe |
| SSL expiry | Silent | Yes - SSL monitor |
Summary
- Add
/healthroute checking DB, Redis, and dependencies - Return 200 when healthy, 503 when degraded
- Connect Vigilmon for external 1-minute probes
- Add SSL monitoring for your domain
Go gives you speed and safety. Vigilmon gives you visibility when infrastructure fails.
Monitor your Go API free at vigilmon.online
Top comments (0)