runtime: increment trigger/service counters on the bus

This commit is contained in:
2026-09-15 16:34:35 +03:00
parent 09479b5c48
commit 2a477b4977
4 changed files with 45 additions and 8 deletions
+14 -2
View File
@@ -6,6 +6,8 @@ module HomeAssistant.Runtime.Bus
, CallIdGen(..)
, mkCallIdGen
, withBus
, recordInbound
, recordOutbound
, channelHassEval
) where
@@ -21,10 +23,12 @@ import Control.Concurrent.STM
import Data.Aeson (Value)
import Data.IORef (atomicModifyIORef', newIORef)
import HomeAssistant.Controller (HASSEff (..), Service)
import HomeAssistant.Runtime.Metrics (AppMetrics (..))
import Network.WebSockets (Connection)
import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitItem, Severity (..), Verbosity (V2), registerScribe, defaultScribeSettings, initLogEnv, ls, sl, logFM, katipAddContext, KatipContext)
import Control.Exception (bracket)
import System.IO (stdout)
import System.Metrics.Counter (inc)
import Data.UUID (toText)
import AFRP (Request(..), Event(..))
import Control.Monad.IO.Class (MonadIO, liftIO)
@@ -38,10 +42,11 @@ data Bus = Bus
, busConn :: TVar (Maybe Connection)
, busGen :: CallIdGen
, busLogEnv :: LogEnv
, busMetrics :: AppMetrics
}
withBus :: Severity -> (Bus -> IO a) -> IO a
withBus severity callback = do
withBus :: Severity -> AppMetrics -> (Bus -> IO a) -> IO a
withBus severity metrics callback = do
handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2
let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "hass-controller" "production"
-- closeScribes will stop accepting new logs, flush existing ones and clean up resources
@@ -52,8 +57,15 @@ withBus severity callback = do
<*> newTVarIO Nothing
<*> mkCallIdGen 0
<*> pure le
<*> pure metrics
callback bus
recordInbound :: Bus -> IO ()
recordInbound bus = inc (amTriggersIn (busMetrics bus))
recordOutbound :: Bus -> IO ()
recordOutbound bus = inc (amServicesOut (busMetrics bus))
channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
channelHassEval bus = \case
CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do
+4 -1
View File
@@ -96,7 +96,9 @@ receiveLoop bus conn = forever $ do
Left () -> atomically $ writeTChan (busInbound bus) Tick
Right msg -> case eitherDecode msg of
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
Right v -> atomically $ writeTChan (busInbound bus) (Event v)
Right v -> do
recordInbound bus
atomically $ writeTChan (busInbound bus) (Event v)
receiveJSON :: WS.Connection -> IO Value
receiveJSON conn = do
@@ -128,6 +130,7 @@ sendWithId bus conn request svc = do
let textData = encode $ encodeService callId svc
runKatipContextT (busLogEnv bus) (sl "traceId" (toText (requestTraceId request))) "connection" $
logFM DebugS (ls textData)
recordOutbound bus
WS.sendTextData conn textData
writerAction :: Bus -> IO Void