|
| 1 | +import { |
| 2 | + GraphWidget, |
| 3 | + HorizontalAnnotation, |
| 4 | + IWidget, |
| 5 | +} from "aws-cdk-lib/aws-cloudwatch"; |
| 6 | +import { |
| 7 | + BaseMonitoringProps, |
| 8 | + CountAxisFromZero, |
| 9 | + DefaultGraphWidgetHeight, |
| 10 | + DefaultSummaryWidgetHeight, |
| 11 | + LatencyType, |
| 12 | + MetricWithAlarmSupport, |
| 13 | + Monitoring, |
| 14 | + MonitoringScope, |
| 15 | + PercentageAxisFromZeroToHundred, |
| 16 | + QuarterWidth, |
| 17 | + ThirdWidth, |
| 18 | + TimeAxisMillisFromZero, |
| 19 | + UsageAlarmFactory, |
| 20 | + UsageThreshold, |
| 21 | +} from "../../common"; |
| 22 | +import { |
| 23 | + MonitoringHeaderWidget, |
| 24 | + MonitoringNamingStrategy, |
| 25 | +} from "../../dashboard"; |
| 26 | +import { |
| 27 | + DocumentDbMetricFactory, |
| 28 | + DocumentDbMetricFactoryProps, |
| 29 | +} from "./DocumentDbMetricFactory"; |
| 30 | + |
| 31 | +export interface DocumentDbMonitoringOptions extends BaseMonitoringProps { |
| 32 | + readonly addCpuUsageAlarm?: Record<string, UsageThreshold>; |
| 33 | +} |
| 34 | + |
| 35 | +export interface DocumentDbMonitoringProps |
| 36 | + extends DocumentDbMetricFactoryProps, |
| 37 | + DocumentDbMonitoringOptions {} |
| 38 | + |
| 39 | +export class DocumentDbMonitoring extends Monitoring { |
| 40 | + protected readonly title: string; |
| 41 | + protected readonly url?: string; |
| 42 | + |
| 43 | + protected readonly usageAlarmFactory: UsageAlarmFactory; |
| 44 | + protected readonly usageAnnotations: HorizontalAnnotation[]; |
| 45 | + |
| 46 | + protected readonly cpuUsageMetric: MetricWithAlarmSupport; |
| 47 | + protected readonly readLatencyMetric: MetricWithAlarmSupport; |
| 48 | + protected readonly writeLatencyMetric: MetricWithAlarmSupport; |
| 49 | + protected readonly connectionsMetric: MetricWithAlarmSupport; |
| 50 | + protected readonly cursorsMetric: MetricWithAlarmSupport; |
| 51 | + protected readonly transactionsMetric: MetricWithAlarmSupport; |
| 52 | + protected readonly throttledMetric: MetricWithAlarmSupport; |
| 53 | + |
| 54 | + constructor(scope: MonitoringScope, props: DocumentDbMonitoringProps) { |
| 55 | + super(scope, props); |
| 56 | + |
| 57 | + const metricFactory = new DocumentDbMetricFactory( |
| 58 | + scope.createMetricFactory(), |
| 59 | + props |
| 60 | + ); |
| 61 | + this.cpuUsageMetric = metricFactory.metricAverageCpuUsageInPercent(); |
| 62 | + this.readLatencyMetric = metricFactory.metricReadLatencyInMillis( |
| 63 | + LatencyType.P90 |
| 64 | + ); |
| 65 | + this.writeLatencyMetric = metricFactory.metricWriteLatencyInMillis( |
| 66 | + LatencyType.P90 |
| 67 | + ); |
| 68 | + this.connectionsMetric = metricFactory.metricMaxConnectionCount(); |
| 69 | + this.cursorsMetric = metricFactory.metricMaxCursorCount(); |
| 70 | + this.transactionsMetric = metricFactory.metricMaxTransactionOpenCount(); |
| 71 | + this.throttledMetric = |
| 72 | + metricFactory.metricOperationsThrottledDueLowMemoryCount(); |
| 73 | + |
| 74 | + const namingStrategy = new MonitoringNamingStrategy({ |
| 75 | + ...props, |
| 76 | + fallbackConstructName: metricFactory.clusterIdentifier, |
| 77 | + namedConstruct: props.cluster, |
| 78 | + }); |
| 79 | + this.title = namingStrategy.resolveHumanReadableName(); |
| 80 | + this.url = scope |
| 81 | + .createAwsConsoleUrlFactory() |
| 82 | + .getDocumentDbClusterUrl(metricFactory.clusterIdentifier); |
| 83 | + const alarmFactory = this.createAlarmFactory( |
| 84 | + namingStrategy.resolveAlarmFriendlyName() |
| 85 | + ); |
| 86 | + |
| 87 | + this.usageAlarmFactory = new UsageAlarmFactory(alarmFactory); |
| 88 | + this.usageAnnotations = []; |
| 89 | + |
| 90 | + for (const disambiguator in props.addCpuUsageAlarm) { |
| 91 | + const alarmProps = props.addCpuUsageAlarm[disambiguator]; |
| 92 | + const createdAlarm = this.usageAlarmFactory.addMaxCpuUsagePercentAlarm( |
| 93 | + this.cpuUsageMetric, |
| 94 | + alarmProps, |
| 95 | + disambiguator |
| 96 | + ); |
| 97 | + this.usageAnnotations.push(createdAlarm.annotation); |
| 98 | + this.addAlarm(createdAlarm); |
| 99 | + } |
| 100 | + |
| 101 | + props.useCreatedAlarms?.consume(this.createdAlarms()); |
| 102 | + } |
| 103 | + |
| 104 | + summaryWidgets(): IWidget[] { |
| 105 | + return [ |
| 106 | + this.createTitleWidget(), |
| 107 | + this.createResourceUsageWidget(ThirdWidth, DefaultSummaryWidgetHeight), |
| 108 | + this.createConnectionsWidget(ThirdWidth, DefaultSummaryWidgetHeight), |
| 109 | + this.createLatencyWidget(ThirdWidth, DefaultSummaryWidgetHeight), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + widgets(): IWidget[] { |
| 114 | + return [ |
| 115 | + this.createTitleWidget(), |
| 116 | + this.createResourceUsageWidget(QuarterWidth, DefaultGraphWidgetHeight), |
| 117 | + this.createConnectionsWidget(QuarterWidth, DefaultGraphWidgetHeight), |
| 118 | + this.createTransactionsWidget(QuarterWidth, DefaultGraphWidgetHeight), |
| 119 | + this.createLatencyWidget(QuarterWidth, DefaultGraphWidgetHeight), |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + protected createTitleWidget() { |
| 124 | + return new MonitoringHeaderWidget({ |
| 125 | + family: "DocumentDB", |
| 126 | + title: this.title, |
| 127 | + goToLinkUrl: this.url, |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + protected createResourceUsageWidget(width: number, height: number) { |
| 132 | + return new GraphWidget({ |
| 133 | + width, |
| 134 | + height, |
| 135 | + title: "CPU Usage", |
| 136 | + left: [this.cpuUsageMetric], |
| 137 | + leftYAxis: PercentageAxisFromZeroToHundred, |
| 138 | + leftAnnotations: this.usageAnnotations, |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + protected createConnectionsWidget(width: number, height: number) { |
| 143 | + return new GraphWidget({ |
| 144 | + width, |
| 145 | + height, |
| 146 | + title: "Connections", |
| 147 | + left: [this.connectionsMetric], |
| 148 | + leftYAxis: CountAxisFromZero, |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + protected createTransactionsWidget(width: number, height: number) { |
| 153 | + return new GraphWidget({ |
| 154 | + width, |
| 155 | + height, |
| 156 | + title: "Transactions", |
| 157 | + left: [this.transactionsMetric, this.cursorsMetric], |
| 158 | + leftYAxis: CountAxisFromZero, |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + protected createLatencyWidget(width: number, height: number) { |
| 163 | + return new GraphWidget({ |
| 164 | + width, |
| 165 | + height, |
| 166 | + title: "Latency", |
| 167 | + left: [this.readLatencyMetric, this.writeLatencyMetric], |
| 168 | + leftYAxis: TimeAxisMillisFromZero, |
| 169 | + }); |
| 170 | + } |
| 171 | +} |
0 commit comments