Prometric-go

A lightweight and configurable Prometheus instrumentation library for Go web applications. Add standardized HTTP, health, and business-level metrics with minimal code.

Quick Start
$ go get github.com/peek8/prometric-go/prometrics

Everything you need for observability

Comprehensive metrics collection with plug-and-play middleware for net/http and Gin frameworks.

🌐

HTTP Middleware

Automatically instrument all your HTTP handlers with comprehensive request metrics.

  • http_requests_total
  • http_request_duration_seconds
  • http_in_flight_requests
  • http_request_size_bytes
  • http_response_size_bytes
💓

Health Middleware

Monitor your application's health with real-time system metrics and resource usage.

  • app_uptime_seconds
  • app_cpu_usage_percent
  • app_allocated_memory
  • app_go_routines
  • app_garbage_collections_count
📦

CRUD Monitoring

Track business-level operations with utility functions for object lifecycle metrics.

  • crud_operations_total
  • object_operation_duration_seconds
  • object_count

Get started in minutes

Simple integration with your existing Go web framework of choice.

Go
package main

import (
    "net/http"
    "time"

    "github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/peek8/prometric-go/prometrics"
)

func createPerson(w http.ResponseWriter, r *http.Request) {
    // Track the person create operation
    defer prometrics.TrackCRUD("person", "Create")(time.Now())
    prometrics.IncObjectCount("person")

    // Simulate creation delay
    time.Sleep(200 * time.Millisecond)
    w.Write([]byte("Person created"))
}

func main() {
    mux := http.NewServeMux()

    // Wrap your business handler
    mux.Handle("/person", prometrics.InstrumentHttpHandler("person_handler",
        http.HandlerFunc(createPerson)))

    // Use HealthMiddleware at /metrics endpoint
    mux.Handle("/metrics", prometrics.HealthMiddleware(promhttp.Handler()))

    http.ListenAndServe(":8080", mux)
}
Go
package main

import (
    "time"

    "github.com/gin-gonic/gin"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/peek8/prometric-go/prometrics"
)

func main() {
    r := gin.Default()
    r.Use(prometrics.GinMiddleware())
    r.Use(prometrics.GinHealthMiddleware())

    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{"msg": "pong"})
    })

    r.GET("/person", func(c *gin.Context) {
        defer prometrics.TrackCRUD("person", "Get")(time.Now())
        c.JSON(200, gin.H{"name": "asraf"})
    })

    r.POST("/person", func(c *gin.Context) {
        defer prometrics.TrackCRUD("person", "create")(time.Now())
        prometrics.IncObjectCount("person")

        time.Sleep(200 * time.Millisecond)
        c.JSON(201, gin.H{"status": "created"})
    })

    r.GET("/metrics", gin.WrapH(promhttp.Handler()))
    r.Run(":7080")
}
Go
// Collect health metrics at regular intervals
// instead of only on endpoint requests

ctx, cancel := context.WithCancel(context.Background(), 10)
go prometrics.CollectSystemMetricsLoop(ctx)

// Cancel when needed:
// cancel()

Works with your stack

100% compatible with the Prometheus ecosystem and your favorite tools.

🔥

Prometheus

Native client library integration

📈

Grafana

Beautiful dashboards ready

🌐

net/http

Standard library support

🍸

Gin

First-class middleware

Ready to add metrics?

Get started with prometric-go in under 5 minutes. Full documentation available on pkg.go.dev.

Read the Docs View on GitHub