packages feed

eventlog-live-0.11.0.0: src-app/GHC/Eventlog/Live/App.hs

{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}

{- |
Module      : GHC.Eventlog.Live.App
Description : The implementation of @eventlog-live@.
Stability   : experimental
Portability : portable
-}
module GHC.Eventlog.Live.App (
  main,
) where

import Control.Concurrent (ThreadId, forkIO)
import Control.Concurrent.Chan (newChan)
import Control.Concurrent.LossyChan qualified as LC
import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, readMVar)
import Control.Exception (SomeException, bracket_, catch, finally)
import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.Class (MonadTrans (..))
import Control.Monad.Trans.Except (runExceptT)
import Data.DList qualified as D
import Data.Default (Default (..))
import Data.Foldable (for_)
import Data.Machine (ProcessT, asParts, await, mapping, repeatedly, runT_, stopped, (~>))
import Data.Maybe (fromMaybe, isNothing)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Void (Void, absurd)
import GHC.Debug.Stub.Compat (withMyGhcDebug)
import GHC.Eventlog.Live.App.Environment (OpenTelemetrySdkOptions (..), ServiceName (..), lookupLogLevel, lookupOpenTelemetrySdkOptions)
import GHC.Eventlog.Live.App.Exporter.Otlp (exportToOtlp)
import GHC.Eventlog.Live.App.Exporter.Otlp.Core (withExporters)
import GHC.Eventlog.Live.App.Options (MyDebugOptions (..), OpenTelemetryOptions (openTelemetryExporter), Options (..), options)
import GHC.Eventlog.Live.App.Server (ControlHooks (..), ServerHandle (..), startServer)
import GHC.Eventlog.Live.Config (FullConfig (..))
import GHC.Eventlog.Live.Config qualified as C
import GHC.Eventlog.Live.Logger (InternalMetric (..), Logger, chanLogger, chanSource, filterBySeverity, logDebug, logException, logFatal, logInfo, logMetric, logTick, lossyChanSource, mkStderrLogger, withScope)
import GHC.Eventlog.Live.Machine.Core (Tick (..), dropTick, onlyTick)
import GHC.Eventlog.Live.Machine.Core qualified as M
import GHC.Eventlog.Live.Machine.Validate (validateInput)
import GHC.Eventlog.Live.Machine.WithStartTime qualified as M
import GHC.Eventlog.Live.Processor (InstrumentationScope (..), Resource (..), processEventlogTelemetry, processInternalTelemetry)
import GHC.Eventlog.Live.Source (runWithEventlogSourceHandle, withEventlogSourceHandle)
import GHC.Eventlog.Live.Types.Attribute (AttrValue (..), (~=))
import GHC.Eventlog.Socket.Compat (startMyEventlogSocket)
import GHC.IsList (IsList (..))
import GHC.RTS.Events (Event (..))
import GHC.Stack.Profiler.Compat (withGhcStackProfiler)
import IpeDB.Database qualified as DB
import IpeDB.Types.CostCentre qualified as CC
import IpeDB.Types.InfoProv qualified as IP
import Options.Applicative qualified as O
import Paths_eventlog_live qualified as App
import System.Exit (die)

--------------------------------------------------------------------------------
-- Instrumentation Scope
--------------------------------------------------------------------------------

-- 2025-09-22:
-- Once `cabal2nix` supports Cabal 3.12, this can once again use the value from:
-- `PackageInfo_eventlog_live.name`.
appName :: Text
appName = "eventlog-live"

appScope :: InstrumentationScope
appScope = InstrumentationScope{name = appName, version = App.version}

