graphql-engine/server/src-lib/Hasura/GraphQL/Logging.hs
Rakesh Emmadi f2a5d7cef3 server/pro/multitenant: Postgres connection routing using kriti templates
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6822
Co-authored-by: paritosh-08 <85472423+paritosh-08@users.noreply.github.com>
Co-authored-by: Naveen Naidu <30195193+Naveenaidu@users.noreply.github.com>
Co-authored-by: Sooraj <8408875+soorajshankar@users.noreply.github.com>
Co-authored-by: Varun Choudhary <68095256+Varun-Choudhary@users.noreply.github.com>
Co-authored-by: Sean Park-Ross <94021366+seanparkross@users.noreply.github.com>
GitOrigin-RevId: 61cfc00a97de88df1ede3f26829a0d78ec9c0bc5
2023-01-25 07:14:31 +00:00

96 lines
3.1 KiB
Haskell

-- |
-- This module holds functions and data types used for logging at the GraphQL
-- layer. In contrast with, logging at the HTTP server layer.
module Hasura.GraphQL.Logging
( QueryLog (..),
GeneratedQuery (..),
MonadQueryLog (..),
QueryLogKind (..),
)
where
import Data.Aeson qualified as J
import Data.HashMap.Strict qualified as Map
import Data.Text.Extended
import Hasura.GraphQL.Namespace (RootFieldAlias)
import Hasura.GraphQL.Transport.HTTP.Protocol (GQLReqUnparsed)
import Hasura.Logging qualified as L
import Hasura.Metadata.Class
import Hasura.Prelude
import Hasura.RQL.DDL.ConnectionTemplate (BackendResolvedConnectionTemplate (..))
import Hasura.Server.Types (RequestId)
import Hasura.Tracing (TraceT)
-- | A GraphQL query, optionally generated SQL, and the request id makes up the
-- | 'QueryLog'
data QueryLog = QueryLog
{ _qlQuery :: !GQLReqUnparsed,
_qlGeneratedSql :: !(Maybe (RootFieldAlias, GeneratedQuery)),
_qlRequestId :: !RequestId,
_qlKind :: !QueryLogKind
}
data QueryLogKind
= QueryLogKindDatabase (Maybe (BackendResolvedConnectionTemplate))
| QueryLogKindAction
| QueryLogKindRemoteSchema
| QueryLogKindCached
| QueryLogKindIntrospection
instance J.ToJSON QueryLogKind where
toJSON = \case
QueryLogKindDatabase _ -> "database"
QueryLogKindAction -> "action"
QueryLogKindRemoteSchema -> "remote-schema"
QueryLogKindCached -> "cached"
QueryLogKindIntrospection -> "introspection"
data GeneratedQuery = GeneratedQuery
{ _gqQueryString :: Text,
_gqPreparedArgs :: J.Value
}
instance J.ToJSON QueryLog where
toJSON (QueryLog gqlQuery generatedQuery reqId kind) =
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
]
<> maybe [] (\val -> ["connection_template" J..= val]) (getResolvedConnectionTemplate kind)
where
fromPair p = Map.fromList [first toTxt p]
getResolvedConnectionTemplate :: QueryLogKind -> Maybe (BackendResolvedConnectionTemplate)
getResolvedConnectionTemplate (QueryLogKindDatabase x) = x
getResolvedConnectionTemplate _ = Nothing
instance J.ToJSON GeneratedQuery where
toJSON (GeneratedQuery queryString preparedArgs) =
J.object
[ "query" J..= queryString,
"prepared_arguments" J..= preparedArgs
]
instance L.ToEngineLog QueryLog L.Hasura where
toEngineLog ql = (L.LevelInfo, L.ELTQueryLog, J.toJSON ql)
class Monad m => MonadQueryLog m where
logQueryLog ::
L.Logger L.Hasura ->
QueryLog ->
m ()
instance MonadQueryLog m => MonadQueryLog (ExceptT e m) where
logQueryLog logger l = lift $ logQueryLog logger l
instance MonadQueryLog m => MonadQueryLog (ReaderT r m) where
logQueryLog logger l = lift $ logQueryLog logger l
instance MonadQueryLog m => MonadQueryLog (TraceT m) where
logQueryLog logger l = lift $ logQueryLog logger l
instance MonadQueryLog m => MonadQueryLog (MetadataStorageT m) where
logQueryLog logger l = lift $ logQueryLog logger l