@@ -2,26 +2,61 @@
22{-# OPTIONS_GHC -fno-warn-orphans #-}
33
44module Control.Logger.Katip
5− ( logSeverityToKSeverity
5+ ( ElasticServer(..)
6+ , logSeverityToKSeverity
7+ , registerElastic
8+ , maybeElastic
69 , KatipContextTState(..)
710 , getKatipLogger
8− , ourItemJson
11+ , ourFormatter
12+ , ourMapping
913 , katipIndexNameString
14+ , reopenableFileLog
1015 ) where
1116
1217import Control.EnvT
1318import Control.Has.Katip ()
1419import Control.Lens hiding ((.=))
1520import Control.Logger.Internal
21+import Control.Logger.Katip.Scribes.Reopenable
1622import Data.Aeson hiding (Error)
23+import Data.ByteString (ByteString)
24+import qualified Data.HashMap.Strict as HM
25+import Data.Proxy as P
1726import Data.Text (Text)
1827import qualified Data.Text as T
28+import qualified Data.Text.Encoding as T
1929import qualified Data.Text.Lazy.Builder as T.Builder
30+import Database.V5.Bloodhound
2031import Katip as K hiding (logMsg)
21−import Katip.Core (LocJs(..), ProcessIDJs(..))
32+import Katip.Core (ItemFunc(..), LocJs(..), ProcessIDJs(..))
2233import Katip.Monadic (KatipContextTState(..))
34+import Katip.Scribes.ElasticSearch
35+import Katip.Scribes.ElasticSearch.Internal as K
36+import Network.HTTP.Client
2337
2438
39+data ElasticServer = ElasticServer
40+ { _esServer :: !(Maybe Server)
41+ , _esUsername :: !(Maybe ByteString)
42+ , _esPassword :: !(Maybe ByteString)
43+ } deriving (Eq, Show)
44+
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"))
50+
51+instance ToJSON ElasticServer where
52+ toJSON es = object
53+ [ "server" .= fmap fromServer (_esServer es)
54+ , "username" .= fmap T.decodeUtf8 (_esUsername es)
55+ , "password" .= fmap T.decodeUtf8 (_esPassword es)
56+ ]
57+ where
58+ fromServer (Server s) = s
59+
2560logSeverityToKSeverity :: LogSeverity -> Severity
2661logSeverityToKSeverity = \case
2762 Debug -> DebugS
@@ -29,6 +64,88 @@ logSeverityToKSeverity = \case
2964 Warn -> WarningS
3065 Error -> ErrorS
3166
67+registerElastic
68+ :: Text
69+ -- ^ git revision
70+ -> Verbosity
71+ -> ElasticServer
72+ -> PermitFunc
73+ -> LogEnv
74+ -> IO LogEnv
75+registerElastic gitRev verbosity ElasticServer{..} permitF env =
76+ case _esServer of
77+ Nothing -> return env
78+ Just server -> do
79+ elastic <- do
80+ mgr <- newManager defaultManagerSettings
81+ let
82+ bloodhoundEnv = (mkBHEnv server mgr)
83+ { bhRequestHook = requestHook }
84+ mkEsScribe cfg bloodhoundEnv permitF verbosity
85+ registerScribe "elastic" elastic defaultScribeSettings env
86+ where
87+ cfg = (defaultEsScribeCfgV5 ixName mappingName)
88+ { essItemFormatter = ourFormatter gitRev
89+ , essIndexMappingValue = ourMapping mappingName }
90+ where
91+ ixName = IndexName katipIndexNameString
92+ mappingName = MappingName "logs"
93+ requestHook = return . case (_esUsername, _esPassword) of
94+ (Just u, Just p) -> applyBasicAuth u p
95+ _ -> id
96+
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.
100+ourFormatter
101+ :: Text
102+ -- ^ git revision string
103+ -> Verbosity
104+ -> ItemFunc Value
105+ourFormatter gitRev verb = ItemFunc $ \item -> go $ ourItemJson verb item
106+ where
107+ go :: Value -> Value
108+ go = \case
109+ Object h -> Object $ setRev h
110+ x -> x -- drop exception here?
111+ where
112+ setRev :: HM.HashMap Text Value -> HM.HashMap Text Value
113+ setRev = HM.insert "rev-hash" (toJSON gitRev)
114+
115+ourMapping :: K.MappingName ESV5 -> Value
116+ourMapping mn = object
117+ [fromMappingName prx mn .= object ["properties" .= object prs]]
118+ where
119+ 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"
131+ , "loc" .= locType
132+ , unanalyzedString "host"
133+ , unanalyzedString "env"
134+ , "at" .= dateType
135+ , unanalyzedString "app"
136+ ]
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]
141+ locPairs =
142+ [ unanalyzedString "loc_pkg"
143+ , unanalyzedString "loc_mod"
144+ , unanalyzedString "loc_ln"
145+ , unanalyzedString "loc_fn"
146+ , unanalyzedString "loc_col"
147+ ]
148+ dateType = object ["format" .= esDateFormat, "type" .= String "date"]
32149
33150katipIndexNameString :: Text
34151katipIndexNameString = "antorica-b2b-logs"
@@ -51,12 +168,18 @@ ourItemJson verb item = formatItem $
51168 , "loc" .= fmap LocJs _itemLoc
52169 ]
53170
171+-- | Return empty server if Nothing
172+maybeElastic :: Maybe ElasticServer -> ElasticServer
173+maybeElastic = \case
174+ Nothing -> ElasticServer Nothing Nothing Nothing
175+ Just a -> a
176+
54177instance LogItem Object where
55178 payloadKeys V0 _ = SomeKeys []
56179 payloadKeys _ _ = AllKeys
57180
58181getKatipLogger :: KatipContextTState -> Logger
59−getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx) mempty
182+getKatipLogger katipCtx = Logger (toObject . ltsContext $ katipCtx)
60183 $ \ ctx stack s msg ->
61184 let ?callStack = stack
62185 in