Skip to content

Commit

Permalink
Create Traffic Generator for Calling Sample App Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
harrryr committed Jul 25, 2024
1 parent b3d7e7c commit 15f9b20
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 230 deletions.
29 changes: 21 additions & 8 deletions .github/workflows/java-eks-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,28 @@ jobs:
working-directory: terraform/java/eks
run: echo "APP_ENDPOINT=$(terraform output sample_app_endpoint)" >> $GITHUB_ENV

# This steps increases the speed of the validation by creating the telemetry data in advance
- name: Call all test APIs
continue-on-error: true
# - name: Deploy the traffic generator
# working-directory: traffic-generator/deployments
# run: |
# sed -e "s/111122223333.dkr.ecr.us-west-2/${{ env.ACCOUNT_ID }}.dkr.ecr.${{ env.E2E_TEST_AWS_REGION }}/g" -e "s/MAIN_SAMPLE_APP_ENDPOINT/${{ env.APP_ENDPOINT }}/g" -e "s/REMOTE_SAMPLE_APP_ENDPOINT/${{ env.REMOTE_SERVICE_POD_IP }}/g" -e "s/TESTING_ID/${{ env.TESTING_ID }}/g" ./traffic-generator.yaml
#
# kubectl apply --namespace=${{ env.SAMPLE_APP_NAMESPACE }} -f ./traffic-generator.yaml
#
# cat ./traffic-generator.yaml
#
# sleep 10000

- name: Deploy the traffic generator
run: |
curl -S -s "http://${{ env.APP_ENDPOINT }}/outgoing-http-call"
curl -S -s "http://${{ env.APP_ENDPOINT }}/aws-sdk-call?ip=${{ env.REMOTE_SERVICE_POD_IP }}&testingId=${{ env.TESTING_ID }}"
curl -S -s "http://${{ env.APP_ENDPOINT }}/remote-service?ip=${{ env.REMOTE_SERVICE_POD_IP }}&testingId=${{ env.TESTING_ID }}"
curl -S -s "http://${{ env.APP_ENDPOINT }}/client-call"
curl -S -s "http://${{ env.APP_ENDPOINT }}/mysql"
kubectl create deployment traffic-generator \
--image=${{ env.ACCOUNT_ID }}.dkr.ecr.${{ env.E2E_TEST_AWS_REGION }}.amazonaws.com/e2e-test-resource:traffic-generator \
--replicas=1
kubectl set env deployment/traffic-generator MAIN_ENDPOINT=${{ env.APP_ENDPOINT }}
kubectl set env deployment/traffic-generator REMOTE_ENDPOINT=${{ env.REMOTE_SERVICE_POD_IP }}
kubectl set env deployment/traffic-generator ID=${{ env.TESTING_ID }}
kubectl delete pods --all -n ${{ env.SAMPLE_APP_NAMESPACE }}
- name: Initiate Gradlew Daemon
if: steps.initiate-gradlew == 'failure'
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
## SPDX-License-Identifier: Apache-2.0

## This workflow aims to run the Application Signals end-to-end tests as a canary to
## test the artifacts for App Signals enablement. It will deploy a sample app and remote
## service on two EC2 instances, call the APIs, and validate the generated telemetry,
## including logs, metrics, and traces.
name: Test
on:
# push:

permissions:
id-token: write
contents: read

jobs:
# default:
# strategy:
# fail-fast: false
# matrix:
# aws-region: ['us-east-1']
# uses: ./.github/workflows/java-ec2-default-e2e-test.yml
# secrets: inherit
# with:
# aws-region: ${{ matrix.aws-region }}
# caller-workflow-name: 'test'

eks:
strategy:
fail-fast: false
matrix:
aws-region: [ 'us-east-1' ]
uses: ./.github/workflows/java-eks-e2e-test.yml
secrets: inherit
with:
aws-region: ${{ matrix.aws-region }}
test-cluster-name: 'e2e-playground'
caller-workflow-name: 'test'

60 changes: 60 additions & 0 deletions .github/workflows/traffic-generator-image-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This workflow will build and the traffic generator image to each region whenever there is an update made to the traffic-generator folder.
# This image will be used by EKS and K8s test to call sample app endpoints
name: Create and Push Traffic Generator Image

on:
workflow_dispatch:
push:
# branches:
# - main
# paths:
# - 'traffic-generator/**'

permissions:
id-token: write
contents: read

jobs:
build-and-push-image:
runs-on: ubuntu-latest
strategy:
matrix:
aws-region: ['af-south-1','ap-east-1','ap-northeast-1','ap-northeast-2','ap-northeast-3','ap-south-1','ap-south-2','ap-southeast-1',
'ap-southeast-2','ap-southeast-3','ap-southeast-4','ca-central-1','eu-central-1','eu-central-2','eu-north-1',
'eu-south-1','eu-south-2','eu-west-1','eu-west-2','eu-west-3','il-central-1','me-central-1','me-south-1', 'sa-east-1',
'us-east-1','us-east-2','us-west-1','us-west-2']
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.E2E_SECRET_TEST_ROLE_ARN }}
aws-region: us-east-1

