@@ -6,23 +6,23 @@ module Cardano.Tracer.Handlers.Metrics.Monitoring
6
6
( runMonitoringServer
7
7
) where
8
8
9
- import Prelude hiding (head )
10
9
import Cardano.Tracer.Configuration
11
10
import Cardano.Tracer.Environment
11
+ import Cardano.Tracer.Handlers.Metrics.Utils
12
12
import Cardano.Tracer.MetaTrace
13
13
import Cardano.Tracer.Types
14
14
15
- import qualified Data.Text as T
16
- import System.Time.Extra (sleep )
15
+ import Prelude hiding (head )
17
16
18
- import qualified Cardano.Tracer.Handlers.Metrics.Utils as Utils
19
- import Cardano.Tracer.Handlers.Metrics.Utils (renderListOfConnectedNodes )
17
+ import Data.ByteString as ByteString (ByteString , isInfixOf )
20
18
import Data.ByteString.Builder (stringUtf8 )
19
+ import qualified Data.Text as T
21
20
import Network.HTTP.Types
22
21
import Network.Wai
23
- import Network.Wai.Handler.Warp (runSettings , defaultSettings )
22
+ import Network.Wai.Handler.Warp (defaultSettings , runSettings )
24
23
import qualified System.Metrics as EKG
25
24
import System.Remote.Monitoring.Wai
25
+ import System.Time.Extra (sleep )
26
26
27
27
-- | 'ekg' package allows to run only one EKG server, to display only one web page
28
28
-- for particular EKG.Store. Since 'cardano-tracer' can be connected to any number
@@ -37,7 +37,7 @@ import System.Remote.Monitoring.Wai
37
37
runMonitoringServer
38
38
:: TracerEnv
39
39
-> Endpoint -- ^ (web page with list of connected nodes, EKG web page).
40
- -> IO Utils. RouteDictionary
40
+ -> IO RouteDictionary
41
41
-> IO ()
42
42
runMonitoringServer TracerEnv {teTracer} endpoint computeRoutes_autoUpdate = do
43
43
-- Pause to prevent collision between "Listening"-notifications from servers.
@@ -50,27 +50,35 @@ runMonitoringServer TracerEnv{teTracer} endpoint computeRoutes_autoUpdate = do
50
50
runSettings (setEndpoint endpoint defaultSettings) do
51
51
renderEkg dummyStore computeRoutes_autoUpdate
52
52
53
- renderEkg :: EKG. Store -> IO Utils. RouteDictionary -> Application
53
+ renderEkg :: EKG. Store -> IO RouteDictionary -> Application
54
54
renderEkg dummyStore computeRoutes_autoUpdate request send = do
55
- routeDictionary :: Utils. RouteDictionary <-
55
+ routeDictionary :: RouteDictionary <-
56
56
computeRoutes_autoUpdate
57
57
58
- let nodeNames :: [NodeName ]
59
- nodeNames = Utils. nodeNames routeDictionary
58
+ let acceptHeader :: Maybe ByteString
59
+ acceptHeader = lookup hAccept $ requestHeaders request
60
+
61
+ let wantsJson :: Bool
62
+ wantsJson = all @ Maybe (" application/json" `ByteString.isInfixOf` ) acceptHeader
60
63
61
64
case pathInfo request of
65
+
62
66
[] ->
63
- send $ responseLBS status200 [] (renderListOfConnectedNodes " EKG metrics" nodeNames)
67
+ send $ uncurry (responseLBS status200) $ if wantsJson
68
+ then (contentHdrJSON , renderJson routeDictionary)
69
+ else (contentHdrUtf8Html, renderListOfConnectedNodes " EKG metrics" routeDictionary)
70
+
64
71
route: rest
65
72
| Just (store :: EKG. Store , _ :: NodeName )
66
- <- lookup route (Utils. getRouteDictionary routeDictionary)
73
+ <- lookup route (getRouteDictionary routeDictionary)
67
74
-> monitor store request { pathInfo = rest } send
68
75
-- all endings in ekg-wai's asset/ folder
76
+
69
77
| any (`T.isSuffixOf` route) [" .html" , " .css" , " .js" , " .png" ]
70
78
-- we actually need an empty dummy store here, as we're sure monitor will internally invoke the staticApp to serve the assets
71
79
-> monitor dummyStore request send
80
+
72
81
| otherwise
73
- -> send $ responseBuilder status404 [] do
82
+ -> send $ responseBuilder status404 contentHdrUtf8Text do
74
83
" Not found: "
75
84
<> stringUtf8 (show route)
76
- <> " \n " <> stringUtf8 (show nodeNames)
0 commit comments