Skip to content

Commit

Permalink
[server] Avoiding Prometheus memory bomb when using the new stdlib ro…
Browse files Browse the repository at this point in the history
…uter (#223)

* no longer using prom instrumentation if using the stdlib router

* docs and downhill updates
  • Loading branch information
jprobinson committed Jul 31, 2019
1 parent f170390 commit c445d54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
// The current available types are 'gorilla' to use the Gorilla tool kit mux and
// 'stdlib' to use the http package's ServeMux.
// If empty, this will default to 'gorilla'.
// NOTE: If 'stdlib' is used, Prometheus monitoring will be disabled for performance
// reasons.
RouterType string `envconfig:"GIZMO_ROUTER_TYPE"`

// JSONContentType can be used to override the default JSONContentType.
Expand Down
28 changes: 18 additions & 10 deletions server/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ func (s *SimpleServer) safelyExecuteRequest(w http.ResponseWriter, r *http.Reque

// lookup metric name if we can
registeredPath := r.URL.Path
if muxr, ok := s.mux.(*GorillaRouter); ok {
registeredPath = "__404__"
var match mux.RouteMatch
if muxr.mux.Match(r, &match) && match.MatchErr == nil {
if tmpl, err := match.Route.GetPathTemplate(); err == nil {
registeredPath = tmpl
}
}
muxr, ok := s.mux.(*GorillaRouter)
if !ok {
// if we cant look up the metric name, dont bother. it'll use too much memory.
s.h.ServeHTTP(w, r)
return
}

registeredPath = "__404__"
var match mux.RouteMatch
if muxr.mux.Match(r, &match) && match.MatchErr == nil {
if tmpl, err := match.Route.GetPathTemplate(); err == nil {
registeredPath = tmpl
}
}
registeredPath = strings.TrimPrefix(registeredPath, "/")
prometheus.InstrumentHandlerWithOpts(
prometheus.SummaryOpts{
Expand All @@ -119,8 +123,12 @@ func (s *SimpleServer) Start() error {
if s.cfg.MetricsPath == "" {
s.cfg.MetricsPath = "/metrics"
}
s.mux.HandleFunc("GET", s.cfg.MetricsPath,
prometheus.InstrumentHandler("prometheus", prometheus.UninstrumentedHandler()))
// only add the instrument handler if the proper router is enabled
_, ok := s.mux.(*GorillaRouter)
if ok {
s.mux.HandleFunc("GET", s.cfg.MetricsPath,
prometheus.InstrumentHandler("prometheus", prometheus.UninstrumentedHandler()))
}

wrappedHandler, err := NewAccessLogMiddleware(s.cfg.HTTPAccessLog, s)
if err != nil {
Expand Down

0 comments on commit c445d54

Please sign in to comment.