2022-09-13 16:03:40 +03:00
|
|
|
package res_tracker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"runtime/debug"
|
2022-10-19 20:00:22 +03:00
|
|
|
"sort"
|
2022-09-13 16:03:40 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-10-19 20:00:22 +03:00
|
|
|
"sync/atomic"
|
2022-09-13 16:03:40 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxStackLen = 1000
|
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
const OFF = 0
|
|
|
|
const LITE = 1
|
|
|
|
const FULL = 2
|
|
|
|
|
|
|
|
var mode int32 = OFF
|
2022-10-14 14:59:16 +03:00
|
|
|
var res2track = &sync.Map{}
|
2022-10-20 12:05:05 +03:00
|
|
|
var customFormatter = func(item *TrackReportItem) string { return "," }
|
2022-09-13 16:03:40 +03:00
|
|
|
|
|
|
|
type TrackedInfo struct {
|
|
|
|
Time time.Time
|
|
|
|
Stack []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type TrackReportItem struct {
|
2022-10-20 12:05:05 +03:00
|
|
|
Obj interface{}
|
2022-09-13 16:03:40 +03:00
|
|
|
Duration time.Duration
|
|
|
|
Stack string
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitHandlers() {
|
|
|
|
http.HandleFunc("/res_tracker/on", onHandler)
|
|
|
|
http.HandleFunc("/res_tracker/off", offHandler)
|
|
|
|
http.HandleFunc("/res_tracker/report", reportHandler)
|
|
|
|
http.HandleFunc("/res_tracker/reset", resetHandler)
|
|
|
|
http.HandleFunc("/res_tracker/status", statusHandler)
|
|
|
|
}
|
|
|
|
|
2022-10-20 12:05:05 +03:00
|
|
|
func SetCustomFormatter(f func(item *TrackReportItem) string) {
|
|
|
|
customFormatter = f
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:03:40 +03:00
|
|
|
func onHandler(w http.ResponseWriter, r *http.Request) {
|
2022-10-19 20:00:22 +03:00
|
|
|
query := r.URL.Query()
|
|
|
|
m := query.Get("m")
|
|
|
|
if len(m) == 0 || m == "lite" {
|
|
|
|
Enable(LITE)
|
|
|
|
fmt.Fprintf(w, "ON(lite)\n")
|
|
|
|
} else if m == "full" {
|
|
|
|
Enable(FULL)
|
|
|
|
fmt.Fprintf(w, "ON(full)\n")
|
|
|
|
}
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func offHandler(w http.ResponseWriter, r *http.Request) {
|
2022-10-19 20:00:22 +03:00
|
|
|
Enable(OFF)
|
2022-09-13 16:03:40 +03:00
|
|
|
fmt.Fprintf(w, "OFF\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func reportHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for _, line := range ReportLines() {
|
|
|
|
fmt.Fprintf(w, line+"\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
Reset()
|
|
|
|
fmt.Fprintf(w, "Reset\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
2022-10-19 20:00:22 +03:00
|
|
|
fmt.Fprintf(w, "mode: %v, entries: %d\n", atomic.LoadInt32(&mode), Count())
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
func Enable(m int32) {
|
|
|
|
atomic.StoreInt32(&mode, m)
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Reset() {
|
2022-10-19 20:00:22 +03:00
|
|
|
res2track.Range(func(k, v interface{}) bool {
|
|
|
|
res2track.Delete(k)
|
|
|
|
return true
|
|
|
|
})
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsOn() bool {
|
2022-10-19 20:00:22 +03:00
|
|
|
return atomic.LoadInt32(&mode) != OFF
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Count() int {
|
|
|
|
var i int
|
2022-10-19 20:00:22 +03:00
|
|
|
res2track.Range(func(k, v interface{}) bool {
|
2022-09-13 16:03:40 +03:00
|
|
|
i++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func Track(o interface{}) {
|
2022-10-19 20:00:22 +03:00
|
|
|
m := atomic.LoadInt32(&mode)
|
|
|
|
if m == OFF {
|
2022-09-13 16:03:40 +03:00
|
|
|
return
|
|
|
|
}
|
2022-10-19 20:00:22 +03:00
|
|
|
var stack []byte = nil
|
|
|
|
if m == FULL {
|
|
|
|
stack = debug.Stack()
|
|
|
|
}
|
2022-09-13 16:03:40 +03:00
|
|
|
res2track.Store(o, TrackedInfo{time.Now(), stack})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Untrack(o interface{}) {
|
2022-10-19 20:00:22 +03:00
|
|
|
if atomic.LoadInt32(&mode) == OFF {
|
2022-09-13 16:03:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
res2track.Delete(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatReportItems() []TrackReportItem {
|
|
|
|
parensReg := regexp.MustCompile(`\([^\)]+\)`)
|
|
|
|
fileReg := regexp.MustCompile(`at\s+.*?/([^/]+\.go:\d+)`)
|
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
//1. let's get a quick snapshot of the map
|
|
|
|
var snapshot []interface{}
|
2022-09-13 16:03:40 +03:00
|
|
|
res2track.Range(func(k, v interface{}) bool {
|
2022-10-19 20:00:22 +03:00
|
|
|
snapshot = append(snapshot, k)
|
|
|
|
snapshot = append(snapshot, v)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
//2. let's add formatted items
|
|
|
|
var items []TrackReportItem
|
|
|
|
for i := 0; i < len(snapshot); i += 2 {
|
|
|
|
k := snapshot[i]
|
|
|
|
v := snapshot[i+1]
|
2022-09-13 16:03:40 +03:00
|
|
|
|
|
|
|
info, _ := v.(TrackedInfo)
|
|
|
|
stackStr := string(info.Stack)
|
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
if len(stackStr) > 0 {
|
|
|
|
//let's remove first stack entries
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
newLineIdx := strings.Index(stackStr, "\n")
|
|
|
|
if newLineIdx != -1 {
|
|
|
|
stackStr = stackStr[newLineIdx+1:]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
stackStr = strings.ReplaceAll(stackStr, " ", " ")
|
|
|
|
stackStr = strings.ReplaceAll(stackStr, "\n", " < ")
|
|
|
|
stackStr = strings.ReplaceAll(stackStr, ") <", ") at")
|
|
|
|
stackStr = parensReg.ReplaceAllString(stackStr, "(..)")
|
|
|
|
stackStr = fileReg.ReplaceAllString(stackStr, "../$1")
|
|
|
|
if len(stackStr) > maxStackLen {
|
|
|
|
stackStr = stackStr[:maxStackLen] + "..."
|
|
|
|
}
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
|
2022-10-20 12:05:05 +03:00
|
|
|
items = append(items, TrackReportItem{Obj: k, Duration: time.Now().Sub(info.Time), Stack: stackStr})
|
2022-10-19 20:00:22 +03:00
|
|
|
}
|
2022-09-13 16:03:40 +03:00
|
|
|
|
2022-10-19 20:00:22 +03:00
|
|
|
//3. let's sort items by duration descending
|
|
|
|
sort.Slice(items, func(i, j int) bool {
|
|
|
|
return items[i].Duration > items[j].Duration
|
2022-09-13 16:03:40 +03:00
|
|
|
})
|
2022-10-19 20:00:22 +03:00
|
|
|
|
2022-09-13 16:03:40 +03:00
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReportLines() []string {
|
|
|
|
var lines []string
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
items := FormatReportItems()
|
2022-10-19 20:00:22 +03:00
|
|
|
|
2022-09-13 16:03:40 +03:00
|
|
|
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 {
|
2022-10-19 20:00:22 +03:00
|
|
|
//TODO: obtain somehow an actual pointer of the object or provide a hook for a custom formatter
|
2022-10-20 12:05:05 +03:00
|
|
|
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))
|
2022-09-13 16:03:40 +03:00
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReportToFile(file string) error {
|
|
|
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, line := range ReportLines() {
|
|
|
|
if _, err := f.WriteString(line + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReportToTmpFile() (string, error) {
|
|
|
|
file := fmt.Sprintf("/tmp/track_report.%d", os.Getpid())
|
|
|
|
return file, ReportToFile(file)
|
|
|
|
}
|