Go Prometheus is a package that allows you to integrate Prometheus, a popular open-source monitoring and alerting toolkit, into your golang applications. Prometheus is widely used for monitoring various aspects of software systems, including metrics, time series data, and alerting. By default this package aggregates metrics in memory, and it can also persist metrics to Redis when you need to share state across multiple processes.
Get Go Prometheus package on your project:
go get github.com/tech-djoin/go-prometheusThis packages provides a middleware using Echo which can be added as a global middleware or as a single route.
// in server file or anywhere middleware should be registered
e.Use(goprometheus.MetricCollector())// in route file or anywhere route should be registered
router.Echo.GET("api/v1/posts", handler, goprometheus.MetricCollector())To exposes all metrics gathered by collectors, you need a route to access all metrics.
router.Echo.GET("/metrics", echo.WrapHandler(promhttp.Handler()))If you want to aggregate metrics across several application instances you can switch to the Redis-backed storage. Provide a Redis client (from github.com/redis/go-redis/v9) and install the backend before you start emitting metrics:
import (
"log"
"github.com/prometheus/client_golang/prometheus"
goprometheus "github.com/tech-djoin/go-prometheus"
)
func init() {
backend, err := goprometheus.NewRedisBackend(goprometheus.RedisBackendOptions{
KeyPrefix: "app_metrics", // optional; defaults to no prefix
})
if err != nil {
log.Fatalf("new redis backend: %v", err)
}
// Use Redis for recording metrics and register a collector to expose them.
goprometheus.UseBackend(backend)
prometheus.MustRegister(goprometheus.NewRedisCollectorFromBackend(backend))
}When RedisBackendOptions.Client is omitted, the package reads its connection details from the following environment variables:
GOPROM_REDIS_ADDR(optional) – fullhost:portvalue when you want to specify the address directly.GOPROM_REDIS_HOSTandGOPROM_REDIS_PORT– used whenGOPROM_REDIS_ADDRis not set (default127.0.0.1:6379).GOPROM_REDIS_USERNAMEandGOPROM_REDIS_PASSWORD– used when your Redis deployment requires credentials.
Metrics are stored in a set of Redis hashes keyed by the prefix you provide. The default latency histogram buckets (goprometheus.DefaultLatencyBuckets) are reused unless you override them in RedisBackendOptions.
This project is licensed under the MIT License - see the LICENSE.md file for details.