Skip to content

Commit

Permalink
chore: dbconn truncate possible too long error messages (#3283)
Browse files Browse the repository at this point in the history
* also: dbconn restrict the max metric label value to 128
  • Loading branch information
Ivansete-status authored Feb 7, 2025
1 parent 48f04d8 commit 9a8e1ef
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions waku/common/databases/db_postgres/dbconn.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ proc check(db: DbConn): Result[void, string] =
return err("exception in check: " & getCurrentExceptionMsg())

if message.len > 0:
return err($message)
let truncatedErr = message[0 .. 80]
## libpq sometimes gives extremely long error messages
return err(truncatedErr)

return ok()

Expand Down Expand Up @@ -249,7 +251,7 @@ proc dbConnQuery*(
## remove everything between ' or " all possible sequence of numbers. e.g. rm partition partition
var querySummary = cleanedQuery.replace(re2("""(['"]).*?\\1"""), "")
querySummary = querySummary.replace(re2"\d+", "")
querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 200)]
querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 128)]

var queryStartTime = getTime().toUnixFloat()

Expand Down Expand Up @@ -300,18 +302,19 @@ proc dbConnQueryPrepared*(
error "error in dbConnQueryPrepared", error = $error
return err("error in dbConnQueryPrepared calling sendQuery: " & $error)

let stmtNameSummary = stmtName[0 ..< min(stmtName.len, 128)]
let sendDuration = getTime().toUnixFloat() - queryStartTime
query_time_secs.set(sendDuration, [stmtName, "sendToDBQuery"])
query_time_secs.set(sendDuration, [stmtNameSummary, "sendToDBQuery"])

queryStartTime = getTime().toUnixFloat()

(await dbConnWrapper.waitQueryToFinish(rowCallback)).isOkOr:
return err("error in dbConnQueryPrepared calling waitQueryToFinish: " & $error)

let waitDuration = getTime().toUnixFloat() - queryStartTime
query_time_secs.set(waitDuration, [stmtName, "waitFinish"])
query_time_secs.set(waitDuration, [stmtNameSummary, "waitFinish"])

query_count.inc(labelValues = [stmtName])
query_count.inc(labelValues = [stmtNameSummary])

if "insert" notin stmtName.toLower():
debug "dbConnQueryPrepared",
Expand Down

0 comments on commit 9a8e1ef

Please sign in to comment.