diff --git a/.gitignore b/.gitignore index 0a4280c7..7a904746 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,8 @@ core/wave-cli/vector* # core/wave-metrics core/wave-metrics/telegraf* core/wave-metrics/vector* +telegraf*/ +vector*/ # VSCode .vscode/ \ No newline at end of file diff --git a/.moon/tasks/rust.yml b/.moon/tasks/rust.yml index af339c63..02a1d89c 100644 --- a/.moon/tasks/rust.yml +++ b/.moon/tasks/rust.yml @@ -1,5 +1,8 @@ tasks: - clean: + clean-cargo: + command: "cargo clean" + platform: "system" + clean-db: command: "find . -type f -name 'wave.db' -exec rm {} + > /dev/null" platform: "system" inputs: @@ -19,7 +22,7 @@ tasks: test: deps: - "lint" - - "clean" + - "clean-db" run: deps: - "test" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e9ffe911..75ce5047 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,7 +19,25 @@ $ moon sync hooks ``` - +## Development + +### When you work on core/wave-autoscale +```bash +# Run +moon run controller:[test|run] + +# If you need a situation that metrics are sent to the database and your plan responses to the metrics +# Edit core/wave-autoscale/tests/simulation.rs for your situation +moon run controller:test-simulation +``` + +### When you work on core/wave-metrics +- Telegraf Tips + ```toml + # Print metrics to stdout + [[outputs.file]] + files = ["stdout"] + ``` ## Coding Rules ### Rust diff --git a/core/wave-autoscale/moon.yml b/core/wave-autoscale/moon.yml index 41204d51..84e37a15 100644 --- a/core/wave-autoscale/moon.yml +++ b/core/wave-autoscale/moon.yml @@ -41,25 +41,7 @@ tasks: - "@globs(tests)" env: RUST_LOG: wave_autoscale=debug - # Run - run-help: - command: "cargo watch -c -x 'run -- --help'" - deps: - - "lint" - inputs: - - "@globs(sources)" - env: - RUST_LOG: debug - run-autoscaling-history-retention: - # command: "cargo watch -c -x 'run -- --autoscaling-history-retention=1d'" - command: "cargo run -- --autoscaling-history-retention=1d" - deps: - - "lint" - inputs: - - "@globs(sources)" - env: - RUST_LOG: wave_controller=debug - test-metrics-simulation: + test-simulation: command: "cargo nextest run -p wave-autoscale test_simulation --no-capture" deps: - "data-layer:lint" diff --git a/core/wave-autoscale/tests/metrics_test.rs b/core/wave-autoscale/tests/simulation.rs similarity index 99% rename from core/wave-autoscale/tests/metrics_test.rs rename to core/wave-autoscale/tests/simulation.rs index 15da984f..56006def 100644 --- a/core/wave-autoscale/tests/metrics_test.rs +++ b/core/wave-autoscale/tests/simulation.rs @@ -1,4 +1,4 @@ -mod metrics_test { +mod simulation_test { use data_layer::data_layer::DataLayer; use log::debug; use serde_json::json; diff --git a/core/wave-autoscale/tests/yaml/metric_cloudwatch_data.yaml b/core/wave-autoscale/tests/yaml/metric_cloudwatch_data.yaml deleted file mode 100644 index 9f8a4601..00000000 --- a/core/wave-autoscale/tests/yaml/metric_cloudwatch_data.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- -kind: Metric -id: cloudwatch_cpu_average -metric_kind: cloudwatch-data -metadata: - region: ap-northeast-2 - access_key: AKIAU7BQ6ZTCA2XGEZNN - secret_key: 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 - polling_interval: 1000 - # https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html - # https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDataQuery.html - expression: "SELECT AVG(CPUUtilization) FROM \"AWS/EC2\"" - period: 5 - duration_seconds: 3600 - # TODO: support MetricStat diff --git a/core/wave-autoscale/tests/yaml/metric_cloudwatch_statistics.yaml b/core/wave-autoscale/tests/yaml/metric_cloudwatch_statistics.yaml deleted file mode 100644 index 47429fe9..00000000 --- a/core/wave-autoscale/tests/yaml/metric_cloudwatch_statistics.yaml +++ /dev/null @@ -1,22 +0,0 @@ ---- -kind: Metric -id: cloudwatch_cpu_average -metric_kind: cloudwatch-statistics -metadata: - region: ap-northeast-2 - access_key: AKIAU7BQ6ZTCA2XGEZNN - secret_key: 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 - polling_interval: 1000 - # https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html - namespace: AWS/EC2 - metric_name: CPUUtilization - dimensions: - # - Name: InstanceId - # Value: i-0b0b0b0b0b0b0b0b0 - - Name: InstanceType - Value: t2.micro - duration_seconds: 3600 - period: 5 - # statistic: Average, Sum, SampleCount, Maximum, Minimum - statistic: Average - unit: Percent diff --git a/core/wave-autoscale/tests/yaml/metric_prometheus.yaml b/core/wave-autoscale/tests/yaml/metric_prometheus.yaml deleted file mode 100644 index 4b69a88d..00000000 --- a/core/wave-autoscale/tests/yaml/metric_prometheus.yaml +++ /dev/null @@ -1,23 +0,0 @@ ---- -kind: Metric -id: prometheus_api_server_cpu_average_each -metric_kind: prometheus -metadata: - endpoint: http://3.34.170.13:9090 - # This query returns a vector of time series, each with a single sample. - # Metric Adapter will save the result of this query as a map of labels to values. - # [metric_id] will be the first value of the result. - # [metric_id]_values will be an array of values of the result. - query: node_cpu_seconds_total - # milliseconds - polling_interval: 1000 ---- -kind: Metric -id: prometheus_api_server_cpu_average_total -metric_kind: prometheus -metadata: - endpoint: http://3.34.170.13:9090 - # Note: query must return a vector/scalar single element response - query: avg(100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[20s])) by (hostname) * 100)) - # milliseconds - polling_interval: 1000 \ No newline at end of file diff --git a/core/wave-cli/moon.yml b/core/wave-cli/moon.yml index bdf8766c..c2541794 100644 --- a/core/wave-cli/moon.yml +++ b/core/wave-cli/moon.yml @@ -41,9 +41,9 @@ tasks: - "--config" - "../../tests/config/wave-config.yaml" - "--definition" - - "../wave-metrics/tests/yaml/cloudwatch-definition.yaml" + - "../wave-metrics/tests/metrics-examples/vector-host-metrics.yaml" - "--collectors-info" - - "../wave-metrics/tests/yaml/collectors.yaml" + - "../wave-metrics/tests/collectors/collectors.yaml" deps: # TODO: Build web-app - "copy-wave-controller" @@ -54,6 +54,7 @@ tasks: - "wave-api-server" - "wave-metrics" - "wave-web-app/**/*" + - "tests/db/wave.db" env: RUST_LOG: wave_cli=debug options: diff --git a/core/wave-metrics/moon.yml b/core/wave-metrics/moon.yml index e3d9b60b..300b684e 100644 --- a/core/wave-metrics/moon.yml +++ b/core/wave-metrics/moon.yml @@ -30,11 +30,13 @@ tasks: - "--config" - "./tests/config/wave-config.yaml" - "--definition" - - "./core/wave-metrics/tests/yaml/cloudwatch-definition.yaml" + - "./core/wave-metrics/tests/metrics-examples/vector-host-metrics.yaml" - "--collectors-info" - - "./core/wave-metrics/tests/yaml/collectors.yaml" + - "./core/wave-metrics/tests/collectors/collectors.yaml" env: RUST_LOG: wave_metrics=debug + inputs: + - "@globs(tests)" # Build for Production # Linux build-aarch64-unknown-linux-gnu: diff --git a/core/wave-metrics/src/metric_collector_manager/mod.rs b/core/wave-metrics/src/metric_collector_manager/mod.rs index aae6dd54..9183c143 100644 --- a/core/wave-metrics/src/metric_collector_manager/mod.rs +++ b/core/wave-metrics/src/metric_collector_manager/mod.rs @@ -542,8 +542,8 @@ mod tests { let _ = std::fs::remove_file("./wave.db"); MetricCollectorManager::new( - "./tests/yaml/collectors.yaml", - "http://localhost:8081/api/metrics-receiver", + "./tests/collectors/collectors.yaml", + "http://localhost:3024/api/metrics-receiver", ) } diff --git a/core/wave-metrics/tests/yaml/collectors.yaml b/core/wave-metrics/tests/collectors/collectors.yaml similarity index 100% rename from core/wave-metrics/tests/yaml/collectors.yaml rename to core/wave-metrics/tests/collectors/collectors.yaml diff --git a/core/wave-metrics/tests/yaml/cloudwatch-definition.yaml b/core/wave-metrics/tests/metrics-examples/telegraf-cloudwatch-metrics.yaml similarity index 90% rename from core/wave-metrics/tests/yaml/cloudwatch-definition.yaml rename to core/wave-metrics/tests/metrics-examples/telegraf-cloudwatch-metrics.yaml index 9d8d293c..20796acc 100644 --- a/core/wave-metrics/tests/yaml/cloudwatch-definition.yaml +++ b/core/wave-metrics/tests/metrics-examples/telegraf-cloudwatch-metrics.yaml @@ -2,7 +2,7 @@ # https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cloudwatch/README.md --- kind: Metric -id: cloudwatch_dynamodb_metric +id: cloudwatch_dynamodb_metrics collector: telegraf metric_kind: cloudwatch metadata: @@ -28,7 +28,7 @@ metadata: # Output in the database will be like this: # [ # { -# "name": "cloudwatch_dynamodb_metric_consumed_write_capacity_units_sample_count", +# "name": "cloudwatch_dynamodb_metrics_consumed_write_capacity_units_sample_count", # "tags": { # "host": "Hwansoos-Laptop.local", # "region": "ap-northeast-3", @@ -48,7 +48,7 @@ metadata: # # JavaScript expression that returns a boolean value. # expression: > # get({ -# name: 'cloudwatch_dynamodb_metric_consumed_write_capacity_units_sample_count' +# name: 'cloudwatch_dynamodb_metrics_consumed_write_capacity_units_sample_count' # }) > 30 # # Higher priority values will be checked first. # priority: 1 diff --git a/core/wave-metrics/tests/metrics-examples/telegraf-google-cloud-monitoring-metrics.yaml b/core/wave-metrics/tests/metrics-examples/telegraf-google-cloud-monitoring-metrics.yaml new file mode 100644 index 00000000..203e8aeb --- /dev/null +++ b/core/wave-metrics/tests/metrics-examples/telegraf-google-cloud-monitoring-metrics.yaml @@ -0,0 +1,70 @@ +# References - Telegraf Stackdriver Google Cloud Monitoring Input Plugin +# https://github.com/influxdata/telegraf/blob/release-1.27/plugins/inputs/stackdriver/README.md +--- +kind: Metric +id: google_cloud_monitoring_mig_metrics +collector: telegraf +metric_kind: stackdriver +metadata: + project: wave-autoscale-test + ## Include timeseries that start with the given metric type. + metric_type_prefix_include: ["compute.googleapis.com/instance/cpu/utilization"] + interval: "1m" + +# Output in the database will be like this: +# [ +# { +# "name": "google_cloud_monitoring_mig_metrics_utilization", +# "tags": { +# "host": "local", +# "instance_id": "3137596874116707338", +# "instance_name": "test-instance-group-1-lp73", +# "project_id": "wave-autoscale-test", +# "resource_type": "gce_instance", +# "zone": "asia-northeast2-a" +# }, +# "value": 0.009973257744559306 +# }, +# { +# "name": "google_cloud_monitoring_mig_metrics_utilization", +# "tags": { +# "host": "local", +# "instance_id": "8022840066979737199", +# "instance_name": "instance-group-2-r32v", +# "project_id": "wave-autoscale-test", +# "resource_type": "gce_instance", +# "zone": "asia-northeast2-a" +# }, +# "value": 0.04300702278317961 +# }, +# { +# "name": "google_cloud_monitoring_mig_metrics_utilization", +# "tags": { +# "host": "local", +# "instance_id": "851205196260609218", +# "instance_name": "instance-group-2-wpng", +# "project_id": "wave-autoscale-test", +# "resource_type": "gce_instance", +# "zone": "asia-northeast2-a" +# }, +# "value": 0.0171343613359871 +# } +# ] + + +# --- +# kind: ScalingPlan +# id: google_scaling_plan +# title: "Scaling Plan for Google VM Instance" +# plans: +# - id: scale-up-plan-1 +# description: "Scale up" +# # JavaScript expression that returns a boolean value. +# expression: > +# get({ +# name: 'google_cloud_monitoring_mig_metrics_utilization' +# }) > 0.04 +# # Higher priority values will be checked first. +# priority: 1 +# scaling_components: +# ... \ No newline at end of file diff --git a/core/wave-metrics/tests/metrics-examples/telegraf-mem-metrics.yaml b/core/wave-metrics/tests/metrics-examples/telegraf-mem-metrics.yaml new file mode 100644 index 00000000..64cca019 --- /dev/null +++ b/core/wave-metrics/tests/metrics-examples/telegraf-mem-metrics.yaml @@ -0,0 +1,7 @@ +# https://github.com/influxdata/telegraf/blob/master/plugins/inputs/mem/README.md +--- +kind: Metric +id: telegraf_mem_metrics +collector: telegraf +metric_kind: mem +metadata: diff --git a/core/wave-metrics/tests/metrics-examples/vector-host-metrics.yaml b/core/wave-metrics/tests/metrics-examples/vector-host-metrics.yaml new file mode 100644 index 00000000..480b65d7 --- /dev/null +++ b/core/wave-metrics/tests/metrics-examples/vector-host-metrics.yaml @@ -0,0 +1,9 @@ +# https://vector.dev/docs/reference/configuration/sources/host_metrics/ +--- +kind: Metric +id: vector_host_metrics +collector: vector +metric_kind: host_metrics +metadata: + namespace: host + scrape_interval_secs: 5 diff --git a/core/wave-metrics/tests/metrics-examples/vector-prometheus-metrics.yaml b/core/wave-metrics/tests/metrics-examples/vector-prometheus-metrics.yaml new file mode 100644 index 00000000..96e4b8c4 --- /dev/null +++ b/core/wave-metrics/tests/metrics-examples/vector-prometheus-metrics.yaml @@ -0,0 +1,14 @@ +# https://vector.dev/docs/reference/configuration/sources/prometheus_scrape/#query +--- +kind: Metric +id: vector_host_metrics +collector: vector +metric_kind: prometheus_scrape +metadata: + endpoints: + - http://localhost:9090/metrics + query: + "match[]": + - '{job="somejob"}' + - '{__name__=~"job:.*"}' + scrape_interval_secs: 15 diff --git a/core/wave-metrics/tests/yaml/definition.yaml b/core/wave-metrics/tests/yaml/definition.yaml deleted file mode 100644 index 51a8d385..00000000 --- a/core/wave-metrics/tests/yaml/definition.yaml +++ /dev/null @@ -1,14 +0,0 @@ ---- -kind: Metric -id: metric_id_vector -collector: vector -metric_kind: host_metrics -metadata: - namespace: host - scrape_interval_secs: 15 ---- -kind: Metric -id: metric_id_telegraf -collector: telegraf -metric_kind: mem -metadata: