chore: better implementation to properly convert database query metrics (#3314)

This commit is contained in:
Ivan FB 2025-03-04 19:14:31 +01:00 committed by Ivan Folgueira Bande
parent 187b41d147
commit 2bb1349162
3 changed files with 20 additions and 6 deletions

View File

@ -5,11 +5,12 @@ import std/[options, sequtils], testutils/unittests, chronos, libp2p/crypto/cryp
import
waku/[
common/databases/db_sqlite,
common/databases/db_postgres/dbconn,
common/paging,
waku_core,
waku_core/message/digest,
waku_archive/driver/sqlite_driver,
waku_archive,
waku_archive
],
../waku_archive/archive_utils,
../testlib/wakucore
@ -109,6 +110,19 @@ suite "Waku Archive - message handling":
check:
(waitFor driver.getMessagesCount()).tryGet() == 0
test "convert query to label":
check:
convertQueryToMetricLabel("SELECT version();") == "select_version"
convertQueryToMetricLabel("SELECT messageHash FROM messages WHERE pubsubTopic = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp DESC, messageHash DESC LIMIT ?") == "msg_hash_no_ctopic"
convertQueryToMetricLabel(""" SELECT child.relname AS partition_name
FROM pg_inherits
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace
JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace
WHERE parent.relname='messages""") == "get_partitions_list"
procSuite "Waku Archive - find messages":
## Fixtures
let timeOrigin = now()

View File

@ -233,10 +233,10 @@ proc isSecureString(input: string): bool =
return true
proc convertQueryToMetricLabel(query: string): string =
proc convertQueryToMetricLabel*(query: string): string =
## Simple query categorization. The output label is the one that should be used in query metrics
for snippetQuery, metric in QueriesToMetricMap.pairs():
if query.contains($snippetQuery):
if $snippetQuery in query:
return $metric
return "unknown_query_metric"

View File

@ -1,4 +1,4 @@
import metrics
import metrics, tables
declarePublicGauge query_time_secs,
"query time measured in nanoseconds", labels = ["query", "phase"]
@ -7,7 +7,7 @@ declarePublicCounter query_count,
"number of times a query is being performed", labels = ["query"]
## Maps parts of the possible known queries with a fixed and shorter query label.
const QueriesToMetricMap* = {
const QueriesToMetricMap* = toTable({
"contentTopic IN": "content_topic",
"SELECT version()": "select_version",
"WITH min_timestamp": "messages_lookup",
@ -28,4 +28,4 @@ const QueriesToMetricMap* = {
"SELECT pg_advisory_unlock": "advisory_unlock",
"ANALYZE messages": "analyze_messages",
"SELECT EXISTS": "check_version_table_exists",
}
})