refactor: standardize query logging to follow code logging other observations
This commit addresses the following TODO in observationMessage function: "TODO pending refactor: The logic for printing the query cannot be done here. Join the observationMessages function into observationLogger to avoid this mempty." Changing observationMessage to observationMessages (that returns [Text] instead of Text) made it possible to address issue documented as TODO and harmonize query logging with other observations.
This commit is contained in:
committed by
Steve Chavez
parent
2408cd332d
commit
8e1f0a0471
+6
-11
@@ -102,9 +102,9 @@ observationLogger loggerState logLevel obs = case obs of
|
|||||||
o@(HasqlPoolObs _) -> do
|
o@(HasqlPoolObs _) -> do
|
||||||
when (logLevel >= LogDebug) $ do
|
when (logLevel >= LogDebug) $ do
|
||||||
logWithZTime loggerState $ observationMessages o
|
logWithZTime loggerState $ observationMessages o
|
||||||
QueryObs gq status -> do
|
o@(QueryObs _ status) -> do
|
||||||
when (shouldLogResponse logLevel status) $
|
when (shouldLogResponse logLevel status) $
|
||||||
logMainQ loggerState gq
|
logWithZTime loggerState $ observationMessages o
|
||||||
o@PoolRequest ->
|
o@PoolRequest ->
|
||||||
when (logLevel >= LogDebug) $ do
|
when (logLevel >= LogDebug) $ do
|
||||||
logWithZTime loggerState $ observationMessages o
|
logWithZTime loggerState $ observationMessages o
|
||||||
@@ -125,13 +125,6 @@ logWithZTime loggerState txts = do
|
|||||||
zTime <- stateGetZTime loggerState
|
zTime <- stateGetZTime loggerState
|
||||||
traverse_ (hPutStrLn stderr . (toS (formatTime defaultTimeLocale "%d/%b/%Y:%T %z: " zTime) <>)) txts
|
traverse_ (hPutStrLn stderr . (toS (formatTime defaultTimeLocale "%d/%b/%Y:%T %z: " zTime) <>)) txts
|
||||||
|
|
||||||
logMainQ :: LoggerState -> MainQuery -> IO ()
|
|
||||||
logMainQ loggerState MainQuery{mqOpenAPI=(x, y, z),..} =
|
|
||||||
let snipts = renderSnippet <$> [mqTxVars, fromMaybe mempty mqPreReq, mqMain, x, y, z, fromMaybe mempty mqExplain]
|
|
||||||
-- Does not log SQL when it's empty (happens on OPTIONS requests and when the openapi queries are not generated)
|
|
||||||
logQ q = when (q /= mempty) $ logWithZTime loggerState $ pure $ showOnSingleLine '\n' $ T.decodeUtf8 q in
|
|
||||||
mapM_ logQ snipts
|
|
||||||
|
|
||||||
-- TODO: maybe patch upstream hasql-dynamic-statements so we have a less hackish way to convert
|
-- TODO: maybe patch upstream hasql-dynamic-statements so we have a less hackish way to convert
|
||||||
-- the SQL.Snippet or maybe don't use hasql-dynamic-statements and resort to plain strings for the queries and use regular hasql
|
-- the SQL.Snippet or maybe don't use hasql-dynamic-statements and resort to plain strings for the queries and use regular hasql
|
||||||
renderSnippet :: SQL.Snippet -> ByteString
|
renderSnippet :: SQL.Snippet -> ByteString
|
||||||
@@ -198,8 +191,10 @@ observationMessages = \case
|
|||||||
pure $ "Received a config reload message on the " <> show channel <> " channel"
|
pure $ "Received a config reload message on the " <> show channel <> " channel"
|
||||||
DBListenerConnectionCleanupFail ex ->
|
DBListenerConnectionCleanupFail ex ->
|
||||||
pure $ "Failed during listener connection cleanup: " <> showOnSingleLine '\t' (show ex)
|
pure $ "Failed during listener connection cleanup: " <> showOnSingleLine '\t' (show ex)
|
||||||
QueryObs{} ->
|
(QueryObs MainQuery{mqOpenAPI=(x, y, z),..} _) ->
|
||||||
mempty -- TODO pending refactor: The logic for printing the query cannot be done here. Join the observationMessages function into observationLogger to avoid this mempty.
|
let snipts = renderSnippet <$> [mqTxVars, fromMaybe mempty mqPreReq, mqMain, x, y, z, fromMaybe mempty mqExplain]
|
||||||
|
in
|
||||||
|
showOnSingleLine '\n' . T.decodeUtf8 <$> filter (/= mempty) snipts
|
||||||
ConfigReadErrorObs usageErr ->
|
ConfigReadErrorObs usageErr ->
|
||||||
pure $ "Failed to query database settings for the config parameters." <> jsonMessage usageErr
|
pure $ "Failed to query database settings for the config parameters." <> jsonMessage usageErr
|
||||||
QueryRoleSettingsErrorObs usageErr ->
|
QueryRoleSettingsErrorObs usageErr ->
|
||||||
|
|||||||
Reference in New Issue
Block a user