This commit is contained in:
MasseR committed 2025-03-28 19:55:59 +02:00
1 parent 05cf5df18f
commit c13f01a012
9 files changed
+296 -91

No files matched your search

+18 -5
View File
@@ -3,6 +3,7 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE LambdaCase #-}
module M where
@@ -23,6 +24,9 @@ import System.Metrics qualified as EKG
import UnliftIO (MonadUnliftIO)
import Wallabag.Auth (Auth, Auth' (..))
import qualified Data.HashMap.Strict as HM
import Tracer (Tracer(..))
type KatipTracer = Tracer (Maybe (K.Severity, K.LogStr))
data S = S
{ _sTokenCache :: Auth,
@@ -31,7 +35,9 @@ data S = S
_sLogContext :: K.LogContexts,
_sCreds :: Creds,
_sWallabagMetrics :: HTTPMetric,
_sOpenrouterMetrics :: HTTPMetric
_sOpenrouterMetrics :: HTTPMetric,
_sKatipTracer :: KatipTracer,
_sMetricsStore :: EKG.Store
}
deriving (Generic)
@@ -64,8 +70,13 @@ instance K.KatipContext M where
getKatipNamespace = view logNamespace
localKatipNamespace = local . over logNamespace
initialS :: K.Severity -> (S -> IO a) -> IO a
initialS severity f = do
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
wbMetrics <-
HTTPMetric
@@ -77,12 +88,14 @@ initialS severity f = do
<$> EKG.createDistribution "openrouter.requests" store
<*> EKG.createCounter "openrouter.retries" store
<*> EKG.createCounter "openrouter.failures" store
c <- getCreds
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 "main" le mempty c wbMetrics orMetrics)
f (S cache ns le mempty c wbMetrics orMetrics kTrace store)
-- TODO: This is just for debug purposes
EKG.sampleAll store >>= mapM_ print . HM.toList
pure res