Skip to content
Merged

ui #4

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions internal/biz/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"sync"
)

type pp struct {
Expand All @@ -24,7 +26,9 @@ type cp struct {
type px struct {
}

type replaceBody func(body string, currPath string) string
type replaceBody func(body []byte, currPath string) []byte

var proxyBufPool = sync.Pool{New: func() any { return new(bytes.Buffer) }}
type Uri struct {
Path string
Dir string
Expand Down Expand Up @@ -54,21 +58,23 @@ func (uc *Usecase) Proxy(u Uri, rw http.ResponseWriter, req *http.Request) (err
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ModifyResponse = func(res *http.Response) error {
if res.StatusCode == 200 {
oldPayload, err := io.ReadAll(res.Body)
if err != nil {
return err
}
newPayLoadStr := string(oldPayload)
newPayLoadStr = u.ReBody(newPayLoadStr, currPath)
newPayLoad := []byte(newPayLoadStr)
res.Body = io.NopCloser(bytes.NewBuffer(newPayLoad))
res.ContentLength = int64(len(newPayLoad))
res.Header.Set("Content-Length", fmt.Sprint(len(newPayLoad)))
return nil
} else {
if res.StatusCode != 200 {
return nil
}
buf := proxyBufPool.Get().(*bytes.Buffer)
buf.Reset()
if _, err := buf.ReadFrom(res.Body); err != nil {
proxyBufPool.Put(buf)
return err
}
transformed := u.ReBody(buf.Bytes(), currPath)
result := make([]byte, len(transformed))
copy(result, transformed)
proxyBufPool.Put(buf)
res.Body = io.NopCloser(bytes.NewReader(result))
res.ContentLength = int64(len(result))
res.Header.Set("Content-Length", strconv.Itoa(len(result)))
return nil
}
proxy.ServeHTTP(rw, req)
return nil
Expand Down Expand Up @@ -101,19 +107,22 @@ func (uc *Usecase) ProxyDiff(u Uri, rw http.ResponseWriter, req *http.Request) (
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ModifyResponse = func(res *http.Response) error {
if res.StatusCode == 200 {
oldPayload, err := io.ReadAll(res.Body)
if err != nil {
return err
}
newPayLoadStr := string(oldPayload)
newPayLoadStr = u.ReBody(newPayLoadStr, currPath)
newPayLoad := []byte(newPayLoadStr)
res.Body = io.NopCloser(bytes.NewBuffer(newPayLoad))
res.ContentLength = int64(len(newPayLoad))
res.Header.Set("Content-Length", fmt.Sprint(len(newPayLoad)))
if res.StatusCode != 200 {
return nil
}
buf := proxyBufPool.Get().(*bytes.Buffer)
buf.Reset()
if _, err := buf.ReadFrom(res.Body); err != nil {
proxyBufPool.Put(buf)
return err
}
transformed := u.ReBody(buf.Bytes(), currPath)
result := make([]byte, len(transformed))
copy(result, transformed)
proxyBufPool.Put(buf)
res.Body = io.NopCloser(bytes.NewReader(result))
res.ContentLength = int64(len(result))
res.Header.Set("Content-Length", strconv.Itoa(len(result)))
return nil
}
proxy.ServeHTTP(rw, req)
Expand Down
40 changes: 28 additions & 12 deletions internal/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ func injectPlugins(body string) string {
return body
}

var pluginLoaderTagBytes = []byte(pluginLoaderTag)
var headCloseTag = []byte("</head>")

func injectPluginsBytes(body []byte) []byte {
idx := bytes.Index(body, headCloseTag)
if idx < 0 {
return body
}
out := make([]byte, 0, len(body)+len(pluginLoaderTagBytes))
out = append(out, body[:idx]...)
out = append(out, pluginLoaderTagBytes...)
out = append(out, body[idx:]...)
return out
}

func newUri(c *gin.Context) biz.Uri {
return biz.Uri{
Path: c.Request.URL.Path,
Expand All @@ -177,9 +192,9 @@ func (s *Service) PprofProxy(c *gin.Context) {
u := newUri(c)
u.Route = RoutePprof
u.ProxyBasePath = "/ui/"
u.ReBody = func(body string, currPath string) string {
body = strings.ReplaceAll(body, "href=\"./", "href=\""+currPath)
body = injectPlugins(body)
u.ReBody = func(body []byte, currPath string) []byte {
body = bytes.ReplaceAll(body, []byte(`href="./`), []byte(`href="`+currPath))
body = injectPluginsBytes(body)
return body
}
s.log.Debugf("%#v", u)
Expand All @@ -194,9 +209,9 @@ func (s *Service) PprofDiffProxy(c *gin.Context) {
u.Base = c.Param("base")
u.Route = RoutePprofDiff
u.ProxyBasePath = "/ui/"
u.ReBody = func(body string, currPath string) string {
body = strings.ReplaceAll(body, "href=\"./", "href=\""+currPath)
body = injectPlugins(body)
u.ReBody = func(body []byte, currPath string) []byte {
body = bytes.ReplaceAll(body, []byte(`href="./`), []byte(`href="`+currPath))
body = injectPluginsBytes(body)
return body
}
s.log.Debugf("%#v", u)
Expand All @@ -210,12 +225,13 @@ func (s *Service) TraceProxy(c *gin.Context) {
u := newUri(c)
u.Route = RouteTrace
u.ProxyBasePath = "/"
u.ReBody = func(body string, currPath string) string {
body = strings.ReplaceAll(body, "href=\"/", "href=\""+currPath)
body = strings.ReplaceAll(body, "src=\"/", "src=\""+currPath)
body = strings.ReplaceAll(body, "action=\"/", "action=\""+currPath)
body = strings.ReplaceAll(body, "getJSON('/", "getJSON('"+currPath)
body = strings.ReplaceAll(body, "url = '/", "url = '"+currPath)
u.ReBody = func(body []byte, currPath string) []byte {
cp := []byte(currPath)
body = bytes.ReplaceAll(body, []byte(`href="/`), append([]byte(`href="`), cp...))
body = bytes.ReplaceAll(body, []byte(`src="/`), append([]byte(`src="`), cp...))
body = bytes.ReplaceAll(body, []byte(`action="/`), append([]byte(`action="`), cp...))
body = bytes.ReplaceAll(body, []byte("getJSON('/"), append([]byte("getJSON('"), cp...))
body = bytes.ReplaceAll(body, []byte("url = '/"), append([]byte("url = '"), cp...))
return body
}
s.log.Debugf("%#v", u)
Expand Down
16 changes: 16 additions & 0 deletions web/static/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,41 @@

"goroutine.modeLabel": "Output Mode",
"goroutine.modePprof": "pprof format",
"goroutine.modePprofDesc": "— goroutine (flame graph)",
"goroutine.modeText": "Text summary",
"goroutine.modeTextDesc": "— goroutine?debug=1 (total + stack distribution)",
"goroutine.modeFull": "Full dump",
"goroutine.modeFullDesc": "— goroutine?debug=2",
"goroutine.stwWarn": "⚠ Triggers Stop-The-World, use with caution for large goroutine counts",

"heap.modeLabel": "Collection Mode",
"heap.snapshot": "Snapshot",
"heap.snapshotDesc": "— heap (current heap)",
"heap.gcSnapshot": "GC + Snapshot",
"heap.gcSnapshotDesc": "— heap?gc=1 (live objects only, more accurate)",
"heap.delta": "Delta",
"heap.deltaDesc": "s — heap?seconds=N",

"allocs.modeLabel": "Collection Mode",
"allocs.snapshot": "Snapshot",
"allocs.snapshotDesc": "— allocs (cumulative allocations)",
"allocs.delta": "Delta",
"allocs.deltaDesc": "s — allocs?seconds=N",

"profile.durationLabel": "Duration",
"profile.durationDesc": "s → profile?seconds=N",
"trace.durationLabel": "Duration",
"trace.durationDesc": "s → trace?seconds=N",
"trace.hint": "Note: view result with go tool trace, not pprof",

"block.prereqWarn": "⚠️ Prerequisite: target must call runtime.SetBlockProfileRate(rate), otherwise empty result",
"block.snapshot": "Snapshot → block",
"block.delta": "Delta",
"block.deltaDesc": "s → block?seconds=N",
"mutex.prereqWarn": "⚠️ Prerequisite: target must call runtime.SetMutexProfileFraction(rate), otherwise empty result",
"mutex.snapshot": "Snapshot → mutex",
"mutex.delta": "Delta",
"mutex.deltaDesc": "s → mutex?seconds=N",

"threadcreate.hint": "Snapshot → threadcreate (OS thread creation stacks, usually small)",
"cmdline.hint": "No params, returns command-line arguments",
Expand Down
16 changes: 16 additions & 0 deletions web/static/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,41 @@

"goroutine.modeLabel": "输出模式",
"goroutine.modePprof": "pprof 格式",
"goroutine.modePprofDesc": "— goroutine(flame graph)",
"goroutine.modeText": "文本摘要",
"goroutine.modeTextDesc": "— goroutine?debug=1(总数 + 栈分布)",
"goroutine.modeFull": "完整 dump",
"goroutine.modeFullDesc": "— goroutine?debug=2",
"goroutine.stwWarn": "⚠ 会触发 Stop-The-World,goroutine 数量大时请谨慎",

"heap.modeLabel": "采集模式",
"heap.snapshot": "快照",
"heap.snapshotDesc": "— heap(当前堆内存)",
"heap.gcSnapshot": "GC 后快照",
"heap.gcSnapshotDesc": "— heap?gc=1(真实存活对象,更准确)",
"heap.delta": "Delta",
"heap.deltaDesc": "秒 — heap?seconds=N",

"allocs.modeLabel": "采集模式",
"allocs.snapshot": "快照",
"allocs.snapshotDesc": "— allocs(历史累计分配)",
"allocs.delta": "Delta",
"allocs.deltaDesc": "秒 — allocs?seconds=N",

"profile.durationLabel": "采集时长",
"profile.durationDesc": "秒 → profile?seconds=N",
"trace.durationLabel": "采集时长",
"trace.durationDesc": "秒 → trace?seconds=N",
"trace.hint": "提示:结果用 go tool trace 查看,不同于 pprof",

"block.prereqWarn": "⚠️ 前置条件:目标程序需调用 runtime.SetBlockProfileRate(rate),否则采集结果为空文件",
"block.snapshot": "快照 → block",
"block.delta": "Delta",
"block.deltaDesc": "秒 → block?seconds=N",
"mutex.prereqWarn": "⚠️ 前置条件:目标程序需调用 runtime.SetMutexProfileFraction(rate),否则采集结果为空文件",
"mutex.snapshot": "快照 → mutex",
"mutex.delta": "Delta",
"mutex.deltaDesc": "秒 → mutex?seconds=N",

"threadcreate.hint": "快照 → threadcreate(OS 线程创建调用栈,通常较小)",
"cmdline.hint": "无参数,直接采集命令行参数",
Expand Down
36 changes: 18 additions & 18 deletions web/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -782,75 +782,75 @@ <h6 class="modal-title mb-0" data-i18n="modal.curlScript">Curl Script</h6>
goroutine: [
'<div class="mb-1 fw-semibold">' + t('goroutine.modeLabel') + '</div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="goroutine_mode" id="g_pprof" value="" checked>',
'<label class="form-check-label" for="g_pprof">' + t('goroutine.modePprof') + ' <span class="text-muted">— goroutine(flame graph)</span></label></div>',
'<label class="form-check-label" for="g_pprof">' + t('goroutine.modePprof') + ' <span class="text-muted">' + t('goroutine.modePprofDesc') + '</span></label></div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="goroutine_mode" id="g_debug1" value="debug=1">',
'<label class="form-check-label" for="g_debug1">' + t('goroutine.modeText') + ' <span class="text-muted">— goroutine?debug=1(总数 + 栈分布)</span></label></div>',
'<label class="form-check-label" for="g_debug1">' + t('goroutine.modeText') + ' <span class="text-muted">' + t('goroutine.modeTextDesc') + '</span></label></div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="goroutine_mode" id="g_debug2" value="debug=2">',
'<label class="form-check-label" for="g_debug2">' + t('goroutine.modeFull') + ' <span class="badge bg-warning text-dark ms-1">⚠ STW</span> <span class="text-muted">— goroutine?debug=2</span></label></div>',
'<label class="form-check-label" for="g_debug2">' + t('goroutine.modeFull') + ' <span class="badge bg-warning text-dark ms-1">⚠ STW</span> <span class="text-muted">' + t('goroutine.modeFullDesc') + '</span></label></div>',
'<div class="text-danger mt-1" id="goroutineStw" style="display:none">' + t('goroutine.stwWarn') + '</div>'
].join(""),

heap: [
'<div class="mb-1 fw-semibold">' + t('heap.modeLabel') + '</div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="heap_mode" id="h_snap" value="" checked>',
'<label class="form-check-label" for="h_snap">' + t('heap.snapshot') + ' <span class="text-muted">— heap(当前堆内存)</span></label></div>',
'<label class="form-check-label" for="h_snap">' + t('heap.snapshot') + ' <span class="text-muted">' + t('heap.snapshotDesc') + '</span></label></div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="heap_mode" id="h_gc" value="gc=1">',
'<label class="form-check-label" for="h_gc">' + t('heap.gcSnapshot') + ' <span class="text-muted">— heap?gc=1(真实存活对象,更准确)</span></label></div>',
'<label class="form-check-label" for="h_gc">' + t('heap.gcSnapshot') + ' <span class="text-muted">' + t('heap.gcSnapshotDesc') + '</span></label></div>',
'<div class="d-flex align-items-center gap-2 mt-1">',
'<input class="form-check-input flex-shrink-0" type="radio" name="heap_mode" id="h_delta" value="seconds=">',
'<label class="form-check-label me-1" for="h_delta">' + t('heap.delta') + '</label>',
'<input type="number" id="heapDeltaSec" value="30" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 — heap?seconds=N</span></div>'
'<span class="text-muted">' + t('heap.deltaDesc') + '</span></div>'
].join(""),

allocs: [
'<div class="mb-1 fw-semibold">' + t('allocs.modeLabel') + '</div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="allocs_mode" id="al_snap" value="" checked>',
'<label class="form-check-label" for="al_snap">' + t('allocs.snapshot') + ' <span class="text-muted">— allocs(历史累计分配)</span></label></div>',
'<label class="form-check-label" for="al_snap">' + t('allocs.snapshot') + ' <span class="text-muted">' + t('allocs.snapshotDesc') + '</span></label></div>',
'<div class="d-flex align-items-center gap-2 mt-1">',
'<input class="form-check-input flex-shrink-0" type="radio" name="allocs_mode" id="al_delta" value="seconds=">',
'<label class="form-check-label me-1" for="al_delta">' + t('allocs.delta') + '</label>',
'<input type="number" id="allocsDeltaSec" value="30" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 — allocs?seconds=N</span></div>'
'<span class="text-muted">' + t('allocs.deltaDesc') + '</span></div>'
].join(""),

profile: [
'<div class="mb-1 fw-semibold">' + t('profile.durationLabel') + '</div>',
'<div class="d-flex align-items-center gap-2">',
'<input type="number" id="profileSec" value="30" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 → profile?seconds=N</span></div>'
'<span class="text-muted">' + t('profile.durationDesc') + '</span></div>'
].join(""),

trace: [
'<div class="mb-1 fw-semibold">' + t('trace.durationLabel') + '</div>',
'<div class="d-flex align-items-center gap-2">',
'<input type="number" id="traceSec1" value="5" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 → trace?seconds=N</span></div>',
'<span class="text-muted">' + t('trace.durationDesc') + '</span></div>',
'<div class="text-muted mt-1">' + t('trace.hint') + '</div>'
].join(""),

block: [
'<div class="alert alert-warning py-1 px-2 mb-2">' + t('block.prereqWarn') + ' <code>runtime.SetBlockProfileRate(rate)</code></div>',
'<div class="alert alert-warning py-1 px-2 mb-2">' + t('block.prereqWarn') + '</div>',
'<div class="mb-1 fw-semibold">' + t('heap.modeLabel') + '</div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="block_mode" id="bl_snap" value="" checked>',
'<label class="form-check-label" for="bl_snap">' + t('heap.snapshot') + ' → block</label></div>',
'<label class="form-check-label" for="bl_snap">' + t('block.snapshot') + '</label></div>',
'<div class="d-flex align-items-center gap-2 mt-1">',
'<input class="form-check-input flex-shrink-0" type="radio" name="block_mode" id="bl_delta" value="seconds=">',
'<label class="form-check-label me-1" for="bl_delta">Delta</label>',
'<label class="form-check-label me-1" for="bl_delta">' + t('block.delta') + '</label>',
'<input type="number" id="blockDeltaSec" value="30" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 → block?seconds=N</span></div>'
'<span class="text-muted">' + t('block.deltaDesc') + '</span></div>'
].join(""),

mutex: [
'<div class="alert alert-warning py-1 px-2 mb-2">' + t('mutex.prereqWarn') + ' <code>runtime.SetMutexProfileFraction(rate)</code></div>',
'<div class="alert alert-warning py-1 px-2 mb-2">' + t('mutex.prereqWarn') + '</div>',
'<div class="mb-1 fw-semibold">' + t('heap.modeLabel') + '</div>',
'<div class="form-check"><input class="form-check-input" type="radio" name="mutex_mode" id="mx_snap" value="" checked>',
'<label class="form-check-label" for="mx_snap">' + t('heap.snapshot') + ' → mutex</label></div>',
'<label class="form-check-label" for="mx_snap">' + t('mutex.snapshot') + '</label></div>',
'<div class="d-flex align-items-center gap-2 mt-1">',
'<input class="form-check-input flex-shrink-0" type="radio" name="mutex_mode" id="mx_delta" value="seconds=">',
'<label class="form-check-label me-1" for="mx_delta">Delta</label>',
'<label class="form-check-label me-1" for="mx_delta">' + t('mutex.delta') + '</label>',
'<input type="number" id="mutexDeltaSec" value="30" min="1" max="180" class="form-control form-control-sm" style="width:60px">',
'<span class="text-muted">秒 → mutex?seconds=N</span></div>'
'<span class="text-muted">' + t('mutex.deltaDesc') + '</span></div>'
].join(""),

threadcreate: '<div class="text-muted">' + t('threadcreate.hint') + '</div>',
Expand Down
Loading