Skip to content

Commit acd69cb

Browse files
author
Eugene Cheung
authored
fix: Revert "feat(dashboards): improve customizability (#349)" (#354)
This reverts commit 6db09b0. This had some unexpected effects on extended uses of the library. We'll do a followup to this to reintroduce the change but with a major version bump. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 7f02cf6 commit acd69cb

File tree

11 files changed

+38
-1566
lines changed

11 files changed

+38
-1566
lines changed

API.md

Lines changed: 10 additions & 1068 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -265,79 +265,6 @@ This is a general procedure on how to do it:
265265

266266
Both of these monitoring base classes are dashboard segments, so you can add them to your monitoring by calling `.addSegment()` on the `MonitoringFacade`.
267267

268-
### Custom dashboards
269-
270-
If you want *even* more flexibility, you can take complete control over dashboard generation by leveraging dynamic dashboarding features. This allows you to create an arbitrary number of dashboards while configuring each of them separately. You can do this in three simple steps:
271-
272-
1. Create a dynamic dashboard factory
273-
2. Create `IDynamicDashboardSegment` implementations
274-
3. Add Dynamic Segments to your `MonitoringFacade`
275-
276-
#### Create a dynamic dashboard factory
277-
278-
The below code sample will generate two dashboards with the following names:
279-
* ExampleDashboards-HostedService
280-
* ExampleDashboards-Infrastructure
281-
282-
283-
```ts
284-
// create the dynamic dashboard factory.
285-
const factory = new DynamicDashboardFactory(stack, "DynamicDashboards", {
286-
dashboardNamePrefix: "ExampleDashboards",
287-
dashboardConfigs: [
288-
// 'name' is the minimum required configuration
289-
{ name: "HostedService" },
290-
// below is an example of additional dashboard-specific config options
291-
{
292-
name: "Infrastructure",
293-
range: Duration.hours(3),
294-
periodOverride: PeriodOverride.AUTO,
295-
renderingPreference: DashboardRenderingPreference.BITMAP_ONLY
296-
},
297-
],
298-
});
299-
```
300-
301-
#### Create `IDynamicDashboardSegment` implementations
302-
For each construct you want monitored, you will need to create an implementation of an `IDynamicDashboardSegment`. The following is a basic reference implementation as an example:
303-
304-
```ts
305-
export enum DashboardTypes {
306-
HostedService = "HostedService",
307-
Infrastructure = "Infrastructure",
308-
}
309-
310-
class ExampleSegment implements IDynamicDashboardSegment {
311-
widgetsForDashboard(name: string): IWidget[] {
312-
// this logic is what's responsible for allowing your dynamic segment to return
313-
// different widgets for different dashboards
314-
switch (name) {
315-
case DashboardTypes.HostedService:
316-
return [new TextWidget({ markdown: "This shows metrics for your service hosted on AWS Infrastructure" })];
317-
case DashboardTypes.Infrastructure:
318-
return [new TextWidget({ markdown: "This shows metrics for the AWS Infrastructure supporting your hosted service" })];
319-
default:
320-
throw new Error("Unexpected dashboard name!");
321-
}
322-
}
323-
}
324-
```
325-
326-
#### Add Dynamic Segments to MonitoringFacade
327-
328-
When you have instances of an `IDynamicDashboardSegment` to use, they can be added to your dashboard like this:
329-
330-
```ts
331-
monitoring.addDynamicSegment(new ExampleSegment());
332-
```
333-
334-
Now, this widget will be added to both dashboards and will show different content depending on the dashboard. Using the above example code, two dashboards will be generated with the following content:
335-
336-
* Dashboard Name: "ExampleDashboards-HostedService"
337-
* Content: "This shows metrics for your service hosted on AWS Infrastructure"
338-
* Dashboard Name: "ExampleDashboards-Infrastructure"
339-
* Content: "This shows metrics for the AWS Infrastructure supporting your hosted service"
340-
341268
### Monitoring scopes
342269

343270
You can monitor complete CDK construct scopes using an aspect. It will automatically discover all monitorable resources within the scope recursively and add them to your dashboard.

lib/common/monitoring/Monitoring.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { IWidget } from "aws-cdk-lib/aws-cloudwatch";
22

33
import { MonitoringScope } from "./MonitoringScope";
44
import {
5-
DefaultDashboards,
65
IDashboardSegment,
7-
IDynamicDashboardSegment,
86
MonitoringDashboardsOverrideProps,
97
UserProvidedNames,
108
} from "../../dashboard";
@@ -30,9 +28,7 @@ export interface BaseMonitoringProps
3028
/**
3129
* An independent unit of monitoring. This is the base for all monitoring classes with alarm support.
3230
*/
33-
export abstract class Monitoring
34-
implements IDashboardSegment, IDynamicDashboardSegment
35-
{
31+
export abstract class Monitoring implements IDashboardSegment {
3632
protected readonly scope: MonitoringScope;
3733
protected readonly alarms: AlarmWithAnnotation[];
3834
protected readonly localAlarmNamePrefixOverride?: string;
@@ -105,17 +101,4 @@ export abstract class Monitoring
105101
* Returns widgets to be placed on the main dashboard.
106102
*/
107103
abstract widgets(): IWidget[];
108-
109-
widgetsForDashboard(name: string): IWidget[] {
110-
switch (name) {
111-
case DefaultDashboards.SUMMARY:
112-
return this.summaryWidgets();
113-
case DefaultDashboards.DETAIL:
114-
return this.widgets();
115-
case DefaultDashboards.ALARMS:
116-
return this.alarmWidgets();
117-
default:
118-
return [];
119-
}
120-
}
121104
}

lib/dashboard/DefaultDashboardFactory.ts

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {
55
PeriodOverride,
66
} from "aws-cdk-lib/aws-cloudwatch";
77
import { Construct } from "constructs";
8+
89
import { BitmapDashboard } from "./BitmapDashboard";
910
import { DashboardRenderingPreference } from "./DashboardRenderingPreference";
1011
import { DashboardWithBitmapCopy } from "./DashboardWithBitmapCopy";
11-
import { IDynamicDashboardSegment } from "./DynamicDashboardSegment";
1212
import { IDashboardFactory, IDashboardFactoryProps } from "./IDashboardFactory";
13-
import { IDynamicDashboardFactory } from "./IDynamicDashboardFactory";
1413

1514
export interface MonitoringDashboardsProps {
1615
/**
@@ -68,25 +67,15 @@ export interface MonitoringDashboardsProps {
6867
readonly renderingPreference?: DashboardRenderingPreference;
6968
}
7069

71-
export enum DefaultDashboards {
72-
SUMMARY = "Summary",
73-
DETAIL = "Detail",
74-
ALARMS = "Alarms",
75-
}
76-
7770
export class DefaultDashboardFactory
7871
extends Construct
79-
implements IDashboardFactory, IDynamicDashboardFactory
72+
implements IDashboardFactory
8073
{
81-
// Legacy Dashboard Fields
8274
readonly dashboard?: Dashboard;
8375
readonly summaryDashboard?: Dashboard;
8476
readonly alarmDashboard?: Dashboard;
8577
readonly anyDashboardCreated: boolean;
8678

87-
// Dynamic Dashboard Fields
88-
readonly dashboards: Record<string, Dashboard> = {};
89-
9079
constructor(scope: Construct, id: string, props: MonitoringDashboardsProps) {
9180
super(scope, id);
9281

@@ -119,7 +108,6 @@ export class DefaultDashboardFactory
119108
periodOverride:
120109
props.detailDashboardPeriodOverride ?? PeriodOverride.INHERIT,
121110
});
122-
this.dashboards[DefaultDashboards.DETAIL] = this.dashboard;
123111
}
124112
if (createSummaryDashboard) {
125113
anyDashboardCreated = true;
@@ -133,7 +121,6 @@ export class DefaultDashboardFactory
133121
props.summaryDashboardPeriodOverride ?? PeriodOverride.INHERIT,
134122
}
135123
);
136-
this.dashboards[DefaultDashboards.SUMMARY] = this.summaryDashboard;
137124
}
138125
if (createAlarmDashboard) {
139126
anyDashboardCreated = true;
@@ -147,12 +134,26 @@ export class DefaultDashboardFactory
147134
props.detailDashboardPeriodOverride ?? PeriodOverride.INHERIT,
148135
}
149136
);
150-
this.dashboards[DefaultDashboards.ALARMS] = this.alarmDashboard;
151137
}
152138

153139
this.anyDashboardCreated = anyDashboardCreated;
154140
}
155141

142+
protected createDashboard(
143+
renderingPreference: DashboardRenderingPreference,
144+
id: string,
145+
props: DashboardProps
146+
) {
147+
switch (renderingPreference) {
148+
case DashboardRenderingPreference.INTERACTIVE_ONLY:
149+
return new Dashboard(this, id, props);
150+
case DashboardRenderingPreference.BITMAP_ONLY:
151+
return new BitmapDashboard(this, id, props);
152+
case DashboardRenderingPreference.INTERACTIVE_AND_BITMAP:
153+
return new DashboardWithBitmapCopy(this, id, props);
154+
}
155+
}
156+
156157
addSegment(props: IDashboardFactoryProps) {
157158
if ((props.overrideProps?.addToDetailDashboard ?? true) && this.dashboard) {
158159
this.dashboard.addWidgets(...props.segment.widgets());
@@ -171,33 +172,6 @@ export class DefaultDashboardFactory
171172
}
172173
}
173174

174-
addDynamicSegment(segment: IDynamicDashboardSegment): void {
175-
this.dashboard?.addWidgets(
176-
...segment.widgetsForDashboard(DefaultDashboards.DETAIL)
177-
);
178-
this.summaryDashboard?.addWidgets(
179-
...segment.widgetsForDashboard(DefaultDashboards.SUMMARY)
180-
);
181-
this.alarmDashboard?.addWidgets(
182-
...segment.widgetsForDashboard(DefaultDashboards.ALARMS)
183-
);
184-
}
185-
186-
protected createDashboard(
187-
renderingPreference: DashboardRenderingPreference,
188-
id: string,
189-
props: DashboardProps
190-
) {
191-
switch (renderingPreference) {
192-
case DashboardRenderingPreference.INTERACTIVE_ONLY:
193-
return new Dashboard(this, id, props);
194-
case DashboardRenderingPreference.BITMAP_ONLY:
195-
return new BitmapDashboard(this, id, props);
196-
case DashboardRenderingPreference.INTERACTIVE_AND_BITMAP:
197-
return new DashboardWithBitmapCopy(this, id, props);
198-
}
199-
}
200-
201175
createdDashboard(): Dashboard | undefined {
202176
return this.dashboard;
203177
}
@@ -209,17 +183,4 @@ export class DefaultDashboardFactory
209183
createdAlarmDashboard(): Dashboard | undefined {
210184
return this.alarmDashboard;
211185
}
212-
213-
getDashboard(name: string): Dashboard | undefined {
214-
switch (name) {
215-
case DefaultDashboards.SUMMARY:
216-
return this.summaryDashboard;
217-
case DefaultDashboards.DETAIL:
218-
return this.dashboard;
219-
case DefaultDashboards.ALARMS:
220-
return this.alarmDashboard;
221-
default:
222-
throw new Error("Unexpected dashboard name!");
223-
}
224-
}
225186
}

lib/dashboard/DynamicDashboardFactory.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)