From 174f52d948de2b1fb9f88902700bd223d16970a9 Mon Sep 17 00:00:00 2001 From: Vladislav Veselskiy Date: Thu, 14 Mar 2024 18:50:31 +0300 Subject: [PATCH] On error save SQL request parameters to span. --- save.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/save.go b/save.go index 0f2dea2..32de2d9 100644 --- a/save.go +++ b/save.go @@ -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,