On error save SQL request parameters to span.

This commit is contained in:
Владислав Весельский 2024-03-14 18:50:31 +03:00
parent 5801184782
commit 174f52d948
1 changed files with 26 additions and 0 deletions

26
save.go
View File

@ -104,6 +104,18 @@ func SaveRow(ctx context.Context, db *db.DBC, dataItem meta.IMetaStruct) error {
sqlSmt = sqlSmt[0:200]
}
span.SetAttributes(
attribute.Int("sql_param_amount", len(params)),
)
// Convert SQL request parameters to slice of strings.
paramStrs := convertInterfacesToStrings(ctx, params)
if len(paramStrs) > 0 {
span.SetAttributes(
attribute.StringSlice("sql_params", paramStrs),
)
}
resultErr := errors.Errorf("Can not execute SaveRow. Got error from updateBuilder.ExecContext. %s (%s)", err.Error(), sqlSmt)
span.RecordError(resultErr)
span.SetStatus(codes.Error, resultErr.Error())
@ -118,6 +130,20 @@ func SaveRow(ctx context.Context, db *db.DBC, dataItem meta.IMetaStruct) error {
return nil
}
func convertInterfacesToStrings(ctx context.Context, items []interface{}) []string {
itemAmount := len(items)
if itemAmount == 0 {
return nil
}
strs := make([]string, itemAmount)
for i, item := range items {
strs[i] = fmt.Sprint(item)
}
return strs
}
func SaveMetaRootStruct(
ctx context.Context,
db *db.DBC,