Skip to content

Commit

Permalink
test with add new api
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <[email protected]>
  • Loading branch information
nolouch committed Aug 9, 2024
1 parent efb03ec commit 651f51e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/pingcap/tidb-dashboard v0.0.0-20240327052925-0f035e0e22ee
github.com/prometheus/client_golang v1.11.1
github.com/prometheus/common v0.26.0
github.com/rhysh/autoprof v0.0.0-20240708032903-aed43a1dedec
github.com/sasha-s/go-deadlock v0.2.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rhysh/autoprof v0.0.0-20240708032903-aed43a1dedec h1:EeYHkttFMOX34U4NBrZOKk1+I0vW5chptpdTvC1i9Hc=
github.com/rhysh/autoprof v0.0.0-20240708032903-aed43a1dedec/go.mod h1:4FBop5l5Ur0n+mQ7rMX7XMdfhewY7N2aB/OjAlwFfuE=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
35 changes: 35 additions & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
package api

import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/pprof"
"reflect"
"runtime"
"strings"
"time"

"github.com/gorilla/mux"
"github.com/pingcap/failpoint"
"github.com/rhysh/autoprof"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/audit"
"github.com/tikv/pd/pkg/ratelimit"
Expand Down Expand Up @@ -343,6 +348,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
registerFunc(apiRouter, "/debug/pprof/goroutine", pprofHandler.PProfGoroutine)
registerFunc(apiRouter, "/debug/pprof/threadcreate", pprofHandler.PProfThreadcreate)
registerFunc(apiRouter, "/debug/pprof/zip", pprofHandler.PProfZip)
registerFunc(apiRouter, "/debug/pprof/autoprof", collectHandler)

// service GC safepoint API
serviceGCSafepointHandler := newServiceGCSafepointHandler(svr, rd)
Expand Down Expand Up @@ -395,3 +401,32 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {

return rootRouter
}

func collectHandler(w http.ResponseWriter, r *http.Request) {
meta := autoprof.CurrentArchiveMeta()
opt := &autoprof.ArchiveOptions{
CPUProfileDuration: 3 * time.Second,
ExecutionTraceDuration: 3 * time.Second,
}

buf, err := collect(r.Context(), meta, opt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename=autoprof.zip")
if _, err := w.Write(buf.Bytes()); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func collect(ctx context.Context, meta *autoprof.ArchiveMeta, opt *autoprof.ArchiveOptions) (bytes.Buffer, error) {

Check failure on line 424 in server/api/router.go

View workflow job for this annotation

GitHub Actions / statics

`collect` - `ctx` is unused (unparam)
var buf bytes.Buffer

err := autoprof.NewZipCollector(&buf, meta, opt).Run(context.Background())
if err != nil {
return buf, fmt.Errorf("autoprof.NewZipCollector.Run: %w", err)
}
return buf, nil
}

0 comments on commit 651f51e

Please sign in to comment.