mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-29 22:26:15 -06:00
bun trace logging hooks
This commit is contained in:
parent
408170b754
commit
0def23b2b5
2 changed files with 55 additions and 0 deletions
|
|
@ -147,6 +147,11 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger)
|
||||||
return nil, fmt.Errorf("database type %s not supported for bundb", strings.ToLower(c.DBConfig.Type))
|
return nil, fmt.Errorf("database type %s not supported for bundb", strings.ToLower(c.DBConfig.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if log.Level >= logrus.TraceLevel {
|
||||||
|
// add a hook to just log queries and the time they take
|
||||||
|
conn.DB.AddQueryHook(&debugQueryHook{log: log})
|
||||||
|
}
|
||||||
|
|
||||||
// actually *begin* the connection so that we can tell if the db is there and listening
|
// actually *begin* the connection so that we can tell if the db is there and listening
|
||||||
if err := conn.Ping(); err != nil {
|
if err := conn.Ping(); err != nil {
|
||||||
return nil, fmt.Errorf("db connection error: %s", err)
|
return nil, fmt.Errorf("db connection error: %s", err)
|
||||||
|
|
|
||||||
50
internal/db/bundb/trace.go
Normal file
50
internal/db/bundb/trace.go
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
GoToSocial
|
||||||
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
||||||
|
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// queryHook is just a wrapper for bun.QueryHook
|
||||||
|
type queryHook bun.QueryHook
|
||||||
|
|
||||||
|
// debugQueryHook implements queryHook
|
||||||
|
type debugQueryHook struct {
|
||||||
|
log *logrus.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *debugQueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
|
||||||
|
// do nothing
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// AfterQuery logs the time taken to query, the operation (select, update, etc), and the query itself as translated by bun.
|
||||||
|
func (q *debugQueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
|
||||||
|
dur := time.Now().Sub(event.StartTime).Round(time.Microsecond)
|
||||||
|
l := q.log.WithFields(logrus.Fields{
|
||||||
|
"queryTime": dur,
|
||||||
|
"operation": event.Operation(),
|
||||||
|
})
|
||||||
|
l.Trace(event.Query)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue