Skip to content

Commit

Permalink
WTEL-4167
Browse files Browse the repository at this point in the history
  • Loading branch information
i.navrotskyj committed May 27, 2024
1 parent 9367aea commit 69b9d5b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type SqlSettings struct {
MaxIdleConns *int
ConnMaxLifetimeMilliseconds *int
MaxOpenConns *int
Trace bool
Trace int
AtRestEncryptKey string
QueryTimeout *int
}
Expand Down
27 changes: 24 additions & 3 deletions store/sqlstore/supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
dbsql "database/sql"
"errors"
"fmt"
sqltrace "log"
"os"
"strings"
"time"

"encoding/json"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 69b9d5b

Please sign in to comment.