Skip to content

Commit

Permalink
Add prometheus middleware to proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Mar 21, 2019
1 parent baac32d commit 94fe8e7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions services/proxy/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ library:
- Proxy.Env
- Proxy.Options
- Proxy.Proxy
- Proxy.Run
dependencies:
- base >=4.6 && <5
- aeson >=1.0
Expand Down
2 changes: 1 addition & 1 deletion services/proxy/src/Main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Main (main) where

import Imports
import Proxy.API
import Proxy.Run (run)
import Proxy.Options
import Util.Options

Expand Down
17 changes: 1 addition & 16 deletions services/proxy/src/Proxy/API.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Proxy.API (Proxy.API.run) where
module Proxy.API (sitemap) where

import Imports hiding (head)
import Control.Monad.Catch
Expand All @@ -7,19 +7,15 @@ import Control.Retry
import Data.ByteString (breakSubstring)
import Data.CaseInsensitive (CI)
import Data.Metrics.Middleware hiding (path)
import Data.Metrics.WaiRoute (treeToPaths)
import Network.HTTP.ReverseProxy
import Network.HTTP.Types
import Network.Wai
import Network.Wai.Handler.Warp
import Network.Wai.Predicate hiding (err, Error, setStatus)
import Network.Wai.Predicate.Request (getRequest)
import Network.Wai.Routing hiding (path, route)
import Network.Wai.Utilities
import Network.Wai.Utilities.Server hiding (serverPort)
import Proxy.Env
import Proxy.Proxy
import Proxy.Options
import System.Logger.Class hiding (Error, info, render)

import qualified Bilge.Request as Req
Expand All @@ -33,17 +29,6 @@ import qualified Network.HTTP.Client as Client
import qualified Network.Wai.Internal as I
import qualified System.Logger as Logger

run :: Opts -> IO ()
run o = do
m <- metrics
e <- createEnv m o
s <- newSettings $ defaultServer (o^.host) (o^.port) (e^.applog) m
let rtree = compile (sitemap e)
let measured = measureRequests m (treeToPaths rtree)
let app r k = runProxy e r (route rtree r k)
let start = measured . catchErrors (e^.applog) m $ app
runSettings s start `finally` destroyEnv e

sitemap :: Env -> Routes a Proxy ()
sitemap e = do

Expand Down
27 changes: 27 additions & 0 deletions services/proxy/src/Proxy/Run.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Proxy.Run (run) where

import Imports hiding (head)
import Control.Monad.Catch
import Control.Lens hiding ((.=))
import Data.Metrics.Middleware hiding (path)
import Data.Metrics.Middleware.Prometheus (waiPrometheusMiddleware)
import Data.Metrics.WaiRoute (treeToPaths)
import Network.Wai.Utilities.Server hiding (serverPort)
import Network.Wai.Handler.Warp (runSettings)
import Proxy.Env
import Proxy.Proxy
import Proxy.Options
import Proxy.API (sitemap)

run :: Opts -> IO ()
run o = do
m <- metrics
e <- createEnv m o
s <- newSettings $ defaultServer (o^.host) (o^.port) (e^.applog) m
let rtree = compile (sitemap e)
let measured = measureRequests m (treeToPaths rtree)
let app r k = runProxy e r (route rtree r k)
let middleware = waiPrometheusMiddleware (sitemap e)
. measured
. catchErrors (e^.applog) m
runSettings s (middleware app) `finally` destroyEnv e

0 comments on commit 94fe8e7

Please sign in to comment.