StevieB
StevieB

Reputation: 1000

Deploying AWS Cloudwatch dashboards with the CDK: how do I hide metrics

I have a custom metric that I push updates to in my code. In the CDK, I have created a derived metric from this custom metric. I would like the derived metric to show up in the dashboard but the original metric to be hidden. How can I achieve this?

Here is my cut-down (TypeScript) CDK code which deploys successfully:

  const createDashboard = (scope: cdk.Construct, namespace: string, statistic = Statistic.AVERAGE) => {
  const customDynamoLatencyMetric: IMetric = new Metric({
    period: Duration.minutes(1),
    metricName: 'MY_DYNAMO_LATENCY_METRIC',
    namespace,
    statistic,
  });

  const derivedAverageDynamoLatencyMetric = new MathExpression({
    expression: 'm1/1000', label: 'To Dynamo Latency', usingMetrics: { m1: customDynamoLatencyMetric }, period: Duration.minutes(1),
  });

  const dashboard = new Dashboard(
    scope,
    'myDashboard', {
      dashboardName: 'myDashboard',
    },
  );

  const widget = new GraphWidget({
    title: 'Average Latency',
    left: [customDynamoLatencyMetric, derivedAverageDynamoLatencyMetric],
    view: GraphWidgetView.TIME_SERIES,
    region: AWS_DEFAULT_REGION,
    width: 12,
  });

  dashboard.addWidgets(widget);
};

If I manually mark this metric as invisible in the AWS Cloudwatch Dasgboard Console then when I view/edit source in the Cloudwatch Console I see the following:

"metrics": [
                    [ "stephenburns-gcs-pipeline", "DYNAMO_LATENCY", { "id": "m1", "visible": false } ],
                    [ { "label": "To Dynamo Latency", "expression": "m1/1000", "period": 60, "id": "e1", "region": "ap-southeast-2" } ]
            ]

My question is how do I get that "visible": false property via the CDK?

I tried using the Metric's dimensions property e.g.

dimensions: { visible: false }

but it fails at deployment time with the error: "Invalid metric field type, only "String" type is allowed"

Does anyone know how to mark a metric as initially invisible?

Upvotes: 4

Views: 3358

Answers (1)

BP18
BP18

Reputation: 31

If you only add the original Metric to the usingMetrics property of the MathExpression and don't add it directly to the GraphWidget, the CDK appears to automatically set visible to false. The CDK documentation does not currently (as of version 1.123.0) indicate a way to set the visibility of a Metric directly.

In the code example you provided, this would simply require changing the line:

left: [customDynamoLatencyMetric, derivedAverageDynamoLatencyMetric],

to:

left: [customDynamoLatencyMetric],

Upvotes: 3

Related Questions