From 6914878043a8d460d071a32221fb237686e51d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20B=C3=A4ck?= Date: Tue, 11 Jun 2024 23:36:21 +0200 Subject: [PATCH] fix(otelgorm): Make metric reporting work with prepared statements 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. --- otelgorm/otelgorm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/otelgorm/otelgorm.go b/otelgorm/otelgorm.go index 41c070ca..921d1b43 100644 --- a/otelgorm/otelgorm.go +++ b/otelgorm/otelgorm.go @@ -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) } }