Skip to content

Commit

Permalink
导出 pprof & 调试变量
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Aug 6, 2024
1 parent 66b7cc8 commit 5cd5e01
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
40 changes: 40 additions & 0 deletions gateway/handlers/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package debug

import (
"expvar"
"net/http"
_ "net/http/pprof"
"strconv"
)

func Handler() http.Handler {
mux := http.NewServeMux()

mux.Handle(`GET /vars`, expvar.Handler())
mux.HandleFunc(`POST /vars/{var}`, func(w http.ResponseWriter, r *http.Request) {
va := expvar.Get(r.PathValue(`var`))
if va == nil {
http.NotFound(w, r)
return
}
switch typed := va.(type) {
case *expvar.Int:
i, err := strconv.Atoi(r.URL.Query().Get(`v`))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
typed.Set(int64(i))
default:
http.Error(w, `unknown var type`, http.StatusBadRequest)
return
}
})

mux.HandleFunc(`/pprof/`, func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = `/debug` + r.URL.Path
http.DefaultServeMux.ServeHTTP(w, r)
})

return mux
}
15 changes: 15 additions & 0 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/movsb/taoblog/gateway/handlers/apidoc"
"github.com/movsb/taoblog/gateway/handlers/assets"
"github.com/movsb/taoblog/gateway/handlers/avatar"
"github.com/movsb/taoblog/gateway/handlers/debug"
"github.com/movsb/taoblog/gateway/handlers/features"
"github.com/movsb/taoblog/gateway/handlers/robots"
"github.com/movsb/taoblog/gateway/handlers/rss"
Expand Down Expand Up @@ -112,6 +113,9 @@ func (g *Gateway) register(ctx context.Context, mux *http.ServeMux) error {

// 订阅:rss
mc.Handle(`GET /rss`, rss.New(g.auther, g.client, rss.WithArticleCount(10)), g.lastPostTimeHandler)

// 调试相关
mc.Handle(`/debug/`, http.StripPrefix(`/debug`, debug.Handler()), g.adminHandler)
}

return nil
Expand All @@ -126,3 +130,14 @@ func (g *Gateway) lastPostTimeHandler(h http.Handler) http.Handler {
).ServeHTTP(w, r)
})
}

func (g *Gateway) adminHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ac := auth.Context(r.Context())
if !ac.User.IsAdmin() {
w.WriteHeader(http.StatusForbidden)
return
}
h.ServeHTTP(w, r)
})
}
8 changes: 8 additions & 0 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"database/sql"
"expvar"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -292,7 +293,14 @@ func grpcLoggerStream(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo
grpcLogger(ss.Context(), info.FullMethod)
return handler(srv, ss)
}

var enableGrpcLogger = expvar.NewInt(`log.grpc`)

func grpcLogger(ctx context.Context, method string) {
logEnabled := enableGrpcLogger.Value() == 1
if !logEnabled {
return
}
if md, ok := metadata.FromIncomingContext(ctx); ok {
log.Println(md)
}
Expand Down

0 comments on commit 5cd5e01

Please sign in to comment.