|
| 1 | +from aws_cdk import Duration, aws_apigateway |
| 2 | +from aws_cdk import aws_dynamodb as dynamodb |
| 3 | +from aws_cdk import aws_lambda as _lambda |
| 4 | +from cdk_monitoring_constructs import CustomMetricGroup, ErrorRateThreshold, LatencyThreshold, MetricStatistic, MonitoringFacade |
| 5 | +from constructs import Construct |
| 6 | + |
| 7 | +from cdk.service import constants |
| 8 | + |
| 9 | + |
| 10 | +class CrudMonitoring(Construct): |
| 11 | + |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + scope: Construct, |
| 15 | + id_: str, |
| 16 | + crud_api: aws_apigateway.RestApi, |
| 17 | + db: dynamodb.Table, |
| 18 | + idempotency_table: dynamodb.Table, |
| 19 | + functions: list[_lambda.Function], |
| 20 | + ) -> None: |
| 21 | + super().__init__(scope, id_) |
| 22 | + self.id_ = id_ |
| 23 | + self._build_high_level_dashboard(crud_api) |
| 24 | + self._build_low_level_dashboard(db, idempotency_table, functions) |
| 25 | + |
| 26 | + def _build_high_level_dashboard(self, crud_api: aws_apigateway.RestApi): |
| 27 | + high_level_facade = MonitoringFacade(self, f'{self.id_}HighFacade') |
| 28 | + high_level_facade.add_large_header('Order REST API High Level Dashboard') |
| 29 | + high_level_facade.monitor_api_gateway( |
| 30 | + api=crud_api, |
| 31 | + add5_xx_fault_rate_alarm={'internal_error': ErrorRateThreshold(max_error_rate=1)}, |
| 32 | + ) |
| 33 | + metric_factory = high_level_facade.create_metric_factory() |
| 34 | + create_metric = metric_factory.create_metric( |
| 35 | + metric_name='ValidCreateOrderEvents', |
| 36 | + namespace=constants.METRICS_NAMESPACE, |
| 37 | + statistic=MetricStatistic.N, |
| 38 | + dimensions_map={constants.METRICS_DIMENSION_KEY: constants.SERVICE_NAME}, |
| 39 | + label='create order events', |
| 40 | + period=Duration.days(1), |
| 41 | + ) |
| 42 | + |
| 43 | + group = CustomMetricGroup(metrics=[create_metric], title='Daily Order Requests') |
| 44 | + high_level_facade.monitor_custom(metric_groups=[group], human_readable_name='Daily KPIs', alarm_friendly_name='KPIs') |
| 45 | + |
| 46 | + def _build_low_level_dashboard(self, db: dynamodb.Table, idempotency_table: dynamodb.Table, functions: list[_lambda.Function]): |
| 47 | + low_level_facade = MonitoringFacade(self, f'{self.id_}LowFacade') |
| 48 | + low_level_facade.add_large_header('Orders REST API Low Level Dashboard') |
| 49 | + for func in functions: |
| 50 | + low_level_facade.monitor_lambda_function( |
| 51 | + lambda_function=func, |
| 52 | + add_latency_p90_alarm={'p90': LatencyThreshold(max_latency=Duration.seconds(3))}, |
| 53 | + ) |
| 54 | + low_level_facade.monitor_log( |
| 55 | + log_group_name=func.log_group.log_group_name, |
| 56 | + human_readable_name='Error logs', |
| 57 | + pattern='ERROR', |
| 58 | + alarm_friendly_name='error logs', |
| 59 | + ) |
| 60 | + |
| 61 | + low_level_facade.monitor_dynamo_table(table=db, billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST) |
| 62 | + low_level_facade.monitor_dynamo_table(table=idempotency_table, billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST) |
0 commit comments