From 921067123653264066dbfdbb84a8073e936950cf Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 20 Oct 2022 12:05:05 +0300 Subject: [PATCH] Adding support for custom formatter and showing the tracked object pointer --- res_tracker.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/res_tracker.go b/res_tracker.go index bf2bb77..cc03899 100644 --- a/res_tracker.go +++ b/res_tracker.go @@ -22,6 +22,7 @@ const FULL = 2 var mode int32 = OFF var res2track = &sync.Map{} +var customFormatter = func(item *TrackReportItem) string { return "," } type TrackedInfo struct { Time time.Time @@ -29,7 +30,7 @@ type TrackedInfo struct { } type TrackReportItem struct { - T reflect.Type + Obj interface{} Duration time.Duration Stack string } @@ -42,6 +43,10 @@ func InitHandlers() { http.HandleFunc("/res_tracker/status", statusHandler) } +func SetCustomFormatter(f func(item *TrackReportItem) string) { + customFormatter = f +} + func onHandler(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() m := query.Get("m") @@ -135,7 +140,6 @@ func FormatReportItems() []TrackReportItem { k := snapshot[i] v := snapshot[i+1] - t := reflect.TypeOf(k) info, _ := v.(TrackedInfo) stackStr := string(info.Stack) @@ -160,7 +164,7 @@ func FormatReportItems() []TrackReportItem { } } - items = append(items, TrackReportItem{T: t, Duration: time.Now().Sub(info.Time), Stack: stackStr}) + items = append(items, TrackReportItem{Obj: k, Duration: time.Now().Sub(info.Time), Stack: stackStr}) } //3. let's sort items by duration descending @@ -181,7 +185,7 @@ func ReportLines() []string { lines = append(lines, fmt.Sprintf("=== Tracked connections report %s (total: %d) ===", now.Format("01-02-2006 15:04:05"), len(items))) for idx, item := range items { //TODO: obtain somehow an actual pointer of the object or provide a hook for a custom formatter - lines = append(lines, fmt.Sprintf("#%d Tracked (%v, duration %v) : %s", idx+1, item.T, item.Duration, item.Stack)) + lines = append(lines, fmt.Sprintf("#%d Tracked (%v %p%s duration %v) : %s", idx+1, reflect.TypeOf(item.Obj), item.Obj, customFormatter(&item), item.Duration, item.Stack)) } return lines }