A lightweight Prometheus monitoring middleware for Go HTTP applications using Gorilla Mux. Provides comprehensive metrics collection for HTTP requests, system resources (CPU/Memory), and custom metrics.
- π HTTP Request Metrics: Request count, duration, body size, slow requests
- π» System Metrics: CPU usage (user, system, idle) and memory utilization
- π₯ Unique Visitor Tracking: Built-in Bloom filter for UV counting
- π― Custom Metrics: Support for Counter, Gauge, Histogram, and Summary types
- β‘ Lightweight: Minimal dependencies and overhead
- π§ Configurable: Customizable metric paths, slow request thresholds, and buckets
go get github.com/bolanosdev/prometheus-mux-monitorpackage main
import (
"net/http"
"github.com/gorilla/mux"
prometheus "github.com/bolanosdev/prometheus-mux-monitor"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
r := mux.NewRouter()
// Get the monitor instance
monitor := prometheus.GetMonitor()
// Apply the monitoring interceptor
r.Use(monitor.Interceptor)
// Expose metrics endpoint
r.Handle("/metrics", promhttp.Handler())
// Your routes
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
})
http.ListenAndServe(":8080", r)
}monitor := prometheus.GetMonitor()
monitor.SetMetricPath("/custom-metrics")monitor := prometheus.GetMonitor()
monitor.SetSlowTime(10) // Requests slower than 10 secondsmonitor := prometheus.GetMonitor()
monitor.SetDuration([]float64{0.1, 0.5, 1.0, 2.0, 5.0, 10.0})monitor := prometheus.GetMonitor()
monitor.SetExcludePaths([]string{"/health", "/ping"})monitor := prometheus.GetMonitor()
monitor.SetMetricPrefix("myapp_")
monitor.SetMetricSuffix("_total")request_total: Total number of HTTP requestsrequest_uv: Unique visitors (client IPs)request_uv_total: Total unique visitors counturi_request_total: Requests per URI, method, and status coderequest_body_total: Total request body bytes receivedresponse_body_total: Total response body bytes sentrequest_duration: Request duration histogramslow_request_total: Count of slow requests (above threshold)
cpu_user_total: CPU time consumed by user processescpu_system_total: CPU time consumed by system processescpu_idle_total: CPU idle timemem_used_total: Memory usage percentagemem_cached_total: Cached memory percentage
monitor := prometheus.GetMonitor()
err := monitor.AddMetric(&prometheus.Metric{
Type: prometheus.Counter,
Name: "custom_counter",
Description: "Description of counter",
Labels: []string{"label1", "label2"},
})
// Increment counter
metric := monitor.GetMetric("custom_counter")
metric.Inc([]string{"value1", "value2"})
// Add specific value
metric.Add([]string{"value1", "value2"}, 5.0)monitor.AddMetric(&prometheus.Metric{
Type: prometheus.Gauge,
Name: "custom_gauge",
Description: "Description of gauge",
Labels: []string{"label1"},
})
metric := monitor.GetMetric("custom_gauge")
metric.SetGaugeValue([]string{"value1"}, 42.0)monitor.AddMetric(&prometheus.Metric{
Type: prometheus.Histogram,
Name: "custom_histogram",
Description: "Description of histogram",
Labels: []string{"label1"},
Buckets: []float64{0.1, 0.5, 1.0, 5.0, 10.0},
})
metric := monitor.GetMetric("custom_histogram")
metric.Observe([]string{"value1"}, 2.5)monitor.AddMetric(&prometheus.Metric{
Type: prometheus.Summary,
Name: "custom_summary",
Description: "Description of summary",
Labels: []string{"label1"},
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
})
metric := monitor.GetMetric("custom_summary")
metric.Observe([]string{"value1"}, 3.7)- Go 1.23 or higher
make buildmake testmake coveragemake fmtmake lintmake allmake test- Run all testsmake build- Build the projectmake coverage- Generate coverage report (HTML)make fmt- Format code with gofmtmake vet- Run go vetmake lint- Run golangci-lintmake clean- Remove build artifactsmake install- Install dependenciesmake all- Run fmt, vet, test, and buildmake help- Show available targets
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a list of changes.
Built with:
- Gorilla Mux - HTTP router
- Prometheus Go Client - Prometheus instrumentation
- go-osstat - System statistics