Skip to content
Permalink
Browse files

Merge pull request #1 from matthewmcneely/master

This PR reworks protection of the PathMap
  • Loading branch information...
Depado committed Nov 21, 2018
2 parents 65cd873 + f365f27 commit be32bd0fa912d77d50b9a1d201e66a71b5906ab7
Showing with 38 additions and 2 deletions.
  1. +4 −2 prom.go
  2. +34 −0 prom_test.go
@@ -95,11 +95,11 @@ func New(options ...func(*Prometheus)) *Prometheus {
}

func (p *Prometheus) update() {
p.PathMap.Lock()
p.Ignored.RLock()
if p.PathMap.values == nil {
p.PathMap.values = make(map[string]string)
}
p.PathMap.Lock()
p.Ignored.RLock()
defer func() {
p.PathMap.Unlock()
p.Ignored.RUnlock()
@@ -164,7 +164,9 @@ func (p *Prometheus) register() {
// single handler
func (p *Prometheus) Instrument() gin.HandlerFunc {
return func(c *gin.Context) {
p.PathMap.RLock()
if p.PathMap.values == nil {
p.PathMap.RUnlock()
p.update()
}
var path string
@@ -3,6 +3,7 @@ package ginprom
import (
"fmt"
"net/http"
"sync"
"testing"

"github.com/appleboy/gofight"
@@ -113,6 +114,39 @@ func TestInstrument(t *testing.T) {
assert.Contains(t, r.Body.String(), lpath, "path must be present in the response")
assert.NotContains(t, r.Body.String(), `path="/user/10"`, "raw path must not be present")
})

unregister(p)
}

func TestThreadedInstrument(t *testing.T) {
r := gin.New()
p := New(Engine(r))
r.Use(p.Instrument())
path := "/user/:id"
lpath := fmt.Sprintf(`path="%s"`, path)

r.GET(path, func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"id": c.Param("id")})
})

var wg sync.WaitGroup
for n := 0; n < 10; n++ {
go func(wg *sync.WaitGroup) {
g := gofight.New()

g.GET("/user/10").Run(r, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { assert.Equal(t, http.StatusOK, r.Code) })

g.GET(p.MetricsPath).Run(r, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
assert.Contains(t, r.Body.String(), fmt.Sprintf("%s_requests_total", p.Subsystem))
assert.Contains(t, r.Body.String(), lpath, "path must be present in the response")
assert.NotContains(t, r.Body.String(), `path="/user/10"`, "raw path must not be present")
})
wg.Done()
}(&wg)
wg.Add(1)
}
wg.Wait()
unregister(p)
}

0 comments on commit be32bd0

Please sign in to comment.
You can’t perform that action at this time.