Skip to content

Commit

Permalink
api: add http_request metric counters
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanCacqueray committed Jan 18, 2024
1 parent 793427d commit b195680
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
## [master]

### Added

- [api] add http_request and http_request_error metrics counter.

### Changed
### Removed
### Fixed
Expand Down
22 changes: 22 additions & 0 deletions src/Monocle/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- | The Monocle entry point.
module Monocle.Main (run, rootServer, ApiConfig (..), defaultApiConfig, RootAPI) where

import Control.Exception (catch, throwIO)
import Data.ByteString qualified as BS
import Data.List qualified
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
Expand Down Expand Up @@ -87,6 +89,25 @@ healthMiddleware app' req resp
| Wai.rawPathInfo req == "/health" = resp $ Wai.responseLBS HTTP.status200 mempty "api is running\n"
| otherwise = app' req resp

-- | This middleware keeps track of user request
metricMiddleware :: Wai.Application -> Wai.Application
metricMiddleware app' req resp = handleExceptions $ app' req handleResp
where
basePath = Wai.rawPathInfo req
handleResp appResp
| -- crawler or static file request
"/api/2/crawler/" `BS.isPrefixOf` basePath || not ("/api/2/" `BS.isPrefixOf` basePath) =
resp appResp
| otherwise = do
incCounter $ case HTTP.statusCode (Wai.responseStatus appResp) of
code | code >= 200 && code < 300 -> monocleHTTPRequestCounter
_ -> monocleHTTPRequestErrorCounter
resp appResp
handleExceptions act =
act `catch` \(e :: SomeException) -> do
incCounter monocleHTTPRequestErrorCounter
throwIO e

data ApiConfig = ApiConfig
{ port :: Int
, elasticUrl :: Text
Expand Down Expand Up @@ -170,6 +191,7 @@ run' ApiConfig {..} aplogger = E.runConcurrent $ runLoggerEffect do
. monitoringMiddleware
. healthMiddleware
. staticMiddleware
. metricMiddleware
logInfo "SystemReady" ["workspace" .= length workspaces, "port" .= port, "elastic" .= elasticUrl]

appEnv <- E.withEffToIO $ \effToIO -> do
Expand Down
12 changes: 12 additions & 0 deletions src/Monocle/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ module Monocle.Prelude (
incrementCounter,
httpRequestCounter,
httpFailureCounter,
monocleHTTPRequestCounter,
monocleHTTPRequestErrorCounter,
monocleQueryCheckCounter,
monocleQueryCounter,
monocleMetricCounter,
Expand Down Expand Up @@ -282,6 +284,16 @@ incrementCounter x l = withLabel x l incCounter

-------------------------------------------------------------------------------
-- Global metrics
{-# NOINLINE monocleHTTPRequestCounter #-}
monocleHTTPRequestCounter :: Prometheus.Counter
monocleHTTPRequestCounter =
unsafePerformIO $ promRegister $ Prometheus.counter (Info "http_request" "")

{-# NOINLINE monocleHTTPRequestErrorCounter #-}
monocleHTTPRequestErrorCounter :: Prometheus.Counter
monocleHTTPRequestErrorCounter =
unsafePerformIO $ promRegister $ Prometheus.counter (Info "http_request_error" "")

{-# NOINLINE monocleQueryCheckCounter #-}
monocleQueryCheckCounter :: Prometheus.Counter
monocleQueryCheckCounter =
Expand Down

0 comments on commit b195680

Please sign in to comment.