Skip to content

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

chore: use UTC for slow query logs (#5754)

rebelice53dbaf5cd67d
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 97dddf319f..f0f8ec5435 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -772,7 +772,7 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid start_time filter %q", expr.value)
}
- t = t.AddDate(0, 0, 1)
+ t = t.AddDate(0, 0, 1).UTC()
startLogDate = &t
case comparatorTypeGreaterEqual:
if startLogDate != nil {
@@ -782,6 +782,7 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid start_time filter %q", expr.value)
}
+ t = t.UTC()
startLogDate = &t
case comparatorTypeLess:
if endLogDate != nil {
@@ -791,6 +792,7 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid start_time filter %q", expr.value)
}
+ t = t.UTC()
endLogDate = &t
case comparatorTypeLessEqual:
if endLogDate != nil {
@@ -800,7 +802,7 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid start_time filter %q", expr.value)
}
- t = t.AddDate(0, 0, 1)
+ t = t.AddDate(0, 0, 1).UTC()
endLogDate = &t
default:
return nil, status.Errorf(codes.InvalidArgument, "invalid start_time filter %q %q %q", expr.key, expr.comparator, expr.value)
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index a5e970e48e..4d307e1a5f 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -476,7 +476,7 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, logDateTs time.Time) (m
logs := make([]*slowLog, 0, db.SlowQueryMaxSamplePerDay)
query := `
SELECT
- start_time,
+ convert_tz(start_time, @@log_timestamps, 'UTC'),
query_time,
lock_time,
rows_sent,
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index 536f4ce787..2dbae683f5 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -715,7 +715,7 @@ func (driver *Driver) SyncSlowQuery(ctx context.Context, _ time.Time) (map[strin
item := storepb.SlowQueryStatisticsItem{
SqlFingerprint: fingerprint,
Count: calls,
- LatestLogTime: timestamppb.New(now),
+ LatestLogTime: timestamppb.New(now.UTC()),
TotalQueryTime: durationpb.New(time.Duration(totalExecTime * float64(time.Millisecond))),
MaximumQueryTime: durationpb.New(time.Duration(maxExecTime * float64(time.Millisecond))),
TotalRowsSent: rows,
diff --git a/backend/runner/mail/slow_query_weekly_mail.go b/backend/runner/mail/slow_query_weekly_mail.go
index 6af71901bd..879d2ea036 100644
--- a/backend/runner/mail/slow_query_weekly_mail.go
+++ b/backend/runner/mail/slow_query_weekly_mail.go
@@ -251,8 +251,8 @@ func (s *SlowQueryWeeklyMailSender) generateWeeklyEmailForProject(ctx context.Co
endDate := now.AddDate(0, 0, -1)
var buf bytes.Buffer
headerString := strings.ReplaceAll(string(header), "{{PROJECT_NAME}}", project.Title)
- headerString = strings.ReplaceAll(headerString, "{{BEGIN_DATE}}", beginDate.Format("2006.01.02"))
- headerString = strings.ReplaceAll(headerString, "{{END_DATE}}", endDate.Format("2006.01.02"))
+ headerString = strings.ReplaceAll(headerString, "{{BEGIN_DATE}}", beginDate.UTC().Format("2006.01.02"))
+ headerString = strings.ReplaceAll(headerString, "{{END_DATE}}", endDate.UTC().Format("2006.01.02"))
beginUnix := beginDate.Truncate(24 * time.Hour).Unix()
endUnix := now.Truncate(24 * time.Hour).Add(-1 * time.Second).Unix()
projectURL := fmt.Sprintf("%s/slow-query?project=%d&fromTime=%d&toTime=%d", strings.TrimSuffix(visitURL, "/"), project.UID, beginUnix, endUnix)
@@ -594,8 +594,8 @@ func (s *SlowQueryWeeklyMailSender) generateEnvironmentContent(
if !hasSlowQueryInInstance {
hasSlowQueryInInstance = true
- beginUnix := beginDate.Truncate(24 * time.Hour).Unix()
- endUnix := endDate.Truncate(24 * time.Hour).Add(-1 * time.Second).Unix()
+ beginUnix := beginDate.Truncate(24 * time.Hour).UTC().Unix()
+ endUnix := endDate.Truncate(24 * time.Hour).Add(-1 * time.Second).UTC().Unix()
instanceURL := fmt.Sprintf("%s/slow-query?instance=%d&fromTime=%d&toTime=%d", strings.TrimSuffix(visitURL, "/"), instance.UID, beginUnix, endUnix)
instanceHeaderString := strings.ReplaceAll(string(instanceHeader), "{{INSTANCE_LINK}}", instanceURL)
instanceHeaderString = strings.ReplaceAll(instanceHeaderString, "{{INSTANCE_NAME}}", instance.Title)
diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.go
index 73c99589e9..a643acc545 100644
--- a/backend/runner/slowquerysync/syncer.go
+++ b/backend/runner/slowquerysync/syncer.go
@@ -127,6 +127,14 @@ func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.Inst
}
func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
+ today := time.Now().UTC().Truncate(24 * time.Hour)
+
+ earliestDate := today.AddDate(0, 0, -retentionCycle)
+
+ if err := s.store.DeleteOutdatedSlowLog(ctx, instance.UID, earliestDate); err != nil {
+ return err
+ }
+
databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
InstanceID: &instance.ResourceID,
})
@@ -275,7 +283,7 @@ func getLatestLogTime(logMap map[string]*storepb.SlowQueryStatistics) time.Time
}
func (s *Syncer) syncMySQLSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
- today := time.Now().Truncate(24 * time.Hour)
+ today := time.Now().UTC().Truncate(24 * time.Hour)
earliestDate := today.AddDate(0, 0, -retentionCycle)