2018-06-27 16:11:32 +03:00
|
|
|
-- This is taken from wai-logger and customised for our use
|
|
|
|
|
|
|
|
module Hasura.Server.Logging
|
2019-01-02 14:24:17 +03:00
|
|
|
( StartupLog(..)
|
2019-03-12 08:46:27 +03:00
|
|
|
, PGLog(..)
|
2019-04-29 09:22:48 +03:00
|
|
|
, mkInconsMetadataLog
|
2019-11-26 15:14:21 +03:00
|
|
|
, mkHttpAccessLogContext
|
|
|
|
, mkHttpErrorLogContext
|
|
|
|
, mkHttpLog
|
|
|
|
, HttpInfoLog(..)
|
|
|
|
, OperationLog(..)
|
|
|
|
, HttpLogContext(..)
|
2018-08-03 11:43:35 +03:00
|
|
|
, WebHookLog(..)
|
2018-12-13 10:26:15 +03:00
|
|
|
, HttpException
|
2019-11-26 15:14:21 +03:00
|
|
|
, HttpLog (..)
|
2018-07-20 10:22:46 +03:00
|
|
|
) where
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson
|
2019-07-11 08:37:06 +03:00
|
|
|
import Data.Aeson.Casing
|
|
|
|
import Data.Aeson.TH
|
2019-09-19 15:54:40 +03:00
|
|
|
import Data.Int (Int64)
|
2018-10-25 12:37:57 +03:00
|
|
|
|
2019-09-19 15:54:40 +03:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.Text as T
|
2019-11-26 15:14:21 +03:00
|
|
|
import qualified Network.HTTP.Types as HTTP
|
2020-06-16 18:23:06 +03:00
|
|
|
import qualified Network.Wai.Extended as Wai
|
2018-10-25 12:37:57 +03:00
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
import Hasura.HTTP
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.Logging
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
2019-07-11 08:37:06 +03:00
|
|
|
import Hasura.RQL.Types
|
2019-09-19 15:54:40 +03:00
|
|
|
import Hasura.Server.Compression
|
2018-07-17 16:23:23 +03:00
|
|
|
import Hasura.Server.Utils
|
2020-04-24 12:10:53 +03:00
|
|
|
import Hasura.Session
|
2020-07-15 13:40:48 +03:00
|
|
|
import Hasura.Tracing (TraceT)
|
2019-01-02 14:24:17 +03:00
|
|
|
|
|
|
|
data StartupLog
|
|
|
|
= StartupLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ slLogLevel :: !LogLevel
|
2019-01-02 14:24:17 +03:00
|
|
|
, slKind :: !T.Text
|
|
|
|
, slInfo :: !Value
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance ToJSON StartupLog where
|
|
|
|
toJSON (StartupLog _ k info) =
|
|
|
|
object [ "kind" .= k
|
|
|
|
, "info" .= info
|
|
|
|
]
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog StartupLog Hasura where
|
2019-01-02 14:24:17 +03:00
|
|
|
toEngineLog startupLog =
|
2019-07-11 08:37:06 +03:00
|
|
|
(slLogLevel startupLog, ELTStartup, toJSON startupLog)
|
2018-09-27 14:22:49 +03:00
|
|
|
|
2019-03-12 08:46:27 +03:00
|
|
|
data PGLog
|
|
|
|
= PGLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ plLogLevel :: !LogLevel
|
2019-03-12 08:46:27 +03:00
|
|
|
, plMessage :: !T.Text
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance ToJSON PGLog where
|
|
|
|
toJSON (PGLog _ msg) =
|
|
|
|
object ["message" .= msg]
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog PGLog Hasura where
|
2019-03-12 08:46:27 +03:00
|
|
|
toEngineLog pgLog =
|
2019-11-26 15:14:21 +03:00
|
|
|
(plLogLevel pgLog, ELTInternal ILTPgClient, toJSON pgLog)
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2019-04-29 09:22:48 +03:00
|
|
|
data MetadataLog
|
|
|
|
= MetadataLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ mlLogLevel :: !LogLevel
|
2019-04-29 09:22:48 +03:00
|
|
|
, mlMessage :: !T.Text
|
|
|
|
, mlInfo :: !Value
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance ToJSON MetadataLog where
|
|
|
|
toJSON (MetadataLog _ msg infoVal) =
|
|
|
|
object [ "message" .= msg
|
|
|
|
, "info" .= infoVal
|
|
|
|
]
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog MetadataLog Hasura where
|
2019-04-29 09:22:48 +03:00
|
|
|
toEngineLog ml =
|
2019-11-26 15:14:21 +03:00
|
|
|
(mlLogLevel ml, ELTInternal ILTMetadata, toJSON ml)
|
2019-04-29 09:22:48 +03:00
|
|
|
|
2019-11-27 01:49:42 +03:00
|
|
|
mkInconsMetadataLog :: [InconsistentMetadata] -> MetadataLog
|
2019-04-29 09:22:48 +03:00
|
|
|
mkInconsMetadataLog objs =
|
2019-11-26 15:14:21 +03:00
|
|
|
MetadataLog LevelWarn "Inconsistent Metadata!" $
|
2019-04-29 09:22:48 +03:00
|
|
|
object [ "objects" .= objs]
|
|
|
|
|
2018-08-03 11:43:35 +03:00
|
|
|
data WebHookLog
|
|
|
|
= WebHookLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ whlLogLevel :: !LogLevel
|
|
|
|
, whlStatusCode :: !(Maybe HTTP.Status)
|
2018-08-03 11:43:35 +03:00
|
|
|
, whlUrl :: !T.Text
|
2019-11-26 15:14:21 +03:00
|
|
|
, whlMethod :: !HTTP.StdMethod
|
2018-12-13 10:26:15 +03:00
|
|
|
, whlError :: !(Maybe HttpException)
|
2018-08-03 11:43:35 +03:00
|
|
|
, whlResponse :: !(Maybe T.Text)
|
2020-04-03 03:00:13 +03:00
|
|
|
, whlMessage :: !(Maybe T.Text)
|
2018-08-03 11:43:35 +03:00
|
|
|
} deriving (Show)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog WebHookLog Hasura where
|
2018-08-03 11:43:35 +03:00
|
|
|
toEngineLog webHookLog =
|
2019-07-11 08:37:06 +03:00
|
|
|
(whlLogLevel webHookLog, ELTWebhookLog, toJSON webHookLog)
|
2018-08-03 11:43:35 +03:00
|
|
|
|
|
|
|
instance ToJSON WebHookLog where
|
2018-12-13 10:26:15 +03:00
|
|
|
toJSON whl =
|
2019-11-26 15:14:21 +03:00
|
|
|
object [ "status_code" .= (HTTP.statusCode <$> whlStatusCode whl)
|
2018-12-13 10:26:15 +03:00
|
|
|
, "url" .= whlUrl whl
|
|
|
|
, "method" .= show (whlMethod whl)
|
|
|
|
, "http_error" .= whlError whl
|
|
|
|
, "response" .= whlResponse whl
|
2020-04-03 03:00:13 +03:00
|
|
|
, "message" .= whlMessage whl
|
2018-12-13 10:26:15 +03:00
|
|
|
]
|
2018-08-03 11:43:35 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
class (Monad m) => HttpLog m where
|
|
|
|
logHttpError
|
|
|
|
:: Logger Hasura
|
|
|
|
-- ^ the logger
|
|
|
|
-> Maybe UserInfo
|
|
|
|
-- ^ user info may or may not be present (error can happen during user resolution)
|
|
|
|
-> RequestId
|
|
|
|
-- ^ request id of the request
|
|
|
|
-> Wai.Request
|
|
|
|
-- ^ the Wai.Request object
|
2020-07-29 17:48:36 +03:00
|
|
|
-> (BL.ByteString, Maybe Value)
|
|
|
|
-- ^ the request body and parsed request
|
2019-11-26 15:14:21 +03:00
|
|
|
-> QErr
|
|
|
|
-- ^ the error
|
|
|
|
-> [HTTP.Header]
|
|
|
|
-- ^ list of request headers
|
|
|
|
-> m ()
|
|
|
|
|
|
|
|
logHttpSuccess
|
|
|
|
:: Logger Hasura
|
|
|
|
-- ^ the logger
|
|
|
|
-> Maybe UserInfo
|
|
|
|
-- ^ user info may or may not be present (error can happen during user resolution)
|
|
|
|
-> RequestId
|
|
|
|
-- ^ request id of the request
|
|
|
|
-> Wai.Request
|
|
|
|
-- ^ the Wai.Request object
|
2020-07-29 17:48:36 +03:00
|
|
|
-> (BL.ByteString, Maybe Value)
|
|
|
|
-- ^ the request body and parsed request
|
2019-11-26 15:14:21 +03:00
|
|
|
-> BL.ByteString
|
|
|
|
-- ^ the response bytes
|
|
|
|
-> BL.ByteString
|
|
|
|
-- ^ the compressed response bytes
|
|
|
|
-- ^ TODO: make the above two type represented
|
2020-01-16 04:56:57 +03:00
|
|
|
-> Maybe (DiffTime, DiffTime)
|
|
|
|
-- ^ IO/network wait time and service time (respectively) for this request, if available.
|
2019-11-26 15:14:21 +03:00
|
|
|
-> Maybe CompressionType
|
|
|
|
-- ^ possible compression type
|
|
|
|
-> [HTTP.Header]
|
|
|
|
-- ^ list of request headers
|
|
|
|
-> m ()
|
|
|
|
|
2020-07-15 13:40:48 +03:00
|
|
|
instance HttpLog m => HttpLog (TraceT m) where
|
|
|
|
logHttpError a b c d e f g = lift $ logHttpError a b c d e f g
|
|
|
|
logHttpSuccess a b c d e f g h i j = lift $ logHttpSuccess a b c d e f g h i j
|
|
|
|
|
2019-07-11 08:37:06 +03:00
|
|
|
-- | Log information about the HTTP request
|
|
|
|
data HttpInfoLog
|
|
|
|
= HttpInfoLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ hlStatus :: !HTTP.Status
|
2019-07-11 08:37:06 +03:00
|
|
|
, hlMethod :: !T.Text
|
2020-06-16 18:23:06 +03:00
|
|
|
, hlSource :: !Wai.IpAddress
|
2019-07-11 08:37:06 +03:00
|
|
|
, hlPath :: !T.Text
|
2019-11-26 15:14:21 +03:00
|
|
|
, hlHttpVersion :: !HTTP.HttpVersion
|
2019-09-19 15:54:40 +03:00
|
|
|
, hlCompression :: !(Maybe CompressionType)
|
2019-11-26 15:14:21 +03:00
|
|
|
, hlHeaders :: ![HTTP.Header]
|
|
|
|
-- ^ all the request headers
|
2018-06-27 16:11:32 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2019-07-11 08:37:06 +03:00
|
|
|
instance ToJSON HttpInfoLog where
|
2019-11-26 15:14:21 +03:00
|
|
|
toJSON (HttpInfoLog st met src path hv compressTypeM _) =
|
|
|
|
object [ "status" .= HTTP.statusCode st
|
2018-06-27 16:11:32 +03:00
|
|
|
, "method" .= met
|
2020-06-16 18:23:06 +03:00
|
|
|
, "ip" .= Wai.showIPAddress src
|
2018-06-27 16:11:32 +03:00
|
|
|
, "url" .= path
|
|
|
|
, "http_version" .= show hv
|
2019-09-19 15:54:40 +03:00
|
|
|
, "content_encoding" .= (compressionTypeToTxt <$> compressTypeM)
|
2018-06-27 16:11:32 +03:00
|
|
|
]
|
|
|
|
|
2019-07-11 08:37:06 +03:00
|
|
|
-- | Information about a GraphQL/Hasura metadata operation over HTTP
|
|
|
|
data OperationLog
|
|
|
|
= OperationLog
|
|
|
|
{ olRequestId :: !RequestId
|
2020-04-24 12:10:53 +03:00
|
|
|
, olUserVars :: !(Maybe SessionVariables)
|
2019-07-11 08:37:06 +03:00
|
|
|
, olResponseSize :: !(Maybe Int64)
|
2020-01-16 04:56:57 +03:00
|
|
|
, olRequestReadTime :: !(Maybe Seconds)
|
|
|
|
-- ^ Request IO wait time, i.e. time spent reading the full request from the socket.
|
|
|
|
, olQueryExecutionTime :: !(Maybe Seconds)
|
|
|
|
-- ^ Service time, not including request IO wait time.
|
2019-07-11 08:37:06 +03:00
|
|
|
, olQuery :: !(Maybe Value)
|
2019-10-07 21:04:33 +03:00
|
|
|
, olRawQuery :: !(Maybe Text)
|
|
|
|
, olError :: !(Maybe QErr)
|
2018-06-27 16:11:32 +03:00
|
|
|
} deriving (Show, Eq)
|
2019-11-26 15:14:21 +03:00
|
|
|
|
2020-07-29 17:48:36 +03:00
|
|
|
$(deriveToJSON (aesonDrop 2 snakeCase){omitNothingFields = True} ''OperationLog)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
data HttpLogContext
|
|
|
|
= HttpLogContext
|
|
|
|
{ hlcHttpInfo :: !HttpInfoLog
|
|
|
|
, hlcOperation :: !OperationLog
|
2019-07-11 08:37:06 +03:00
|
|
|
} deriving (Show, Eq)
|
2019-11-26 15:14:21 +03:00
|
|
|
$(deriveToJSON (aesonDrop 3 snakeCase) ''HttpLogContext)
|
2019-07-11 08:37:06 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
mkHttpAccessLogContext
|
|
|
|
:: Maybe UserInfo
|
|
|
|
-- ^ Maybe because it may not have been resolved
|
2019-07-11 08:37:06 +03:00
|
|
|
-> RequestId
|
|
|
|
-> Wai.Request
|
|
|
|
-> BL.ByteString
|
2020-01-16 04:56:57 +03:00
|
|
|
-> Maybe (DiffTime, DiffTime)
|
2019-09-19 15:54:40 +03:00
|
|
|
-> Maybe CompressionType
|
2019-11-26 15:14:21 +03:00
|
|
|
-> [HTTP.Header]
|
|
|
|
-> HttpLogContext
|
2020-01-06 22:09:27 +03:00
|
|
|
mkHttpAccessLogContext userInfoM reqId req res mTiming compressTypeM headers =
|
2019-07-11 08:37:06 +03:00
|
|
|
let http = HttpInfoLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ hlStatus = status
|
|
|
|
, hlMethod = bsToTxt $ Wai.requestMethod req
|
2020-06-16 18:23:06 +03:00
|
|
|
, hlSource = Wai.getSourceFromFallback req
|
2019-11-26 15:14:21 +03:00
|
|
|
, hlPath = bsToTxt $ Wai.rawPathInfo req
|
|
|
|
, hlHttpVersion = Wai.httpVersion req
|
2019-09-19 15:54:40 +03:00
|
|
|
, hlCompression = compressTypeM
|
2019-11-26 15:14:21 +03:00
|
|
|
, hlHeaders = headers
|
2019-07-11 08:37:06 +03:00
|
|
|
}
|
|
|
|
op = OperationLog
|
|
|
|
{ olRequestId = reqId
|
2020-04-24 12:10:53 +03:00
|
|
|
, olUserVars = _uiSession <$> userInfoM
|
2019-07-11 08:37:06 +03:00
|
|
|
, olResponseSize = respSize
|
2020-01-16 04:56:57 +03:00
|
|
|
, olRequestReadTime = Seconds . fst <$> mTiming
|
|
|
|
, olQueryExecutionTime = Seconds . snd <$> mTiming
|
2019-07-11 08:37:06 +03:00
|
|
|
, olQuery = Nothing
|
2019-10-07 21:04:33 +03:00
|
|
|
, olRawQuery = Nothing
|
2019-07-11 08:37:06 +03:00
|
|
|
, olError = Nothing
|
|
|
|
}
|
2019-11-26 15:14:21 +03:00
|
|
|
in HttpLogContext http op
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-11-26 15:14:21 +03:00
|
|
|
status = HTTP.status200
|
2019-07-11 08:37:06 +03:00
|
|
|
respSize = Just $ BL.length res
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
mkHttpErrorLogContext
|
|
|
|
:: Maybe UserInfo
|
|
|
|
-- ^ Maybe because it may not have been resolved
|
2019-07-11 08:37:06 +03:00
|
|
|
-> RequestId
|
|
|
|
-> Wai.Request
|
2020-07-29 17:48:36 +03:00
|
|
|
-> (BL.ByteString, Maybe Value)
|
2019-07-11 08:37:06 +03:00
|
|
|
-> QErr
|
2020-01-16 04:56:57 +03:00
|
|
|
-> Maybe (DiffTime, DiffTime)
|
2019-09-19 15:54:40 +03:00
|
|
|
-> Maybe CompressionType
|
2019-11-26 15:14:21 +03:00
|
|
|
-> [HTTP.Header]
|
|
|
|
-> HttpLogContext
|
2020-07-29 17:48:36 +03:00
|
|
|
mkHttpErrorLogContext userInfoM reqId waiReq (reqBody, parsedReq) err mTiming compressTypeM headers =
|
2019-07-11 08:37:06 +03:00
|
|
|
let http = HttpInfoLog
|
2019-11-26 15:14:21 +03:00
|
|
|
{ hlStatus = qeStatus err
|
2020-07-29 17:48:36 +03:00
|
|
|
, hlMethod = bsToTxt $ Wai.requestMethod waiReq
|
|
|
|
, hlSource = Wai.getSourceFromFallback waiReq
|
|
|
|
, hlPath = bsToTxt $ Wai.rawPathInfo waiReq
|
|
|
|
, hlHttpVersion = Wai.httpVersion waiReq
|
|
|
|
, hlCompression = compressTypeM
|
2019-11-26 15:14:21 +03:00
|
|
|
, hlHeaders = headers
|
2019-07-11 08:37:06 +03:00
|
|
|
}
|
|
|
|
op = OperationLog
|
2019-10-07 21:04:33 +03:00
|
|
|
{ olRequestId = reqId
|
2020-04-24 12:10:53 +03:00
|
|
|
, olUserVars = _uiSession <$> userInfoM
|
2019-10-07 21:04:33 +03:00
|
|
|
, olResponseSize = Just $ BL.length $ encode err
|
2020-01-16 04:56:57 +03:00
|
|
|
, olRequestReadTime = Seconds . fst <$> mTiming
|
|
|
|
, olQueryExecutionTime = Seconds . snd <$> mTiming
|
2020-07-29 17:48:36 +03:00
|
|
|
, olQuery = parsedReq
|
|
|
|
, olRawQuery = maybe (Just $ bsToTxt $ BL.toStrict reqBody) (const Nothing) parsedReq
|
2019-10-07 21:04:33 +03:00
|
|
|
, olError = Just err
|
2019-07-11 08:37:06 +03:00
|
|
|
}
|
2019-11-26 15:14:21 +03:00
|
|
|
in HttpLogContext http op
|
|
|
|
|
|
|
|
data HttpLogLine
|
|
|
|
= HttpLogLine
|
|
|
|
{ _hlLogLevel :: !LogLevel
|
|
|
|
, _hlLogLine :: !HttpLogContext
|
|
|
|
}
|
|
|
|
|
|
|
|
instance ToEngineLog HttpLogLine Hasura where
|
|
|
|
toEngineLog (HttpLogLine logLevel logLine) =
|
|
|
|
(logLevel, ELTHttpLog, toJSON logLine)
|
|
|
|
|
|
|
|
mkHttpLog :: HttpLogContext -> HttpLogLine
|
|
|
|
mkHttpLog httpLogCtx =
|
|
|
|
let isError = isJust $ olError $ hlcOperation httpLogCtx
|
|
|
|
logLevel = bool LevelInfo LevelError isError
|
|
|
|
in HttpLogLine logLevel httpLogCtx
|