cat query string at VALUES if is set to not log values in query

This commit is contained in:
Marek Goc 2024-11-14 20:30:18 +01:00
parent 1230538295
commit e6016ceed8

View File

@ -187,18 +187,17 @@ func (p *gormPlugin) after() gormHookFunc {
attrs = append(attrs, sys) attrs = append(attrs, sys)
} }
vars := tx.Statement.Vars query := ""
if p.excludeQueryVars { if p.excludeQueryVars {
// Replace query variables with '?' to mask them // cat query at VALUES and add tree dots
vars = make([]interface{}, len(tx.Statement.Vars)) pos := strings.Index(tx.Statement.SQL.String(), "VALUES (")
if pos != -1 {
for i := 0; i < len(vars); i++ { query = tx.Statement.SQL.String()[:pos+len("VALUES ")] + " ..."
vars[i] = "?" } else {
query = tx.Statement.SQL.String()
} }
} }
query := tx.Dialector.Explain(tx.Statement.SQL.String(), vars...)
attrs = append(attrs, semconv.DBStatementKey.String(p.formatQuery(query))) attrs = append(attrs, semconv.DBStatementKey.String(p.formatQuery(query)))
if tx.Statement.Table != "" { if tx.Statement.Table != "" {
attrs = append(attrs, semconv.DBSQLTableKey.String(tx.Statement.Table)) attrs = append(attrs, semconv.DBSQLTableKey.String(tx.Statement.Table))