Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support check slow query log enabled (#5598)
rebelice5e55ae9158df
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex b17a9a76b3..9c19bf9ea7 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -11,6 +11,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/state"enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi"@@ -30,16 +31,18 @@ type InstanceService struct {metricReporter *metricreport.Reportersecret stringstateCfg *state.State+ dbFactory *dbfactory.DBFactory}// NewInstanceService creates a new InstanceService.-func NewInstanceService(store *store.Store, licenseService enterpriseAPI.LicenseService, metricReporter *metricreport.Reporter, secret string, stateCfg *state.State) *InstanceService {+func NewInstanceService(store *store.Store, licenseService enterpriseAPI.LicenseService, metricReporter *metricreport.Reporter, secret string, stateCfg *state.State, dbFactory *dbfactory.DBFactory) *InstanceService {return &InstanceService{store: store,licenseService: licenseService,metricReporter: metricReporter,secret: secret,stateCfg: stateCfg,+ dbFactory: dbFactory,}}@@ -186,6 +189,15 @@ func (s *InstanceService) SyncSlowQueries(ctx context.Context, request *v1pb.Synreturn nil, status.Errorf(codes.NotFound, "instance %q not found", request.Instance)}+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)+ if err != nil {+ return nil, err+ }+ defer driver.Close(ctx)+ if err := driver.CheckSlowQueryLogEnabled(ctx); err != nil {+ return nil, status.Errorf(codes.FailedPrecondition, "slow query log is not enabled: %s", err.Error())+ }+if instance.Engine == db.MySQL {// Sync slow queries for instance.s.stateCfg.InstanceSlowQuerySyncChan <- composedInstancediff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 7d06eff97c..f8df596b4a 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -332,6 +332,11 @@ func (*MockDriver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*streturn nil, nil}+// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*MockDriver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return nil+}+// SetDefaultSQLReviewRulePayload sets the default payload for this rule.func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {var payload []bytediff --git a/backend/plugin/db/clickhouse/sync.go b/backend/plugin/db/clickhouse/sync.goindex 5850af39ea..631667d07e 100644--- a/backend/plugin/db/clickhouse/sync.go+++ b/backend/plugin/db/clickhouse/sync.go@@ -182,3 +182,8 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseMetadafunc (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 7717ef1010..280d0173a8 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -476,6 +476,8 @@ type Driver interface {// SyncSlowQuery syncs the slow query logs.// The returned map is keyed by database name, and the value is a map keyed by query fingerprint.SyncSlowQuery(ctx context.Context, logDateTs time.Time) (map[string]*storepb.SlowQueryStatistics, error)+ // CheckSlowQueryLogEnabled checks if the slow query log is enabled.+ CheckSlowQueryLogEnabled(ctx context.Context) error// Role// CreateRole creates the role.diff --git a/backend/plugin/db/mongodb/sync.go b/backend/plugin/db/mongodb/sync.goindex fb487db547..7437c16585 100644--- a/backend/plugin/db/mongodb/sync.go+++ b/backend/plugin/db/mongodb/sync.go@@ -309,3 +309,8 @@ func convertEmptyInterfaceToInt64(value any) (int64, error) {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/mssql/sync.go b/backend/plugin/db/mssql/sync.goindex d77f454b5f..fba13b9302 100644--- a/backend/plugin/db/mssql/sync.go+++ b/backend/plugin/db/mssql/sync.go@@ -317,3 +317,8 @@ func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.goindex 8adc6ce204..d956faf6f2 100644--- a/backend/plugin/db/mysql/sync.go+++ b/backend/plugin/db/mysql/sync.go@@ -637,3 +637,47 @@ func extractDatabase(defaultDB string, sql string) []string {}return result}++// CheckSlowQueryLogEnabled checks whether the slow query log is enabled.+func (driver *Driver) CheckSlowQueryLogEnabled(ctx context.Context) error {+ showSlowQueryLog := "SHOW GLOBAL VARIABLES LIKE 'slow_query_log'"++ slowQueryLogRows, err := driver.db.QueryContext(ctx, showSlowQueryLog)+ if err != nil {+ return util.FormatErrorWithQuery(err, showSlowQueryLog)+ }+ defer slowQueryLogRows.Close()+ for slowQueryLogRows.Next() {+ var name, value string+ if err := slowQueryLogRows.Scan(&name, &value); err != nil {+ return err+ }+ if value != "ON" {+ return errors.New("slow query log is not enabled: slow_query_log = " + value)+ }+ }+ if err := slowQueryLogRows.Err(); err != nil {+ return util.FormatErrorWithQuery(err, showSlowQueryLog)+ }++ showLogOutput := "SHOW GLOBAL VARIABLES LIKE 'log_output'"+ logOutputRows, err := driver.db.QueryContext(ctx, showLogOutput)+ if err != nil {+ return util.FormatErrorWithQuery(err, showLogOutput)+ }+ defer logOutputRows.Close()+ for logOutputRows.Next() {+ var name, value string+ if err := logOutputRows.Scan(&name, &value); err != nil {+ return err+ }+ if !strings.Contains(value, "TABLE") {+ return errors.New("slow query log is not contained in TABLE: log_output = " + value)+ }+ }+ if err := logOutputRows.Err(); err != nil {+ return util.FormatErrorWithQuery(err, showLogOutput)+ }++ return nil+}diff --git a/backend/plugin/db/oracle/sync.go b/backend/plugin/db/oracle/sync.goindex 0a844fc782..91231516fb 100644--- a/backend/plugin/db/oracle/sync.go+++ b/backend/plugin/db/oracle/sync.go@@ -332,3 +332,8 @@ func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.goindex 003edf523d..911938e650 100644--- a/backend/plugin/db/pg/sync.go+++ b/backend/plugin/db/pg/sync.go@@ -606,3 +606,8 @@ func getIndexMethodType(stmt string) string {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/redis/sync.go b/backend/plugin/db/redis/sync.goindex 81c6fee87f..2c8bcf6d5c 100644--- a/backend/plugin/db/redis/sync.go+++ b/backend/plugin/db/redis/sync.go@@ -119,3 +119,8 @@ func (d *Driver) getDatabaseCount(ctx context.Context) (int, error) {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/redshift/sync.go b/backend/plugin/db/redshift/sync.goindex c14c707312..fa78b2e4a5 100644--- a/backend/plugin/db/redshift/sync.go+++ b/backend/plugin/db/redshift/sync.go@@ -720,3 +720,8 @@ func (driver *Driver) getDatabases(ctx context.Context) ([]*storepb.DatabaseMetafunc (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/snowflake/sync.go b/backend/plugin/db/snowflake/sync.goindex 86aeefb006..2475ce2fd4 100644--- a/backend/plugin/db/snowflake/sync.go+++ b/backend/plugin/db/snowflake/sync.go@@ -298,3 +298,8 @@ func (driver *Driver) getTableSchema(ctx context.Context, database string) (map[func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/spanner/sync.go b/backend/plugin/db/spanner/sync.goindex e1df8b610a..4239d81ae1 100644--- a/backend/plugin/db/spanner/sync.go+++ b/backend/plugin/db/spanner/sync.go@@ -374,3 +374,8 @@ func getView(ctx context.Context, tx *spanner.ReadOnlyTransaction) (map[string][func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/plugin/db/sqlite/sync.go b/backend/plugin/db/sqlite/sync.goindex 1273f270b8..5bc0d26cbc 100644--- a/backend/plugin/db/sqlite/sync.go+++ b/backend/plugin/db/sqlite/sync.go@@ -266,3 +266,8 @@ func getViews(txn *sql.Tx) ([]*storepb.ViewMetadata, error) {func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {return nil, errors.Errorf("not implemented")}++// CheckSlowQueryLogEnabled checks if slow query log is enabled.+func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {+ return errors.Errorf("not implemented")+}diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.goindex faca509641..a52d00ddf3 100644--- a/backend/runner/slowquerysync/syncer.go+++ b/backend/runner/slowquerysync/syncer.go@@ -133,6 +133,9 @@ func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.Instreturn err}defer driver.Close(ctx)+ if err := driver.CheckSlowQueryLogEnabled(ctx); err != nil {+ return err+ }for date := latestSlowLogDate.Truncate(24 * time.Hour); !date.After(today); date = date.AddDate(0, 0, 1) {logs, err := driver.SyncSlowQuery(ctx, date)diff --git a/backend/server/server.go b/backend/server/server.goindex b7869904a0..b8ef3a1790 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -558,7 +558,8 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.licenseService,s.MetricReporter,s.secret,- s.stateCfg))+ s.stateCfg,+ s.dbFactory))v1pb.RegisterProjectServiceServer(s.grpcServer, v1.NewProjectService(s.store))v1pb.RegisterDatabaseServiceServer(s.grpcServer, v1.NewDatabaseService(s.store, s.BackupRunner))v1pb.RegisterInstanceRoleServiceServer(s.grpcServer, v1.NewInstanceRoleService(s.store, s.dbFactory))