Skip to content

Commit

Permalink
fix(otelgorm): Make metric reporting work with prepared statements
Browse files Browse the repository at this point in the history
Previously we only initialized metrics reporting by calling
otelsql.ReportDBStatsMetrics if the gorm.DB.ConnPool interface
pointer we received happened to be a *sql.DB. This was often
the case, but not if prepared statements were enabled (and
possibly in other circumstances too). This caused metrics
reporting to be silently disabled. We fix this by calling
gorm.DB.DB whose sole purpose is to extract the underlying
*sql.DB.
  • Loading branch information
magnusbaeck committed Jun 11, 2024
1 parent 33d1fd3 commit 6914878
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions otelgorm/otelgorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type gormRegister interface {

func (p otelPlugin) Initialize(db *gorm.DB) (err error) {
if !p.excludeMetrics {
if db, ok := db.ConnPool.(*sql.DB); ok {
otelsql.ReportDBStatsMetrics(db)
if sqlDB, err := db.DB(); err == nil {
otelsql.ReportDBStatsMetrics(sqlDB)
}
}

Expand Down

0 comments on commit 6914878

Please sign in to comment.