added log level field on stordb opts for controlling sql verbosity

This commit is contained in:
gezimbll
2025-02-24 14:57:20 +01:00
committed by Dan Christian Bogos
parent 4ad5bee6ab
commit 8fd871b304
11 changed files with 25 additions and 9 deletions

View File

@@ -184,6 +184,7 @@ const CGRATES_CFG_JSON = `
"opts": {
"sqlMaxOpenConns": 100, // maximum database connections opened, not applying for mongo
"sqlMaxIdleConns": 10, // maximum database connections idle, not applying for mongo
"sqlLogLevel": 3, // sql logger verbosity: 1=Silent, 2=Error, 3=Warn, 4=Info
"sqlConnMaxLifetime": "0", // maximum amount of time a connection may be reused (0 for unlimited), not applying for mongo
"mysqlDSNParams":{}, // DSN params for opening db
"pgSSLMode": "disable", // determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server

File diff suppressed because one or more lines are too long

View File

@@ -511,6 +511,7 @@ type DBOptsJson struct {
MongoConnScheme *string `json:"mongoConnScheme"`
SQLMaxOpenConns *int `json:"sqlMaxOpenConns"`
SQLMaxIdleConns *int `json:"sqlMaxIdleConns"`
SQLLogLevel *int `json:"sqlLogLevel"`
SQLConnMaxLifetime *string `json:"sqlConnMaxLifetime"`
MYSQLDSNParams map[string]string `json:"mysqlDSNParams"`
PgSSLMode *string `json:"pgSSLMode"`

View File

@@ -33,6 +33,7 @@ import (
type StorDBOpts struct {
SQLMaxOpenConns int
SQLMaxIdleConns int
SQLLogLevel int
SQLConnMaxLifetime time.Duration
SQLDSNParams map[string]string
PgSSLMode string
@@ -81,6 +82,9 @@ func (dbOpts *StorDBOpts) loadFromJSONCfg(jsnCfg *DBOptsJson) (err error) {
if jsnCfg.SQLMaxIdleConns != nil {
dbOpts.SQLMaxIdleConns = *jsnCfg.SQLMaxIdleConns
}
if jsnCfg.SQLLogLevel != nil {
dbOpts.SQLLogLevel = *jsnCfg.SQLLogLevel
}
if jsnCfg.SQLConnMaxLifetime != nil {
if dbOpts.SQLConnMaxLifetime, err = utils.ParseDurationWithNanosecs(*jsnCfg.SQLConnMaxLifetime); err != nil {
return
@@ -199,6 +203,7 @@ func (dbOpts *StorDBOpts) Clone() *StorDBOpts {
return &StorDBOpts{
SQLMaxOpenConns: dbOpts.SQLMaxOpenConns,
SQLMaxIdleConns: dbOpts.SQLMaxIdleConns,
SQLLogLevel: dbOpts.SQLLogLevel,
SQLConnMaxLifetime: dbOpts.SQLConnMaxLifetime,
SQLDSNParams: dbOpts.SQLDSNParams,
PgSSLMode: dbOpts.PgSSLMode,
@@ -249,6 +254,7 @@ func (dbcfg StorDbCfg) AsMapInterface() any {
opts := map[string]any{
utils.SQLMaxOpenConnsCfg: dbcfg.Opts.SQLMaxOpenConns,
utils.SQLMaxIdleConnsCfg: dbcfg.Opts.SQLMaxIdleConns,
utils.SQLLogLevelCfg: dbcfg.Opts.SQLLogLevel,
utils.SQLConnMaxLifetime: dbcfg.Opts.SQLConnMaxLifetime.String(),
utils.MYSQLDSNParams: dbcfg.Opts.SQLDSNParams,
utils.PgSSLModeCfg: dbcfg.Opts.PgSSLMode,
@@ -307,6 +313,9 @@ func diffStorDBOptsJsonCfg(d *DBOptsJson, v1, v2 *StorDBOpts) *DBOptsJson {
if v1.SQLMaxIdleConns != v2.SQLMaxIdleConns {
d.SQLMaxIdleConns = utils.IntPointer(v2.SQLMaxIdleConns)
}
if v1.SQLLogLevel != v2.SQLLogLevel {
d.SQLLogLevel = utils.IntPointer(v2.SQLLogLevel)
}
if v1.SQLConnMaxLifetime != v2.SQLConnMaxLifetime {
d.SQLConnMaxLifetime = utils.StringPointer(v2.SQLConnMaxLifetime.String())
}

View File

@@ -56,6 +56,7 @@ func TestStoreDbCfgloadFromJsonCfgCase1(t *testing.T) {
Opts: &StorDBOpts{
SQLMaxOpenConns: 100,
SQLMaxIdleConns: 10,
SQLLogLevel: 3,
SQLDSNParams: make(map[string]string),
MongoQueryTimeout: 10 * time.Second,
MongoConnScheme: "mongodb+srv",
@@ -246,6 +247,7 @@ func TestStorDbCfgAsMapInterface(t *testing.T) {
utils.OptsCfg: map[string]any{
utils.SQLMaxOpenConnsCfg: 100,
utils.SQLMaxIdleConnsCfg: 10,
utils.SQLLogLevelCfg: 3,
utils.SQLConnMaxLifetimeCfg: "0s",
utils.MYSQLDSNParams: make(map[string]string),
utils.MongoQueryTimeoutCfg: "10s",

View File

@@ -524,7 +524,7 @@ func TestRankingProfileSet(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rp := &RankingProfile{}
err := rp.Set(tt.path, tt.val, false, "")
err := rp.Set(tt.path, tt.val, false)
if err != tt.expectedErr {
t.Errorf("Test %s failed: expected error %v, got %v", tt.name, tt.expectedErr, err)

View File

@@ -760,7 +760,7 @@ func TestTrendProfileSet(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tp.Set(tt.path, tt.val, false, "")
err := tp.Set(tt.path, tt.val, false)
if tt.hasError && err == nil {
t.Errorf("Expected error for path %v, but got none", tt.path)

View File

@@ -25,6 +25,7 @@ import (
"github.com/cgrates/cgrates/utils"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type MySQLStorage struct {
@@ -32,10 +33,10 @@ type MySQLStorage struct {
}
func NewMySQLStorage(host, port, name, user, password string,
maxConn, maxIdleConn int, connMaxLifetime time.Duration, location string, dsnParams map[string]string) (*SQLStorage, error) {
maxConn, maxIdleConn, logLevel int, connMaxLifetime time.Duration, location string, dsnParams map[string]string) (*SQLStorage, error) {
connectString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&loc=%s&parseTime=true&sql_mode='ALLOW_INVALID_DATES'",
user, password, host, port, name, location)
db, err := gorm.Open(mysql.Open(connectString+AppendToMysqlDSNOpts(dsnParams)), &gorm.Config{AllowGlobalUpdate: true})
db, err := gorm.Open(mysql.Open(connectString+AppendToMysqlDSNOpts(dsnParams)), &gorm.Config{AllowGlobalUpdate: true, Logger: logger.Default.LogMode(logger.LogLevel(logLevel))})
if err != nil {
return nil, err
}

View File

@@ -25,6 +25,7 @@ import (
"github.com/cgrates/cgrates/utils"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type PostgresStorage struct {
@@ -34,7 +35,7 @@ type PostgresStorage struct {
// NewPostgresStorage returns the posgres storDB
func NewPostgresStorage(host, port, name, user, password,
sslmode, sslcert, sslkey, sslpassword, sslcertmode, sslrootcert string,
maxConn, maxIdleConn int, connMaxLifetime time.Duration) (*SQLStorage, error) {
maxConn, maxIdleConn, sqlLogLevel int, connMaxLifetime time.Duration) (*SQLStorage, error) {
connStr := fmt.Sprintf(
"host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
host, port, name, user, password, sslmode)
@@ -53,7 +54,7 @@ func NewPostgresStorage(host, port, name, user, password,
if sslrootcert != "" {
connStr = connStr + " sslrootcert=" + sslrootcert
}
db, err := gorm.Open(postgres.Open(connStr), &gorm.Config{AllowGlobalUpdate: true})
db, err := gorm.Open(postgres.Open(connStr), &gorm.Config{AllowGlobalUpdate: true, Logger: logger.Default.LogMode(logger.LogLevel(sqlLogLevel))})
if err != nil {
return nil, err
}

View File

@@ -71,10 +71,10 @@ func NewStorDBConn(dbType, host, port, name, user, pass, marshaler string,
case utils.MetaPostgres:
db, err = NewPostgresStorage(host, port, name, user, pass, opts.PgSSLMode,
opts.PgSSLCert, opts.PgSSLKey, opts.PgSSLPassword, opts.PgSSLCertMode, opts.PgSSLRootCert,
opts.SQLMaxOpenConns, opts.SQLMaxIdleConns, opts.SQLConnMaxLifetime)
opts.SQLMaxOpenConns, opts.SQLMaxIdleConns, opts.SQLLogLevel, opts.SQLConnMaxLifetime)
case utils.MetaMySQL:
db, err = NewMySQLStorage(host, port, name, user, pass, opts.SQLMaxOpenConns, opts.SQLMaxIdleConns,
opts.SQLConnMaxLifetime, opts.MySQLLocation, opts.SQLDSNParams)
opts.SQLLogLevel, opts.SQLConnMaxLifetime, opts.MySQLLocation, opts.SQLDSNParams)
case utils.MetaInternal:
db = NewInternalDB(stringIndexedFields, prefixIndexedFields, itmsCfg)
default:

View File

@@ -1887,6 +1887,7 @@ const (
TypeCfg = "type"
SQLMaxOpenConnsCfg = "sqlMaxOpenConns"
SQLMaxIdleConnsCfg = "sqlMaxIdleConns"
SQLLogLevelCfg = "sqlLogLevel"
SQLConnMaxLifetimeCfg = "sqlConnMaxLifetime"
StringIndexedFieldsCfg = "string_indexed_fields"
PrefixIndexedFieldsCfg = "prefix_indexed_fields"