add metrics for actions (#4139)

- The metrics will include
  - no of synchronous actions
  - no of asynchronous actions
  - no of type relationships with the output
  - no of custom types defined

Co-authored-by: Vamshi Surabhi <0x777@users.noreply.github.com>
This commit is contained in:
Karthikeyan Chinnakonda 2020-03-27 17:46:22 +05:30 committed by GitHub
parent 04e5a20dc0
commit 482fa41e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,17 +24,17 @@ import Hasura.Server.Telemetry.Counters
import Hasura.Server.Version
import qualified CI
import qualified Control.Concurrent.Extended as C
import qualified Data.Aeson as A
import qualified Data.Aeson.Casing as A
import qualified Data.Aeson.TH as A
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as Map
import qualified Data.Text as T
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types as HTTP
import qualified Network.Wreq as Wreq
import qualified Control.Concurrent.Extended as C
import qualified Data.Aeson as A
import qualified Data.Aeson.Casing as A
import qualified Data.Aeson.TH as A
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as Map
import qualified Data.Text as T
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types as HTTP
import qualified Network.Wreq as Wreq
import qualified Language.GraphQL.Draft.Syntax as G
data RelationshipMetric
= RelationshipMetric
@ -53,6 +53,15 @@ data PermissionMetric
} deriving (Show, Eq)
$(A.deriveToJSON (A.aesonDrop 3 A.snakeCase) ''PermissionMetric)
data ActionMetric
= ActionMetric
{ _amSynchronous :: !Int
, _amAsynchronous :: !Int
, _amTypeRelationships :: !Int
, _amCustomTypes :: !Int
} deriving (Show, Eq)
$(A.deriveToJSON (A.aesonDrop 3 A.snakeCase) ''ActionMetric)
data Metrics
= Metrics
{ _mtTables :: !Int
@ -64,6 +73,7 @@ data Metrics
, _mtRemoteSchemas :: !Int
, _mtFunctions :: !Int
, _mtServiceTimings :: !ServiceTimingMetrics
, _mtActions :: !ActionMetric
} deriving (Show, Eq)
$(A.deriveToJSON (A.aesonDrop 3 A.snakeCase) ''Metrics)
@ -153,6 +163,7 @@ computeMetrics sc _mtServiceTimings =
$ Map.map _tiEventTriggerInfoMap userTables
_mtRemoteSchemas = Map.size $ scRemoteSchemas sc
_mtFunctions = Map.size $ Map.filter (not . isSystemDefined . fiSystemDefined) $ scFunctions sc
_mtActions = computeActionsMetrics (scActions sc) (snd . scCustomTypes $ sc)
in Metrics{..}
@ -166,6 +177,24 @@ computeMetrics sc _mtServiceTimings =
permsOfTbl :: TableInfo -> [(RoleName, RolePermInfo)]
permsOfTbl = Map.toList . _tiRolePermInfoMap
computeActionsMetrics :: ActionCache -> AnnotatedObjects -> ActionMetric
computeActionsMetrics ac ao = ActionMetric syncActionsLen asyncActionsLen typeRelationships customTypesLen
where actions = Map.elems ac
syncActionsLen = length . filter ((==ActionSynchronous) . _adKind . _aiDefinition) $ actions
asyncActionsLen = (length actions) - syncActionsLen
outputTypesLen = length . nub . (map (_adOutputType . _aiDefinition)) $ actions
inputTypesLen = length . nub . concat . (map ((map _argType) . _adArguments . _aiDefinition)) $ actions
customTypesLen = inputTypesLen + outputTypesLen
typeRelationships = length . nub . concat . map ((getActionTypeRelationshipNames ao) . _aiDefinition) $ actions
-- gives the count of relationships associated with an action
getActionTypeRelationshipNames :: AnnotatedObjects -> ResolvedActionDefinition -> [RelationshipName]
getActionTypeRelationshipNames annotatedObjs actionDefn =
let typeName = G.getBaseType $ unGraphQLType $ _adOutputType actionDefn
annotatedObj = Map.lookup (ObjectTypeName typeName) annotatedObjs
in maybe [] (Map.keys . _aotRelationships) annotatedObj
-- | Logging related