logging-effect handlers for google-logging and google-pubsub
Handlers for logging-effect which can fetch logs to Google Logging and to Google PubSub.
This package is not in hackage yet. So for install it add to stack.yaml
in packages
next:
- location:
git: https://github.com/d61h6k4/logging-effect-google.git
commit: <insert commit here>
and add logging-effect-google
to your cabal
file.
Here example how I use it with wai server on GCP. Additionally here used wai-logging-effect-google. Here google logging handler used to store logs from wai and google pubsub handler to store events from clients.
events
:: Handler IO PubsubMessage -> Request -> IO Response
events pubsub request =
case checkAuth request of
Left err -> return err
Right token -> do
reqbody <- liftIO $ requestBody request
case (checkMethod request >> extractCheckSum request >>=
checkIntegrity (calculateMD5 reqbody)) of
Left response -> return response
Right response ->
case decodeMessage reqbody :: Either String EventLogs of
Left err ->
return
(responseLBS
status400
[("Content-Type", "plain/text")]
(BSLC8.pack err))
Right msg' ->
mapM_
(\entity ->
(pubsub
(pubsubMessage & pmData .~
Just (encodeMessage entity))))
(localiso token msg') >>
return response
app :: Handler IO PubsubMessage -> Application
app pubsub request respond =
case rawPathInfo request of
"/events" ->
events pubsub request >>= respond
main :: IO ()
main = do
lgr <- newLogger Debug stdout
env <- newEnv <&> (envScopes .~ ( loggingWriteScope ! pubSubScope)) . (envLogger .~ lgr)
withGooglePubSubHandler
(defaultBatchingOptions
{ flushMaxDelay = 3 * 1000000
, flushMaxQueueSize = 128
})
env
("pubsub_topic_name") $
\pubsub ->
withGoogleLoggingHandler
(defaultBatchingOptions
{ flushMaxDelay = 10 * 1000000
, flushMaxQueueSize = 128
})
env
(Just
("projects/" <> ("test_project_id") <>
("/logs/negotians")))
(Just
((monitoredResource & mrLabels .~
(Just
(monitoredResourceLabels
(HashMap.fromList
([ ( "instanceId"
, ("0000"))
, ("zone", ("eu-west1-c"))]))))) &
mrType .~
(Just "gce_instance")))
Nothing $
\wailogger ->
run 3000 $ loggerMiddleware wailogger $ gzip def $
app pubsub
Fork it.
Distributed under BSD license. See LICENSE
for more information.