Skip to content

Commit

Permalink
Use prometheus.Timer instead of timing manually
Browse files Browse the repository at this point in the history
This is a little more idiomatic / elegant than doing the timing manually
  • Loading branch information
richardTowers committed Aug 29, 2023
1 parent 7783d14 commit 624060b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ type mongoDatabase interface {
// create a new proxy mux, load applications (backends) and routes into it, and
// then flip the "mux" pointer in the Router.
func (rt *Router) reloadRoutes(db *mgo.Database, currentOptime bson.MongoTimestamp) {
startTime := time.Now()
var success bool
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
labels := prometheus.Labels{"success": strconv.FormatBool(success)}
routeReloadDurationMetric.With(labels).Observe(v)
}))
defer func() {
success := true
success = true
if r := recover(); r != nil {
success = false
logWarn("router: recovered from panic in reloadRoutes:", r)
Expand All @@ -227,8 +231,7 @@ func (rt *Router) reloadRoutes(db *mgo.Database, currentOptime bson.MongoTimesta
} else {
rt.mongoReadToOptime = currentOptime
}
labels := prometheus.Labels{"success": strconv.FormatBool(success)}
routeReloadDurationMetric.With(labels).Observe(time.Since(startTime).Seconds())
timer.ObserveDuration()
}()

logInfo("router: reloading routes")
Expand Down

0 comments on commit 624060b

Please sign in to comment.