Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: runtime pprof (#12740)

p0ny87acfd91faaf
diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.go
index 198070dfbb..494513e75c 100644
--- a/backend/api/v1/actuator_service.go
+++ b/backend/api/v1/actuator_service.go
@@ -49,8 +49,11 @@ func (s *ActuatorService) GetActuatorInfo(ctx context.Context, _ *v1pb.GetActuat
func (s *ActuatorService) UpdateActuatorInfo(ctx context.Context, request *v1pb.UpdateActuatorInfoRequest) (*v1pb.ActuatorInfo, error) {
for _, path := range request.UpdateMask.Paths {
if path == "debug" {
+ debug := request.GetActuator().GetDebug()
+
+ s.profile.RuntimeDebug.Store(debug)
level := slog.LevelInfo
- if request.Actuator.Debug {
+ if debug {
level = slog.LevelDebug
}
log.LogLevel.Set(level)
@@ -144,7 +147,7 @@ func (s *ActuatorService) getServerInfo(ctx context.Context) (*v1pb.ActuatorInfo
LastActiveTime: timestamppb.New(time.Unix(s.profile.LastActiveTs, 0)),
WorkspaceId: workspaceID,
GitopsWebhookUrl: setting.GitopsWebhookUrl,
- Debug: slog.Default().Enabled(ctx, slog.LevelDebug),
+ Debug: s.profile.RuntimeDebug.Load(),
Lsp: s.profile.Lsp,
PreUpdateBackup: s.profile.PreUpdateBackup,
UnlicensedFeatures: unlicensedFeaturesString,
diff --git a/backend/bin/server/cmd/profile_dev.go b/backend/bin/server/cmd/profile_dev.go
index 2416f7a45a..4047b3f15f 100644
--- a/backend/bin/server/cmd/profile_dev.go
+++ b/backend/bin/server/cmd/profile_dev.go
@@ -11,6 +11,7 @@ import (
func activeProfile(dataDir string) *config.Profile {
p := getBaseProfile(dataDir)
+ p.RuntimeDebug.Store(p.Debug)
p.Mode = common.ReleaseModeDev
p.PgUser = "bbdev"
p.BackupRunnerInterval = 10 * time.Second
diff --git a/backend/bin/server/cmd/profile_release.go b/backend/bin/server/cmd/profile_release.go
index 0966934e4c..567a7cd93c 100644
--- a/backend/bin/server/cmd/profile_release.go
+++ b/backend/bin/server/cmd/profile_release.go
@@ -11,6 +11,7 @@ import (
func activeProfile(dataDir string) *config.Profile {
p := getBaseProfile(dataDir)
+ p.RuntimeDebug.Store(p.Debug)
p.Mode = common.ReleaseModeProd
p.PgUser = "bb"
p.BackupRunnerInterval = 10 * time.Minute
diff --git a/backend/component/config/profile.go b/backend/component/config/profile.go
index 9fcc011423..2d197382a4 100644
--- a/backend/component/config/profile.go
+++ b/backend/component/config/profile.go
@@ -2,6 +2,7 @@
package config
import (
+ "sync/atomic"
"time"
"github.com/bytebase/bytebase/backend/common"
@@ -9,6 +10,7 @@ import (
)
// Profile is the configuration to start main server.
+// Profile must not be copied, its fields must not be modified unless mentioned otherwise.
type Profile struct {
// Mode can be "prod" or "dev"
Mode common.ReleaseMode
@@ -67,6 +69,9 @@ type Profile struct {
ExecuteDetail bool
DevelopmentAudit bool
+
+ // can be set in runtime
+ RuntimeDebug atomic.Bool
}
// UseEmbedDB returns whether to use embedDB.
diff --git a/backend/server/pprof.go b/backend/server/pprof.go
new file mode 100644
index 0000000000..2dba948495
--- /dev/null
+++ b/backend/server/pprof.go
@@ -0,0 +1,34 @@
+package server
+
+import (
+ "net/http"
+ "net/http/pprof"
+ "sync/atomic"
+
+ "github.com/labstack/echo/v4"
+)
+
+func registerPprof(e *echo.Echo, runtimeDebug *atomic.Bool) {
+ router := e.Group("/debug/pprof")
+ handler := func(h http.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ if !runtimeDebug.Load() {
+ return echo.NewHTTPError(echo.ErrNotFound.Code, "")
+ }
+ h.ServeHTTP(c.Response().Writer, c.Request())
+ return nil
+ }
+ }
+ router.GET("/", handler(pprof.Index))
+ router.GET("/allocs", handler(pprof.Handler("allocs").ServeHTTP))
+ router.GET("/block", handler(pprof.Handler("block").ServeHTTP))
+ router.GET("/cmdline", handler(pprof.Cmdline))
+ router.GET("/goroutine", handler(pprof.Handler("goroutine").ServeHTTP))
+ router.GET("/heap", handler(pprof.Handler("heap").ServeHTTP))
+ router.GET("/mutex", handler(pprof.Handler("mutex").ServeHTTP))
+ router.GET("/profile", handler(pprof.Profile))
+ router.POST("/symbol", handler(pprof.Symbol))
+ router.GET("/symbol", handler(pprof.Symbol))
+ router.GET("/threadcreate", handler(pprof.Handler("threadcreate").ServeHTTP))
+ router.GET("/trace", handler(pprof.Trace))
+}
diff --git a/backend/server/routers.go b/backend/server/routers.go
index 5b57fa2f95..167f47fc31 100644
--- a/backend/server/routers.go
+++ b/backend/server/routers.go
@@ -8,7 +8,6 @@ import (
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
- "github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@@ -70,9 +69,9 @@ func configureEchoRouters(e *echo.Echo, grpcServer *grpc.Server, lspServer *lsp.
return context.JSON(http.StatusTooManyRequests, nil)
},
}))
- if profile.Debug {
- pprof.Register(e)
- }
+
+ registerPprof(e, &profile.RuntimeDebug)
+
p := prometheus.NewPrometheus("api", nil)
p.Use(e)