Skip to content

Commit b47d28a

Browse files
authored
smp server: prometheus metrics for delievered and not acknowledged messages (to monitor stuck deliveries) (#1572)
1 parent c5b7d3c commit b47d28a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Simplex/Messaging/Server.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,24 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
684684
let threadsCount = 0
685685
#endif
686686
clientsCount <- IM.size <$> getServerClients srv
687+
deliveredSubs <- getDeliveredMetrics
687688
smpSubs <- getSubscribersMetrics subscribers
688689
ntfSubs <- getSubscribersMetrics ntfSubscribers
689690
loadedCounts <- loadedQueueCounts $ fromMsgStore ms
690-
pure RealTimeMetrics {socketStats, threadsCount, clientsCount, smpSubs, ntfSubs, loadedCounts}
691+
pure RealTimeMetrics {socketStats, threadsCount, clientsCount, deliveredSubs, smpSubs, ntfSubs, loadedCounts}
691692
where
692693
getSubscribersMetrics ServerSubscribers {queueSubscribers, serviceSubscribers, subClients} = do
693694
subsCount <- M.size <$> getSubscribedClients queueSubscribers
694695
subClientsCount <- IS.size <$> readTVarIO subClients
695696
subServicesCount <- M.size <$> getSubscribedClients serviceSubscribers
696697
pure RTSubscriberMetrics {subsCount, subClientsCount, subServicesCount}
698+
getDeliveredMetrics = foldM countClnt (RTSubscriberMetrics 0 0 0) =<< getServerClients srv
699+
countClnt metrics Client {subscriptions} = do
700+
cnt <- foldM countSubs 0 =<< readTVarIO subscriptions
701+
pure $ if cnt > 0
702+
then metrics {subsCount = subsCount metrics + cnt, subClientsCount = subClientsCount metrics + 1}
703+
else metrics
704+
countSubs !cnt Sub {delivered} = (\empty -> if empty then cnt else cnt + 1) <$> atomically (isEmptyTMVar delivered)
697705

698706
runClient :: Transport c => X.CertificateChain -> C.APrivateSignKey -> TProxy c 'TServer -> c 'TServer -> M s ()
699707
runClient srvCert srvSignKey tp h = do

src/Simplex/Messaging/Server/Prometheus.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ data RealTimeMetrics = RealTimeMetrics
3434
{ socketStats :: [(ServiceName, SocketStats)],
3535
threadsCount :: Int,
3636
clientsCount :: Int,
37+
deliveredSubs :: RTSubscriberMetrics,
3738
smpSubs :: RTSubscriberMetrics,
3839
ntfSubs :: RTSubscriberMetrics,
3940
loadedCounts :: LoadedQueueCounts
@@ -55,6 +56,7 @@ prometheusMetrics sm rtm ts =
5556
{ socketStats,
5657
threadsCount,
5758
clientsCount,
59+
deliveredSubs,
5860
smpSubs,
5961
ntfSubs,
6062
loadedCounts
@@ -426,6 +428,14 @@ prometheusMetrics sm rtm ts =
426428
\# TYPE simplex_smp_clients_total gauge\n\
427429
\simplex_smp_clients_total " <> mshow clientsCount <> "\n\
428430
\\n\
431+
\# HELP simplex_smp_delivered_total Total SMP subscriptions with delivered messages\n\
432+
\# TYPE simplex_smp_delivered_total gauge\n\
433+
\simplex_smp_delivered_total " <> mshow (subsCount deliveredSubs) <> "\n# delivered.subsCount\n\
434+
\\n\
435+
\# HELP simplex_smp_delivered_clients_total Subscribed clients\n\
436+
\# TYPE simplex_smp_delivered_clients_total gauge\n\
437+
\simplex_smp_delivered_clients_total " <> mshow (subClientsCount deliveredSubs) <> "\n# delivered.subClientsCount\n\
438+
\\n\
429439
\# HELP simplex_smp_subscribtion_total Total SMP subscriptions\n\
430440
\# TYPE simplex_smp_subscribtion_total gauge\n\
431441
\simplex_smp_subscribtion_total " <> mshow (subsCount smpSubs) <> "\n# smp.subsCount\n\

0 commit comments

Comments
 (0)