From 2bb13491620a248ef96afcd52f4dafb8e8a221c5 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Tue, 4 Mar 2025 19:14:31 +0100 Subject: [PATCH] chore: better implementation to properly convert database query metrics (#3314) --- tests/waku_archive/test_waku_archive.nim | 16 +++++++++++++++- waku/common/databases/db_postgres/dbconn.nim | 4 ++-- .../databases/db_postgres/query_metrics.nim | 6 +++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/waku_archive/test_waku_archive.nim b/tests/waku_archive/test_waku_archive.nim index 9e1b927e0..fda1f54e6 100644 --- a/tests/waku_archive/test_waku_archive.nim +++ b/tests/waku_archive/test_waku_archive.nim @@ -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() diff --git a/waku/common/databases/db_postgres/dbconn.nim b/waku/common/databases/db_postgres/dbconn.nim index c4c77c0ff..6c5555058 100644 --- a/waku/common/databases/db_postgres/dbconn.nim +++ b/waku/common/databases/db_postgres/dbconn.nim @@ -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" diff --git a/waku/common/databases/db_postgres/query_metrics.nim b/waku/common/databases/db_postgres/query_metrics.nim index 124d7cdf4..8553763b5 100644 --- a/waku/common/databases/db_postgres/query_metrics.nim +++ b/waku/common/databases/db_postgres/query_metrics.nim @@ -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", -} +})