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

api: add http_request metric counters #1109

Merged
merged 1 commit into from
Jan 19, 2024
Merged
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
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
Loading