@@ -2,61 +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
18import Control.Has.Katip ()
13import Control.Has.Katip ()
19import Control.Lens hiding ((.=))
14import Control.Lens hiding ((.=))
20import Control.Logger.Internal
15import Control.Logger.Internal
21−import Control.Logger.Katip.Scribes.Reopenable
22import Data.Aeson hiding (Error)
16import Data.Aeson hiding (Error)
23−import Data.ByteString (ByteString)
24−import qualified Data.HashMap.Strict as HM
25−import Data.Proxy as P
26import Data.Text (Text)
17import Data.Text (Text)
27import qualified Data.Text as T
18import qualified Data.Text as T
28−import qualified Data.Text.Encoding as T
29import qualified Data.Text.Lazy.Builder as T.Builder
19import qualified Data.Text.Lazy.Builder as T.Builder
30−import Database.V5.Bloodhound
31import Katip as K hiding (logMsg)
20import Katip as K hiding (logMsg)
32−import Katip.Core (ItemFunc(..), LocJs(..), ProcessIDJs(..))
21+import Katip.Core (LocJs(..), ProcessIDJs(..))
33import Katip.Monadic (KatipContextTState(..))
22import Katip.Monadic (KatipContextTState(..))
34−import Katip.Scribes.ElasticSearch
35−import Katip.Scribes.ElasticSearch.Internal as K
36−import Network.HTTP.Client
39−data ElasticServer = ElasticServer
40− { _esServer :: !(Maybe Server)
41− , _esUsername :: !(Maybe ByteString)
42− , _esPassword :: !(Maybe ByteString)
43− } deriving (Eq, Show)
45−instance FromJSON ElasticServer where
46− parseJSON = withObject "ElasticServer" $ \obj -> ElasticServer
47− <$> (obj .: "server")
48− <*> (fmap T.encodeUtf8 <$> (obj .: "username"))
49− <*> (fmap T.encodeUtf8 <$> (obj .: "password"))
51−instance ToJSON ElasticServer where
53− [ "server" .= fmap fromServer (_esServer es)
54− , "username" .= fmap T.decodeUtf8 (_esUsername es)
55− , "password" .= fmap T.decodeUtf8 (_esPassword es)
58− fromServer (Server s) = s
60logSeverityToKSeverity :: LogSeverity -> Severity
25logSeverityToKSeverity :: LogSeverity -> Severity
61logSeverityToKSeverity = \case
26logSeverityToKSeverity = \case
62 Debug -> DebugS
27 Debug -> DebugS
@@ -64,88 +29,6 @@ logSeverityToKSeverity = \case
64 Warn -> WarningS
29 Warn -> WarningS
65 Error -> ErrorS
30 Error -> ErrorS
75−registerElastic gitRev verbosity ElasticServer{..} permitF env =
77− Nothing -> return env
80− mgr <- newManager defaultManagerSettings
82− bloodhoundEnv = (mkBHEnv server mgr)
83− { bhRequestHook = requestHook }
84− mkEsScribe cfg bloodhoundEnv permitF verbosity
85− registerScribe "elastic" elastic defaultScribeSettings env
87− cfg = (defaultEsScribeCfgV5 ixName mappingName)
88− { essItemFormatter = ourFormatter gitRev
89− , essIndexMappingValue = ourMapping mappingName }
91− ixName = IndexName katipIndexNameString
92− mappingName = MappingName "logs"
93− requestHook = return . case (_esUsername, _esPassword) of
94− (Just u, Just p) -> applyBasicAuth u p
97−-- | Adds the @rev-hash@ value to each record.
98−-- Early we copied `msg` to `msg-keyword` here but drop it
99−-- because purpose of this duplication is not clear.
102− -- ^ git revision string
105−ourFormatter gitRev verb = ItemFunc $ \item -> go $ ourItemJson verb item
107− go :: Value -> Value
109− Object h -> Object $ setRev 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)
115−ourMapping :: K.MappingName ESV5 -> Value
116−ourMapping mn = object
117− [fromMappingName prx mn .= object ["properties" .= object prs]]
120− [ unanalyzedString "thread"
121− , unanalyzedString "sev"
122− , unanalyzedString "pid"
123− -- ns is frequently fulltext searched
124− , analyzedString "ns"
125− -- we want message to be fulltext searchable
126− , analyzedString "msg"
127− -- we want to group by the message as well
128− , unanalyzedString "msg-keyword"
129− -- we want to group by revision hash
130− , unanalyzedString "rev-hash"
132− , unanalyzedString "host"
133− , unanalyzedString "env"
135− , unanalyzedString "app"
137− prx = P.Proxy :: P.Proxy ESV5
138− unanalyzedString k = k .= unanalyzedStringSpec prx
139− analyzedString k = k .= analyzedStringSpec prx
140− locType = object ["properties" .= object locPairs]
142− [ unanalyzedString "loc_pkg"
143− , unanalyzedString "loc_mod"
144− , unanalyzedString "loc_ln"
145− , unanalyzedString "loc_fn"
146− , unanalyzedString "loc_col"
148− dateType = object ["format" .= esDateFormat, "type" .= String "date"]
150katipIndexNameString :: Text
33katipIndexNameString :: Text
151katipIndexNameString = "antorica-b2b-logs"
34katipIndexNameString = "antorica-b2b-logs"
@@ -168,18 +51,12 @@ ourItemJson verb item = formatItem $
168 , "loc" .= fmap LocJs _itemLoc
51 , "loc" .= fmap LocJs _itemLoc
171−-- | Return empty server if Nothing
172−maybeElastic :: Maybe ElasticServer -> ElasticServer
174− Nothing -> ElasticServer Nothing Nothing Nothing
177instance LogItem Object where
54instance LogItem Object where
178 payloadKeys V0 _ = SomeKeys []
55 payloadKeys V0 _ = SomeKeys []
179 payloadKeys _ _ = AllKeys
56 payloadKeys _ _ = AllKeys
181getKatipLogger :: KatipContextTState -> Logger
58getKatipLogger :: KatipContextTState -> Logger
182−getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx)
59+getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx) mempty
183 $ \ ctx stack s msg ->
60 $ \ ctx stack s msg ->
184 let ?callStack = stack
61 let ?callStack = stack