-
Notifications
You must be signed in to change notification settings - Fork 90
[Observability] Emit metric ClustermgtdHeartbeat to signal clustermgtd heartbeat.
#685
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
Merged
gmarciani
merged 1 commit into
aws:develop
from
gmarciani:wip/mgiacomo/3150/clustermgtd-metrics-0126-1
Jan 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). | ||
| # You may not use this file except in compliance with the License. | ||
| # A copy of the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
| from datetime import datetime, timezone | ||
| from typing import Dict, List, Optional | ||
|
|
||
| import boto3 | ||
| from botocore.config import Config | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| METRICS_NAMESPACE = "ParallelCluster" | ||
| METRICS_DIMENSION_CLUSTER_NAME = "ClusterName" | ||
| METRICS_DIMENSION_INSTANCE_ID = "InstanceId" | ||
|
|
||
|
|
||
| class CloudWatchMetricsPublisher: | ||
| """Class for publishing metrics to CloudWatch.""" | ||
|
|
||
| def __init__(self, region: str, cluster_name: str, instance_id: str, boto3_config: Config): | ||
| """ | ||
| Initialize CloudWatchMetricsPublisher. | ||
|
|
||
| Args: | ||
| region: AWS region | ||
| cluster_name: Name of the ParallelCluster cluster | ||
| instance_id: EC2 instance ID to include in metric dimensions | ||
| boto3_config: Boto3 configuration for retries and proxies | ||
| """ | ||
| self._region = region | ||
| self._cluster_name = cluster_name | ||
| self._instance_id = instance_id | ||
| self._boto3_config = boto3_config | ||
| self._cloudwatch_client = None | ||
|
|
||
| @property | ||
| def cloudwatch_client(self): | ||
| """Lazy initialization of CloudWatch client.""" | ||
| if self._cloudwatch_client is None: | ||
| self._cloudwatch_client = boto3.client("cloudwatch", region_name=self._region, config=self._boto3_config) | ||
| return self._cloudwatch_client | ||
|
|
||
| def put_metric( | ||
| self, | ||
| metric_name: str, | ||
| value: float, | ||
| unit: str = "Count", | ||
| additional_dimensions: Optional[List[Dict[str, str]]] = None, | ||
| ): | ||
| """ | ||
| Publish a metric to CloudWatch. | ||
|
|
||
| Automatically sets timestamp and includes ClusterName as a dimension. | ||
|
|
||
| Args: | ||
| metric_name: Name of the metric to publish | ||
| value: Metric value | ||
| unit: CloudWatch unit (default: "Count") | ||
| additional_dimensions: Optional list of additional dimensions [{"Name": "...", "Value": "..."}] | ||
| """ | ||
| dimensions = [ | ||
| {"Name": METRICS_DIMENSION_CLUSTER_NAME, "Value": self._cluster_name}, | ||
| {"Name": METRICS_DIMENSION_INSTANCE_ID, "Value": self._instance_id}, | ||
| ] | ||
| if additional_dimensions: | ||
| dimensions.extend(additional_dimensions) | ||
|
|
||
| try: | ||
| self.cloudwatch_client.put_metric_data( | ||
| Namespace=METRICS_NAMESPACE, | ||
| MetricData=[ | ||
| { | ||
| "MetricName": metric_name, | ||
| "Dimensions": dimensions, | ||
| "Timestamp": datetime.now(tz=timezone.utc), | ||
| "Value": value, | ||
| "Unit": unit, | ||
| } | ||
| ], | ||
| ) | ||
| logger.debug("Published metric %s with value %s", metric_name, value) | ||
| except Exception as e: | ||
| logger.error("Failed to publish metric %s: %s", metric_name, e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| # Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with | ||
| # the License. A copy of the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
| from datetime import datetime, timezone | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from assertpy import assert_that | ||
| from botocore.config import Config | ||
| from botocore.exceptions import ClientError | ||
| from slurm_plugin.cloudwatch_utils import METRICS_NAMESPACE, CloudWatchMetricsPublisher | ||
|
|
||
|
|
||
| class TestCloudWatchMetricsPublisher: | ||
| """Tests for CloudWatchMetricsPublisher class.""" | ||
|
|
||
| @pytest.fixture | ||
| def boto3_config(self): | ||
| return Config(retries={"max_attempts": 1, "mode": "standard"}) | ||
|
|
||
| @pytest.fixture | ||
| def metrics_publisher(self, boto3_config): | ||
| return CloudWatchMetricsPublisher( | ||
| region="us-east-1", | ||
| cluster_name="test-cluster", | ||
| instance_id="i-1234567890abcdef0", | ||
| boto3_config=boto3_config, | ||
| ) | ||
|
|
||
| def test_init(self, metrics_publisher, boto3_config): | ||
| """Test CloudWatchMetricsPublisher initialization.""" | ||
| assert_that(metrics_publisher._region).is_equal_to("us-east-1") | ||
| assert_that(metrics_publisher._cluster_name).is_equal_to("test-cluster") | ||
| assert_that(metrics_publisher._boto3_config).is_equal_to(boto3_config) | ||
| assert_that(metrics_publisher._instance_id).is_equal_to("i-1234567890abcdef0") | ||
| assert_that(metrics_publisher._cloudwatch_client).is_none() | ||
|
|
||
| def test_cloudwatch_client_lazy_initialization(self, metrics_publisher, mocker): | ||
| """Test that CloudWatch client is lazily initialized.""" | ||
| mock_client = MagicMock() | ||
| mock_boto3 = mocker.patch("slurm_plugin.cloudwatch_utils.boto3") | ||
| mock_boto3.client.return_value = mock_client | ||
|
|
||
| # First access should create the client | ||
| client = metrics_publisher.cloudwatch_client | ||
| assert_that(client).is_equal_to(mock_client) | ||
| mock_boto3.client.assert_called_once_with( | ||
| "cloudwatch", | ||
| region_name="us-east-1", | ||
| config=metrics_publisher._boto3_config, | ||
| ) | ||
|
|
||
| # Second access should return the cached client | ||
| mock_boto3.client.reset_mock() | ||
| client2 = metrics_publisher.cloudwatch_client | ||
| assert_that(client2).is_equal_to(mock_client) | ||
| mock_boto3.client.assert_not_called() | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "metric_name, value, unit, additional_dimensions, expected_dimensions", | ||
| [ | ||
| pytest.param( | ||
| "TestMetric", | ||
| 42, | ||
| "Count", | ||
| None, | ||
| [ | ||
| {"Name": "ClusterName", "Value": "test-cluster"}, | ||
| {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, | ||
| ], | ||
| id="basic", | ||
| ), | ||
| pytest.param( | ||
| "HeadNodeDaemonHeartbeat", | ||
| 1, | ||
| "Count", | ||
| [{"Name": "DaemonName", "Value": "clustermgtd"}], | ||
| [ | ||
| {"Name": "ClusterName", "Value": "test-cluster"}, | ||
| {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, | ||
| {"Name": "DaemonName", "Value": "clustermgtd"}, | ||
| ], | ||
| id="with_additional_dimension", | ||
| ), | ||
| pytest.param( | ||
| "LatencyMetric", | ||
| 150.5, | ||
| "Milliseconds", | ||
| None, | ||
| [ | ||
| {"Name": "ClusterName", "Value": "test-cluster"}, | ||
| {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, | ||
| ], | ||
| id="with_custom_unit", | ||
| ), | ||
| pytest.param( | ||
| "CustomMetric", | ||
| 100, | ||
| "Count", | ||
| [ | ||
| {"Name": "DaemonName", "Value": "clustermgtd"}, | ||
| {"Name": "NodeType", "Value": "HeadNode"}, | ||
| ], | ||
| [ | ||
| {"Name": "ClusterName", "Value": "test-cluster"}, | ||
| {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, | ||
| {"Name": "DaemonName", "Value": "clustermgtd"}, | ||
| {"Name": "NodeType", "Value": "HeadNode"}, | ||
| ], | ||
| id="with_multiple_additional_dimensions", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_put_metric( | ||
| self, | ||
| metrics_publisher, | ||
| mocker, | ||
| metric_name: str, | ||
| value: float, | ||
| unit: str, | ||
| additional_dimensions: list, | ||
| expected_dimensions: list, | ||
| ): | ||
| """Test put_metric with various parameter combinations.""" | ||
| mock_client = MagicMock() | ||
| metrics_publisher._cloudwatch_client = mock_client | ||
| mock_datetime = mocker.patch("slurm_plugin.cloudwatch_utils.datetime") | ||
| fixed_time = datetime(2024, 1, 15, 12, 0, 0, tzinfo=timezone.utc) | ||
| mock_datetime.now.return_value = fixed_time | ||
|
|
||
| metrics_publisher.put_metric( | ||
| metric_name=metric_name, | ||
| value=value, | ||
| unit=unit, | ||
| additional_dimensions=additional_dimensions, | ||
| ) | ||
|
|
||
| mock_client.put_metric_data.assert_called_once_with( | ||
| Namespace=METRICS_NAMESPACE, | ||
| MetricData=[ | ||
| { | ||
| "MetricName": metric_name, | ||
| "Dimensions": expected_dimensions, | ||
| "Timestamp": fixed_time, | ||
| "Value": value, | ||
| "Unit": unit, | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| def test_put_metric_handles_exception(self, metrics_publisher, caplog): | ||
| """Test that put_metric handles exceptions gracefully.""" | ||
| mock_client = MagicMock() | ||
| mock_client.put_metric_data.side_effect = ClientError( | ||
| {"Error": {"Code": "WHATEVER_CODE", "Message": "WHATEVER_MESSAGE"}}, | ||
| "PutMetricData", | ||
| ) | ||
| metrics_publisher._cloudwatch_client = mock_client | ||
|
|
||
| with caplog.at_level(logging.WARNING): | ||
| # Should not raise exception | ||
| metrics_publisher.put_metric(metric_name="WHATEVER_METRIC_NAME", value=1) | ||
|
|
||
| assert_that(caplog.text).matches( | ||
| r"Failed to publish metric WHATEVER_METRIC_NAME:.*WHATEVER_CODE.*WHATEVER_MESSAGE" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about surrounding this with a try catch and adding a warning log line? This is not a critical cluster management logic. We should avoid it throwing an Exception out of the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is done inside the metrics publisher itself.