Skip to content

Instantly share code, notes, and snippets.

@flowerinthenight
Last active November 15, 2019 10:28
Show Gist options
  • Save flowerinthenight/b6e639978dc2c21042ccea526700f214 to your computer and use it in GitHub Desktop.
Save flowerinthenight/b6e639978dc2c21042ccea526700f214 to your computer and use it in GitHub Desktop.
Serving expvar when using gorilla/mux.
package main
import (
"expvar"
"log"
"net/http"
"github.com/gorilla/mux"
)
var counter *expvar.Int
func init() {
counter = expvar.NewInt("counter")
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!\n"))
counter.Add(1)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", rootHandler)
r.Handle("/debug/vars", http.DefaultServeMux)
log.Fatal(http.ListenAndServe(":8000", r))
}
@RobHumphris
Copy link

RobHumphris commented Nov 15, 2019

If you use Mux.Handle("/debug/{*}", http.DefaultServeMux) then as well as the /debug/vars routes the traces

  • /debug/requests
  • /debug/events

Routes are also served.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment