Skip to content
Permalink
Browse files

Don't panic when the router has not been set

  • Loading branch information...
Depado committed Apr 20, 2018
1 parent e35fa08 commit ca112af1cf163cd379cc9519e81240100c8c9274
Showing with 39 additions and 5 deletions.
  1. +19 −1 README.md
  2. +6 −4 prom.go
  3. +14 −0 prom_test.go
@@ -34,6 +34,7 @@ import (
func main() {
r := gin.Default()
p := ginprom.New(
ginprom.Engine(r),
ginprom.Subsystem("gin"),
ginprom.Path("/metrics"),
)
@@ -61,4 +62,21 @@ Saves a call to `Use(e *gin.Engine)`
Default : `nil`

`Ignore(paths ...string)`
Specify which paths should not be taken into account by the middleware.
Specify which paths should not be taken into account by the middleware.

## Troubleshooting

### The instrumentation doesn't seem to work

Make sure you have set the `gin.Engine` in the `ginprom` middleware, either when
initializing it using `ginprom.New(ginprom.Engine(r))` or using the `Use`
function after the initialization like this :

```go
p := ginprom.New(
ginprom.Subsystem("gin"),
ginprom.Path("/metrics"),
)
p.Use(r)
r.Use(p.Instrument())
```
10 prom.go
@@ -104,11 +104,13 @@ func (p *Prometheus) update() {
p.PathMap.Unlock()
p.Ignored.RUnlock()
}()
for _, ri := range p.Engine.Routes() {
if _, ok := p.Ignored.values[ri.Path]; ok {
continue
if p.Engine != nil {
for _, ri := range p.Engine.Routes() {
if _, ok := p.Ignored.values[ri.Path]; ok {
continue
}
p.PathMap.values[ri.Handler] = ri.Path
}
p.PathMap.values[ri.Handler] = ri.Path
}
}

@@ -116,6 +116,20 @@ func TestInstrument(t *testing.T) {
unregister(p)
}

func TestEmptyRouter(t *testing.T) {
r := gin.New()
p := New()

r.Use(p.Instrument())
r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) })

g := gofight.New()
assert.NotPanics(t, func() {
g.GET("/").Run(r, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {})
})
unregister(p)
}

func TestIgnore(t *testing.T) {
r := gin.New()
ipath := "/ping"

0 comments on commit ca112af

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