Skip to content

Commit

Permalink
Merge branch 'feat/api-v0.2' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
demeringo committed Dec 7, 2022
2 parents 0bd836e + 8ab7479 commit 7d5c6d3
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 179 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.2.0-alpha.1]-2022-12-07

### Changed

- Use the instances workload (cpu load) to tune the results.
- Use Boavizta API v0.2.x.
- Take in consideration the instances workload (cpu load) to calculate the impacts.

## [0.1.1]- 2022-12-07

Expand Down
18 changes: 14 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ At the moment:
- Cloud scanner returns _empty_ impacts (i.e. zero values) for EC2 the instance _types_ that are not listed in Boavizta database.
- `--aws-region` flag only supports eu-based aws regions (eu-east-1,eu-central-1,eu-north-1,eu-south-1,eu-west-1,eu-west-2,eu-west-3).
- Returns _default_ impacts of AWS instances. It does not yet analyses instance usage (cpu workload) to calculate the impacts, but rather returns the _default_ impact data provided by Boavizta API for each instance type for a given use duration. (i.e. using instance CPU load through the `measured` command line flag has no effect).
- Filtering instances by tag is not yet supported.
This is work in progress, and development version may already implement theses functionalities. So have a look at the [changelog](https://github.com/Boavizta/cloud-scanner/blob/main/CHANGELOG.md) and [Issues · Boavizta/cloud-scanner](https://github.com/Boavizta/cloud-scanner/issues) on this repository.
4 changes: 2 additions & 2 deletions cloud-scanner-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
authors = ["boavizta.org", "Olivier de Meringo <demeringo@gmail.com>"]
edition = "2021"
name = "cloud-scanner-cli"
version = "0.1.1"
version = "0.2.0-alpha.1"
[dependencies]
boavizta_api_sdk = "0.1.2"
boavizta_api_sdk = "0.2.0-alpha.2"
aws-types = "0.49.0"
chrono = "^0.4"
isocountry = "^0.3"
Expand Down
45 changes: 24 additions & 21 deletions cloud-scanner-cli/src/aws_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl AwsInventory {
///
async fn get_average_cpu(self, instance_id: &str) -> Result<f64> {
let res = self
.get_average_cpu_usage_of_last_5_minutes(instance_id)
.get_average_cpu_usage_of_last_10_minutes(instance_id)
.await
.with_context(|| {
format!(
Expand All @@ -94,31 +94,31 @@ impl AwsInventory {
)
})?;
if let Some(points) = res.datapoints {
// dbg!(points.clone());
if !points.is_empty() {
if points.len() > 1 {
warn!("Some datapoints were skipped when getting instance CPU usage, whe expected a single result but received {}. Only the first was considered", points.len());
debug!("Averaging cpu load datapoint: {:#?}", points);
let mut sum: f64 = 0.0;
for x in &points {
sum = sum + x.average().unwrap();
}
let first_point = &points[0];
return Ok(first_point.average.unwrap());
let avg = sum / points.len() as f64;
return Ok(avg);
}
}
warn!(
"No CPU load data was returned for instance {}, it is likely stopped, using 0 as load",
"Unable to get CPU load of instance {}, it is likely stopped, using 0 as load",
instance_id
);
Ok(0 as f64)
}

/// Returns the instance CPU utilization usage on the last 24 hours
/// duration seconds seems to be the sampling period
async fn get_average_cpu_usage_of_last_5_minutes(
/// Returns the instance CPU utilization usage on the last 10 minutes
async fn get_average_cpu_usage_of_last_10_minutes(
self,
instance_id: &str,
) -> Result<GetMetricStatisticsOutput, aws_sdk_cloudwatch::Error> {
// We want statistics about the last 5 minutes using 60 sec sample
let measure_duration = Duration::minutes(5);
let sample_period_seconds = 60;
// We want statistics about the last 10 minutes using 5min sample
let measure_duration = Duration::minutes(10);
let sample_period_seconds = 300; // 5*60 (the default granularity of cloudwatch standard CPU metris)
let now: chrono::DateTime<Utc> = Utc::now();
let start_time: chrono::DateTime<Utc> = now - measure_duration;

Expand Down Expand Up @@ -228,16 +228,19 @@ mod tests {
// Verify tests from here
#[tokio::test]
#[ignore]
async fn test_get_instance_usage_metrics_of_running_instance() {
async fn get_cpu_usage_metrics_of_running_instance_should_return_right_number_of_data_points() {
let inventory: AwsInventory = AwsInventory::new("eu-west-1").await;

let res = inventory
.get_average_cpu_usage_of_last_5_minutes(&RUNNING_INSTANCE_ID)
.get_average_cpu_usage_of_last_10_minutes(&RUNNING_INSTANCE_ID)
.await
.unwrap();
let datapoints = res.datapoints.unwrap();
println!("{:#?}", datapoints);
assert_eq!(1, datapoints.len());
assert!(
0 < datapoints.len() && datapoints.len() < 3,
"Stange number of datapoint returned. I was expecting 1 or 2 but got {} .\n {:#?}",
datapoints.len(),
datapoints
)
}

#[tokio::test]
Expand All @@ -246,19 +249,19 @@ mod tests {
let inventory: AwsInventory = AwsInventory::new("eu-west-1").await;
let instance_id = "i-03e0b3b1246001382";
let res = inventory
.get_average_cpu_usage_of_last_5_minutes(instance_id)
.get_average_cpu_usage_of_last_10_minutes(instance_id)
.await
.unwrap();
let datapoints = res.datapoints.unwrap();
assert_eq!(0, datapoints.len());
assert_eq!(0, datapoints.len(), "Wrong number of datapoint returned");
}

#[tokio::test]
async fn test_get_instance_usage_metrics_of_non_existing_instance() {
let inventory: AwsInventory = AwsInventory::new("eu-west-1").await;
let instance_id = "IDONOTEXISTS";
let res = inventory
.get_average_cpu_usage_of_last_5_minutes(instance_id)
.get_average_cpu_usage_of_last_10_minutes(instance_id)
.await
.unwrap();
let datapoints = res.datapoints.unwrap();
Expand Down
Loading

0 comments on commit 7d5c6d3

Please sign in to comment.