Skip to content

Commit

Permalink
feat(cloudwatch): Add support for showing full precision with SingleV…
Browse files Browse the repository at this point in the history
…alueWidgets

Closes aws#8940 and Closes aws#12066
  • Loading branch information
yattoni committed Dec 29, 2020
1 parent eb45ca8 commit 9924725
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
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

0 comments on commit 9924725

Please sign in to comment.