From bb2c09cd81dc899fafcd6e5c15b53274618bf5a2 Mon Sep 17 00:00:00 2001 From: Michal Stolarczyk Date: Wed, 23 Nov 2022 14:46:01 -0500 Subject: [PATCH] add metric_description property, fix uptime determination --- cloudwatcher/metric_handlers.py | 1 + cloudwatcher/metricwatcher.py | 11 ++++++----- cloudwatcher/preset.py | 1 + cloudwatcher/presets/nephele_cpu_usage_user.json | 5 +++-- .../presets/nephele_disk_used_percent.json | 7 ++++--- .../nephele_disk_used_percent_nephele_data.json | 7 ++++--- cloudwatcher/presets/nephele_mem.json | 5 +++-- cloudwatcher/presets/nephele_mem_cached.json | 5 +++-- docs/API_usage.md | 15 +++++---------- 9 files changed, 30 insertions(+), 27 deletions(-) diff --git a/cloudwatcher/metric_handlers.py b/cloudwatcher/metric_handlers.py index 07e3a1b..7b1761b 100644 --- a/cloudwatcher/metric_handlers.py +++ b/cloudwatcher/metric_handlers.py @@ -161,6 +161,7 @@ def __call__(self, target: str, metric_unit: str) -> None: if self.timed_metric.label.startswith("mem") and metric_unit == "Bytes": metric_unit = "GB" values = [convert_mem(v, force_suffix=metric_unit)[0] for v in values] + plt.figure() plt.plot( self.timed_metric.timestamps, values, diff --git a/cloudwatcher/metricwatcher.py b/cloudwatcher/metricwatcher.py index e236024..184e32d 100644 --- a/cloudwatcher/metricwatcher.py +++ b/cloudwatcher/metricwatcher.py @@ -35,6 +35,7 @@ def __init__( metric_name: str, metric_id: str, metric_unit: Optional[str] = None, + metric_description: Optional[str] = None, aws_access_key_id: Optional[str] = None, aws_secret_access_key: Optional[str] = None, aws_session_token: Optional[str] = None, @@ -162,12 +163,12 @@ def get_ec2_uptime( """ if not self.is_ec2_running(ec2_instance_id): _LOGGER.info( - f"Instance '{self.dimension_value}' is not running anymore. " + f"Instance '{ec2_instance_id}' is not running anymore. " f"Uptime will be estimated based on reported metrics in " f"the last {days} days" ) instances = self.ec2_resource.instances.filter( - Filters=[{"Name": "instance-id", "Values": [self.dimension_value]}] + Filters=[{"Name": "instance-id", "Values": [ec2_instance_id]}] ) # get the latest reported metric metrics_response = self.query_ec2_metrics( @@ -187,14 +188,14 @@ def get_ec2_uptime( earliest_metric_report_time - latest_metric_report_time ).total_seconds() except IndexError: - _LOGGER.warning(f"No metric data found for EC2: {self.dimension_value}") + _LOGGER.warning(f"No metric data found for EC2: {ec2_instance_id}") return instances = self.ec2_resource.instances.filter( - Filters=[{"Name": "instance-id", "Values": [self.dimension_value]}] + Filters=[{"Name": "instance-id", "Values": [ec2_instance_id]}] ) for instance in instances: _LOGGER.info( - f"Instance '{self.dimension_value}' is still running. " + f"Instance '{ec2_instance_id}' is still running. " f"Launch time: {instance.launch_time}" ) return (datetime.now(pytz.utc) - instance.launch_time).total_seconds() diff --git a/cloudwatcher/preset.py b/cloudwatcher/preset.py index 98768ef..d9648ad 100644 --- a/cloudwatcher/preset.py +++ b/cloudwatcher/preset.py @@ -140,6 +140,7 @@ class MetricWatcherSetup: aws_secret_access_key: str = None aws_session_token: str = None aws_region_name: str = None + metric_description: str = None def __post_init__(self): self.aws_access_key_id = self.aws_access_key_id or os.environ.get( diff --git a/cloudwatcher/presets/nephele_cpu_usage_user.json b/cloudwatcher/presets/nephele_cpu_usage_user.json index 3d96967..4d0755e 100644 --- a/cloudwatcher/presets/nephele_cpu_usage_user.json +++ b/cloudwatcher/presets/nephele_cpu_usage_user.json @@ -3,7 +3,7 @@ "dimensions_list": [ { "Name": "InstanceId", - "Value": "i-0c4d9523c99fbc1da" + "Value": "" }, { "Name": "cpu", @@ -12,5 +12,6 @@ ], "metric_name": "cpu_usage_user", "metric_id": "nephele", - "metric_unit": "Percent" + "metric_unit": "Percent", + "metric_description": "Precentage of total CPU used over time" } \ No newline at end of file diff --git a/cloudwatcher/presets/nephele_disk_used_percent.json b/cloudwatcher/presets/nephele_disk_used_percent.json index bde9744..1368c37 100644 --- a/cloudwatcher/presets/nephele_disk_used_percent.json +++ b/cloudwatcher/presets/nephele_disk_used_percent.json @@ -3,7 +3,7 @@ "dimensions_list": [ { "Name": "InstanceId", - "Value": "i-04127265744c54ce6" + "Value": "" }, { "Name": "path", @@ -19,6 +19,7 @@ } ], "metric_name": "disk_used_percent", - "metric_id": "nephele", - "metric_unit": "Percent" + "metric_id": "root", + "metric_unit": "Percent", + "metric_description": "Percentage of disk used over time. Path: / (root)" } \ No newline at end of file diff --git a/cloudwatcher/presets/nephele_disk_used_percent_nephele_data.json b/cloudwatcher/presets/nephele_disk_used_percent_nephele_data.json index 4aba16a..761c172 100644 --- a/cloudwatcher/presets/nephele_disk_used_percent_nephele_data.json +++ b/cloudwatcher/presets/nephele_disk_used_percent_nephele_data.json @@ -3,7 +3,7 @@ "dimensions_list": [ { "Name": "InstanceId", - "Value": "i-04127265744c54ce6" + "Value": "" }, { "Name": "path", @@ -19,6 +19,7 @@ } ], "metric_name": "disk_used_percent", - "metric_id": "nephele", - "metric_unit": "Percent" + "metric_id": "nephele_data", + "metric_unit": "Percent", + "metric_description": "Percentage of disk used over time. Path: /nephele_data" } \ No newline at end of file diff --git a/cloudwatcher/presets/nephele_mem.json b/cloudwatcher/presets/nephele_mem.json index 78f5b5e..a08618b 100644 --- a/cloudwatcher/presets/nephele_mem.json +++ b/cloudwatcher/presets/nephele_mem.json @@ -3,10 +3,11 @@ "dimensions_list": [ { "Name": "InstanceId", - "Value": "i-0c4d9523c99fbc1da" + "Value": "" } ], "metric_name": "mem_used", "metric_id": "nephele", - "metric_unit": "Bytes" + "metric_unit": "Bytes", + "metric_description": "Memory used over time" } \ No newline at end of file diff --git a/cloudwatcher/presets/nephele_mem_cached.json b/cloudwatcher/presets/nephele_mem_cached.json index 0d96580..2db46a1 100644 --- a/cloudwatcher/presets/nephele_mem_cached.json +++ b/cloudwatcher/presets/nephele_mem_cached.json @@ -3,10 +3,11 @@ "dimensions_list": [ { "Name": "InstanceId", - "Value": "i-0c4d9523c99fbc1da" + "Value": "" } ], "metric_name": "mem_cached", "metric_id": "nephele", - "metric_unit": "Bytes" + "metric_unit": "Bytes", + "metric_description": "Memory cache used over time" } \ No newline at end of file diff --git a/docs/API_usage.md b/docs/API_usage.md index a64802d..7e0e14e 100644 --- a/docs/API_usage.md +++ b/docs/API_usage.md @@ -20,14 +20,12 @@ Alternatively, you can pass the values as arguments to the `MetricWatcher` const ```python from cloudwatcher.metricwatcher import MetricWatcher from cloudwatcher.preset import Dimension -from cloudwatcher._version import __version__ from dotenv import load_dotenv +import os load_dotenv() -print(f"CloudWatcher version: {__version__}") - -instance_id = "i-00beb88d31184ae62" +instance_id = os.environ.get("INSTANCE_ID") mw = MetricWatcher( namespace="NepheleNamespaceEC2", metric_name="mem_used", @@ -37,9 +35,6 @@ mw = MetricWatcher( ) ``` - CloudWatcher version: 0.1.0 - - ### `MetricWatcher` presets As you can see there are multiple arguments that can be passed to the `MetricWatcher` constructor. In order to improve the UX when using `MetricWatcher`. The `cloudwatcher` package provides a few presets that can be used to query the data reported by `CloudWatchAgent` within certain systems. Additionally, custom presets can be defined by the user and used in the same way. @@ -151,12 +146,12 @@ response["ResponseMetadata"] - {'RequestId': 'd64c4f8e-8544-48ee-894e-e5acf2a66464', + {'RequestId': 'bb3880e0-17e6-45d2-a007-a7b797c6d52f', 'HTTPStatusCode': 200, - 'HTTPHeaders': {'x-amzn-requestid': 'd64c4f8e-8544-48ee-894e-e5acf2a66464', + 'HTTPHeaders': {'x-amzn-requestid': 'bb3880e0-17e6-45d2-a007-a7b797c6d52f', 'content-type': 'text/xml', 'content-length': '33663', - 'date': 'Tue, 08 Nov 2022 16:09:50 GMT'}, + 'date': 'Tue, 08 Nov 2022 16:46:07 GMT'}, 'RetryAttempts': 0}