From 40fc09a93de761842a9cc8f89ae1557eee426130 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Fri, 18 Jul 2025 09:26:25 +0100 Subject: [PATCH] smp server: additional stat counter for ntf credentials created together with the queue (#1589) * smp server: additional stat counter for ntf credentials created together with the queue * fix prometheus * fix test * fix qSub --- src/Simplex/Messaging/Server.hs | 6 +++--- src/Simplex/Messaging/Server/Prometheus.hs | 11 ++++++++--- src/Simplex/Messaging/Server/Stats.hs | 10 ++++++++++ tests/ServerTests.hs | 6 +++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 1069fafdf..a439e8709 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -1512,7 +1512,7 @@ client stats <- asks serverStats incStat $ qCreated stats incStat $ qCount stats - when (isJust ntf) $ incStat $ ntfCreated stats + when (isJust ntf) $ incStat $ ntfNewCreated stats case subMode of SMOnlyCreate -> pure () SMSubscribe -> subscribeNewQueue rcvId qr -- no need to check if message is available, it's a new queue @@ -1580,7 +1580,7 @@ client subscribeQueueAndDeliver :: StoreQueue s -> QueueRec -> M s ResponseAndMessage subscribeQueueAndDeliver q qr = liftIO (TM.lookupIO entId $ subscriptions clnt) >>= \case - Nothing -> subscribeRcvQueue qr >>= deliver True + Nothing -> subscribeRcvQueue qr >>= deliver False Just s@Sub {subThread} -> do stats <- asks serverStats case subThread of @@ -1590,7 +1590,7 @@ client pure (err (CMD PROHIBITED), Nothing) _ -> do incStat $ qSubDuplicate stats - atomically (writeTVar (delivered s) Nothing) >> deliver False s + atomically (writeTVar (delivered s) Nothing) >> deliver True s where deliver :: Bool -> Sub -> M s ResponseAndMessage deliver hasSub sub = do diff --git a/src/Simplex/Messaging/Server/Prometheus.hs b/src/Simplex/Messaging/Server/Prometheus.hs index cdcf10cfd..859587b60 100644 --- a/src/Simplex/Messaging/Server/Prometheus.hs +++ b/src/Simplex/Messaging/Server/Prometheus.hs @@ -82,6 +82,7 @@ prometheusMetrics sm rtm ts = _qSubEnd, _qSubEndB, _ntfCreated, + _ntfNewCreated, _ntfDeleted, _ntfDeletedB, _ntfSub, @@ -262,15 +263,19 @@ prometheusMetrics sm rtm ts = \simplex_smp_messages_notify_get_errors{type=\"duplicate\"} " <> mshow _msgGetDuplicate <> "\n# msgGetDuplicate\n\ \simplex_smp_messages_notify_get_errors{type=\"prohibited\"} " <> mshow _msgGetProhibited <> "\n# msgGetProhibited\n\ \\n\ - \# HELP simplex_smp_queues_notify_created Created queues with notification flag (client).\n\ + \# HELP simplex_smp_queues_notify_created Created queue notification credentials.\n\ \# TYPE simplex_smp_queues_notify_created counter\n\ \simplex_smp_queues_notify_created " <> mshow _ntfCreated <> "\n# ntfCreated\n\ \\n\ - \# HELP simplex_smp_queues_notify_deleted Deleted queues with notification flag (client).\n\ + \# HELP simplex_smp_queues_notify_new_created Created new queues with notification credentials.\n\ + \# TYPE simplex_smp_queues_notify_new_created counter\n\ + \simplex_smp_queues_notify_new_created " <> mshow _ntfNewCreated <> "\n# ntfNewCreated\n\ + \\n\ + \# HELP simplex_smp_queues_notify_deleted Deleted queue notification credentials.\n\ \# TYPE simplex_smp_queues_notify_deleted counter\n\ \simplex_smp_queues_notify_deleted " <> mshow _ntfDeleted <> "\n# ntfDeleted\n\ \\n\ - \# HELP simplex_smp_queues_notify_deleted_batch Deleted batched queues with notification flag (client).\n\ + \# HELP simplex_smp_queues_notify_deleted_batch Deleted batched queue notification credentials.\n\ \# TYPE simplex_smp_queues_notify_deleted_batch counter\n\ \simplex_smp_queues_notify_deleted_batch " <> mshow _ntfDeletedB <> "\n# ntfDeletedB\n\ \\n\ diff --git a/src/Simplex/Messaging/Server/Stats.hs b/src/Simplex/Messaging/Server/Stats.hs index 50c611009..4af195295 100644 --- a/src/Simplex/Messaging/Server/Stats.hs +++ b/src/Simplex/Messaging/Server/Stats.hs @@ -48,6 +48,7 @@ data ServerStats = ServerStats qSubEnd :: IORef Int, qSubEndB :: IORef Int, ntfCreated :: IORef Int, + ntfNewCreated :: IORef Int, -- credentials created at the time of queue creation ntfDeleted :: IORef Int, ntfDeletedB :: IORef Int, ntfSub :: IORef Int, @@ -107,6 +108,7 @@ data ServerStatsData = ServerStatsData _qSubEnd :: Int, _qSubEndB :: Int, _ntfCreated :: Int, + _ntfNewCreated :: Int, _ntfDeleted :: Int, _ntfDeletedB :: Int, _ntfSub :: Int, @@ -167,6 +169,7 @@ newServerStats ts = do qSubEnd <- newIORef 0 qSubEndB <- newIORef 0 ntfCreated <- newIORef 0 + ntfNewCreated <- newIORef 0 ntfDeleted <- newIORef 0 ntfDeletedB <- newIORef 0 ntfSub <- newIORef 0 @@ -224,6 +227,7 @@ newServerStats ts = do qSubEnd, qSubEndB, ntfCreated, + ntfNewCreated, ntfDeleted, ntfDeletedB, ntfSub, @@ -283,6 +287,7 @@ getServerStatsData s = do _qSubEnd <- readIORef $ qSubEnd s _qSubEndB <- readIORef $ qSubEndB s _ntfCreated <- readIORef $ ntfCreated s + _ntfNewCreated <- readIORef $ ntfNewCreated s _ntfDeleted <- readIORef $ ntfDeleted s _ntfDeletedB <- readIORef $ ntfDeletedB s _ntfSub <- readIORef $ ntfSub s @@ -340,6 +345,7 @@ getServerStatsData s = do _qSubEnd, _qSubEndB, _ntfCreated, + _ntfNewCreated, _ntfDeleted, _ntfDeletedB, _ntfSub, @@ -400,6 +406,7 @@ setServerStats s d = do writeIORef (qSubEnd s) $! _qSubEnd d writeIORef (qSubEndB s) $! _qSubEndB d writeIORef (ntfCreated s) $! _ntfCreated d + writeIORef (ntfNewCreated s) $! _ntfNewCreated d writeIORef (ntfDeleted s) $! _ntfDeleted d writeIORef (ntfDeletedB s) $! _ntfDeletedB d writeIORef (ntfSub s) $! _ntfSub d @@ -460,6 +467,7 @@ instance StrEncoding ServerStatsData where "qSubEnd=" <> strEncode (_qSubEnd d), "qSubEndB=" <> strEncode (_qSubEndB d), "ntfCreated=" <> strEncode (_ntfCreated d), + "ntfNewCreated=" <> strEncode (_ntfNewCreated d), "ntfDeleted=" <> strEncode (_ntfDeleted d), "ntfDeletedB=" <> strEncode (_ntfDeletedB d), "ntfSub=" <> strEncode (_ntfSub d), @@ -523,6 +531,7 @@ instance StrEncoding ServerStatsData where _qSubEnd <- opt "qSubEnd=" _qSubEndB <- opt "qSubEndB=" _ntfCreated <- opt "ntfCreated=" + _ntfNewCreated <- opt "ntfNewCreated=" _ntfDeleted <- opt "ntfDeleted=" _ntfDeletedB <- opt "ntfDeletedB=" _ntfSub <- opt "ntfSub=" @@ -590,6 +599,7 @@ instance StrEncoding ServerStatsData where _qSubEnd, _qSubEndB, _ntfCreated, + _ntfNewCreated, _ntfDeleted, _ntfDeletedB, _ntfSub, diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 5ff65c9c4..407b9f182 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -795,7 +795,7 @@ testRestoreMessages = pure () rId <- readTVarIO recipientId logSize testStoreLogFile `shouldReturn` 2 - logSize testServerStatsBackupFile `shouldReturn` 94 + logSize testServerStatsBackupFile `shouldReturn` 95 Right stats1 <- strDecode <$> B.readFile testServerStatsBackupFile checkStats stats1 [rId] 5 1 withSmpServerConfigOn at cfg' testPort . runTest t $ \h -> do @@ -811,7 +811,7 @@ testRestoreMessages = logSize testStoreLogFile `shouldReturn` (if compacting then 1 else 2) -- the last message is not removed because it was not ACK'd -- logSize testStoreMsgsFile `shouldReturn` 3 - logSize testServerStatsBackupFile `shouldReturn` 94 + logSize testServerStatsBackupFile `shouldReturn` 95 Right stats2 <- strDecode <$> B.readFile testServerStatsBackupFile checkStats stats2 [rId] 5 3 @@ -829,7 +829,7 @@ testRestoreMessages = pure () logSize testStoreLogFile `shouldReturn` (if compacting then 1 else 2) removeFile testStoreLogFile - logSize testServerStatsBackupFile `shouldReturn` 94 + logSize testServerStatsBackupFile `shouldReturn` 95 Right stats3 <- strDecode <$> B.readFile testServerStatsBackupFile checkStats stats3 [rId] 5 5 removeFileIfExists testStoreMsgsFile