From 77686da9698a6c270d3cb69a8c33bc7ff94f66c2 Mon Sep 17 00:00:00 2001 From: owensmallwood Date: Mon, 13 May 2024 08:09:25 -0600 Subject: [PATCH] Unified Storage: Adds sql metrics (#87651) * registers sql metrics for unified storage and removes single quotes from postgres connection string format * uses db logger * only register US sql metrics when connecting to another postgres or mysql db * use engine --- pkg/services/store/entity/db/dbimpl/dbimpl.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/services/store/entity/db/dbimpl/dbimpl.go b/pkg/services/store/entity/db/dbimpl/dbimpl.go index 6d2402dfad6..e537b713db7 100644 --- a/pkg/services/store/entity/db/dbimpl/dbimpl.go +++ b/pkg/services/store/entity/db/dbimpl/dbimpl.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "github.com/dlmiddlecote/sqlstats" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -14,6 +15,7 @@ import ( "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" "github.com/jmoiron/sqlx" + "github.com/prometheus/client_golang/prometheus" "xorm.io/xorm" ) @@ -69,7 +71,7 @@ func (db *EntityDB) GetEngine() (*xorm.Engine, error) { } connectionString := fmt.Sprintf( - "user='%s' password='%s' host='%s' port='%s' dbname='%s' sslmode='%s'", // sslcert='%s' sslkey='%s' sslrootcert='%s'", + "user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", // sslcert='%s' sslkey='%s' sslrootcert='%s'", dbUser, dbPass, addr.Host, addr.Port, dbName, dbSslMode, // ss.dbCfg.ClientCertPath, ss.dbCfg.ClientKeyPath, ss.dbCfg.CaCertPath ) @@ -112,6 +114,11 @@ func (db *EntityDB) GetEngine() (*xorm.Engine, error) { return nil, fmt.Errorf("invalid db type specified: %s", dbType) } + // register sql stat metrics + if err := prometheus.Register(sqlstats.NewStatsCollector("unified_storage", engine.DB().DB)); err != nil { + db.log.Warn("Failed to register unified storage sql stats collector", "error", err) + } + // configure sql logging debugSQL := cfgSection.Key("log_queries").MustBool(false) if !debugSQL {