Skip to content

Commit 979c011

Browse files
committed
gaugehistogram
1 parent eda3373 commit 979c011

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/Simplex/Messaging/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
695695
subClientsCount <- IS.size <$> readTVarIO subClients
696696
subServicesCount <- M.size <$> getSubscribedClients serviceSubscribers
697697
pure RTSubscriberMetrics {subsCount, subClientsCount, subServicesCount}
698-
getDeliveredMetrics ts' = foldM countClnt (RTSubscriberMetrics 0 0 0, TimeBuckets 0 0 IM.empty) =<< getServerClients srv
698+
getDeliveredMetrics ts' = foldM countClnt (RTSubscriberMetrics 0 0 0, emptyTimeBuckets) =<< getServerClients srv
699699
where
700700
countClnt acc@(metrics, times) Client {subscriptions} = do
701701
(cnt, times') <- foldM countSubs (0, times) =<< readTVarIO subscriptions

src/Simplex/Messaging/Server/Prometheus.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,23 +442,24 @@ prometheusMetrics sm rtm ts =
442442
\simplex_smp_delivered_clients_total " <> mshow (subClientsCount deliveredSubs) <> "\n# delivered.subClientsCount\n\
443443
\\n\
444444
\# HELP simplex_smp_delivery_ack_time Times to confirm message delivery, including pending confirmation\n\
445-
\# TYPE simplex_smp_delivery_ack_time histogram\n\
446-
\simplex_smp_delivery_ack_time_sum " <> mshow (sumTime _msgRecvAckTimes + sumTime deliveredTimes) <> "\n# delivered.sumTime\n\
447-
\simplex_smp_delivery_ack_time_count " <> mshow (_msgRecv + _msgRecvGet + subsCount deliveredSubs) <> "\n# delivered.subsCount\n"
445+
\# TYPE simplex_smp_delivery_ack_time gaugehistogram\n\
446+
\simplex_smp_delivery_ack_time_gsum " <> mshow (sumTime _msgRecvAckTimes + sumTime deliveredTimes) <> "\n# delivered.sumTime\n\
447+
\simplex_smp_delivery_ack_time_gcount " <> mshow (_msgRecv + _msgRecvGet + subsCount deliveredSubs) <> "\n# delivered.subsCount\n"
448448
<> showTimeBuckets "simplex_smp_delivery_ack_time" (IM.unionWith (+) (timeBuckets _msgRecvAckTimes) (timeBuckets deliveredTimes))
449449
<> showTimeBucket "simplex_smp_delivery_ack_time" "+Inf" (_msgRecv + _msgRecvGet + subsCount deliveredSubs)
450450
<> "\n\
451451
\# HELP simplex_smp_delivery_ack_confirmed_time Times to confirm message delivery, only confirmed deliveries\n\
452452
\# TYPE simplex_smp_delivery_ack_confirmed_time histogram\n\
453+
\simplex_smp_delivery_ack_confirmed_time_created " <> mshow (createdAt _msgRecvAckTimes) <> "\n# delivered.createdAt\n\
453454
\simplex_smp_delivery_ack_confirmed_time_sum " <> mshow (sumTime _msgRecvAckTimes) <> "\n# delivered.sumTime\n\
454455
\simplex_smp_delivery_ack_confirmed_time_count " <> mshow (_msgRecv + _msgRecvGet) <> "\n# delivered.subsCount\n"
455456
<> showTimeBuckets "simplex_smp_delivery_ack_confirmed_time" (timeBuckets _msgRecvAckTimes)
456457
<> showTimeBucket "simplex_smp_delivery_ack_confirmed_time" "+Inf" (_msgRecv + _msgRecvGet)
457458
<> "\n\
458459
\# HELP simplex_smp_delivery_ack_pending_time Times to confirm message delivery, only pending confirmations\n\
459-
\# TYPE simplex_smp_delivery_ack_pending_time histogram\n\
460-
\simplex_smp_delivery_ack_pending_time_sum " <> mshow (sumTime deliveredTimes) <> "\n# delivered.sumTime\n\
461-
\simplex_smp_delivery_ack_pending_time_count " <> mshow (subsCount deliveredSubs) <> "\n# delivered.subsCount\n"
460+
\# TYPE simplex_smp_delivery_ack_pending_time gaugehistogram\n\
461+
\simplex_smp_delivery_ack_pending_time_gsum " <> mshow (sumTime deliveredTimes) <> "\n# delivered.sumTime\n\
462+
\simplex_smp_delivery_ack_pending_time_gcount " <> mshow (subsCount deliveredSubs) <> "\n# delivered.subsCount\n"
462463
<> showTimeBuckets "simplex_smp_delivery_ack_pending_time" (timeBuckets deliveredTimes)
463464
<> showTimeBucket "simplex_smp_delivery_ack_pending_time" "+Inf" (subsCount deliveredSubs)
464465
<> "\n\
@@ -511,7 +512,13 @@ prometheusMetrics sm rtm ts =
511512
\simplex_smp_loaded_queues_ntf_lock_count " <> mshow (notifierLockCount loadedCounts) <> "\n# loadedCounts.notifierLockCount\n"
512513

513514
showTimeBuckets :: Text -> IM.IntMap Int -> Text
514-
showTimeBuckets metric = T.concat . snd . mapAccumL (\total (sec, cnt) -> (total + cnt, showTimeBucket metric (tshow sec) (total + cnt))) 0 . IM.assocs
515+
showTimeBuckets metric = T.concat . snd . mapAccumL accumBucket (0, 0) . IM.assocs
516+
where
517+
accumBucket (prevSec, total) (sec, cnt) =
518+
let t
519+
| sec - 60 > prevSec = showTimeBucket metric (tshow (sec - 60)) total
520+
| otherwise = ""
521+
in ((sec, total + cnt), t <> showTimeBucket metric (tshow sec) (total + cnt))
515522
showTimeBucket :: Text -> Text -> Int -> Text
516523
showTimeBucket metric sec count = metric <> "_bucket{le=\"" <> sec <> "\"} " <> mshow count <> "\n# delivered.timeBuckets\n"
517524
socketsMetric :: (SocketStats -> Int) -> Text -> Text -> Text

src/Simplex/Messaging/Server/Stats.hs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Data.Text (Text)
2424
import Data.Time.Calendar.Month (pattern MonthDay)
2525
import Data.Time.Calendar.OrdinalDate (mondayStartWeek)
2626
import Data.Time.Clock (UTCTime (..))
27+
import Data.Time.Clock.System (systemEpochDay)
2728
import GHC.IORef (atomicSwapIORef)
2829
import Simplex.Messaging.Encoding.String
2930
import Simplex.Messaging.Protocol (EntityId (..))
@@ -179,7 +180,7 @@ newServerStats ts = do
179180
msgSentLarge <- newIORef 0
180181
msgSentBlock <- newIORef 0
181182
msgRecv <- newIORef 0
182-
msgRecvAckTimes <- newIORef $ TimeBuckets 0 0 IM.empty
183+
msgRecvAckTimes <- newIORef $ TimeBuckets 0 0 IM.empty ts
183184
msgRecvGet <- newIORef 0
184185
msgGet <- newIORef 0
185186
msgGetNoMsg <- newIORef 0
@@ -472,7 +473,6 @@ instance StrEncoding ServerStatsData where
472473
"msgSentLarge=" <> strEncode (_msgSentLarge d),
473474
"msgSentBlock=" <> strEncode (_msgSentBlock d),
474475
"msgRecv=" <> strEncode (_msgRecv d),
475-
"msgRecvAckTimes=" <> strEncode (_msgRecvAckTimes d),
476476
"msgRecvGet=" <> strEncode (_msgRecvGet d),
477477
"msgGet=" <> strEncode (_msgGet d),
478478
"msgGetNoMsg=" <> strEncode (_msgGetNoMsg d),
@@ -536,7 +536,6 @@ instance StrEncoding ServerStatsData where
536536
_msgSentLarge <- opt "msgSentLarge="
537537
_msgSentBlock <- opt "msgSentBlock="
538538
_msgRecv <- "msgRecv=" *> strP <* A.endOfLine
539-
_msgRecvAckTimes <- "msgRecvAckTimes=" *> strP <* A.endOfLine <|> pure (TimeBuckets 0 0 IM.empty)
540539
_msgRecvGet <- opt "msgRecvGet="
541540
_msgGet <- opt "msgGet="
542541
_msgGetNoMsg <- opt "msgGetNoMsg="
@@ -604,7 +603,7 @@ instance StrEncoding ServerStatsData where
604603
_msgSentLarge,
605604
_msgSentBlock,
606605
_msgRecv,
607-
_msgRecvAckTimes,
606+
_msgRecvAckTimes = emptyTimeBuckets,
608607
_msgRecvGet,
609608
_msgGet,
610609
_msgGetNoMsg,
@@ -961,16 +960,20 @@ instance StrEncoding ServiceStatsData where
961960
data TimeBuckets = TimeBuckets
962961
{ sumTime :: Int64,
963962
maxTime :: Int64,
964-
timeBuckets :: IM.IntMap Int
963+
timeBuckets :: IM.IntMap Int,
964+
createdAt :: UTCTime
965965
}
966966
deriving (Show)
967967

968+
emptyTimeBuckets :: TimeBuckets
969+
emptyTimeBuckets = TimeBuckets 0 0 IM.empty (UTCTime systemEpochDay 0)
970+
968971
updateTimeBuckets :: RoundedSystemTime -> RoundedSystemTime -> TimeBuckets -> TimeBuckets
969972
updateTimeBuckets
970973
(RoundedSystemTime deliveryTime)
971974
(RoundedSystemTime currTime)
972-
TimeBuckets {sumTime, maxTime, timeBuckets} =
973-
TimeBuckets
975+
times@TimeBuckets {sumTime, maxTime, timeBuckets} =
976+
times
974977
{ sumTime = sumTime + t,
975978
maxTime = max maxTime t,
976979
timeBuckets = IM.alter (Just . maybe 1 (+ 1)) seconds timeBuckets
@@ -984,11 +987,3 @@ updateTimeBuckets
984987
| t <= 180 = t `toBucket` 30
985988
| otherwise = t `toBucket` 60
986989
toBucket n m = - fromIntegral (((- n) `div` m) * m) -- round up
987-
988-
instance StrEncoding TimeBuckets where
989-
strEncode TimeBuckets {sumTime, maxTime, timeBuckets} =
990-
strEncode (sumTime, maxTime) <> " " <> strEncodeList (IM.toList timeBuckets)
991-
strP = do
992-
(sumTime, maxTime) <- strP_
993-
tbs <- strListP
994-
pure TimeBuckets {sumTime, maxTime, timeBuckets = IM.fromList tbs}

0 commit comments

Comments
 (0)