Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): full precision for SingleValueWidgets #12274

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ dashboard.addWidgets(new SingleValueWidget({
}));
```

Show as many digits as can fit, before rounding.

```ts
dashboard.addWidgets(new SingleValueWidget({
// ..
// ..

fullPrecision: true,
}));
```

### Text widget

A text widget shows an arbitrary piece of MarkDown. Use this to add explanations
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ export interface SingleValueWidgetProps extends MetricWidgetProps {
* @default false
*/
readonly setPeriodToTimeRange?: boolean;

/**
* Whether to show as many digits as can fit, before rounding.
*
* @default false
*/
readonly fullPrecision?: boolean;
}

/**
Expand All @@ -322,6 +329,7 @@ export class SingleValueWidget extends ConcreteWidget {
region: this.props.region || cdk.Aws.REGION,
metrics: allMetricsGraphJson(this.props.metrics, []),
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
singleValueFullPrecision: this.props.fullPrecision,
},
}];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,29 @@
{
"Ref": "AWS::Region"
},
"\",\"query\":\"SOURCE 'my-log-group' | fields @message\\n | filter @message like /Error/\"}}]}"
"\",\"query\":\"SOURCE 'my-log-group' | fields @message\\n | filter @message like /Error/\"}},{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":47,\"properties\":{\"view\":\"singleValue\",\"title\":\"Sent message size\",\"region\":\"",
{
"Ref": "AWS::Region"
},
"\",\"metrics\":[[\"AWS/SQS\",\"SentMessageSize\",\"QueueName\",\"",
{
"Fn::GetAtt": [
"queue",
"QueueName"
]
},
"\"]],\"singleValueFullPrecision\":false}},{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":50,\"properties\":{\"view\":\"singleValue\",\"title\":\"Sent message size with full precision\",\"region\":\"",
{
"Ref": "AWS::Region"
},
"\",\"metrics\":[[\"AWS/SQS\",\"SentMessageSize\",\"QueueName\",\"",
{
"Fn::GetAtt": [
"queue",
"QueueName"
]
},
"\"]],\"singleValueFullPrecision\":true}}]}"
]
]
},
Expand Down
24 changes: 20 additions & 4 deletions packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-and-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ const stack = new cdk.Stack(app, 'aws-cdk-cloudwatch-alarms');

const queue = new cdk.CfnResource(stack, 'queue', { type: 'AWS::SQS::Queue' });

const metric = new cloudwatch.Metric({
const numberOfMessagesVisibleMetric = new cloudwatch.Metric({
namespace: 'AWS/SQS',
metricName: 'ApproximateNumberOfMessagesVisible',
dimensions: { QueueName: queue.getAtt('QueueName') },
});

const alarm = metric.createAlarm(stack, 'Alarm', {
const sentMessageSizeMetric = new cloudwatch.Metric({
namespace: 'AWS/SQS',
metricName: 'SentMessageSize',
dimensions: { QueueName: queue.getAtt('QueueName') },
});

const alarm = numberOfMessagesVisibleMetric.createAlarm(stack, 'Alarm', {
threshold: 100,
evaluationPeriods: 3,
datapointsToAlarm: 2,
Expand All @@ -41,12 +47,12 @@ dashboard.addWidgets(new cloudwatch.AlarmWidget({
}));
dashboard.addWidgets(new cloudwatch.GraphWidget({
title: 'More messages in queue with alarm annotation',
left: [metric],
left: [numberOfMessagesVisibleMetric],
leftAnnotations: [alarm.toAnnotation()],
}));
dashboard.addWidgets(new cloudwatch.SingleValueWidget({
title: 'Current messages in queue',
metrics: [metric],
metrics: [numberOfMessagesVisibleMetric],
}));
dashboard.addWidgets(new cloudwatch.LogQueryWidget({
title: 'Errors in my log group',
Expand Down Expand Up @@ -82,5 +88,15 @@ dashboard.addWidgets(new cloudwatch.LogQueryWidget({
queryString: `fields @message
| filter @message like /Error/`,
}));
dashboard.addWidgets(new cloudwatch.SingleValueWidget({
title: 'Sent message size',
metrics: [sentMessageSizeMetric],
fullPrecision: false,
}));
dashboard.addWidgets(new cloudwatch.SingleValueWidget({
title: 'Sent message size with full precision',
metrics: [sentMessageSizeMetric],
fullPrecision: true,
}));

app.synth();
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,35 @@ export = {
test.done();
},

'add singleValueFullPrecision to singleValueWidget'(test: Test) {
// GIVEN
const stack = new Stack();
const metric = new Metric({ namespace: 'CDK', metricName: 'Test' });

// WHEN
const widget = new SingleValueWidget({
metrics: [metric],
fullPrecision: true,
});

// THEN
test.deepEqual(stack.resolve(widget.toJson()), [{
type: 'metric',
width: 6,
height: 3,
properties: {
view: 'singleValue',
region: { Ref: 'AWS::Region' },
metrics: [
['CDK', 'Test'],
],
singleValueFullPrecision: true,
},
}]);

test.done();
},

'allows overriding custom values of dashboard widgets'(test: Test) {
class HiddenMetric extends Metric {
public toMetricConfig() {
Expand Down