import { SERVERLESS_MONITORS } from "./serverless_monitors" ; import { updateMonitor , createMonitor , deleteMonitor , getExistingMonitors } from "./monitor-api-requests" ; import { Response } from "node-fetch" ; export interface MonitorParams { [ key ] : any ; } export interface Monitor { [ key ] : MonitorParams ; } param monitor param cloudFormationStackId param service param env returns export function buildMonitorParams ( monitor , cloudFormationStackId , service , env ) { const serverlessMonitorId = Object . keys ( monitor ) [ 0 ] ; if ( ! monitor [ serverlessMonitorId ] ) { monitor [ serverlessMonitorId ] = { } ; } const monitorParams = { ... monitor [ serverlessMonitorId ] } ; if ( ! monitorParams . tags ) { monitorParams . tags = [ ] ; } if ( ! monitorParams . options ) { monitorParams . options = { } ; } if ( monitorParams . type === undefined ) { monitorParams . type = "metric alert" ; } monitorParams . tags = [ ... monitorParams . tags , "serverless_monitor_type:single_function" , `serverless_monitor_id:${ serverlessMonitorId }` , `aws_cloudformation_stack-id:${ cloudFormationStackId }` , "created_by:dd_sls_plugin" , `env:${ env }` , `service:${ service }` , ] ; if ( checkIfRecommendedMonitor ( serverlessMonitorId ) ) { let criticalThreshold = SERVERLESS_MONITORS [ serverlessMonitorId ] . threshold ; if ( monitorParams . options ) { if ( monitorParams . options . thresholds ) { if ( monitorParams . options . thresholds . critical ) { criticalThreshold = monitorParams . options . thresholds . critical ; } } } monitorParams . query = SERVERLESS_MONITORS [ serverlessMonitorId ] . query ( cloudFormationStackId , criticalThreshold ) ; if ( ! monitorParams . message ) { monitorParams . message = SERVERLESS_MONITORS [ serverlessMonitorId ] . message ; } if ( ! monitorParams . name ) { monitorParams . name = SERVERLESS_MONITORS [ serverlessMonitorId ] . name ; } } return monitorParams ; } param serverlessMonitorId returns function checkIfRecommendedMonitor ( serverlessMonitorId ) { return Object . keys ( SERVERLESS_MONITORS ) . includes ( serverlessMonitorId ) ; } param serverlessMonitorId param existingMonitors returns function doesMonitorExist ( serverlessMonitorId , existingMonitors ) { return Object . keys ( existingMonitors ) . includes ( serverlessMonitorId ) ; } param pluginMonitors param existingMonitors param monitorsApiKey param monitorsAppKey returns async function deleteRemovedMonitors ( site , pluginMonitors , existingMonitors , monitorsApiKey , monitorsAppKey ) { const successfullyDeletedMonitors = [ ] ; const currentMonitorIds = [ ] ; pluginMonitors . forEach ( ( currentMonitor ) => currentMonitorIds . push ( Object . keys ( currentMonitor ) [ 0 ] ) ) ; for ( const pluginMonitorId of Object . keys ( existingMonitors ) ) { if ( ! currentMonitorIds . includes ( pluginMonitorId ) ) { const response = await deleteMonitor ( site , existingMonitors [ pluginMonitorId ] , monitorsApiKey , monitorsAppKey ) ; const successfullyDeleted = handleMonitorsApiResponse ( response , pluginMonitorId ) ; if ( successfullyDeleted ) { successfullyDeletedMonitors . push ( ` ${ pluginMonitorId }` ) ; } } } return successfullyDeletedMonitors ; } param response param serverlessMonitorId export function handleMonitorsApiResponse ( response , serverlessMonitorId ? ) { if ( response . status === 200 ) { return true ; } else if ( response . status === 400 ) { throw new Error ( `400 Bad Request: This could be due to incorrect syntax for ${ serverlessMonitorId }` ) ; } else { throw new Error ( `${ response . status } ${ response . statusText }` ) ; } } param monitors param monitorsApiKey param monitorsAppKey param cloudFormationStackId param service param env returns export async function setMonitors ( site , monitors , monitorsApiKey , monitorsAppKey , cloudFormationStackId , service , env ) { const serverlessMonitorIdByMonitorId = await getExistingMonitors ( site , cloudFormationStackId , monitorsApiKey , monitorsAppKey ) ; const successfullyUpdatedMonitors = [ ] ; const successfullyCreatedMonitors = [ ] ; for ( const monitor of monitors ) { const serverlessMonitorId = Object . keys ( monitor ) [ 0 ] ; const monitorIdNumber = serverlessMonitorIdByMonitorId [ serverlessMonitorId ] ; const monitorParams = buildMonitorParams ( monitor , cloudFormationStackId , service , env ) ; const monitorExists = await doesMonitorExist ( serverlessMonitorId , serverlessMonitorIdByMonitorId ) ; if ( monitorExists ) { const response = await updateMonitor ( site , monitorIdNumber , monitorParams , monitorsApiKey , monitorsAppKey ) ; const successfullyCreated = handleMonitorsApiResponse ( response , serverlessMonitorId ) ; if ( successfullyCreated ) { successfullyUpdatedMonitors . push ( ` ${ serverlessMonitorId }` ) ; } } else { const response = await createMonitor ( site , monitorParams , monitorsApiKey , monitorsAppKey ) ; const successfullyUpdated = handleMonitorsApiResponse ( response , serverlessMonitorId ) ; if ( successfullyUpdated ) { successfullyCreatedMonitors . push ( ` ${ serverlessMonitorId }` ) ; } } } const successfullyDeletedMonitors = await deleteRemovedMonitors ( site , monitors , serverlessMonitorIdByMonitorId , monitorsApiKey , monitorsAppKey ) ; const logStatements = [ ] ; if ( successfullyUpdatedMonitors . length > 0 ) { logStatements . push ( `Successfully updated${ successfullyUpdatedMonitors }` ) ; } if ( successfullyCreatedMonitors . length > 0 ) { logStatements . push ( `Successfully created${ successfullyCreatedMonitors }` ) ; } if ( successfullyDeletedMonitors . length > 0 ) { logStatements . push ( `Successfully deleted${ successfullyDeletedMonitors }` ) ; } return logStatements ; }