Skip to content

Commit

Permalink
Fixing concurrency access to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Oct 31, 2017
1 parent 16930c4 commit 0c28c9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -17,7 +17,6 @@ Simply run :
## Differences with go-gin-prometheus ## Differences with go-gin-prometheus


- No support for Prometheus' Push Gateway - No support for Prometheus' Push Gateway
- Need to call middleware on each route
- Options on constructor - Options on constructor
- Adds a `path` label to get the matched route - Adds a `path` label to get the matched route


Expand All @@ -34,9 +33,10 @@ import (
func main() { func main() {
r := gin.Default() r := gin.Default()
p := ginprom.New(ginprom.Subsystem("gin"), ginprom.Path("/metrics"), ginprom.Engine(r)) p := ginprom.New(ginprom.Subsystem("gin"), ginprom.Path("/metrics"), ginprom.Engine(r))
r.Use(p.Instrument())


r.GET("/hello/:id", p.Instrument("/hello/:id"), func(c *gin.Context) {}) r.GET("/hello/:id", func(c *gin.Context) {})
r.GET("/world/:id", p.Instrument("/world/:id"), func(c *gin.Context) {}) r.GET("/world/:id", func(c *gin.Context) {})
r.Run("127.0.0.1:8080") r.Run("127.0.0.1:8080")
} }
``` ```
Expand Down
38 changes: 27 additions & 11 deletions prom.go
Expand Up @@ -2,6 +2,7 @@ package ginprom


import ( import (
"strconv" "strconv"
"sync"
"time" "time"


"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
Expand All @@ -12,6 +13,11 @@ import (
var defaultPath = "/metrics" var defaultPath = "/metrics"
var defaultSys = "gin" var defaultSys = "gin"


type pmap struct {
sync.RWMutex
values map[string]string
}

// Prometheus contains the metrics gathered by the instance and its path // Prometheus contains the metrics gathered by the instance and its path
type Prometheus struct { type Prometheus struct {
reqCnt *prometheus.CounterVec reqCnt *prometheus.CounterVec
Expand All @@ -20,7 +26,7 @@ type Prometheus struct {
MetricsPath string MetricsPath string
Subsystem string Subsystem string
Engine *gin.Engine Engine *gin.Engine
PathMap map[string]string PathMap pmap
} }


// Path is an option allowing to set the metrics path when intializing with New. // Path is an option allowing to set the metrics path when intializing with New.
Expand Down Expand Up @@ -66,14 +72,31 @@ func New(options ...func(*Prometheus)) *Prometheus {
if p.Engine != nil { if p.Engine != nil {
p.Engine.GET(p.MetricsPath, prometheusHandler()) p.Engine.GET(p.MetricsPath, prometheusHandler())
} }
p.PathMap = make(map[string]string) p.PathMap.values = make(map[string]string)
return p return p
} }


func (p *Prometheus) updatePathMap() { func (p *Prometheus) updatePathMap() {
p.PathMap.Lock()
defer p.PathMap.Unlock()
for _, ri := range p.Engine.Routes() { for _, ri := range p.Engine.Routes() {
p.PathMap[ri.Handler] = ri.Path p.PathMap.values[ri.Handler] = ri.Path
}
}

func (p *Prometheus) getPathFromHandler(handler string) string {
p.PathMap.RLock()
defer p.PathMap.RUnlock()
if in, ok := p.PathMap.values[handler]; ok {
return in
}
p.PathMap.RUnlock()
p.updatePathMap()
p.PathMap.RLock()
if in, ok := p.PathMap.values[handler]; ok {
return in
} }
return ""
} }


func (p *Prometheus) register() { func (p *Prometheus) register() {
Expand Down Expand Up @@ -129,14 +152,7 @@ func (p *Prometheus) Instrument() gin.HandlerFunc {
return return
} }


if in, ok := p.PathMap[c.HandlerName()]; ok { path = p.getPathFromHandler(c.HandlerName())
path = in
} else {
p.updatePathMap()
if in, ok := p.PathMap[c.HandlerName()]; ok {
path = in
}
}


c.Next() c.Next()


Expand Down

0 comments on commit 0c28c9e

Please sign in to comment.