diff --git a/CHANGELOG.md b/CHANGELOG.md index 6179ac4a0..85d7e81d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Monocle/Main.hs b/src/Monocle/Main.hs index b594ef848..4c8511c00 100644 --- a/src/Monocle/Main.hs +++ b/src/Monocle/Main.hs @@ -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 @@ -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 @@ -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 diff --git a/src/Monocle/Prelude.hs b/src/Monocle/Prelude.hs index 76941d46e..e6df55636 100644 --- a/src/Monocle/Prelude.hs +++ b/src/Monocle/Prelude.hs @@ -205,6 +205,8 @@ module Monocle.Prelude ( incrementCounter, httpRequestCounter, httpFailureCounter, + monocleHTTPRequestCounter, + monocleHTTPRequestErrorCounter, monocleQueryCheckCounter, monocleQueryCounter, monocleMetricCounter, @@ -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 =