@@ -88,7 +88,7 @@ resource "aws_instance" "main_service_instance" {
8888 user_data = base64encode (<<- EOF
8989#!/bin/bash
9090yum update -y
91- yum install -y python3.12 python3.12-pip unzip
91+ yum install -y python3.12 python3.12-pip unzip bc
9292
9393mkdir -p /app
9494cd /app
@@ -112,6 +112,136 @@ export OTEL_RESOURCE_ATTRIBUTES="service.name=langchain-traceloop-app"
112112export AGENT_OBSERVABILITY_ENABLED="true"
113113
114114nohup opentelemetry-instrument python3.12 server.py > /var/log/langchain-service.log 2>&1 &
115+
116+ # Wait for service to be ready
117+ echo "Waiting for service to be ready..."
118+ for i in {1..60}; do
119+ if curl -s http://localhost:8000/health > /dev/null 2>&1; then
120+ echo "Service is ready!"
121+ break
122+ fi
123+ echo "Attempt $i: Service not ready, waiting 5 seconds..."
124+ sleep 5
125+ done
126+
127+ # Create traffic generator script
128+ cat > /app/generate_traffic.sh << 'TRAFFIC_EOF'
129+ #!/bin/bash
130+
131+ # Configuration
132+ SERVER_URL="${ SERVER_URL : - http : // localhost : 8000 } "
133+ ENDPOINT="${ SERVER_URL } /ai-chat"
134+ DELAY_SECONDS="${ DELAY_SECONDS : - 3600 } "
135+ NUM_REQUESTS="${ NUM_REQUESTS : - 0 } "
136+ TIMEOUT="${ TIMEOUT : - 30 } "
137+
138+ # Color codes for output
139+ GREEN='\033[0;32m'
140+ RED='\033[0;31m'
141+ YELLOW='\033[1;33m'
142+ NC='\033[0m'
143+
144+ # Array of sample messages
145+ MESSAGES=(
146+ "What is the weather like today?"
147+ "Tell me a joke"
148+ "How do I make a cup of coffee?"
149+ "What are the benefits of exercise?"
150+ "Explain quantum computing in simple terms"
151+ "What's the capital of France?"
152+ "How do I learn programming?"
153+ "What are some healthy breakfast ideas?"
154+ "Tell me about artificial intelligence"
155+ "How can I improve my productivity?"
156+ "What's the difference between a list and a tuple in Python?"
157+ "Explain the concept of microservices"
158+ "What are some best practices for API design?"
159+ "How does machine learning work?"
160+ "What's the purpose of unit testing?"
161+ )
162+
163+ # Function to send a request
164+ send_request() {
165+ local message="$1"
166+ local request_num="$2"
167+ local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
168+
169+ echo -e "${ YELLOW } [$timestamp] Request #$request_num${ NC } "
170+ echo "Message: \"$message\""
171+
172+ local trace_id_header="${ TRACE_ID : - Root =1 - 5759e988 - bd862e3fe1be46a994272793 ;Parent =53995c3f42cd8ad8;Sampled =1 } "
173+
174+ echo "Using Trace ID: $trace_id_header"
175+
176+ response=$(curl -s -X POST "$ENDPOINT" \
177+ -H "Content-Type: application/json" \
178+ -H "X-Amzn-Trace-Id: $trace_id_header" \
179+ -d "{\"message\": \"$message\"}" \
180+ -m "$TIMEOUT" \
181+ -w "\nHTTP_STATUS:%{ http_code } \nTIME_TOTAL:%{ time_total } ")
182+
183+ http_status=$(echo "$response" | grep "HTTP_STATUS:" | cut -d: -f2)
184+ time_total=$(echo "$response" | grep "TIME_TOTAL:" | cut -d: -f2)
185+ body=$(echo "$response" | sed '/HTTP_STATUS:/d' | sed '/TIME_TOTAL:/d')
186+
187+ if [ "$http_status" = "200" ]; then
188+ echo -e "${ GREEN } ✓ Success${ NC } (${ time_total } s)"
189+ echo "Response: $body"
190+ else
191+ echo -e "${ RED } ✗ Error: HTTP $http_status${ NC } "
192+ if [ -n "$body" ]; then
193+ echo "Response: $body"
194+ fi
195+ fi
196+ echo "---"
197+ }
198+
199+ # Trap Ctrl+C to exit gracefully
200+ trap 'echo -e "\n${ YELLOW } Traffic generation stopped by user${ NC } "; exit 0' INT
201+
202+ echo -e "${ GREEN } Starting traffic generation to $ENDPOINT${ NC } "
203+ echo "Configuration:"
204+ echo " - Delay between requests: ${ DELAY_SECONDS } s"
205+ echo " - Request timeout: ${ TIMEOUT } s"
206+ echo " - Number of requests: ${ NUM_REQUESTS } (0 = infinite)"
207+ echo " - Requests per minute: ~$((60 / DELAY_SECONDS))"
208+ echo -e "${ YELLOW } Press Ctrl+C to stop${ NC } "
209+ echo "=================================="
210+
211+ count=0
212+ start_time=$(date +%s)
213+
214+ while true; do
215+ random_index=$((RANDOM % ${ #MESSAGES [@]} ))
216+ message="${ MESSAGES [$random_index ]} "
217+
218+ count=$((count + 1))
219+
220+ send_request "$message" "$count"
221+
222+ if [ "$NUM_REQUESTS" -gt 0 ] && [ "$count" -ge "$NUM_REQUESTS" ]; then
223+ end_time=$(date +%s)
224+ duration=$((end_time - start_time))
225+ echo -e "${ GREEN } Completed $count requests in ${ duration } s${ NC } "
226+ break
227+ fi
228+
229+ if [ $((count % 10)) -eq 0 ]; then
230+ current_time=$(date +%s)
231+ elapsed=$((current_time - start_time))
232+ rate=$(echo "scale=2; $count / $elapsed * 60" | bc 2>/dev/null || echo "N/A")
233+ echo -e "${ YELLOW } Progress: $count requests sent, Rate: ${ rate } req/min${ NC } "
234+ fi
235+
236+ sleep "$DELAY_SECONDS"
237+ done
238+ TRAFFIC_EOF
239+
240+ chmod +x /app/generate_traffic.sh
241+
242+ # Start traffic generator in background
243+ echo "Starting traffic generator..."
244+ nohup /app/generate_traffic.sh > /var/log/traffic-generator.log 2>&1 &
115245EOF
116246 )
117247
0 commit comments