- name: Retrieve account
uses: aws-actions/aws-secretsmanager-get-secrets@v1
with:
secret-ids: |
ACCOUNT_ID, region-account/${{ matrix.aws-region }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ env.ACCOUNT_ID }}:role/${{ secrets.APPLICATION_SIGNALS_E2E_TEST_ROLE_NAME }}
aws-region: ${{ matrix.aws-region }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push image to Amazon ECR
working-directory: traffic-generator
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: e2e-test-resource
IMAGE_TAG: traffic-generator
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
38 changes: 38 additions & 0 deletions terraform/java/ec2/default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ resource "aws_instance" "main_service_instance" {
}
}



resource "null_resource" "main_service_setup" {
connection {
type = "ssh"
Expand Down Expand Up @@ -212,3 +214,39 @@ resource "null_resource" "remote_service_setup" {

depends_on = [aws_instance.remote_service_instance]
}

resource "null_resource" "traffic_generator_setup" {
connection {
type = "ssh"
user = var.user
private_key = local.private_key_content
host = aws_instance.main_service_instance.public_ip
}

provisioner "remote-exec" {
inline = [
<<-EOF
# Bring in the traffic generator files to EC2 Instance
traffic_generator_index='${file("../../../traffic-generator/index.js")}'
traffic_generator_package='${file("../../../traffic-generator/package.json")}'
echo $traffic_generator_index > index.js
echo $traffic_generator_package > package.json
# Install the traffic generator dependencies
sudo yum install nodejs -y && npm install
# Start the traffic generator in the background
tmux new -s traffic-generator -d
tmux send-keys -t traffic-generator 'cd /traffic-generator/' C-m
tmux send-keys -t traffic-generator "export MAIN_ENDPOINT=\"${aws_instance.main_service_instance.public_dns}\"" C-m
tmux send-keys -t traffic-generator "export REMOTE_ENDPOINT=\"${aws_instance.remote_service_instance.public_ip}\"" C-m
tmux send-keys -t traffic-generator "export ID=\"${var.test_id}\"" C-m
tmux send-keys -t traffic-generator "node index.js" C-m
EOF
]
}

depends_on = [null_resource.main_service_setup, null_resource.remote_service_setup]
}
21 changes: 21 additions & 0 deletions traffic-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use the official lightweight Node.js 16 image.
# https://hub.docker.com/_/node
# FROM node:16-slim
FROM public.ecr.aws/eks-distro-build-tooling/nodejs:16

# Create and change to the app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (if available).
# Copying this first prevents re-running npm install on every code change.
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy local code to the container image.
COPY . .

# Run the web service on container startup.
CMD [ "npm", "start" ]
26 changes: 26 additions & 0 deletions traffic-generator/deployments/traffic-generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: traffic-generator
labels:
app: traffic-generator
spec:
replicas: 1
selector:
matchLabels:
app: traffic-generator
template:
metadata:
labels:
app: traffic-generator
spec:
containers:
- name: traffic-generator
image: 111122223333.dkr.ecr.us-west-2.amazonaws.com/traffic-generator:latest
env:
- name: MAIN_ENDPOINT
value: "MAIN_SAMPLE_APP_ENDPOINT"
- name: REMOTE_ENDPOINT
value: "REMOTE_SAMPLE_APP_ENDPOINT"
- name: ID
value: "TESTING_ID"
42 changes: 42 additions & 0 deletions traffic-generator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const axios = require('axios');

const mainEndpoint = process.env.MAIN_ENDPOINT || 'your-main-sample-app-end-point';
const remoteEndpoint = process.env.REMOTE_ENDPOINT || 'your-remote-sample-app-end-point';
const id = process.env.ID || 'your-testing-id';

const urls = [
`http://${mainEndpoint}/outgoing-http-call`,
`http://${mainEndpoint}/aws-sdk-call?ip=${remoteEndpoint}&testingId=${id}`,
`http://${mainEndpoint}/remote-service?ip=${remoteEndpoint}&testingId=${id}`,
`http://${mainEndpoint}/client-call`,
`http://${mainEndpoint}/mysql`
]

// Send API requests to the sample app
async function sendRequests(urls) {
try {
const fetchPromises = urls.map(url => axios.get(url));
const responses = await Promise.all(fetchPromises);

// Handle the responses
responses.forEach((response, index) => {
if (response.status === 200) {
const data = response.data;
console.log(`Response from ${urls[index]}:`, data);
} else {
console.error(`Failed to fetch ${urls[index]}:`, response.statusText);
}
});
} catch (error) {
console.error('Error sending GET requests:', error);
}
}

// Traffic generator that sends traffic every specified interval. Send request immediately then every 2 minutes afterwards
function trafficGenerator(urls, interval) {
sendGetRequests(urls);
setInterval(() => sendRequests(urls), interval);
}

// Start sending GET requests every 2 minutes (120,000 milliseconds)
startInterval(urls, 120000);
12 changes: 12 additions & 0 deletions traffic-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "traffic-generator",
"version": "1.0.0",
"description": "A simple traffic generator that sends GET requests to a list of URLs every 2 minutes",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"node-fetch": "^3.2.0"
}
}
102 changes: 0 additions & 102 deletions validator/src/main/java/com/amazon/aoc/callers/HttpCaller.java

This file was deleted.

Loading

0 comments on commit 15f9b20

Please sign in to comment.