-
Notifications
You must be signed in to change notification settings - Fork 6
/
router.go
68 lines (56 loc) · 1.41 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package metrics
import (
"fmt"
"math/rand"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
krakendgin "github.com/devopsfaith/krakend/router/gin"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/_integrations/nrgin/v1"
)
var errNoApp = fmt.Errorf("No NewRelic app defined")
// Middleware adds NewRelic middleware
func Middleware() (gin.HandlerFunc, error) {
if app == nil {
return emptyMW, errNoApp
}
if app.Config.InstrumentationRate == 0 {
return emptyMW, nil
}
nrMiddleware := nrgin.Middleware(app)
if app.Config.InstrumentationRate == 100 {
return nrMiddleware, nil
}
rate := float64(app.Config.InstrumentationRate) / 100.0
next := make(chan float64, 1000)
go func(out chan<- float64) {
for {
out <- rand.Float64()
}
}(next)
return func(c *gin.Context) {
if n := <-next; n <= rate {
nrMiddleware(c)
return
}
emptyMW(c)
}, nil
}
// HandlerFactory includes NewRelic transaction specific configuration endpoint naming
func HandlerFactory(handlerFactory krakendgin.HandlerFactory) krakendgin.HandlerFactory {
if app == nil {
return handlerFactory
}
return func(conf *config.EndpointConfig, p proxy.Proxy) gin.HandlerFunc {
handler := handlerFactory(conf, p)
return func(c *gin.Context) {
if txn := nrgin.Transaction(c); txn != nil {
txn.SetName(conf.Endpoint)
}
handler(c)
}
}
}
func emptyMW(c *gin.Context) {
c.Next()
}