Skip to content

Commit d988f5c

Browse files
authored
fix: sql instrumentation dual registration error (grafana#89508)
fix dual registration error
1 parent 880c180 commit d988f5c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pkg/services/sqlstore/database_wrapper.go

+21
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ func WrapDatabaseDriverWithHooks(dbType string, tracer tracing.Tracer) string {
5858
return driverWithHooks
5959
}
6060

61+
// WrapDatabaseDriverWithHooks creates a fake database driver that
62+
// executes pre and post functions which we use to gather metrics about
63+
// database queries. It also registers the metrics.
64+
func WrapDatabaseReplDriverWithHooks(dbType string, tracer tracing.Tracer) string {
65+
drivers := map[string]driver.Driver{
66+
migrator.SQLite: &sqlite3.SQLiteDriver{},
67+
migrator.MySQL: &mysql.MySQLDriver{},
68+
migrator.Postgres: &pq.Driver{},
69+
}
70+
71+
d, exist := drivers[dbType]
72+
if !exist {
73+
return dbType
74+
}
75+
76+
driverWithHooks := dbType + "ReplicaWithHooks"
77+
sql.Register(driverWithHooks, sqlhooks.Wrap(d, &databaseQueryWrapper{log: log.New("sqlstore.metrics"), tracer: tracer}))
78+
core.RegisterDriver(driverWithHooks, &databaseQueryWrapperDriver{dbType: dbType})
79+
return driverWithHooks
80+
}
81+
6182
// databaseQueryWrapper satisfies the sqlhook.databaseQueryWrapper interface
6283
// which allow us to wrap all SQL queries with a `Before` & `After` hook.
6384
type databaseQueryWrapper struct {

pkg/services/sqlstore/replstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (ss *SQLStore) initReadOnlyEngine(engine *xorm.Engine) error {
118118
ss.dbCfg = dbCfg
119119

120120
if ss.cfg.DatabaseInstrumentQueries {
121-
ss.dbCfg.Type = WrapDatabaseDriverWithHooks(ss.dbCfg.Type, ss.tracer)
121+
ss.dbCfg.Type = WrapDatabaseReplDriverWithHooks(ss.dbCfg.Type, ss.tracer)
122122
}
123123

124124
if engine == nil {

0 commit comments

Comments
 (0)