Skip to content

(aws-cloudwatch): Allow setting id and visible properties for cloudwatch Metric #19876

Open
@adam-kiss-sg

Description

@adam-kiss-sg

Describe the feature

Currently some cloudwatch Metric properties can't be set using the cdk. Two of these are "id" and "visible".

Use Case

I would like to use SUM(METRICS("metrix_prefix")), while hiding the basic metrics, eg:

{
    "view": "timeSeries",
    "title": "Http Requests/Errors (SUM)",
    "region": "us-east-1",
    "metrics": [
        [ { "label": "SumErrors", "expression": "SUM(METRICS(\"http_error\"))", "period": 10, "region": "us-east-1" } ],
        [ { "label": "SumRequests", "expression": "SUM(METRICS(\"http_requests\"))", "period": 10, "region": "us-east-1" } ],
        [ ".", "HttpErrors", ".", ".", { "label": "HttpErrors - us-east-1", "id": "http_errors_us_east_1", "visible": false, "period": 10 } ],
        [ ".", "HttpRequest", ".", ".", { "label": "HttpRequests - eu-west-1", "id": "http_requests_eu_west_1", "visible": false, "period": 10 } ,
        [ ".", "HttpErrors", ".", ".", { "label": "HttpErrors - us-east-1", "id": "http_errors_us_east_1", "visible": false, "period": 10 } ],
        [ ".", "HttpRequest", ".", ".", { "label": "HttpRequests - eu-west-1", "id": "http_requests_eu_west_1", "visible": false, "period": 10 } ]
    ],
    "period": 300
}

(I apologize if there is some syntax error above, this is a simplification of our actual use case.)

Proposed Solution

Allow setting "id" and "visible" properties from the cdk (there might be other properties as well, that i haven't run into).

Other Information

There is a similar issue that got closed recently: #13942 (comment), but i feel the use-case is different enough to warrant a separate issue.

I currently use the following workaround in the python sdk:

...
sumWidget.add_left_metric(MyMetric(label=f"{name} - {region}", metric_name=name, **_kwargs).addProperties({"id": f"{id}_{regionUnderscore}", "visible": False}))
...

class MyMetric(aws_cloudwatch.Metric):
    def __init__(self, *args, **kwargs):
        super(MyMetric, self).__init__(*args, **kwargs)
    
    def addProperties(self, props):
        self.props = props
        return self
    
    def to_metric_config(self) -> aws_cloudwatch.MetricConfig:
        result = super(MyMetric, self).to_metric_config()
        if not hasattr(self, "props"):
            return result
        
        if "rendering_properties" in result._values:
            result._values["rendering_properties"].update(self.props)
        else:
            result._values["rendering_properties"] = self.props
        return result

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

aws-cdk.aws-cloudwatch-1.132.0, but also checked the latest documentations (1.152.0 and 2.20.0):

Environment details (OS name and version, etc.)

doesn't matter

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-cloudwatchRelated to Amazon CloudWatcheffort/smallSmall work item – less than a day of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions