Skip to content

Commit

Permalink
Exporting functions to allow users to have more control on where they…
Browse files Browse the repository at this point in the history
… set the middleware and run engine
  • Loading branch information
Marco Vazquez committed Aug 1, 2018
1 parent 3f93884 commit 65d6c75
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *gin.Eng
}
}

func (p *Prometheus) setMetricsPath(e *gin.Engine) {
// SetMetricsPath set metrics paths
func (p *Prometheus) SetMetricsPath(e *gin.Engine) {

if p.listenAddress != "" {
p.router.GET(p.MetricsPath, prometheusHandler())
Expand All @@ -185,7 +186,8 @@ func (p *Prometheus) setMetricsPath(e *gin.Engine) {
}
}

func (p *Prometheus) setMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts) {
// SetMetricsPathWithAuth set metrics paths with authentication
func (p *Prometheus) SetMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts) {

if p.listenAddress != "" {
p.router.GET(p.MetricsPath, gin.BasicAuth(accounts), prometheusHandler())
Expand Down Expand Up @@ -338,17 +340,18 @@ func (p *Prometheus) registerMetrics(subsystem string) {

// Use adds the middleware to a gin engine.
func (p *Prometheus) Use(e *gin.Engine) {
e.Use(p.handlerFunc())
p.setMetricsPath(e)
e.Use(p.HandlerFunc())
p.SetMetricsPath(e)
}

// UseWithAuth adds the middleware to a gin engine with BasicAuth.
func (p *Prometheus) UseWithAuth(e *gin.Engine, accounts gin.Accounts) {
e.Use(p.handlerFunc())
p.setMetricsPathWithAuth(e, accounts)
e.Use(p.HandlerFunc())
p.SetMetricsPathWithAuth(e, accounts)
}

func (p *Prometheus) handlerFunc() gin.HandlerFunc {
// HandlerFunc defines handler function for middleware
func (p *Prometheus) HandlerFunc() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.URL.String() == p.MetricsPath {
c.Next()
Expand Down

0 comments on commit 65d6c75

Please sign in to comment.