@@ -2,60 +2,26 @@
2{-# OPTIONS_GHC -fno-warn-orphans #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
4module Control.Logger.Katip
4module Control.Logger.Katip
6− , logSeverityToKSeverity
5+ ( logSeverityToKSeverity
9 , KatipContextTState(..)
6 , KatipContextTState(..)
10 , getKatipLogger
7 , getKatipLogger
11− , ourFormatter
8+ , ourItemJson
13 , katipIndexNameString
9 , katipIndexNameString
17import Control.EnvT
12import Control.EnvT
13+import Control.Has.Katip ()
18import Control.Lens hiding ((.=))
14import Control.Lens hiding ((.=))
19import Control.Logger.Internal
15import Control.Logger.Internal
20−import Control.Logger.Katip.Scribes.Reopenable
21import Data.Aeson hiding (Error)
16import Data.Aeson hiding (Error)
22−import Data.ByteString (ByteString)
23−import qualified Data.HashMap.Strict as HM
24−import Data.Proxy as P
25import Data.Text (Text)
17import Data.Text (Text)
26import qualified Data.Text as T
18import qualified Data.Text as T
27−import qualified Data.Text.Encoding as T
28import qualified Data.Text.Lazy.Builder as T.Builder
19import qualified Data.Text.Lazy.Builder as T.Builder
29−import Database.V5.Bloodhound
30import Katip as K hiding (logMsg)
20import Katip as K hiding (logMsg)
31−import Katip.Core (ItemFunc(..), LocJs(..), ProcessIDJs(..))
21+import Katip.Core (LocJs(..), ProcessIDJs(..))
32import Katip.Monadic (KatipContextTState(..))
22import Katip.Monadic (KatipContextTState(..))
33−import Katip.Scribes.ElasticSearch
34−import Katip.Scribes.ElasticSearch.Internal as K
35−import Network.HTTP.Client
38−data ElasticServer = ElasticServer
39− { _esServer :: !(Maybe Server)
40− , _esUsername :: !(Maybe ByteString)
41− , _esPassword :: !(Maybe ByteString)
42− } deriving (Eq, Show)
44−instance FromJSON ElasticServer where
45− parseJSON = withObject "ElasticServer" $ \obj -> ElasticServer
46− <$> (obj .: "server")
47− <*> (fmap T.encodeUtf8 <$> (obj .: "username"))
48− <*> (fmap T.encodeUtf8 <$> (obj .: "password"))
50−instance ToJSON ElasticServer where
52− [ "server" .= fmap fromServer (_esServer es)
53− , "username" .= fmap T.decodeUtf8 (_esUsername es)
54− , "password" .= fmap T.decodeUtf8 (_esPassword es)
57− fromServer (Server s) = s
59logSeverityToKSeverity :: LogSeverity -> Severity
25logSeverityToKSeverity :: LogSeverity -> Severity
60logSeverityToKSeverity = \case
26logSeverityToKSeverity = \case
61 Debug -> DebugS
27 Debug -> DebugS
@@ -63,91 +29,6 @@ logSeverityToKSeverity = \case
63 Warn -> WarningS
29 Warn -> WarningS
64 Error -> ErrorS
30 Error -> ErrorS
74−registerElastic gitRev verbosity ElasticServer{..} permitF env =
76− Nothing -> return env
79− mgr <- newManager defaultManagerSettings
81− bloodhoundEnv = (mkBHEnv server mgr)
82− { bhRequestHook = requestHook }
83− mkEsScribe cfg bloodhoundEnv permitF verbosity
84− registerScribe "elastic" elastic defaultScribeSettings env
86− cfg = (defaultEsScribeCfgV5 ixName mappingName)
87− { essItemFormatter = ourFormatter gitRev
88− , essIndexMappingValue = ourMapping mappingName }
90− ixName = IndexName katipIndexNameString
91− mappingName = MappingName "logs"
92− requestHook = return . case (_esUsername, _esPassword) of
93− (Just u, Just p) -> applyBasicAuth u p
96−-- | Doubles the value of @msg@ key into the @msg-keyword@ and adds
97−-- the @rev-hash@ value to each record. The @msg-keyword@ is needed to
98−-- group errors by message. The @rev-hash@ is needed for many purposes
99−-- and for grouping as well.
102− -- ^ git revision string
105−ourFormatter gitRev verb = ItemFunc $ \item -> go $ ourItemJson verb item
107− go :: Value -> Value
109− Object h -> Object $ setRev $ dupMsg h
110− x -> x -- drop exception here?
112− setRev :: HM.HashMap Text Value -> HM.HashMap Text Value
113− setRev = HM.insert "rev-hash" (toJSON gitRev)
114− dupMsg :: HM.HashMap Text Value -> HM.HashMap Text Value
115− dupMsg m = HM.alter (const $ HM.lookup "msg" m) "msg-keyword" m
117−ourMapping :: K.MappingName ESV5 -> Value
118−ourMapping mn = object
119− [fromMappingName prx mn .= object ["properties" .= object prs]]
122− [ unanalyzedString "thread"
123− , unanalyzedString "sev"
124− , unanalyzedString "pid"
125− -- ns is frequently fulltext searched
126− , analyzedString "ns"
127− -- we want message to be fulltext searchable
128− , analyzedString "msg"
129− -- we want to group by the message as well
130− , unanalyzedString "msg-keyword"
131− -- we want to group by revision hash
132− , unanalyzedString "rev-hash"
134− , unanalyzedString "host"
135− , unanalyzedString "env"
137− , unanalyzedString "app"
139− prx = P.Proxy :: P.Proxy ESV5
140− unanalyzedString k = k .= unanalyzedStringSpec prx
141− analyzedString k = k .= analyzedStringSpec prx
142− locType = object ["properties" .= object locPairs]
144− [ unanalyzedString "loc_pkg"
145− , unanalyzedString "loc_mod"
146− , unanalyzedString "loc_ln"
147− , unanalyzedString "loc_fn"
148− , unanalyzedString "loc_col"
150− dateType = object ["format" .= esDateFormat, "type" .= String "date"]
152katipIndexNameString :: Text
33katipIndexNameString :: Text
153katipIndexNameString = "antorica-b2b-logs"
34katipIndexNameString = "antorica-b2b-logs"
@@ -170,18 +51,12 @@ ourItemJson verb item = formatItem $
170 , "loc" .= fmap LocJs _itemLoc
51 , "loc" .= fmap LocJs _itemLoc
173−-- | Return empty server if Nothing
174−maybeElastic :: Maybe ElasticServer -> ElasticServer
176− Nothing -> ElasticServer Nothing Nothing Nothing
179instance LogItem Object where
54instance LogItem Object where
180 payloadKeys V0 _ = SomeKeys []
55 payloadKeys V0 _ = SomeKeys []
181 payloadKeys _ _ = AllKeys
56 payloadKeys _ _ = AllKeys
183getKatipLogger :: KatipContextTState -> Logger
58getKatipLogger :: KatipContextTState -> Logger
184−getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx)
59+getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx) mempty
185 $ \ ctx stack s msg ->
60 $ \ ctx stack s msg ->
186 let ?callStack = stack
61 let ?callStack = stack