|
| 1 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE TypeApplications #-} |
| 5 | +{-# OPTIONS_GHC -fno-warn-unrecognised-pragmas #-} |
| 6 | + |
| 7 | +module Simplex.FileTransfer.Server.Prometheus where |
| 8 | + |
| 9 | +import Data.Int (Int64) |
| 10 | +import Data.Text (Text) |
| 11 | +import qualified Data.Text as T |
| 12 | +import Data.Time.Clock (UTCTime (..), diffUTCTime) |
| 13 | +import Data.Time.Clock.System (systemEpochDay) |
| 14 | +import Data.Time.Format.ISO8601 (iso8601Show) |
| 15 | +import Simplex.FileTransfer.Server.Stats |
| 16 | +import Simplex.Messaging.Server.Stats (PeriodStatCounts (..)) |
| 17 | +import Simplex.Messaging.Transport (simplexMQVersion) |
| 18 | +import Simplex.Messaging.Util (tshow) |
| 19 | + |
| 20 | +data FileServerMetrics = FileServerMetrics |
| 21 | + { statsData :: FileServerStatsData, |
| 22 | + filesDownloadedPeriods :: PeriodStatCounts, |
| 23 | + rtsOptions :: Text |
| 24 | + } |
| 25 | + |
| 26 | +rtsOptionsEnv :: Text |
| 27 | +rtsOptionsEnv = "XFTP_RTS_OPTIONS" |
| 28 | + |
| 29 | +{-# FOURMOLU_DISABLE\n#-} |
| 30 | +xftpPrometheusMetrics :: FileServerMetrics -> UTCTime -> Text |
| 31 | +xftpPrometheusMetrics sm ts = |
| 32 | + time <> files <> info |
| 33 | + where |
| 34 | + FileServerMetrics {statsData, filesDownloadedPeriods, rtsOptions} = sm |
| 35 | + FileServerStatsData |
| 36 | + { _fromTime, |
| 37 | + _filesCreated, |
| 38 | + _fileRecipients, |
| 39 | + _filesUploaded, |
| 40 | + _filesExpired, |
| 41 | + _filesDeleted, |
| 42 | + _filesBlocked, |
| 43 | + _fileDownloads, |
| 44 | + _fileDownloadAcks, |
| 45 | + _filesCount, |
| 46 | + _filesSize |
| 47 | + } = statsData |
| 48 | + time = |
| 49 | + "# Recorded at: " <> T.pack (iso8601Show ts) <> "\n\ |
| 50 | + \# Stats from: " <> T.pack (iso8601Show _fromTime) <> "\n\ |
| 51 | + \\n" |
| 52 | + files = |
| 53 | + "# Files\n\ |
| 54 | + \# -----\n\ |
| 55 | + \\n\ |
| 56 | + \# HELP simplex_xftp_files_created Created files\n\ |
| 57 | + \# TYPE simplex_xftp_files_created counter\n\ |
| 58 | + \simplex_xftp_files_created " <> mshow _filesCreated <> "\n\ |
| 59 | + \# filesCreated\n\ |
| 60 | + \\n\ |
| 61 | + \# HELP simplex_xftp_files_recipients Files recipients\n\ |
| 62 | + \# TYPE simplex_xftp_files_recipients counter\n\ |
| 63 | + \simplex_xftp_files_recipients " <> mshow _fileRecipients <> "\n\ |
| 64 | + \# fileRecipients\n\ |
| 65 | + \\n\ |
| 66 | + \# HELP simplex_xftp_files_uploaded Uploaded files\n\ |
| 67 | + \# TYPE simplex_xftp_files_uploaded counter\n\ |
| 68 | + \simplex_xftp_files_uploaded " <> mshow _filesUploaded <> "\n\ |
| 69 | + \# filesUploaded\n\ |
| 70 | + \\n\ |
| 71 | + \# HELP simplex_xftp_files_expired Expired files\n\ |
| 72 | + \# TYPE simplex_xftp_files_expired counter\n\ |
| 73 | + \simplex_xftp_files_expired " <> mshow _filesExpired <> "\n\ |
| 74 | + \# filesExpired\n\ |
| 75 | + \\n\ |
| 76 | + \# HELP simplex_xftp_files_deleted Deleted files\n\ |
| 77 | + \# TYPE simplex_xftp_files_deleted counter\n\ |
| 78 | + \simplex_xftp_files_deleted " <> mshow _filesDeleted <> "\n\ |
| 79 | + \# filesDeleted\n\ |
| 80 | + \\n\ |
| 81 | + \# HELP simplex_xftp_files_blocked Blocked files\n\ |
| 82 | + \# TYPE simplex_xftp_files_blocked counter\n\ |
| 83 | + \simplex_xftp_files_blocked " <> mshow _filesBlocked <> "\n\ |
| 84 | + \# filesBlocked\n\ |
| 85 | + \\n\ |
| 86 | + \# HELP simplex_xftp_file_downloads File downloads\n\ |
| 87 | + \# TYPE simplex_xftp_file_downloads counter\n\ |
| 88 | + \simplex_xftp_file_downloads " <> mshow _fileDownloads <> "\n\ |
| 89 | + \# fileDownloads\n\ |
| 90 | + \\n\ |
| 91 | + \# HELP simplex_xftp_file_download_acks File download ACKs\n\ |
| 92 | + \# TYPE simplex_xftp_file_download_acks counter\n\ |
| 93 | + \simplex_xftp_file_download_acks " <> mshow _fileDownloadAcks <> "\n\ |
| 94 | + \# fileDownloadAcks\n\ |
| 95 | + \\n\ |
| 96 | + \# HELP simplex_xftp_files_count_total Total files count \n\ |
| 97 | + \# TYPE simplex_xftp_files_count_total gauge\n\ |
| 98 | + \simplex_xftp_files_count_total " <> mshow _filesCount <> "\n\ |
| 99 | + \# filesCount\n\ |
| 100 | + \\n\ |
| 101 | + \# HELP simplex_xftp_files_size Size of files \n\ |
| 102 | + \# TYPE simplex_xftp_files_size gauge\n\ |
| 103 | + \simplex_xftp_files_size " <> mshow _filesSize <> "\n\ |
| 104 | + \# filesSize \n\ |
| 105 | + \\n\ |
| 106 | + \# HELP simplex_xftp_files_count_daily Daily files count\n\ |
| 107 | + \# TYPE simplex_xftp_files_count_daily gauge\n\ |
| 108 | + \simplex_xftp_files_count_daily " <> mstr (dayCount filesDownloadedPeriods) <> "\n\ |
| 109 | + \# filesDownloaded.dayCount\n\ |
| 110 | + \\n\ |
| 111 | + \# HELP simplex_xftp_files_count_weekly Weekly files count\n\ |
| 112 | + \# TYPE simplex_xftp_files_count_weekly gauge\n\ |
| 113 | + \simplex_xftp_files_count_weekly " <> mstr (weekCount filesDownloadedPeriods) <> "\n\ |
| 114 | + \# filesDownloaded.weekCount\n\ |
| 115 | + \\n\ |
| 116 | + \# HELP simplex_xftp_files_count_monthly Monthly files count\n\ |
| 117 | + \# TYPE simplex_xftp_files_count_monthly gauge\n\ |
| 118 | + \simplex_xftp_files_count_monthly " <> mstr (monthCount filesDownloadedPeriods) <> "\n\ |
| 119 | + \# filesDownloaded.monthCount\n\ |
| 120 | + \\n" |
| 121 | + info = |
| 122 | + "# Info\n\ |
| 123 | + \# ----\n\ |
| 124 | + \\n\ |
| 125 | + \# HELP simplex_xftp_info Server information. RTS options have to be passed via " <> rtsOptionsEnv <> " env var\n\ |
| 126 | + \# TYPE simplex_xftp_info gauge\n\ |
| 127 | + \simplex_xftp_info{version=\"" <> T.pack simplexMQVersion <> "\",rts_options=\"" <> rtsOptions <> "\"} 1\n\ |
| 128 | + \\n" |
| 129 | + mstr a = a <> " " <> tsEpoch |
| 130 | + mshow :: Show a => a -> Text |
| 131 | + mshow = mstr . tshow |
| 132 | + tsEpoch = tshow @Int64 $ floor @Double $ realToFrac (ts `diffUTCTime` epoch) * 1000 |
| 133 | + epoch = UTCTime systemEpochDay 0 |
| 134 | +{-# FOURMOLU_ENABLE\n#-} |
0 commit comments