{- |
The main function for @eventlog-live@.
-}
main :: IO ()
main =
  withGhcStackProfiler $ do
    -- Parse the command-line options
    Options{..} <- O.execParser options

    -- Create a queue for internal telemetry
    internalTelemetryQueue <- newChan

    -- Create a queue for export requests
    (exportRequestInChan, exportRequestOutChan) <- LC.new 8

    -- Construct a logger
    logLevel <- either die pure =<< runExceptT lookupLogLevel
    stderrLogger <- mkStderrLogger
    let logger =
          filterBySeverity logLevel $
            stderrLogger <> chanLogger internalTelemetryQueue

    -- Lookup the OpenTelemetry SDK options
    OpenTelemetrySdkOptions{..} <-
      either die pure =<< runExceptT (lookupOpenTelemetrySdkOptions logger)

    -- Instument THIS PROGRAM with eventlog-socket and/or ghc-debug.
    let MyDebugOptions{..} = myDebugOptions
    startMyEventlogSocket logger maybeMyEventlogSocket
    withMyGhcDebug logger maybeMyGhcDebugSocket $ do
      --
      -- Start the server.
      server <- startServer logger serverOptions exportRequestInChan

      -- Read the configuration file.
      let readConfigFile configFile = do
            logDebug logger $ "Reading configuration file from " <> T.pack configFile
            config <- either (logFatal logger . T.pack) pure =<< C.readConfigFile configFile
            logDebug logger $ "Configuration file:\n" <> C.prettyConfig config
            pure config

      -- Read the configuration file and add derived settings.
      fullConfig <-
        C.toFullConfig eventlogFlushIntervalS
          <$> maybe (pure def) readConfigFile maybeConfigFile
      logDebug logger $
        "Batch interval is " <> T.pack (show fullConfig.batchIntervalMs) <> "ms"
      logDebug logger $
        "Eventlog flush interval is " <> T.pack (show fullConfig.eventlogFlushIntervalX) <> "x"

      -- Find the service name, if any:
      let !serviceName =
            fromMaybe (ServiceName "undefined") $
              (.serviceName) =<< maybeResourceAttributes

      -- Create a resource to represent the monitored process.
      let eventlogResource :: Resource
          eventlogResource =
            Resource
              { attrs =
                  fromList $
                    uncurry (~=) <$> maybe [] (.attributes) maybeResourceAttributes
              }

      -- Create machine to process eventlog into export requests.
      let eventlogProcessor ccdb ipedb =
            M.liftTick M.withStartTime
              ~> M.fanoutTick
                [ -- Log a warning if no input has been received after 10 ticks.
                  validateInput logger 10
                , -- Log event counts.
                  countEvents logger
                , -- Log ticks.
                  logTicks logger
                , -- If no cost-centre database was provided, index the cost-centre events.
                  indexCostCentreEvents (ccdb `onlyIf` isNothing maybeCCDBPath)
                , -- If no info-prov database was provided, index the info-prov events.
                  indexInfoProvEvents (ipedb `onlyIf` isNothing maybeIpeDBPath)
                , -- Process the eventlog events.
                  processEventlogTelemetry logger fullConfig eventlogResource appScope maybeHeapProfBreakdown ccdb ipedb
                    ~> M.liftTick (mapping D.singleton)
                ]
              ~> M.liftTick asParts
              ~> sending exportRequestInChan

      -- Create thread to process eventlog into export requests.
      eventlogProcessorHandle <-
        withScope "eventlogProcessor" logger $ \logger' ->
          fork logger' $ do
            DB.withNewSession def $ \session -> do
              withCostCentreTable maybeCCDBPath session $ \ccdb ->
                withInfoProvTable maybeIpeDBPath session $ \ipedb ->
                  withEventlogSourceHandle
                    logger'
                    eventlogSocketTimeoutS
                    eventlogSocketTimeoutExponent
                    eventlogSourceOptions
                    $ \eventlogSourceHandle -> do
                      -- Notify the control server of the connection status.
                      let newConnection = server.controlHooks.notifyNewConnection serviceName eventlogSourceHandle
                      let endConnection = server.controlHooks.notifyEndConnection serviceName
                      bracket_ newConnection endConnection $
                        -- Run the eventlog processor.
                        runWithEventlogSourceHandle
                          logger'
                          eventlogSourceHandle
                          fullConfig.batchIntervalMs
                          Nothing
                          maybeEventlogLogFile
                          (eventlogProcessor ccdb ipedb)

      -- Create a resource to represent the eventlog-live process.
      let internalResource :: Resource
          internalResource =
            Resource
              { attrs =
                  [ "service.name" ~= AttrText (appName <> "-for-" <> serviceName.serviceName)
                  , "service.version" ~= App.version
                  ]
              }

      -- Create thread to process internal telemetry into export requests.
      let internalTelemetryProcessor =
            runT_ $
              chanSource internalTelemetryQueue
                ~> processInternalTelemetry fullConfig internalResource appScope
                -- NOTE: eventlogProcessor writes ticks to the exportRequestInChan,
                --       and we don't want to duplicate those.
                ~> dropTick
                ~> mapping Item
                ~> sending exportRequestInChan
      internalTelemetryProcessorHandle <-
        withScope "internalTelemetryProcessor" logger $ \logger' ->
          fork logger' internalTelemetryProcessor

      -- Create machine to process export requests.
      let otelExporter =
            withExporters logger exporterOptions $ \exporters ->
              runT_ $
                lossyChanSource exportRequestOutChan
                  ~> exportToOtlp logger fullConfig exporters
      maybeExportRequestProcessorHandle <-
        withScope "otelExporter" logger $ \logger' ->
          if openTelemetryOptions.openTelemetryExporter
            then Just <$> fork logger' otelExporter
            else pure Nothing

      -- Wait for these threads to finish, then exit.
      join eventlogProcessorHandle
      join internalTelemetryProcessorHandle
      for_ maybeExportRequestProcessorHandle join
      server.stop

