Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use monotonic clock instead of system clock #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/Servant/Ekg/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Time.Clock
import GHC.Generics (Generic)
import Network.HTTP.Types (Method, Status (..))
import Network.Wai (Middleware, responseStatus)
import System.Clock (getTime, Clock(Monotonic), TimeSpec(..))
import System.Metrics
import qualified System.Metrics.Counter as Counter
import qualified System.Metrics.Distribution as Distribution
Expand Down Expand Up @@ -58,12 +58,14 @@ countResponseCodes (c2XX, c4XX, c5XX, cXXX) application request respond =

responseTimeDistribution :: Distribution.Distribution -> Middleware
responseTimeDistribution dist application request respond =
bracket getCurrentTime stop $ const $ application request respond
bracket (getTime Monotonic) stop $ const $ application request respond
where
stop t1 = do
t2 <- getCurrentTime
let dt = diffUTCTime t2 t1
Distribution.add dist $ fromRational $ (*1000) $ toRational dt
t2 <- getTime Monotonic
let
dt = t2 - t1
milliseconds = fromRational $ toRational (sec dt) * 1000 + toRational (nsec dt) / (1000000)
Distribution.add dist milliseconds

initializeMeters :: Store -> APIEndpoint -> IO Meters
initializeMeters store APIEndpoint{..} = do
Expand Down
2 changes: 1 addition & 1 deletion servant-ekg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ library
hs-source-dirs: lib
build-depends:
base >=4.9 && <4.13
, clock >=0.7.1 && <0.8
, ekg-core >=0.1.1.4 && <0.2
, http-types >=0.12.2 && <0.13
, hashable >=1.2.7.0 && <1.4
, servant >=0.14 && <0.17
, text >=1.2.3.0 && <1.3
, time >=1.6.0.1 && <1.9
, unordered-containers >=0.2.9.0 && <0.3
, wai >=3.2.0 && <3.3

Expand Down