|
| 1 | +# ------------------------------------------------------------------------ |
| 2 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | +# You may not use this file except in compliance with the License. |
| 6 | +# A copy of the License is located at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# or in the "license" file accompanying this file. This file is distributed |
| 11 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | +# express or implied. See the License for the specific language governing |
| 13 | +# permissions and limitations under the License. |
| 14 | +# ------------------------------------------------------------------------- |
| 15 | + |
| 16 | +## install cwagent on the instance to collect metric from otel-collector |
| 17 | +data "template_file" "cwagent_config" { |
| 18 | + count = var.soaking ? 1 : 0 |
| 19 | + template = file(local.ami_family["soaking_cwagent_config"]) |
| 20 | + |
| 21 | + vars = { |
| 22 | + soaking_metric_namespace = var.soaking_metric_namespace |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +resource "null_resource" "install_cwagent" { |
| 27 | + count = var.soaking ? 1 : 0 |
| 28 | + |
| 29 | + // copy cwagent config to the instance |
| 30 | + provisioner "file" { |
| 31 | + content = data.template_file.cwagent_config[0].rendered |
| 32 | + destination = local.ami_family["soaking_cwagent_config_destination"] |
| 33 | + |
| 34 | + connection { |
| 35 | + type = local.connection_type |
| 36 | + user = local.login_user |
| 37 | + private_key = local.connection_type == "ssh" ? data.aws_s3_bucket_object.ssh_private_key.body : null |
| 38 | + password = local.connection_type == "winrm" ? rsadecrypt(aws_instance.aoc.password_data, data.aws_s3_bucket_object.ssh_private_key.body) : null |
| 39 | + host = aws_instance.aoc.public_ip |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + provisioner "remote-exec" { |
| 44 | + inline = [ |
| 45 | + local.ami_family["cwagent_download_command"], |
| 46 | + local.ami_family["cwagent_start_command"] |
| 47 | + ] |
| 48 | + |
| 49 | + connection { |
| 50 | + type = local.connection_type |
| 51 | + user = local.login_user |
| 52 | + private_key = local.connection_type == "ssh" ? data.aws_s3_bucket_object.ssh_private_key.body : null |
| 53 | + password = local.connection_type == "winrm" ? rsadecrypt(aws_instance.aoc.password_data, data.aws_s3_bucket_object.ssh_private_key.body) : null |
| 54 | + host = aws_instance.aoc.public_ip |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +## create cloudwatch alarm base on the metrics emitted by cwagent |
| 60 | +# wait 2 minute for the metrics to be available on cloudwatch |
| 61 | +resource "time_sleep" "wait_2_minutes" { |
| 62 | + depends_on = [null_resource.install_cwagent[0]] |
| 63 | + |
| 64 | + create_duration = "120s" |
| 65 | +} |
| 66 | +# cpu alarm |
| 67 | +resource "aws_cloudwatch_metric_alarm" "cpu_alarm" { |
| 68 | + depends_on = [time_sleep.wait_2_minutes] |
| 69 | + alarm_name = "otel-soaking-cpu-alarm-${module.common.testing_id}" |
| 70 | + comparison_operator = "GreaterThanOrEqualToThreshold" |
| 71 | + evaluation_periods = 2 |
| 72 | + threshold = "50" |
| 73 | + |
| 74 | + metric_query { |
| 75 | + id = "cpu" |
| 76 | + return_data = true |
| 77 | + |
| 78 | + metric { |
| 79 | + metric_name = local.ami_family["soaking_cpu_metric_name"] |
| 80 | + namespace = var.soaking_metric_namespace |
| 81 | + period = 60 |
| 82 | + stat = "Average" |
| 83 | + unit = "Percent" |
| 84 | + |
| 85 | + # use this dimension to identify each test |
| 86 | + dimensions = { |
| 87 | + InstanceId = aws_instance.aoc.id |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +# soaking alarm pulling |
| 94 | +resource "null_resource" "bake_alarms" { |
| 95 | + depends_on = [aws_cloudwatch_metric_alarm.cpu_alarm] |
| 96 | + count = var.soaking ? 1 : 0 |
| 97 | + provisioner "local-exec" { |
| 98 | + command = "${module.common.validator_path} --args='-c ${var.validation_config} -t ${module.common.testing_id} --region ${var.region} --alarm-names ${aws_cloudwatch_metric_alarm.cpu_alarm.alarm_name}'" |
| 99 | + working_dir = "../../" |
| 100 | + } |
| 101 | +} |
0 commit comments