Skip to content

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

feat: add slow query syncer (#5471)

rebelice8ebe22be6e40
diff --git a/backend/component/state/state.go b/backend/component/state/state.go
index 6e172cbc86..7990d64bf4 100644
--- a/backend/component/state/state.go
+++ b/backend/component/state/state.go
@@ -14,6 +14,8 @@ const InstanceMaximumConnectionNumber = 20
type State struct {
// InstanceDatabaseSyncChan is the channel for synchronizing schemas for instances.
InstanceDatabaseSyncChan chan *api.Instance
+ // InstanceSlowQuerySyncChan is the channel for synchronizing slow query logs for instances.
+ InstanceSlowQuerySyncChan chan *api.Instance
// RollbackGenerate is the set of tasks for generating rollback statements.
RollbackGenerate sync.Map // map[task.ID]*store.TaskMessage
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index a986e43951..7d06eff97c 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -328,7 +328,7 @@ func (*MockDriver) DeleteRole(_ context.Context, _ string) error {
}
// SyncSlowQuery implements the Driver interface.
-func (*MockDriver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*MockDriver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, nil
}
diff --git a/backend/plugin/db/clickhouse/sync.go b/backend/plugin/db/clickhouse/sync.go
index 6ece981a9a..5850af39ea 100644
--- a/backend/plugin/db/clickhouse/sync.go
+++ b/backend/plugin/db/clickhouse/sync.go
@@ -179,6 +179,6 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseMetada
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index 4fff400336..7717ef1010 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -475,7 +475,7 @@ type Driver interface {
// Sync slow query logs
// 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]map[string]*storepb.SlowQueryStatistics, error)
+ SyncSlowQuery(ctx context.Context, logDateTs time.Time) (map[string]*storepb.SlowQueryStatistics, error)
// Role
// CreateRole creates the role.
diff --git a/backend/plugin/db/mongodb/sync.go b/backend/plugin/db/mongodb/sync.go
index abdb02a19f..fb487db547 100644
--- a/backend/plugin/db/mongodb/sync.go
+++ b/backend/plugin/db/mongodb/sync.go
@@ -306,6 +306,6 @@ func convertEmptyInterfaceToInt64(value any) (int64, error) {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/mssql/sync.go b/backend/plugin/db/mssql/sync.go
index 24b8ffbad7..d77f454b5f 100644
--- a/backend/plugin/db/mssql/sync.go
+++ b/backend/plugin/db/mssql/sync.go
@@ -314,6 +314,6 @@ func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index 083727d801..8adc6ce204 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -10,6 +10,8 @@ import (
"time"
"go.uber.org/zap"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/pkg/errors"
@@ -464,7 +466,7 @@ type slowLog struct {
}
// SyncSlowQuery syncs slow query from mysql.slow_log.
-func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
logs := make([]*slowLog, 0, db.SlowQueryMaxSamplePerDay)
query := `
SELECT
@@ -488,11 +490,14 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (m
}
defer slowLogRows.Close()
for slowLogRows.Next() {
- var log slowLog
+ log := slowLog{
+ details: &storepb.SlowQueryDetails{},
+ }
+ var startTime, queryTime, lockTime string
if err := slowLogRows.Scan(
- &log.details.StartTime,
- &log.details.QueryTime,
- &log.details.LockTime,
+ &startTime,
+ &queryTime,
+ &lockTime,
&log.details.RowsSent,
&log.details.RowsExamined,
&log.database,
@@ -501,6 +506,24 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (m
return nil, err
}
+ startTimeTs, err := time.Parse("2006-01-02 15:04:05.999999", startTime)
+ if err != nil {
+ return nil, err
+ }
+ log.details.StartTime = timestamppb.New(startTimeTs)
+
+ queryTimeDuration, err := parseDuration(queryTime)
+ if err != nil {
+ return nil, err
+ }
+ log.details.QueryTime = durationpb.New(queryTimeDuration)
+
+ lockTimeDuration, err := parseDuration(lockTime)
+ if err != nil {
+ return nil, err
+ }
+ log.details.LockTime = durationpb.New(lockTimeDuration)
+
// Use Reservoir Sampling to sample slow logs.
// See https://en.wikipedia.org/wiki/Reservoir_sampling
if len(logs) < db.SlowQueryMaxSamplePerDay {
@@ -518,8 +541,20 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (m
return analyzeSlowLog(logs)
}
-func analyzeSlowLog(logs []*slowLog) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
- result := make(map[string]map[string]*storepb.SlowQueryStatistics)
+func parseDuration(s string) (time.Duration, error) {
+ if s == "" {
+ return 0, nil
+ }
+ list := strings.Split(s, ":")
+ if len(list) != 3 {
+ return 0, errors.Errorf("invalid duration: %s", s)
+ }
+ duration := fmt.Sprintf("%sh%sm%ss", list[0], list[1], list[2])
+ return time.ParseDuration(duration)
+}
+
+func analyzeSlowLog(logs []*slowLog) (map[string]*storepb.SlowQueryStatistics, error) {
+ logMap := make(map[string]map[string]*storepb.SlowQueryStatisticsItem)
for _, log := range logs {
databaseList := extractDatabase(log.database, log.details.SqlText)
@@ -535,21 +570,32 @@ func analyzeSlowLog(logs []*slowLog) (map[string]map[string]*storepb.SlowQuerySt
}
for _, db := range databaseList {
- var dbLog map[string]*storepb.SlowQueryStatistics
+ var dbLog map[string]*storepb.SlowQueryStatisticsItem
var exists bool
- if dbLog, exists = result[db]; !exists {
- dbLog = make(map[string]*storepb.SlowQueryStatistics)
- result[db] = dbLog
+ if dbLog, exists = logMap[db]; !exists {
+ dbLog = make(map[string]*storepb.SlowQueryStatisticsItem)
+ logMap[db] = dbLog
}
dbLog[fingerprint] = mergeSlowLog(fingerprint, dbLog[fingerprint], log.details)
}
}
+
+ var result = make(map[string]*storepb.SlowQueryStatistics)
+
+ for db, dblog := range logMap {
+ var statisticsList storepb.SlowQueryStatistics
+ for _, statistics := range dblog {
+ statisticsList.Items = append(statisticsList.Items, statistics)
+ }
+ result[db] = &statisticsList
+ }
+
return result, nil
}
-func mergeSlowLog(fingerprint string, statistics *storepb.SlowQueryStatistics, details *storepb.SlowQueryDetails) *storepb.SlowQueryStatistics {
+func mergeSlowLog(fingerprint string, statistics *storepb.SlowQueryStatisticsItem, details *storepb.SlowQueryDetails) *storepb.SlowQueryStatisticsItem {
if statistics == nil {
- return &storepb.SlowQueryStatistics{
+ return &storepb.SlowQueryStatisticsItem{
SqlFingerprint: fingerprint,
Count: 1,
LatestLogTime: details.StartTime,
@@ -586,5 +632,8 @@ func extractDatabase(defaultDB string, sql string) []string {
result = append(result, db)
}
}
+ if len(result) == 0 {
+ result = append(result, defaultDB)
+ }
return result
}
diff --git a/backend/plugin/db/oracle/sync.go b/backend/plugin/db/oracle/sync.go
index 98dc125e47..0a844fc782 100644
--- a/backend/plugin/db/oracle/sync.go
+++ b/backend/plugin/db/oracle/sync.go
@@ -329,6 +329,6 @@ func getViews(txn *sql.Tx) (map[string][]*storepb.ViewMetadata, error) {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index 47d0841639..003edf523d 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -603,6 +603,6 @@ func getIndexMethodType(stmt string) string {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/redis/sync.go b/backend/plugin/db/redis/sync.go
index 8728315c20..81c6fee87f 100644
--- a/backend/plugin/db/redis/sync.go
+++ b/backend/plugin/db/redis/sync.go
@@ -116,6 +116,6 @@ func (d *Driver) getDatabaseCount(ctx context.Context) (int, error) {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/redshift/sync.go b/backend/plugin/db/redshift/sync.go
index 1f812a377c..c14c707312 100644
--- a/backend/plugin/db/redshift/sync.go
+++ b/backend/plugin/db/redshift/sync.go
@@ -717,6 +717,6 @@ func (driver *Driver) getDatabases(ctx context.Context) ([]*storepb.DatabaseMeta
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/snowflake/sync.go b/backend/plugin/db/snowflake/sync.go
index f73fcf15e9..86aeefb006 100644
--- a/backend/plugin/db/snowflake/sync.go
+++ b/backend/plugin/db/snowflake/sync.go
@@ -295,6 +295,6 @@ func (driver *Driver) getTableSchema(ctx context.Context, database string) (map[
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/spanner/sync.go b/backend/plugin/db/spanner/sync.go
index 3c1efe8d70..e1df8b610a 100644
--- a/backend/plugin/db/spanner/sync.go
+++ b/backend/plugin/db/spanner/sync.go
@@ -371,6 +371,6 @@ func getView(ctx context.Context, tx *spanner.ReadOnlyTransaction) (map[string][
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/plugin/db/sqlite/sync.go b/backend/plugin/db/sqlite/sync.go
index 8e4c59357b..1273f270b8 100644
--- a/backend/plugin/db/sqlite/sync.go
+++ b/backend/plugin/db/sqlite/sync.go
@@ -263,6 +263,6 @@ func getViews(txn *sql.Tx) ([]*storepb.ViewMetadata, error) {
}
// SyncSlowQuery syncs the slow query.
-func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]map[string]*storepb.SlowQueryStatistics, error) {
+func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepb.SlowQueryStatistics, error) {
return nil, errors.Errorf("not implemented")
}
diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.go
new file mode 100644
index 0000000000..faca509641
--- /dev/null
+++ b/backend/runner/slowquerysync/syncer.go
@@ -0,0 +1,159 @@
+// Package slowquerysync is a runner that synchronize slow query logs.
+package slowquerysync
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+
+ "github.com/pkg/errors"
+ "go.uber.org/zap"
+
+ "github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/component/config"
+ "github.com/bytebase/bytebase/backend/component/dbfactory"
+ "github.com/bytebase/bytebase/backend/component/state"
+ api "github.com/bytebase/bytebase/backend/legacyapi"
+ "github.com/bytebase/bytebase/backend/store"
+)
+
+const (
+ slowQuerySyncInterval = 12 * time.Hour
+ // retentionCycle is the number of days to keep slow query logs.
+ retentionCycle = 30
+)
+
+// NewSyncer creates a new slow query syncer.
+func NewSyncer(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, profile config.Profile) *Syncer {
+ return &Syncer{
+ store: store,
+ dbFactory: dbFactory,
+ stateCfg: stateCfg,
+ profile: profile,
+ }
+}
+
+// Syncer is the slow query syncer.
+type Syncer struct {
+ store *store.Store
+ dbFactory *dbfactory.DBFactory
+ stateCfg *state.State
+ profile config.Profile
+}
+
+// Run will run the slow query syncer.
+func (s *Syncer) Run(ctx context.Context, wg *sync.WaitGroup) {
+ ticker := time.NewTicker(slowQuerySyncInterval)
+ defer ticker.Stop()
+ defer wg.Done()
+ log.Debug(fmt.Sprintf("Slow query syncer started and will run every %s", slowQuerySyncInterval.String()))
+ for {
+ select {
+ case <-ctx.Done():
+ log.Debug("Slow query syncer received context cancellation")
+ return
+ case instance := <-s.stateCfg.InstanceSlowQuerySyncChan:
+ log.Debug("Slow query syncer received instance slow query sync request", zap.String("instance", instance.ResourceID))
+ s.syncSlowQuery(ctx, instance)
+ case <-ticker.C:
+ log.Debug("Slow query syncer received tick")
+ s.syncSlowQuery(ctx, nil)
+ }
+ }
+}
+
+func (s *Syncer) syncSlowQuery(ctx context.Context, instance *api.Instance) {
+ defer func() {
+ if r := recover(); r != nil {
+ err, ok := r.(error)
+ if !ok {
+ err = errors.Errorf("%v", r)
+ }
+ log.Error("slow query syncer PANIC RECOVER", zap.Error(err), zap.Stack("panic-stack"))
+ }
+ }()
+
+ find := &store.FindInstanceMessage{}
+ if instance != nil {
+ find.UID = &instance.ID
+ }
+ instances, err := s.store.ListInstancesV2(ctx, find)
+ if err != nil {
+ log.Error("Failed to list instances", zap.Error(err))
+ return
+ }
+
+ var instanceWG sync.WaitGroup
+ for _, instance := range instances {
+ if instance.Deleted {
+ continue
+ }
+ instanceWG.Add(1)
+ go func(instance *store.InstanceMessage) {
+ defer instanceWG.Done()
+ if err := s.syncInstanceSlowQuery(ctx, instance); err != nil {
+ log.Debug("Failed to sync instance slow query",
+ zap.String("instance", instance.ResourceID),
+ zap.Error(err))
+ }
+ }(instance)
+ }
+ instanceWG.Wait()
+}
+
+func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
+ slowQueryPolicy, err := s.store.GetSlowQueryPolicy(ctx, api.PolicyResourceTypeInstance, instance.UID)
+ if err != nil {
+ return err
+ }
+ if slowQueryPolicy == nil || !slowQueryPolicy.Active {
+ return nil
+ }
+
+ today := time.Now().Truncate(24 * time.Hour)
+
+ earliestDate := today.AddDate(0, 0, -retentionCycle)
+
+ if err := s.store.DeleteOutdatedSlowLog(ctx, instance.UID, earliestDate); err != nil {
+ return err
+ }
+
+ latestSlowLogDate, err := s.store.GetLatestSlowLogDate(ctx, instance.UID)
+ if err != nil {
+ return err
+ }
+
+ if latestSlowLogDate == nil || latestSlowLogDate.AddDate(0, 0, retentionCycle).Before(today) {
+ latestSlowLogDate = &earliestDate
+ }
+
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "")
+ if err != nil {
+ return err
+ }
+ defer driver.Close(ctx)
+
+ for date := latestSlowLogDate.Truncate(24 * time.Hour); !date.After(today); date = date.AddDate(0, 0, 1) {
+ logs, err := driver.SyncSlowQuery(ctx, date)
+ if err != nil {
+ return err
+ }
+
+ for dbName, slowLog := range logs {
+ if err := s.store.UpsertSlowLog(ctx, &store.UpsertSlowLogMessage{
+ EnvironmentID: &instance.EnvironmentID,
+ InstanceID: &instance.ResourceID,
+ DatabaseName: dbName,
+ InstanceUID: instance.UID,
+ LogDate: date,
+ SlowLog: slowLog,
+ UpdaterID: api.SystemBotID,
+ }); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
diff --git a/backend/server/server.go b/backend/server/server.go
index a18e8f1e58..298fc61513 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -70,6 +70,7 @@ import (
"github.com/bytebase/bytebase/backend/runner/metricreport"
"github.com/bytebase/bytebase/backend/runner/rollbackrun"
"github.com/bytebase/bytebase/backend/runner/schemasync"
+ "github.com/bytebase/bytebase/backend/runner/slowquerysync"
"github.com/bytebase/bytebase/backend/runner/taskcheck"
"github.com/bytebase/bytebase/backend/runner/taskrun"
"github.com/bytebase/bytebase/backend/store"
@@ -139,6 +140,7 @@ type Server struct {
TaskCheckScheduler *taskcheck.Scheduler
MetricReporter *metricreport.Reporter
SchemaSyncer *schemasync.Syncer
+ SlowQuerySyncer *slowquerysync.Syncer
BackupRunner *backuprun.Runner
AnomalyScanner *anomaly.Scanner
ApplicationRunner *apprun.Runner
@@ -309,6 +311,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.stateCfg = &state.State{
InstanceDatabaseSyncChan: make(chan *api.Instance, 100),
+ InstanceSlowQuerySyncChan: make(chan *api.Instance, 100),
InstanceOutstandingConnections: make(map[int]int),
}
s.store = storeInstance
@@ -405,6 +408,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.MetricReporter = metricreport.NewReporter(s.store, s.licenseService, s.profile, false)
if !profile.Readonly {
s.SchemaSyncer = schemasync.NewSyncer(storeInstance, s.dbFactory, s.stateCfg, profile)
+ s.SlowQuerySyncer = slowquerysync.NewSyncer(storeInstance, s.dbFactory, s.stateCfg, profile)
// TODO(p0ny): enable Feishu provider only when it is needed.
s.feishuProvider = feishu.NewProvider(profile.FeishuAPIURL)
s.ApplicationRunner = apprun.NewRunner(storeInstance, s.ActivityManager, s.feishuProvider, profile)
@@ -833,6 +837,8 @@ func (s *Server) Run(ctx context.Context, port int) error {
s.runnerWG.Add(1)
go s.SchemaSyncer.Run(ctx, &s.runnerWG)
s.runnerWG.Add(1)
+ go s.SlowQuerySyncer.Run(ctx, &s.runnerWG)
+ s.runnerWG.Add(1)
go s.BackupRunner.Run(ctx, &s.runnerWG)
s.runnerWG.Add(1)
go s.AnomalyScanner.Run(ctx, &s.runnerWG)
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 42a007f474..09a26cc87c 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -119,6 +119,25 @@ func (s *Store) GetAccessControlPolicy(ctx context.Context, resourceType api.Pol
return accessControlPolicy, policy.InheritFromParent, nil
}
+// GetSlowQueryPolicy will get the slow query policy for instance ID.
+func (s *Store) GetSlowQueryPolicy(ctx context.Context, resourceType api.PolicyResourceType, resourceID int) (*api.SlowQueryPolicy, error) {
+ pType := api.PolicyTypeSlowQuery
+ policy, err := s.GetPolicyV2(ctx, &FindPolicyMessage{
+ ResourceType: &resourceType,
+ ResourceUID: &resourceID,
+ Type: &pType,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ if policy == nil {
+ return &api.SlowQueryPolicy{Active: false}, nil
+ }
+
+ return api.UnmarshalSlowQueryPolicy(policy.Payload)
+}
+
// PolicyMessage is the mssage for policy.
type PolicyMessage struct {
ResourceUID int
diff --git a/backend/store/slow_query.go b/backend/store/slow_query.go
new file mode 100644
index 0000000000..d1affccad9
--- /dev/null
+++ b/backend/store/slow_query.go
@@ -0,0 +1,173 @@
+package store
+
+import (
+ "context"
+ "database/sql"
+ "strconv"
+ "time"
+
+ "google.golang.org/protobuf/encoding/protojson"
+
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+// UpsertSlowLogMessage is the message to upsert slow query logs.
+type UpsertSlowLogMessage struct {
+ // We need EnvironmentID, InstanceID, and DatabaseName to find the database UID.
+ EnvironmentID *string
+ InstanceID *string
+ DatabaseName string
+
+ InstanceUID int
+ LogDate time.Time
+ SlowLog *storepb.SlowQueryStatistics
+
+ UpdaterID int
+}
+
+// UpsertSlowLog upserts slow query logs.
+func (s *Store) UpsertSlowLog(ctx context.Context, upsert *UpsertSlowLogMessage) error {
+ var databaseUID sql.NullInt32
+ if upsert.DatabaseName != "" {
+ database, err := s.GetDatabaseV2(ctx, &FindDatabaseMessage{
+ EnvironmentID: upsert.EnvironmentID,
+ InstanceID: upsert.InstanceID,
+ DatabaseName: &upsert.DatabaseName,
+ })
+ if err != nil {
+ return err
+ }
+ if database != nil {
+ databaseUID.Int32 = int32(database.UID)
+ databaseUID.Valid = true
+ }
+ }
+
+ logDate, err := strconv.Atoi(upsert.LogDate.Format("20060102"))
+ if err != nil {
+ return err
+ }
+
+ logBytes, err := protojson.Marshal(upsert.SlowLog)
+ if err != nil {
+ return err
+ }
+
+ query := `
+ INSERT INTO slow_query (
+ creator_id,
+ updater_id,
+ instance_id,
+ database_id,
+ log_date_ts,
+ slow_query_statistics
+ ) VALUES ( $1, $2, $3, $4, $5, $6 )
+ ON CONFLICT (database_id, log_date_ts) DO UPDATE SET
+ updater_id = EXCLUDED.updater_id,
+ slow_query_statistics = EXCLUDED.slow_query_statistics
+ `
+
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ if _, err := tx.ExecContext(ctx, query,
+ upsert.UpdaterID,
+ upsert.UpdaterID,
+ upsert.InstanceUID,
+ databaseUID,
+ logDate,
+ logBytes,
+ ); err != nil {
+ return err
+ }
+
+ return tx.Commit()
+}
+
+// DeleteOutdatedSlowLog deletes outdated slow query logs.
+func (s *Store) DeleteOutdatedSlowLog(ctx context.Context, instanceUID int, earliestDate time.Time) error {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ if err := s.deleteSlowLogImpl(ctx, tx, instanceUID, earliestDate); err != nil {
+ return err
+ }
+
+ return tx.Commit()
+}
+
+func (*Store) deleteSlowLogImpl(ctx context.Context, tx *Tx, instanceUID int, earliestDate time.Time) error {
+ if _, err := tx.ExecContext(ctx, `
+ DELETE FROM
+ slow_query
+ WHERE
+ instance_id = $1
+ AND log_date_ts < $2`,
+ instanceUID,
+ earliestDate.Format("20060102"),
+ ); err != nil {
+ return err
+ }
+ return nil
+}
+
+// GetLatestSlowLogDate returns the latest slow query log date.
+func (s *Store) GetLatestSlowLogDate(ctx context.Context, instanceUID int) (*time.Time, error) {
+ tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
+ if err != nil {
+ return nil, err
+ }
+ defer tx.Rollback()
+
+ result, err := s.getLatestSlowLogDateImpl(ctx, tx, instanceUID)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, err
+ }
+ return result, nil
+}
+
+func (*Store) getLatestSlowLogDateImpl(ctx context.Context, tx *Tx, instanceUID int) (*time.Time, error) {
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ MAX(log_date_ts)
+ FROM
+ slow_query
+ WHERE
+ instance_id = $1`,
+ instanceUID,
+ )
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var result *time.Time
+ for rows.Next() {
+ var logDate sql.NullInt32
+ if err := rows.Scan(&logDate); err != nil {
+ return nil, err
+ }
+ if logDate.Valid {
+ t, err := time.Parse("20060102", strconv.Itoa(int(logDate.Int32)))
+ if err != nil {
+ return nil, err
+ }
+ result = &t
+ } else {
+ result = nil
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return result, nil
+}
diff --git a/frontend/src/types/proto/store/slow_query.ts b/frontend/src/types/proto/store/slow_query.ts
index be678c2b63..b27fa293eb 100644
--- a/frontend/src/types/proto/store/slow_query.ts
+++ b/frontend/src/types/proto/store/slow_query.ts
@@ -5,8 +5,14 @@ import { Timestamp } from "../google/protobuf/timestamp";
export const protobufPackage = "bytebase.store";
-/** SlowQueryStatistics is a summary of slow queries. */
+/** SlowQueryStatistics is the slow query statistics. */
export interface SlowQueryStatistics {
+ /** Items is the list of slow query statistics. */
+ items: SlowQueryStatisticsItem[];
+}
+
+/** SlowQueryStatisticsItem is the item of slow query statistics. */
+export interface SlowQueryStatisticsItem {
/** sql_fingerprint is the fingerprint of the slow query. */
sqlFingerprint: string;
/** count is the number of slow queries with the same fingerprint. */
@@ -34,11 +40,73 @@ export interface SlowQueryDetails {
}
function createBaseSlowQueryStatistics(): SlowQueryStatistics {
- return { sqlFingerprint: "", count: 0, latestLogTime: undefined, samples: [] };
+ return { items: [] };
}
export const SlowQueryStatistics = {
encode(message: SlowQueryStatistics, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
+ for (const v of message.items) {
+ SlowQueryStatisticsItem.encode(v!, writer.uint32(10).fork()).ldelim();
+ }
+ return writer;
+ },
+
+ decode(input: _m0.Reader | Uint8Array, length?: number): SlowQueryStatistics {
+ const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
+ let end = length === undefined ? reader.len : reader.pos + length;
+ const message = createBaseSlowQueryStatistics();
+ while (reader.pos < end) {
+ const tag = reader.uint32();
+ switch (tag >>> 3) {
+ case 1:
+ if (tag != 10) {
+ break;
+ }
+
+ message.items.push(SlowQueryStatisticsItem.decode(reader, reader.uint32()));
+ continue;
+ }
+ if ((tag & 7) == 4 || tag == 0) {
+ break;
+ }
+ reader.skipType(tag & 7);
+ }
+ return message;
+ },
+
+ fromJSON(object: any): SlowQueryStatistics {
+ return {
+ items: Array.isArray(object?.items) ? object.items.map((e: any) => SlowQueryStatisticsItem.fromJSON(e)) : [],
+ };
+ },
+
+ toJSON(message: SlowQueryStatistics): unknown {
+ const obj: any = {};
+ if (message.items) {
+ obj.items = message.items.map((e) => e ? SlowQueryStatisticsItem.toJSON(e) : undefined);
+ } else {
+ obj.items = [];
+ }
+ return obj;
+ },
+
+ create(base?: DeepPartial<SlowQueryStatistics>): SlowQueryStatistics {
+ return SlowQueryStatistics.fromPartial(base ?? {});
+ },
+
+ fromPartial(object: DeepPartial<SlowQueryStatistics>): SlowQueryStatistics {
+ const message = createBaseSlowQueryStatistics();
+ message.items = object.items?.map((e) => SlowQueryStatisticsItem.fromPartial(e)) || [];
+ return message;
+ },
+};
+
+function createBaseSlowQueryStatisticsItem(): SlowQueryStatisticsItem {
+ return { sqlFingerprint: "", count: 0, latestLogTime: undefined, samples: [] };
+}
+
+export const SlowQueryStatisticsItem = {
+ encode(message: SlowQueryStatisticsItem, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.sqlFingerprint !== "") {
writer.uint32(10).string(message.sqlFingerprint);
}
@@ -54,10 +122,10 @@ export const SlowQueryStatistics = {
return writer;
},
- decode(input: _m0.Reader | Uint8Array, length?: number): SlowQueryStatistics {
+ decode(input: _m0.Reader | Uint8Array, length?: number): SlowQueryStatisticsItem {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
- const message = createBaseSlowQueryStatistics();
+ const message = createBaseSlowQueryStatisticsItem();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
@@ -98,7 +166,7 @@ export const SlowQueryStatistics = {
return message;
},
- fromJSON(object: any): SlowQueryStatistics {
+ fromJSON(object: any): SlowQueryStatisticsItem {
return {
sqlFingerprint: isSet(object.sqlFingerprint) ? String(object.sqlFingerprint) : "",
count: isSet(object.count) ? Number(object.count) : 0,
@@ -107,7 +175,7 @@ export const SlowQueryStatistics = {
};
},
- toJSON(message: SlowQueryStatistics): unknown {
+ toJSON(message: SlowQueryStatisticsItem): unknown {
const obj: any = {};
message.sqlFingerprint !== undefined && (obj.sqlFingerprint = message.sqlFingerprint);
message.count !== undefined && (obj.count = Math.round(message.count));
@@ -120,12 +188,12 @@ export const SlowQueryStatistics = {
return obj;
},
- create(base?: DeepPartial<SlowQueryStatistics>): SlowQueryStatistics {
- return SlowQueryStatistics.fromPartial(base ?? {});
+ create(base?: DeepPartial<SlowQueryStatisticsItem>): SlowQueryStatisticsItem {
+ return SlowQueryStatisticsItem.fromPartial(base ?? {});
},
- fromPartial(object: DeepPartial<SlowQueryStatistics>): SlowQueryStatistics {
- const message = createBaseSlowQueryStatistics();
+ fromPartial(object: DeepPartial<SlowQueryStatisticsItem>): SlowQueryStatisticsItem {
+ const message = createBaseSlowQueryStatisticsItem();
message.sqlFingerprint = object.sqlFingerprint ?? "";
message.count = object.count ?? 0;
message.latestLogTime = object.latestLogTime ?? undefined;
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index b01586b326..382d1c262e 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -64,6 +64,7 @@
- [store/slow_query.proto](#store_slow_query-proto)
- [SlowQueryDetails](#bytebase-store-SlowQueryDetails)
- [SlowQueryStatistics](#bytebase-store-SlowQueryStatistics)
+ - [SlowQueryStatisticsItem](#bytebase-store-SlowQueryStatisticsItem)
- [store/user.proto](#store_user-proto)
- [MFAConfig](#bytebase-store-MFAConfig)
@@ -925,7 +926,22 @@ SlowQueryDetails is the details of a slow query.
<a name="bytebase-store-SlowQueryStatistics"></a>
### SlowQueryStatistics
-SlowQueryStatistics is a summary of slow queries.
+SlowQueryStatistics is the slow query statistics.
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| items | [SlowQueryStatisticsItem](#bytebase-store-SlowQueryStatisticsItem) | repeated | Items is the list of slow query statistics. |
+
+
+
+
+
+
+<a name="bytebase-store-SlowQueryStatisticsItem"></a>
+
+### SlowQueryStatisticsItem
+SlowQueryStatisticsItem is the item of slow query statistics.
| Field | Type | Label | Description |
diff --git a/proto/generated-go/store/slow_query.pb.go b/proto/generated-go/store/slow_query.pb.go
index 5ea4f2d9ff..772da4fec3 100644
--- a/proto/generated-go/store/slow_query.pb.go
+++ b/proto/generated-go/store/slow_query.pb.go
@@ -22,20 +22,14 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
-// SlowQueryStatistics is a summary of slow queries.
+// SlowQueryStatistics is the slow query statistics.
type SlowQueryStatistics struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
- // sql_fingerprint is the fingerprint of the slow query.
- SqlFingerprint string `protobuf:"bytes,1,opt,name=sql_fingerprint,json=sqlFingerprint,proto3" json:"sql_fingerprint,omitempty"`
- // count is the number of slow queries with the same fingerprint.
- Count int32 `protobuf:"varint,2,opt,name=count,proto3" json:"count,omitempty"`
- // latest_log_time is the time of the latest slow query with the same fingerprint.
- LatestLogTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=latest_log_time,json=latestLogTime,proto3" json:"latest_log_time,omitempty"`
- // samples are the details of the sample slow queries with the same fingerprint.
- Samples []*SlowQueryDetails `protobuf:"bytes,4,rep,name=samples,proto3" json:"samples,omitempty"`
+ // Items is the list of slow query statistics.
+ Items []*SlowQueryStatisticsItem `protobuf:"bytes,1,rep,name=items,proto3" json:"items,omitempty"`
}
func (x *SlowQueryStatistics) Reset() {
@@ -70,28 +64,83 @@ func (*SlowQueryStatistics) Descriptor() ([]byte, []int) {
return file_store_slow_query_proto_rawDescGZIP(), []int{0}
}
-func (x *SlowQueryStatistics) GetSqlFingerprint() string {
+func (x *SlowQueryStatistics) GetItems() []*SlowQueryStatisticsItem {
+ if x != nil {
+ return x.Items
+ }
+ return nil
+}
+
+// SlowQueryStatisticsItem is the item of slow query statistics.
+type SlowQueryStatisticsItem struct {
+ state protoimpl.MessageState
+ sizeCache protoimpl.SizeCache
+ unknownFields protoimpl.UnknownFields
+
+ // sql_fingerprint is the fingerprint of the slow query.
+ SqlFingerprint string `protobuf:"bytes,1,opt,name=sql_fingerprint,json=sqlFingerprint,proto3" json:"sql_fingerprint,omitempty"`
+ // count is the number of slow queries with the same fingerprint.
+ Count int32 `protobuf:"varint,2,opt,name=count,proto3" json:"count,omitempty"`
+ // latest_log_time is the time of the latest slow query with the same fingerprint.
+ LatestLogTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=latest_log_time,json=latestLogTime,proto3" json:"latest_log_time,omitempty"`
+ // samples are the details of the sample slow queries with the same fingerprint.
+ Samples []*SlowQueryDetails `protobuf:"bytes,4,rep,name=samples,proto3" json:"samples,omitempty"`
+}
+
+func (x *SlowQueryStatisticsItem) Reset() {
+ *x = SlowQueryStatisticsItem{}
+ if protoimpl.UnsafeEnabled {
+ mi := &file_store_slow_query_proto_msgTypes[1]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+ }
+}
+
+func (x *SlowQueryStatisticsItem) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*SlowQueryStatisticsItem) ProtoMessage() {}
+
+func (x *SlowQueryStatisticsItem) ProtoReflect() protoreflect.Message {
+ mi := &file_store_slow_query_proto_msgTypes[1]
+ if protoimpl.UnsafeEnabled && x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use SlowQueryStatisticsItem.ProtoReflect.Descriptor instead.
+func (*SlowQueryStatisticsItem) Descriptor() ([]byte, []int) {
+ return file_store_slow_query_proto_rawDescGZIP(), []int{1}
+}
+
+func (x *SlowQueryStatisticsItem) GetSqlFingerprint() string {
if x != nil {
return x.SqlFingerprint
}
return ""
}
-func (x *SlowQueryStatistics) GetCount() int32 {
+func (x *SlowQueryStatisticsItem) GetCount() int32 {
if x != nil {
return x.Count
}
return 0
}
-func (x *SlowQueryStatistics) GetLatestLogTime() *timestamppb.Timestamp {
+func (x *SlowQueryStatisticsItem) GetLatestLogTime() *timestamppb.Timestamp {
if x != nil {
return x.LatestLogTime
}
return nil
}
-func (x *SlowQueryStatistics) GetSamples() []*SlowQueryDetails {
+func (x *SlowQueryStatisticsItem) GetSamples() []*SlowQueryDetails {
if x != nil {
return x.Samples
}
@@ -121,7 +170,7 @@ type SlowQueryDetails struct {
func (x *SlowQueryDetails) Reset() {
*x = SlowQueryDetails{}
if protoimpl.UnsafeEnabled {
- mi := &file_store_slow_query_proto_msgTypes[1]
+ mi := &file_store_slow_query_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -134,7 +183,7 @@ func (x *SlowQueryDetails) String() string {
func (*SlowQueryDetails) ProtoMessage() {}
func (x *SlowQueryDetails) ProtoReflect() protoreflect.Message {
- mi := &file_store_slow_query_proto_msgTypes[1]
+ mi := &file_store_slow_query_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -147,7 +196,7 @@ func (x *SlowQueryDetails) ProtoReflect() protoreflect.Message {
// Deprecated: Use SlowQueryDetails.ProtoReflect.Descriptor instead.
func (*SlowQueryDetails) Descriptor() ([]byte, []int) {
- return file_store_slow_query_proto_rawDescGZIP(), []int{1}
+ return file_store_slow_query_proto_rawDescGZIP(), []int{2}
}
func (x *SlowQueryDetails) GetStartTime() *timestamppb.Timestamp {
@@ -201,40 +250,46 @@ var file_store_slow_query_proto_rawDesc = []byte{
0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74,
0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x75, 0x72, 0x61, 0x74,
- 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xd4, 0x01, 0x0a, 0x13, 0x53, 0x6c,
- 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
- 0x73, 0x12, 0x27, 0x0a, 0x0f, 0x73, 0x71, 0x6c, 0x5f, 0x66, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x70,
- 0x72, 0x69, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x73, 0x71, 0x6c, 0x46,
- 0x69, 0x6e, 0x67, 0x65, 0x72, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f,
- 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74,
- 0x12, 0x42, 0x0a, 0x0f, 0x6c, 0x61, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6c, 0x6f, 0x67, 0x5f, 0x74,
- 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
- 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65,
- 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0d, 0x6c, 0x61, 0x74, 0x65, 0x73, 0x74, 0x4c, 0x6f, 0x67,
- 0x54, 0x69, 0x6d, 0x65, 0x12, 0x3a, 0x0a, 0x07, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x18,
- 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65,
- 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79,
- 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x52, 0x07, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73,
- 0x22, 0x9c, 0x02, 0x0a, 0x10, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x44, 0x65,
- 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, 0x39, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74,
- 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
- 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65,
- 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x73, 0x74, 0x61, 0x72, 0x74, 0x54, 0x69, 0x6d, 0x65,
- 0x12, 0x38, 0x0a, 0x0a, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02,
- 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72,
- 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52,
- 0x09, 0x71, 0x75, 0x65, 0x72, 0x79, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x36, 0x0a, 0x09, 0x6c, 0x6f,
- 0x63, 0x6b, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e,
- 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e,
- 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x6c, 0x6f, 0x63, 0x6b, 0x54, 0x69,
- 0x6d, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x6f, 0x77, 0x73, 0x5f, 0x73, 0x65, 0x6e, 0x74, 0x18,
- 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x72, 0x6f, 0x77, 0x73, 0x53, 0x65, 0x6e, 0x74, 0x12,
- 0x23, 0x0a, 0x0d, 0x72, 0x6f, 0x77, 0x73, 0x5f, 0x65, 0x78, 0x61, 0x6d, 0x69, 0x6e, 0x65, 0x64,
- 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0c, 0x72, 0x6f, 0x77, 0x73, 0x45, 0x78, 0x61, 0x6d,
- 0x69, 0x6e, 0x65, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x71, 0x6c, 0x5f, 0x74, 0x65, 0x78, 0x74,
- 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x71, 0x6c, 0x54, 0x65, 0x78, 0x74, 0x42,
- 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f,
- 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+ 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x54, 0x0a, 0x13, 0x53, 0x6c, 0x6f,
+ 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
+ 0x12, 0x3d, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32,
+ 0x27, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65,
+ 0x2e, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73,
+ 0x74, 0x69, 0x63, 0x73, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x22,
+ 0xd8, 0x01, 0x0a, 0x17, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61,
+ 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73, 0x49, 0x74, 0x65, 0x6d, 0x12, 0x27, 0x0a, 0x0f, 0x73,
+ 0x71, 0x6c, 0x5f, 0x66, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x18, 0x01,
+ 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x73, 0x71, 0x6c, 0x46, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x70,
+ 0x72, 0x69, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20,
+ 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x42, 0x0a, 0x0f, 0x6c, 0x61,
+ 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6c, 0x6f, 0x67, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20,
+ 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
+ 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52,
+ 0x0d, 0x6c, 0x61, 0x74, 0x65, 0x73, 0x74, 0x4c, 0x6f, 0x67, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x3a,
+ 0x0a, 0x07, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32,
+ 0x20, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65,
+ 0x2e, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c,
+ 0x73, 0x52, 0x07, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x22, 0x9c, 0x02, 0x0a, 0x10, 0x53,
+ 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x79, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12,
+ 0x39, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20,
+ 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
+ 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52,
+ 0x09, 0x73, 0x74, 0x61, 0x72, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x38, 0x0a, 0x0a, 0x71, 0x75,
+ 0x65, 0x72, 0x79, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19,
+ 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
+ 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x71, 0x75, 0x65, 0x72, 0x79,
+ 0x54, 0x69, 0x6d, 0x65, 0x12, 0x36, 0x0a, 0x09, 0x6c, 0x6f, 0x63, 0x6b, 0x5f, 0x74, 0x69, 0x6d,
+ 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
+ 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69,
+ 0x6f, 0x6e, 0x52, 0x08, 0x6c, 0x6f, 0x63, 0x6b, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1b, 0x0a, 0x09,
+ 0x72, 0x6f, 0x77, 0x73, 0x5f, 0x73, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52,
+ 0x08, 0x72, 0x6f, 0x77, 0x73, 0x53, 0x65, 0x6e, 0x74, 0x12, 0x23, 0x0a, 0x0d, 0x72, 0x6f, 0x77,
+ 0x73, 0x5f, 0x65, 0x78, 0x61, 0x6d, 0x69, 0x6e, 0x65, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05,
+ 0x52, 0x0c, 0x72, 0x6f, 0x77, 0x73, 0x45, 0x78, 0x61, 0x6d, 0x69, 0x6e, 0x65, 0x64, 0x12, 0x19,
+ 0x0a, 0x08, 0x73, 0x71, 0x6c, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09,
+ 0x52, 0x07, 0x73, 0x71, 0x6c, 0x54, 0x65, 0x78, 0x74, 0x42, 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e,
+ 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62,
+ 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -249,24 +304,26 @@ func file_store_slow_query_proto_rawDescGZIP() []byte {
return file_store_slow_query_proto_rawDescData
}
-var file_store_slow_query_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
+var file_store_slow_query_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_store_slow_query_proto_goTypes = []interface{}{
- (*SlowQueryStatistics)(nil), // 0: bytebase.store.SlowQueryStatistics
- (*SlowQueryDetails)(nil), // 1: bytebase.store.SlowQueryDetails
- (*timestamppb.Timestamp)(nil), // 2: google.protobuf.Timestamp
- (*durationpb.Duration)(nil), // 3: google.protobuf.Duration
+ (*SlowQueryStatistics)(nil), // 0: bytebase.store.SlowQueryStatistics
+ (*SlowQueryStatisticsItem)(nil), // 1: bytebase.store.SlowQueryStatisticsItem
+ (*SlowQueryDetails)(nil), // 2: bytebase.store.SlowQueryDetails
+ (*timestamppb.Timestamp)(nil), // 3: google.protobuf.Timestamp
+ (*durationpb.Duration)(nil), // 4: google.protobuf.Duration
}
var file_store_slow_query_proto_depIdxs = []int32{
- 2, // 0: bytebase.store.SlowQueryStatistics.latest_log_time:type_name -> google.protobuf.Timestamp
- 1, // 1: bytebase.store.SlowQueryStatistics.samples:type_name -> bytebase.store.SlowQueryDetails
- 2, // 2: bytebase.store.SlowQueryDetails.start_time:type_name -> google.protobuf.Timestamp
- 3, // 3: bytebase.store.SlowQueryDetails.query_time:type_name -> google.protobuf.Duration
- 3, // 4: bytebase.store.SlowQueryDetails.lock_time:type_name -> google.protobuf.Duration
- 5, // [5:5] is the sub-list for method output_type
- 5, // [5:5] is the sub-list for method input_type
- 5, // [5:5] is the sub-list for extension type_name
- 5, // [5:5] is the sub-list for extension extendee
- 0, // [0:5] is the sub-list for field type_name
+ 1, // 0: bytebase.store.SlowQueryStatistics.items:type_name -> bytebase.store.SlowQueryStatisticsItem
+ 3, // 1: bytebase.store.SlowQueryStatisticsItem.latest_log_time:type_name -> google.protobuf.Timestamp
+ 2, // 2: bytebase.store.SlowQueryStatisticsItem.samples:type_name -> bytebase.store.SlowQueryDetails
+ 3, // 3: bytebase.store.SlowQueryDetails.start_time:type_name -> google.protobuf.Timestamp
+ 4, // 4: bytebase.store.SlowQueryDetails.query_time:type_name -> google.protobuf.Duration
+ 4, // 5: bytebase.store.SlowQueryDetails.lock_time:type_name -> google.protobuf.Duration
+ 6, // [6:6] is the sub-list for method output_type
+ 6, // [6:6] is the sub-list for method input_type
+ 6, // [6:6] is the sub-list for extension type_name
+ 6, // [6:6] is the sub-list for extension extendee
+ 0, // [0:6] is the sub-list for field type_name
}
func init() { file_store_slow_query_proto_init() }
@@ -288,6 +345,18 @@ func file_store_slow_query_proto_init() {
}
}
file_store_slow_query_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
+ switch v := v.(*SlowQueryStatisticsItem); i {
+ case 0:
+ return &v.state
+ case 1:
+ return &v.sizeCache
+ case 2:
+ return &v.unknownFields
+ default:
+ return nil
+ }
+ }
+ file_store_slow_query_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SlowQueryDetails); i {
case 0:
return &v.state
@@ -306,7 +375,7 @@ func file_store_slow_query_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_store_slow_query_proto_rawDesc,
NumEnums: 0,
- NumMessages: 2,
+ NumMessages: 3,
NumExtensions: 0,
NumServices: 0,
},
diff --git a/proto/store/slow_query.proto b/proto/store/slow_query.proto
index 122de5a560..7898b34de8 100644
--- a/proto/store/slow_query.proto
+++ b/proto/store/slow_query.proto
@@ -7,8 +7,14 @@ import "google/protobuf/duration.proto";
option go_package = "generated-go/store";
-// SlowQueryStatistics is a summary of slow queries.
+// SlowQueryStatistics is the slow query statistics.
message SlowQueryStatistics {
+ // Items is the list of slow query statistics.
+ repeated SlowQueryStatisticsItem items = 1;
+}
+
+// SlowQueryStatisticsItem is the item of slow query statistics.
+message SlowQueryStatisticsItem {
// sql_fingerprint is the fingerprint of the slow query.
string sql_fingerprint = 1;