2021-09-11 12:19:06 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-09-11 12:19:06 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package bundb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
)
|
|
|
|
|
2021-10-11 13:37:33 +01:00
|
|
|
func newDebugQueryHook() bun.QueryHook {
|
2021-10-13 12:50:24 +01:00
|
|
|
return &debugQueryHook{}
|
2021-09-11 12:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// debugQueryHook implements bun.QueryHook
|
|
|
|
type debugQueryHook struct {
|
|
|
|
}
|
|
|
|
|
2021-10-11 13:37:33 +01:00
|
|
|
func (q *debugQueryHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.Context {
|
2021-09-11 12:19:06 +01:00
|
|
|
// do nothing
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterQuery logs the time taken to query, the operation (select, update, etc), and the query itself as translated by bun.
|
2021-10-11 13:37:33 +01:00
|
|
|
func (q *debugQueryHook) AfterQuery(_ context.Context, event *bun.QueryEvent) {
|
2021-09-11 12:19:06 +01:00
|
|
|
dur := time.Since(event.StartTime).Round(time.Microsecond)
|
2021-10-11 13:37:33 +01:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-10-04 14:24:19 +01:00
|
|
|
"duration": dur,
|
2021-09-11 12:19:06 +01:00
|
|
|
"operation": event.Operation(),
|
|
|
|
})
|
2021-10-04 14:24:19 +01:00
|
|
|
|
2022-05-02 11:53:46 +01:00
|
|
|
if dur > 1*time.Second {
|
|
|
|
l.Warnf("SLOW DATABASE QUERY [%s] %s", dur, event.Query)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:33:18 +00:00
|
|
|
if logrus.GetLevel() == logrus.TraceLevel {
|
|
|
|
l.Tracef("[%s] %s", dur, event.Query)
|
|
|
|
} else {
|
|
|
|
l.Debugf("[%s] %s", dur, event.Operation())
|
|
|
|
}
|
2021-09-11 12:19:06 +01:00
|
|
|
}
|