2021-09-24 01:56:37 +03:00
|
|
|
-- |
|
|
|
|
-- This module holds functions and data types used for logging at the GraphQL
|
|
|
|
-- layer. In contrast with, logging at the HTTP server layer.
|
2019-07-11 08:37:06 +03:00
|
|
|
module Hasura.GraphQL.Logging
|
2021-09-24 01:56:37 +03:00
|
|
|
( QueryLog (..),
|
|
|
|
GeneratedQuery (..),
|
|
|
|
MonadQueryLog (..),
|
|
|
|
QueryLogKind (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
2021-10-29 17:42:07 +03:00
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.GraphQL.Namespace (RootFieldAlias)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol (GQLReqUnparsed)
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.Server.Types (RequestId)
|
|
|
|
import Hasura.Tracing (TraceT)
|
2019-07-11 08:37:06 +03:00
|
|
|
|
|
|
|
-- | A GraphQL query, optionally generated SQL, and the request id makes up the
|
|
|
|
-- | 'QueryLog'
|
2021-03-13 17:40:50 +03:00
|
|
|
data QueryLog = QueryLog
|
2021-09-24 01:56:37 +03:00
|
|
|
{ _qlQuery :: !GQLReqUnparsed,
|
2021-10-29 17:42:07 +03:00
|
|
|
_qlGeneratedSql :: !(Maybe (RootFieldAlias, GeneratedQuery)),
|
2021-09-24 01:56:37 +03:00
|
|
|
_qlRequestId :: !RequestId,
|
|
|
|
_qlKind :: !QueryLogKind
|
2019-07-11 08:37:06 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 04:21:34 +03:00
|
|
|
data QueryLogKind
|
2021-04-28 20:38:05 +03:00
|
|
|
= QueryLogKindDatabase
|
|
|
|
| QueryLogKindAction
|
|
|
|
| QueryLogKindRemoteSchema
|
|
|
|
| QueryLogKindCached
|
|
|
|
| QueryLogKindIntrospection
|
2021-04-19 04:21:34 +03:00
|
|
|
|
|
|
|
instance J.ToJSON QueryLogKind where
|
|
|
|
toJSON = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
QueryLogKindDatabase -> "database"
|
|
|
|
QueryLogKindAction -> "action"
|
|
|
|
QueryLogKindRemoteSchema -> "remote-schema"
|
|
|
|
QueryLogKindCached -> "cached"
|
2021-04-28 20:38:05 +03:00
|
|
|
QueryLogKindIntrospection -> "introspection"
|
2021-04-19 04:21:34 +03:00
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
data GeneratedQuery = GeneratedQuery
|
2021-09-24 01:56:37 +03:00
|
|
|
{ _gqQueryString :: Text,
|
|
|
|
_gqPreparedArgs :: J.Value
|
2021-03-13 17:40:50 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 08:37:06 +03:00
|
|
|
instance J.ToJSON QueryLog where
|
2021-04-19 04:21:34 +03:00
|
|
|
toJSON (QueryLog gqlQuery generatedQuery reqId kind) =
|
2021-09-24 01:56:37 +03:00
|
|
|
J.object
|
|
|
|
[ "query" J..= gqlQuery,
|
|
|
|
-- NOTE: this customizes the default JSON instance of a pair
|
|
|
|
"generated_sql" J..= fmap fromPair generatedQuery,
|
|
|
|
"request_id" J..= reqId,
|
|
|
|
"kind" J..= kind
|
|
|
|
]
|
2021-04-28 20:38:05 +03:00
|
|
|
where
|
2021-10-29 17:42:07 +03:00
|
|
|
fromPair p = Map.fromList [first toTxt p]
|
2019-07-11 08:37:06 +03:00
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
instance J.ToJSON GeneratedQuery where
|
|
|
|
toJSON (GeneratedQuery queryString preparedArgs) =
|
2021-09-24 01:56:37 +03:00
|
|
|
J.object
|
|
|
|
[ "query" J..= queryString,
|
|
|
|
"prepared_arguments" J..= preparedArgs
|
|
|
|
]
|
2021-03-13 17:40:50 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance L.ToEngineLog QueryLog L.Hasura where
|
2019-07-11 08:37:06 +03:00
|
|
|
toEngineLog ql = (L.LevelInfo, L.ELTQueryLog, J.toJSON ql)
|
|
|
|
|
2020-06-19 09:42:32 +03:00
|
|
|
class Monad m => MonadQueryLog m where
|
2021-09-24 01:56:37 +03:00
|
|
|
logQueryLog ::
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
QueryLog ->
|
|
|
|
m ()
|
2020-06-19 09:42:32 +03:00
|
|
|
|
|
|
|
instance MonadQueryLog m => MonadQueryLog (ExceptT e m) where
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger l = lift $ logQueryLog logger l
|
2020-06-19 09:42:32 +03:00
|
|
|
|
|
|
|
instance MonadQueryLog m => MonadQueryLog (ReaderT r m) where
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger l = lift $ logQueryLog logger l
|
2020-07-15 13:40:48 +03:00
|
|
|
|
|
|
|
instance MonadQueryLog m => MonadQueryLog (TraceT m) where
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger l = lift $ logQueryLog logger l
|
2020-12-14 07:30:19 +03:00
|
|
|
|
|
|
|
instance MonadQueryLog m => MonadQueryLog (MetadataStorageT m) where
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger l = lift $ logQueryLog logger l
|