-- Log all ticks.
logTicks :: (Monad m) => Logger m -> ProcessT m (Tick x) y
logTicks logger = onlyTick ~> logTicks'
 where
  logTicks' = repeatedly $ await >>= lift . logTick logger

-- Count all input events.
countEvents :: (Monad m) => Logger m -> ProcessT m (Tick x) y
countEvents logger =
  M.batchByTickList ~> countEvents'
 where
  countEvents' = repeatedly $ await >>= lift . logMetric logger EventCount . fromIntegral . length

-- Run an action with access to a cost-centre table.
withCostCentreTable :: Maybe FilePath -> DB.Session -> (DB.Table CC.CostCentreId CC.CostCentre -> IO ()) -> IO ()
withCostCentreTable maybeCCDBPath session =
  maybe
    (DB.withNewTable session def)
    (\ccDBPath -> DB.withTableFrom session ccDBPath def)
    maybeCCDBPath

-- Run an action with access to an info-prov table.
withInfoProvTable :: Maybe FilePath -> DB.Session -> (DB.Table IP.InfoProvId IP.InfoProv -> IO ()) -> IO ()
withInfoProvTable maybeIpeDBPath session =
  maybe
    (DB.withNewTable session def)
    (\ipeDBPath -> DB.withTableFrom session ipeDBPath def)
    maybeIpeDBPath

-- Create machine that indexes CostCentre data.
indexCostCentreEvents ::
  Maybe (DB.Table CC.CostCentreId CC.CostCentre) ->
  ProcessT IO (Tick (M.WithStartTime Event)) x
indexCostCentreEvents =
  -- If a cost-centre database was not provided, don't index any new entries.
  maybe stopped (\ccdb -> dropTick ~> DB.indexer (CC.toCostCentre . (.value)) def ccdb ~> mapping absurd)

-- Create machine that indexes InfoProv data.
indexInfoProvEvents ::
  Maybe (DB.Table IP.InfoProvId IP.InfoProv) ->
  ProcessT IO (Tick (M.WithStartTime Event)) x
indexInfoProvEvents =
  -- If an IPE database was not provided, don't index any new entries.
  maybe stopped (\ipedb -> dropTick ~> DB.indexer (IP.toInfoProv . (.value)) def ipedb ~> mapping absurd)

--------------------------------------------------------------------------------
-- Internal helpers
--------------------------------------------------------------------------------

-- A machine that sends values to a `Chan`.
sending :: LC.InChan a -> ProcessT IO a Void
sending chan = repeatedly $ await >>= liftIO . LC.send chan

data ThreadHandle = ThreadHandle {threadId :: ThreadId, finished :: MVar ()}

fork :: Logger IO -> IO () -> IO ThreadHandle
fork logger action = do
  finished <- newEmptyMVar
  threadId <- forkIO $ do
    let finish :: IO ()
        finish = logInfo logger "thread finished" >> putMVar finished ()
        handler :: SomeException -> IO ()
        handler = logException logger
    (action `catch` handler) `finally` finish
  pure ThreadHandle{..}

join :: ThreadHandle -> IO ()
join ThreadHandle{..} =
  readMVar finished

onlyIf :: a -> Bool -> Maybe a
onlyIf a b =
  if b then Just a else Nothing