Skip to content

Commit

Permalink
prometheus: fix prometheus metrics if not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Dec 2, 2023
1 parent 4d51a91 commit 513b52b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 3 additions & 0 deletions cmd/whawty-nginx-sso/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@ func (m *MetricsHandler) run() {
}

func (m *MetricsHandler) reg() prometheus.Registerer {
if m.registry == nil {
return nil
}
return prometheus.WrapRegistererWithPrefix(m.namespace+"_", m.registry)
}
17 changes: 9 additions & 8 deletions cmd/whawty-nginx-sso/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,16 @@ func runWeb(config *WebConfig, prom *MetricsHandler, cookies *cookie.Store, auth
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusSeeOther, path.Join(h.getBasePath(c), "login")) })
r.StaticFS("/ui/", ui.StaticAssets)
prom.install(r)
if err = prom.reg().Register(webRequests); err != nil {
return
}
if err = prom.reg().Register(webRequestDuration); err != nil {
return
}

g := r.Group("/")
g.Use(promMiddleware())
if reg := prom.reg(); reg != nil {
g.Use(promMiddleware())
if err = reg.Register(webRequests); err != nil {
return
}
if err = reg.Register(webRequestDuration); err != nil {
return
}
}
g.GET("/auth", h.handleAuth)
g.GET("/login", h.handleLoginGet)
g.POST("/login", h.handleLoginPost)
Expand Down

0 comments on commit 513b52b

Please sign in to comment.