Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WTEL-4167 #62

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading