mirror of
https://github.com/grafana/grafana.git
synced 2024-11-24 09:50:29 -06:00
fix: sql instrumentation dual registration error (#89508)
fix dual registration error
This commit is contained in:
parent
880c180424
commit
d988f5c3b0
@ -58,6 +58,27 @@ func WrapDatabaseDriverWithHooks(dbType string, tracer tracing.Tracer) string {
|
||||
return driverWithHooks
|
||||
}
|
||||
|
||||
// WrapDatabaseDriverWithHooks creates a fake database driver that
|
||||
// executes pre and post functions which we use to gather metrics about
|
||||
// database queries. It also registers the metrics.
|
||||
func WrapDatabaseReplDriverWithHooks(dbType string, tracer tracing.Tracer) string {
|
||||
drivers := map[string]driver.Driver{
|
||||
migrator.SQLite: &sqlite3.SQLiteDriver{},
|
||||
migrator.MySQL: &mysql.MySQLDriver{},
|
||||
migrator.Postgres: &pq.Driver{},
|
||||
}
|
||||
|
||||
d, exist := drivers[dbType]
|
||||
if !exist {
|
||||
return dbType
|
||||
}
|
||||
|
||||
driverWithHooks := dbType + "ReplicaWithHooks"
|
||||
sql.Register(driverWithHooks, sqlhooks.Wrap(d, &databaseQueryWrapper{log: log.New("sqlstore.metrics"), tracer: tracer}))
|
||||
core.RegisterDriver(driverWithHooks, &databaseQueryWrapperDriver{dbType: dbType})
|
||||
return driverWithHooks
|
||||
}
|
||||
|
||||
// databaseQueryWrapper satisfies the sqlhook.databaseQueryWrapper interface
|
||||
// which allow us to wrap all SQL queries with a `Before` & `After` hook.
|
||||
type databaseQueryWrapper struct {
|
||||
|
@ -118,7 +118,7 @@ func (ss *SQLStore) initReadOnlyEngine(engine *xorm.Engine) error {
|
||||
ss.dbCfg = dbCfg
|
||||
|
||||
if ss.cfg.DatabaseInstrumentQueries {
|
||||
ss.dbCfg.Type = WrapDatabaseDriverWithHooks(ss.dbCfg.Type, ss.tracer)
|
||||
ss.dbCfg.Type = WrapDatabaseReplDriverWithHooks(ss.dbCfg.Type, ss.tracer)
|
||||
}
|
||||
|
||||
if engine == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user