111 lines
3.4 KiB
Haskell
111 lines
3.4 KiB
Haskell
{-# LANGUAGE FunctionalDependencies #-}
|
|
{-# LANGUAGE LambdaCase #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
{-# OPTIONS_GHC -Wall #-}
|
|
|
|
module M where
|
|
|
|
import Control.Concurrent.STM
|
|
import Control.Exception (bracket)
|
|
import Control.Lens (over, view)
|
|
import Control.Lens.TH (makeFields)
|
|
import Control.Monad.Base (MonadBase)
|
|
import Control.Monad.Catch (MonadCatch, MonadThrow, MonadMask)
|
|
import Control.Monad.Reader (MonadIO, MonadReader, ReaderT, local, runReaderT)
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
import Creds
|
|
|
|
import GHC.Generics (Generic)
|
|
import HTTP (HTTPMetric (HTTPMetric))
|
|
import Katip qualified as K
|
|
import System.IO (stdout)
|
|
import System.Metrics qualified as EKG
|
|
import Tracer (Tracer (..))
|
|
import UnliftIO (MonadUnliftIO)
|
|
import Wallabag.Auth (Auth, Auth' (..))
|
|
import qualified Network.Wreq.Session as Wreq
|
|
|
|
type KatipTracer = Tracer (Maybe (K.Severity, K.LogStr))
|
|
|
|
data S = S
|
|
{ _sTokenCache :: Auth,
|
|
_sLogNamespace :: K.Namespace,
|
|
_sLogEnv :: K.LogEnv,
|
|
_sLogContext :: K.LogContexts,
|
|
_sCreds :: Creds,
|
|
_sWallabagMetrics :: HTTPMetric,
|
|
_sOpenrouterMetrics :: HTTPMetric,
|
|
_sEmbeddingMetrics :: HTTPMetric,
|
|
_sKatipTracer :: KatipTracer,
|
|
_sMetricsStore :: EKG.Store,
|
|
_sSession :: Wreq.Session
|
|
}
|
|
deriving (Generic)
|
|
|
|
makeFields ''S
|
|
|
|
newtype M a = M (ReaderT S IO a)
|
|
deriving
|
|
( Functor,
|
|
Applicative,
|
|
Monad,
|
|
MonadReader S,
|
|
MonadIO,
|
|
MonadUnliftIO,
|
|
MonadThrow,
|
|
MonadCatch,
|
|
MonadBaseControl IO,
|
|
MonadBase IO,
|
|
MonadMask
|
|
)
|
|
|
|
runM :: S -> M a -> IO a
|
|
runM s (M m) = runReaderT m s
|
|
|
|
instance K.Katip M where
|
|
getLogEnv = view logEnv
|
|
localLogEnv = local . over logEnv
|
|
|
|
instance K.KatipContext M where
|
|
getKatipContext = view logContext
|
|
localKatipContext = local . over logContext
|
|
getKatipNamespace = view logNamespace
|
|
localKatipNamespace = local . over logNamespace
|
|
|
|
mkKatipTrace :: K.LogEnv -> K.Namespace -> KatipTracer
|
|
mkKatipTrace le ns = Tracer $ \case
|
|
Nothing -> pure ()
|
|
Just (severity, msg) -> K.runKatipContextT le () ns $ K.logFM severity msg
|
|
|
|
initialS :: Maybe FilePath -> K.Severity -> (S -> IO a) -> IO a
|
|
initialS credsPath severity f = do
|
|
store <- EKG.newStore
|
|
wsession <- Wreq.newSession
|
|
EKG.registerGcMetrics store
|
|
wbMetrics <-
|
|
HTTPMetric
|
|
<$> EKG.createDistribution "wallabag.requests" store
|
|
<*> EKG.createCounter "wallabag.retries" store
|
|
<*> EKG.createCounter "wallabag.failures" store
|
|
orMetrics <-
|
|
HTTPMetric
|
|
<$> EKG.createDistribution "openrouter.requests" store
|
|
<*> EKG.createCounter "openrouter.retries" store
|
|
<*> EKG.createCounter "openrouter.failures" store
|
|
emMetrics <-
|
|
HTTPMetric
|
|
<$> EKG.createDistribution "embeddings.requests" store
|
|
<*> EKG.createCounter "embeddings.retries" store
|
|
<*> EKG.createCounter "embeddings.failures" store
|
|
c <- maybe getDefaultCreds readCreds credsPath
|
|
handleScribe <- K.mkHandleScribe K.ColorIfTerminal stdout (K.permitItem severity) K.V2
|
|
let makeLogEnv = K.registerScribe "stdout" handleScribe K.defaultScribeSettings =<< K.initLogEnv "wallabag" "production"
|
|
res <- bracket makeLogEnv K.closeScribes $ \le -> do
|
|
let kTrace = mkKatipTrace le ns
|
|
ns = "main"
|
|
cache <- newTVarIO Unauthenticated
|
|
f (S cache ns le mempty c wbMetrics orMetrics emMetrics kTrace store wsession)
|
|
pure res
|