Skip to content

Commit

Permalink
add PromMetricsGinHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Sep 6, 2020
1 parent 314e554 commit c412ba5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 31 deletions.
70 changes: 42 additions & 28 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,42 @@ import (
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)

var (
// GinPromNamespace gin prometheus namespace
GinPromNamespace = "logging_prom_gin"
// GinPromLabels gin prometheus labels
GinPromLabels = []string{
// prometheus namespace
promNamespace = "logging"
// gin prometheus labels
promGinLabels = []string{
"status_code",
"path",
"method",
}
promGinUptime = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: GinPromNamespace,
Name: "uptime",
Help: "server uptime",
Namespace: promNamespace,
Name: "server_uptime",
Help: "gin server uptime in seconds",
}, nil,
)
promGinReqCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: GinPromNamespace,
Name: "request_count",
Help: "http request count",
}, GinPromLabels,
Namespace: promNamespace,
Name: "req_count",
Help: "gin server request count",
}, promGinLabels,
)
promGinReqLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: GinPromNamespace,
Name: "request_latency",
Help: "http request latency in seconds",
}, GinPromLabels,
Namespace: promNamespace,
Name: "req_latency",
Help: "gin server request latency in seconds",
}, promGinLabels,
)
)

// registers the prometheus metrics
func init() {
if err := prometheus.Register(promGinUptime); err != nil {
Error(nil, "Register prometheus gin uptime error:"+err.Error())
}
if err := prometheus.Register(promGinReqCount); err != nil {
Error(nil, "Register prometheus gin request count error:"+err.Error())
}
if err := prometheus.Register(promGinReqLatency); err != nil {
Error(nil, "Register prometheus gin request latency error:"+err.Error())
}
}

// GetGinTraceIDFromHeader 从 gin 的 request header 中获取 key 为 TraceIDKeyname 的值作为 traceid
func GetGinTraceIDFromHeader(c *gin.Context) string {
return c.Request.Header.Get(string(TraceIDKeyname))
Expand Down Expand Up @@ -426,3 +414,29 @@ func (w responseBodyWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}

// PromMetricsGinHandler handler of the prometheus metrics exporter
// not a middleware
func PromMetricsGinHandler(collectors ...prometheus.Collector) gin.HandlerFunc {
if err := prometheus.Register(promGinUptime); err != nil {
Error(nil, "Register prometheus gin uptime error:"+err.Error())
}
if err := prometheus.Register(promGinReqCount); err != nil {
Error(nil, "Register prometheus gin request count error:"+err.Error())
}
if err := prometheus.Register(promGinReqLatency); err != nil {
Error(nil, "Register prometheus gin request latency error:"+err.Error())
}
for _, collector := range collectors {
if err := prometheus.Register(collector); err != nil {
Error(nil, "Register collector error:"+err.Error())
}
}
// uptime
go func() {
for range time.Tick(time.Second) {
promGinUptime.WithLabelValues().Inc()
}
}()
return gin.WrapH(promhttp.Handler())
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ go 1.13
require (
github.com/getsentry/sentry-go v0.6.0
github.com/gin-gonic/gin v1.6.2
github.com/golang/protobuf v1.4.0 // indirect
github.com/jinzhu/gorm v1.9.12
github.com/json-iterator/go v1.1.9
github.com/json-iterator/go v1.1.10
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.7.1
github.com/rs/xid v1.2.1
go.uber.org/zap v1.15.0
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc // indirect
google.golang.org/appengine v1.6.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
Expand Down
Loading

0 comments on commit c412ba5

Please sign in to comment.