DEV Community

Vigilmon
Vigilmon

Posted on

How to Monitor Your Go (Golang) API with Vigilmon

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)
}
Enter fullscreen mode Exit fullscreen mode

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\"})
})
Enter fullscreen mode Exit fullscreen mode

Step 3: Connect Vigilmon

  1. Sign up at vigilmon.online
  2. Add Monitor -> HTTP Monitor
  3. URL: https://your-go-api.com/health
  4. Interval: 1 minute
  5. Expected status: 200
  6. 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

  1. Add /health route checking DB, Redis, and dependencies
  2. Return 200 when healthy, 503 when degraded
  3. Connect Vigilmon for external 1-minute probes
  4. 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)