server: always add query type in http-log logs (whenever available)

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8353
GitOrigin-RevId: f7a2f8ab033043ed331c15b861218547de6f2744
This commit is contained in:
Puru Gupta 2023-03-20 21:42:32 +05:30 committed by hasura-bot
parent c8f4ea27fc
commit 5ec4d6e91b
2 changed files with 16 additions and 6 deletions

View File

@ -35,7 +35,9 @@ module Hasura.Server.Logging
)
where
import Control.Lens ((^?))
import Data.Aeson
import Data.Aeson.Lens (key, _String)
import Data.Aeson.TH
import Data.ByteString.Lazy qualified as BL
import Data.Environment qualified as Env
@ -460,6 +462,14 @@ isQueryIncludedInLogs urlPath LoggingSettings {..}
metadataUrlPaths = ["/v1/metadata", "/v1/query"]
isMetadataRequest = urlPath `elem` metadataUrlPaths
-- | Add the 'query' field to the http-log if `MetadataQueryLoggingMode`
-- is set to `MetadataQueryLoggingEnabled` else only adds the `query.type` field.
addQuery :: Maybe Value -> Text -> LoggingSettings -> Maybe Value
addQuery parsedReq path loggingSettings =
if isQueryIncludedInLogs path loggingSettings
then parsedReq
else Just $ object ["type" .= (fmap (^? key "type" . _String)) parsedReq]
mkHttpAccessLogContext ::
-- | Maybe because it may not have been resolved
Maybe UserInfo ->
@ -496,7 +506,7 @@ mkHttpAccessLogContext userInfoM loggingSettings reqId req (_, parsedReq) uncomp
olRequestReadTime = Seconds . fst <$> mTiming,
olQueryExecutionTime = Seconds . snd <$> mTiming,
olRequestMode = batching,
olQuery = if (isQueryIncludedInLogs (hlPath http) loggingSettings) then parsedReq else Nothing,
olQuery = addQuery parsedReq (hlPath http) loggingSettings,
olRawQuery = Nothing,
olError = Nothing
}
@ -511,13 +521,13 @@ mkHttpAccessLogContext userInfoM loggingSettings reqId req (_, parsedReq) uncomp
GQLQueryOperationSuccess (GQLQueryOperationSuccessLog {..}) ->
BatchOperationSuccess $
BatchOperationSuccessLog
(if (isQueryIncludedInLogs (hlPath http) loggingSettings) then parsedReq else Nothing)
(addQuery parsedReq (hlPath http) loggingSettings)
gqolResponseSize
(convertDuration gqolQueryExecutionTime)
GQLQueryOperationError (GQLQueryOperationErrorLog {..}) ->
BatchOperationError $
BatchOperationErrorLog
(if (isQueryIncludedInLogs (hlPath http) loggingSettings) then parsedReq else Nothing)
(addQuery parsedReq (hlPath http) loggingSettings)
gqelError
)
opLogs
@ -559,7 +569,7 @@ mkHttpErrorLogContext userInfoM loggingSettings reqId waiReq (reqBody, parsedReq
olUncompressedResponseSize = responseSize,
olRequestReadTime = Seconds . fst <$> mTiming,
olQueryExecutionTime = Seconds . snd <$> mTiming,
olQuery = if (isQueryIncludedInLogs (hlPath http) loggingSettings) then parsedReq else Nothing,
olQuery = addQuery parsedReq (hlPath http) loggingSettings,
-- if parsedReq is Nothing, add the raw query
olRawQuery = maybe (reqToLog $ Just $ bsToTxt $ BL.toStrict reqBody) (const Nothing) parsedReq,
olError = Just err,

View File

@ -201,11 +201,11 @@ class TestLogging():
print(http_logs[0])
assert 'error' in http_logs[0]['detail']['operation']
assert http_logs[0]['detail']['operation']['error']['code'] == 'access-denied'
assert http_logs[0]['detail']['operation'].get('query') is None
assert "type" in http_logs[0]['detail']['operation'].get('query')
# By default, 'raw_query' field is ignored for metadata queries. To allow
# logging this field use the flag HASURA_GRAPHQL_ENABLE_METADATA_QUERY_LOGGING
assert http_logs[0]['detail']['operation'].get('raw_query') is None
class TestWebsocketLogging():
"""
Test logs emitted on websocket transport