-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.sh
More file actions
104 lines (90 loc) · 2.85 KB
/
Copy pathcommands.sh
File metadata and controls
104 lines (90 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# --- Install Required Tools ---
sudo apt update
sudo apt install -y python3 python3-pip python3-venv
mkdir -p ~/logging-lab
cd ~/logging-lab
pwd
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install flask python-json-logger requests
ls -la
# --- Step 1: Create Base Application with Structured Logging ---
nano app.py
python3 -m py_compile app.py
echo $?
# --- Step 2: Test Structured Logging ---
python3 app.py > app.log 2>&1 &
sleep 2
ps -fp 8014
curl http://localhost:5000/api/users
curl -X POST http://localhost:5000/api/orders \
-H "Content-Type: application/json" \
-d '{"user_id": 1, "amount": 99.99}'
tail -20 app.log
# --- Step 1: Create Downstream Service ---
nano service_b.py
python3 -m py_compile service_b.py
echo $?
# --- Step 2: Update Main Service to Propagate Correlation ID ---
nano app.py
python3 -m py_compile app.py
echo $?
# --- Step 3: Test Correlation ID Propagation ---
pkill -f "python3 app.py" 2>/dev/null || true
pkill -f "python3 service_b.py" 2>/dev/null || true
python3 service_b.py > service_b.log 2>&1 &
sleep 2
tail -5 service_b.log
python3 app.py > app.log 2>&1 &
sleep 2
tail -5 app.log
curl -X POST http://localhost:5000/api/orders/full \
-H "Content-Type: application/json" \
-H "X-Correlation-ID: test-12345" \
-d '{"product_id": "prod-001", "user_id": 1}'
tail -20 app.log
tail -20 service_b.log
# --- Step 4: Create Log Analysis Script ---
nano analyze_logs.py
cat app.log service_b.log > combined.log
python3 analyze_logs.py combined.log
# --- Verify Structured Logging ---
grep '"correlation_id"' app.log | tail -5
grep '"correlation_id"' app.log | tail -1
# --- Verify Correlation ID Propagation ---
CORRELATION_ID="verify-$(date +%s)"
curl -X POST http://localhost:5000/api/orders/full \
-H "Content-Type: application/json" \
-H "X-Correlation-ID: $CORRELATION_ID" \
-d '{"product_id": "test-prod", "user_id": 1}' \
> /dev/null 2>&1
echo "Checking correlation ID propagation..."
grep "$CORRELATION_ID" app.log
grep "$CORRELATION_ID" service_b.log
ps aux | grep -E "python3 .*app.py|python3 .*service_b.py" | grep -v grep
# --- Test Log Traceability ---
for i in {1..3}; do
curl -X POST http://localhost:5000/api/orders/full \
-H "Content-Type: application/json" \
-d "{\"product_id\": \"prod-$i\", \"user_id\": $i}" \
> /dev/null 2>&1
sleep 1
done
cat app.log service_b.log > combined.log
python3 analyze_logs.py combined.log
# --- Logs not in JSON format ---
python3 -m pip show python-json-logger
# --- Correlation ID not propagating ---
tail -20 app.log
tail -20 service_b.log
# --- Services not responding ---
ss -tlnp | grep -E ':5000|:5001'
ps aux | grep python3 | grep -E 'app.py|service_b.py' | grep -v grep
tail -20 app.log
tail -20 service_b.log
# --- Missing log fields ---
grep '"correlation_id"' app.log | tail -1
# --- Final Project Snapshot ---
find . -maxdepth 1 -type f | sort
jobs -l