eventlog-live-0.11.0.0: src-app/GHC/Eventlog/Live/App/Server/Viewer.hs
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module GHC.Eventlog.Live.App.Server.Viewer (
ViewerApi,
viewerServer,
viewerWsServerApp,
) where
import Control.Concurrent (forkIO, killThread)
import Control.Concurrent.LossyChan qualified as LC
import Control.Exception (bracket, handle)
import Control.Monad (void)
import Data.Aeson qualified as JSON
import Data.Aeson.Types (KeyValue (..), KeyValueOmit (..), ToJSON, Value (..), pairs)
import Data.ByteString.Lazy qualified as BSL
import Data.DList (DList)
import Data.DList qualified as D
import Data.Kind (Type)
import Data.Maybe (mapMaybe)
import Data.Proxy (Proxy (..))
import Data.Text qualified as T
import Data.Word (Word64)
import GHC.Eventlog.Live.App.Server.Viewer.AlignedData (AlignedData, toAlignedData)
import GHC.Eventlog.Live.App.Server.Viewer.AlignedData qualified as AD
import GHC.Eventlog.Live.App.Server.Viewer.Embedded (mkEmbedded)
import GHC.Eventlog.Live.Logger (Logger, logDebug, logException, logInfo, logTrace)
import GHC.Eventlog.Live.Machine.Core (Tick (..))
import GHC.Eventlog.Live.Processor (ExportRequest (..), InstrumentationScope, Resource, toSomeMetrics)
import GHC.Eventlog.Live.Types.Attribute ((~=))
import GHC.Eventlog.Live.Types.Metrics (KnownMetric (..), Metric, SomeMetrics, getMetrics)
import GHC.Eventlog.Live.Types.Metrics qualified as M
import GHC.TypeLits (symbolVal)
import Network.HTTP.Media (MediaType, (//), (/:))
import Network.Wai (Application)
import Network.Wai.Application.Static (staticApp)
import Network.WebSockets qualified as WS
import Network.WebSockets.Connection.PingPong qualified as WS (PongTimeout (..))
import Servant.API (Accept (..), Header, Headers, NoContent (..), Raw, StdMethod (..), Verb, addHeader, (:<|>) (..), (:>))
import Servant.Server (Handler, Server, Tagged (..))
import WaiAppStatic.Storage.Embedded (mkSettings)
import WaiAppStatic.Types qualified as WAS
import Prelude hiding (null)
--------------------------------------------------------------------------------
-- Viewer App Server
--------------------------------------------------------------------------------
type ViewerApi =
ViewerRedirect
:<|> "viewer" :> Raw
viewerServer :: Server ViewerApi
viewerServer =
viewerRedirectServer :<|> Tagged viewerApp
viewerApp :: Application
viewerApp =
staticApp
$(mkSettings mkEmbedded)
{ WAS.ssIndices = WAS.unsafeToPiece <$> ["index.html"]
, WAS.ssRedirectToIndex = True
, WAS.ssAddTrailingSlash = True
}
--------------------------------------------------------------------------------
-- HTML content type
--------------------------------------------------------------------------------
data HTML
instance Accept HTML where
contentType :: Proxy HTML -> MediaType
contentType _ = "text" // "html" /: ("charset", "utf-8")
--------------------------------------------------------------------------------
-- Redirect from index
--------------------------------------------------------------------------------
type ViewerRedirect =
Verb 'GET 302 '[HTML] (Headers '[Header "Location" String] NoContent)
viewerRedirectServer :: Handler (Headers '[Header "Location" String] NoContent)
viewerRedirectServer = pure $ addHeader "viewer" NoContent
--------------------------------------------------------------------------------
-- Viewer Websocket Server
--------------------------------------------------------------------------------
viewerWsServerApp ::
Logger IO -> LC.InChan (Tick ExportRequest) -> WS.ServerApp
viewerWsServerApp logger exportRequestChan pending = do
conn <- WS.acceptRequest pending
logInfo logger $ "Accepted new viewer connection."
handle handlePongTimeout $
withMessageDiscarder logger conn $
WS.withPingPong pingPongOptions conn $ \conn' ->
LC.out exportRequestChan >>= \chan ->
viewer chan conn'
where
pingPongOptions :: WS.PingPongOptions
pingPongOptions = WS.defaultPingPongOptions
handlePongTimeout :: WS.PongTimeout -> IO ()
handlePongTimeout WS.PongTimeout =
logInfo logger $ "Viewer timed out."
viewer :: LC.OutChan (Tick ExportRequest) -> WS.Connection -> IO ()
viewer chan conn =
handle handleConnectionException viewerLoop
where
viewerLoop =
LC.receive chan >>= \case
Tick ->
viewerLoop
Item er -> do
let ver = toViewerExportRequest er
let verBytes = JSON.encode ver
let verByteSize = BSL.length verBytes
logTrace logger $ "Send " <> T.show verByteSize <> " bytes to viewer."
WS.sendTextData conn verBytes
viewerLoop
handleConnectionException :: WS.ConnectionException -> IO ()
handleConnectionException = \case
WS.ConnectionClosed{} -> do
logInfo logger $ "Viewer closed the connection."
_wsErr ->
-- NOTE: Using viewerLoop would uninstall the handler.
viewer chan conn
{- |
This function spawns a thread that receives all incoming data messages on
the websocket connection and discards them. This is needed, because, with the
current @websockets@ library, the user must call `WS.receiveDataMessage` for
the library to handle control messages.
-}
withMessageDiscarder :: Logger IO -> WS.Connection -> IO () -> IO ()
withMessageDiscarder logger conn action =
bracket (forkIO messageDiscarder) killThread (const action)
where
messageDiscarder :: IO ()
messageDiscarder = handle handleConnectionException mesageDiscarderLoop
mesageDiscarderLoop :: IO ()
mesageDiscarderLoop = void (WS.receiveDataMessage conn) >> mesageDiscarderLoop
handleConnectionException :: WS.ConnectionException -> IO ()
handleConnectionException = \case
WS.CloseRequest{} -> do
logInfo logger $ "Viewer requested to close the connection."
-- NOTE: Using mesageDiscarderLoop would uninstall the handler.
messageDiscarder
WS.ConnectionClosed{} ->
logDebug logger $ "Viewer closed the connection; stopping message discarder."
wsErr ->
logException logger wsErr
data ViewerExportRequest = ViewerExportRequest
{ resource :: !Resource
, scope :: !InstrumentationScope
, telemetry :: !ViewerTelemetry
}
viewerExportRequestToKV :: (KeyValueOmit e kv, Monoid kv) => ViewerExportRequest -> kv
viewerExportRequestToKV vt =
mconcat
[ "resource" .= vt.resource
, "scope" .= vt.scope
, "telemetry" .= vt.telemetry
]
{-# INLINE viewerExportRequestToKV #-}
instance ToJSON ViewerExportRequest where
toJSON :: ViewerExportRequest -> JSON.Value
toJSON = Object . viewerExportRequestToKV
toEncoding :: ViewerExportRequest -> JSON.Encoding
toEncoding = pairs . viewerExportRequestToKV
data ViewerTelemetry = ViewerTelemetry
{ heapSize :: Maybe (AlignedData Word64)
, heapProfSample :: Maybe (AlignedData Word64)
, gcCopied :: Maybe (AlignedData Word64)
, gcSlop :: Maybe (AlignedData Word64)
, gcFragmentation :: Maybe (AlignedData Word64)
}
viewerTelemetryToKV :: (KeyValueOmit e kv, Monoid kv) => ViewerTelemetry -> kv
viewerTelemetryToKV vt =
mconcat
[ "heapSize" .?= vt.heapSize
, "heapProfSample" .?= vt.heapProfSample
, "gcCopied" .?= vt.gcCopied
, "gcSlop" .?= vt.gcSlop
, "gcFragmentation" .?= vt.gcFragmentation
]
{-# INLINE viewerTelemetryToKV #-}
instance ToJSON ViewerTelemetry where
toJSON :: ViewerTelemetry -> JSON.Value
toJSON = Object . viewerTelemetryToKV
toEncoding :: ViewerTelemetry -> JSON.Encoding
toEncoding = pairs . viewerTelemetryToKV
toViewerExportRequest :: ExportRequest -> ViewerExportRequest
toViewerExportRequest r =
ViewerExportRequest
{ resource = r.resource
, scope = r.scope
, telemetry =
ViewerTelemetry
{ heapSize = heapSize
, heapProfSample = heapProfSample
, gcCopied = gcCopied
, gcSlop = gcSlop
, gcFragmentation = gcFragmentation
}
}
where
someMetrics :: [SomeMetrics]
someMetrics = mapMaybe toSomeMetrics r.telemetry
heapProfSample, heapSize, gcCopied, gcSlop, gcFragmentation :: Maybe (AlignedData Word64)
heapProfSample = alignKnownMetric (Proxy @"heapProfSample") someMetrics
heapSize = alignKnownMetrics (Proxy @"heapSize" :&: Proxy @"blocksSize" :&: Proxy @"heapLive" :&: End) someMetrics
gcCopied = alignKnownMetric (Proxy @"gcCopied") someMetrics
gcSlop = alignKnownMetric (Proxy @"gcSlop") someMetrics
gcFragmentation = alignKnownMetric (Proxy @"gcFragmentation") someMetrics
infixr 0 :&:
data KnownMetrics (a :: Type) where
(:&:) :: (KnownMetric metric, GetMetricType metric ~ a) => Proxy metric -> KnownMetrics a -> KnownMetrics a
End :: KnownMetrics a
alignKnownMetric :: (KnownMetric metric) => Proxy metric -> [SomeMetrics] -> Maybe (AlignedData (GetMetricType metric))
alignKnownMetric metric =
alignKnownMetrics (metric :&: End)
alignKnownMetrics :: KnownMetrics a -> [SomeMetrics] -> Maybe (AlignedData a)
alignKnownMetrics metrics =
(`onlyIf` not . AD.null)
. toAlignedData
. D.toList
. foldMap (getNamedMetrics metrics)
getNamedMetrics :: KnownMetrics a -> SomeMetrics -> DList (Metric a)
getNamedMetrics End _someMetrics = mempty
getNamedMetrics (metric :&: metrics) someMetrics =
getNamedMetric metric someMetrics <> getNamedMetrics metrics someMetrics
getNamedMetric :: (KnownMetric metric) => Proxy metric -> SomeMetrics -> DList (Metric (GetMetricType metric))
getNamedMetric (metric :: Proxy metric) =
fmap (addName metric) . maybe mempty D.fromList . getMetrics metric
addName :: (KnownMetric metric) => Proxy metric -> Metric (GetMetricType metric) -> Metric (GetMetricType metric)
addName metric x = x{M.attrs = ["name" ~= symbolVal metric] <> x.attrs}
onlyIf :: a -> (a -> Bool) -> Maybe a
onlyIf a p = if p a then Just a else Nothing