github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/lib/metricbeat/metric.go (about) 1 package metricbeat 2 3 import ( 4 "expvar" 5 "fmt" 6 "net/http" 7 ) 8 9 func metricsHandler(w http.ResponseWriter, r *http.Request) { 10 w.Header().Set("Content-Type", "application/json; charset=utf-8") 11 12 first := true 13 report := func(key string, value interface{}) { 14 if !first { 15 fmt.Fprintf(w, ",\n") 16 } 17 first = false 18 if str, ok := value.(string); ok { 19 fmt.Fprintf(w, "%q: %q", key, str) 20 } else { 21 fmt.Fprintf(w, "%q: %v", key, value) 22 } 23 } 24 25 fmt.Fprintf(w, "{\n") 26 expvar.Do(func(kv expvar.KeyValue) { 27 report(kv.Key, kv.Value) 28 }) 29 fmt.Fprintf(w, "\n}\n") 30 } 31 32 func StartMetricBeat() { 33 mux := http.NewServeMux() 34 mux.HandleFunc("/debug/vars", metricsHandler) 35 http.ListenAndServe("localhost:6060", mux) 36 }