Hi Team,
Currently GraphWidget only takes IMetric object in its 'Left' property array. and IMetric can only take MathExpression or Metric object. There is no support for SEARCH Expression in it.
Reference : https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.GraphWidget.html
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.IMetric.html
While working on dashboard I realized we do need to have SEARCH option enabled for GraphWidget in order to create dashboard properly.
Sample Use Case: Lets say you have a Fargate service where under your Cluster there are X number of Task running.
Now to have a graph on lets say Heap Used, we need one SEARCH Expression on 'Cluster Name' so that it will show Heap Usage for all the available Tasks under that Cluster.
Currently with CDK , you can't really get each TaskIds (as it will be generated at runtime) and that in turns will restrict you to create a METRIC expression with ClusterName and List
So we really need to enable GraphWidget to take 'SEARCH Expression'.
Metric Expression is yet to be supported in CDK, https://github.com/aws/aws-cdk/pull/4942 https://github.com/aws/aws-cdk/issues/5261
do let me know if we already have some workaround
Hello there. I'm from the Amazon CodeGuru Profiler team and we also need this feature. I've gotten it to work for now using a number of hacks (discussed below), but it's all pretty awful.
Metric search is ...somewhat... supported, but it's quite broken. The following works:
Dashboard dashboard = new Dashboard(this, "test", DashboardProps.builder().dashboardName("hello").build());
List<IMetric> metrics = new ArrayList<>();
metrics.add(
MathExpression.Builder.create()
.expression("SEARCH(' Errors ', 'Average', 300)")
.usingMetrics(new HashMap<>()) // HACK to avoid NullPointerException
.build()
);
dashboard.addWidgets(new GraphWidget(
GraphWidgetProps.builder()
.title("hello")
.left(metrics)
.build()
));
This gets me a graph with one of the examples from https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/search-expression-examples.html.
But it seems like CDK really expects math expressions to always be attached to metrics, so I needed to specify the dummy usingMetrics() and another issue is that MathExpression has no support for specifying things such as account, unit, region, because they're always "assumed" to come from the attached metric, which does not exist in this case.
I've devised a further hack that allows CDK to output those fields:
Dashboard dashboard = new Dashboard(this, "test", DashboardProps.builder().dashboardName("hello").build());
List<IMetric> metrics = new ArrayList<>();
metrics.add(new IMetric() {
@Override
public MetricAlarmConfig toAlarmConfig() {
return null;
}
@Override
public MetricGraphConfig toGraphConfig() {
return null;
}
@Override
public MetricConfig toMetricConfig() {
return MetricConfig.builder()
.mathExpression(MathExpression.Builder.create()
.expression("SEARCH(' Errors ', 'Average', 300)")
.usingMetrics(new HashMap<>())
.build().toMetricConfig().getMathExpression())
.renderingProperties(new HashMap<String, Object>() {
{ put("region", "us-east-1"); }
})
.build();
}
});
dashboard.addWidgets(new GraphWidget(
GraphWidgetProps.builder()
.title("hello")
.left(metrics)
.build()
));
...which is awful. So please help us ;)
If the above hack was not awful enough, I've discovered that when the expression is the same, and only the region/account changes, then the CDK only emits the metric once.
So... one more hack I need in our code is to add a variable number of spaces at the end of the search expression:
"SEARCH(' Errors ', 'Average', 300)" for the first region
"SEARCH(' Errors ', 'Average', 300) " for the second region
"SEARCH(' Errors ', 'Average', 300) " for the third region
"SEARCH(' Errors ', 'Average', 300) " for the fourth region
Help, CDK folks! This rabbit hole is really deep :scream: :scream: :scream:
+1, is there any update?
+1, we would love to see this feature as well!
Most helpful comment
+1, is there any update?