From 69b9d5b03a39b04478b2c57fbb1f15f91723994c Mon Sep 17 00:00:00 2001 From: "i.navrotskyj" Date: Mon, 27 May 2024 17:16:19 +0300 Subject: [PATCH] WTEL-4167 --- app/config.go | 3 ++- model/config.go | 2 +- store/sqlstore/supplier.go | 27 ++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/config.go b/app/config.go index e545ec49..4418b65e 100644 --- a/app/config.go +++ b/app/config.go @@ -17,6 +17,7 @@ var ( enableOmnichannel = flag.Int("enable_omnichannel", 0, "Set enabled omnichannel") resourceCidType = flag.String("resource_cid_type", "", "CID Type: none / Remote-Party-ID / P-Asserted-Identity") resourceIgnoreEarlyMedia = flag.String("resource_ignore_early_media", "", "Ignore Early Media: True / False / Consume / Ring Ready") + sqlDebug = flag.Int("sql_debug", 0, "Debug sql lvl (0-9)") ) func (a *App) Config() *model.Config { @@ -64,7 +65,7 @@ func loadConfig() (*model.Config, error) { MaxIdleConns: model.NewInt(5), MaxOpenConns: model.NewInt(5), ConnMaxLifetimeMilliseconds: model.NewInt(300000), - Trace: false, + Trace: *sqlDebug, }, MessageQueueSettings: model.MessageQueueSettings{ Url: *amqpSource, diff --git a/model/config.go b/model/config.go index 0ae7570c..c0581109 100644 --- a/model/config.go +++ b/model/config.go @@ -46,7 +46,7 @@ type SqlSettings struct { MaxIdleConns *int ConnMaxLifetimeMilliseconds *int MaxOpenConns *int - Trace bool + Trace int AtRestEncryptKey string QueryTimeout *int } diff --git a/store/sqlstore/supplier.go b/store/sqlstore/supplier.go index 00f6bee5..ddc61bb8 100644 --- a/store/sqlstore/supplier.go +++ b/store/sqlstore/supplier.go @@ -5,8 +5,8 @@ import ( dbsql "database/sql" "errors" "fmt" - sqltrace "log" "os" + "strings" "time" "encoding/json" @@ -104,6 +104,25 @@ func (ss *SqlSupplier) GetAllConns() []*gorp.DbMap { return all } +type logger struct { + ms time.Duration +} + +func (l *logger) Printf(format string, v ...interface{}) { + if len(v) == 4 { + if dur, ok := v[3].(time.Duration); ok && dur > l.ms { + if s, ok := v[1].(string); ok { + s = strings.Replace(s, "\n", "\\n", -1) + if len(s) > 300 { + s = s[0:299] + } + wlog.Warn(fmt.Sprintf("[sql_debug] time = %v, sql: %s", dur, s)) + } + //wlog.Warn(fmt.Sprintf(format, v...)) + } + } +} + func setupConnection(con_type string, dataSource string, settings *model.SqlSettings) *gorp.DbMap { db, err := dbsql.Open(*settings.DriverName, dataSource) if err != nil { @@ -145,8 +164,10 @@ func setupConnection(con_type string, dataSource string, settings *model.SqlSett os.Exit(EXIT_NO_DRIVER) } - if settings.Trace { - dbmap.TraceOn("[SQL]", sqltrace.New(os.Stdout, "", sqltrace.LstdFlags)) + if settings.Trace > 0 { + dbmap.TraceOn("[SQL]", &logger{ + ms: time.Duration(time.Millisecond * 100 * time.Duration(settings.Trace)), + }) } return dbmap