Skip to content

Commit 2bb1349

Browse files
chore: better implementation to properly convert database query metrics (#3314)
1 parent 187b41d commit 2bb1349

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

tests/waku_archive/test_waku_archive.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import std/[options, sequtils], testutils/unittests, chronos, libp2p/crypto/cryp
55
import
66
waku/[
77
common/databases/db_sqlite,
8+
common/databases/db_postgres/dbconn,
89
common/paging,
910
waku_core,
1011
waku_core/message/digest,
1112
waku_archive/driver/sqlite_driver,
12-
waku_archive,
13+
waku_archive
1314
],
1415
../waku_archive/archive_utils,
1516
../testlib/wakucore
@@ -109,6 +110,19 @@ suite "Waku Archive - message handling":
109110
check:
110111
(waitFor driver.getMessagesCount()).tryGet() == 0
111112

113+
test "convert query to label":
114+
check:
115+
convertQueryToMetricLabel("SELECT version();") == "select_version"
116+
convertQueryToMetricLabel("SELECT messageHash FROM messages WHERE pubsubTopic = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp DESC, messageHash DESC LIMIT ?") == "msg_hash_no_ctopic"
117+
convertQueryToMetricLabel(""" SELECT child.relname AS partition_name
118+
FROM pg_inherits
119+
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
120+
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
121+
JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace
122+
JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace
123+
WHERE parent.relname='messages""") == "get_partitions_list"
124+
125+
112126
procSuite "Waku Archive - find messages":
113127
## Fixtures
114128
let timeOrigin = now()

waku/common/databases/db_postgres/dbconn.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ proc isSecureString(input: string): bool =
233233

234234
return true
235235

236-
proc convertQueryToMetricLabel(query: string): string =
236+
proc convertQueryToMetricLabel*(query: string): string =
237237
## Simple query categorization. The output label is the one that should be used in query metrics
238238
for snippetQuery, metric in QueriesToMetricMap.pairs():
239-
if query.contains($snippetQuery):
239+
if $snippetQuery in query:
240240
return $metric
241241
return "unknown_query_metric"
242242

waku/common/databases/db_postgres/query_metrics.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import metrics
1+
import metrics, tables
22

33
declarePublicGauge query_time_secs,
44
"query time measured in nanoseconds", labels = ["query", "phase"]
@@ -7,7 +7,7 @@ declarePublicCounter query_count,
77
"number of times a query is being performed", labels = ["query"]
88

99
## Maps parts of the possible known queries with a fixed and shorter query label.
10-
const QueriesToMetricMap* = {
10+
const QueriesToMetricMap* = toTable({
1111
"contentTopic IN": "content_topic",
1212
"SELECT version()": "select_version",
1313
"WITH min_timestamp": "messages_lookup",
@@ -28,4 +28,4 @@ const QueriesToMetricMap* = {
2828
"SELECT pg_advisory_unlock": "advisory_unlock",
2929
"ANALYZE messages": "analyze_messages",
3030
"SELECT EXISTS": "check_version_table_exists",
31-
}
31+
})

0 commit comments

Comments
 (0)