-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
561 lines (462 loc) · 19.5 KB
/
Copy pathapp.py
File metadata and controls
561 lines (462 loc) · 19.5 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""Payments API — FastAPI app instrumented with Argus SDK.
A fintech payments service instrumented with Argus for observability.
Provides payment processing, account management, compliance screening,
and transfer initiation endpoints.
"""
import asyncio
import logging
import os
import random
import shutil
import subprocess
import argus
from argus.decorators import trace
from argus.exceptions import install as install_exception_hook
from argus.logger import ArgusHandler
from argus.middleware.fastapi import ArgusMiddleware
import httpx
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
# Initialize Argus SDK
argus.init(
server_url=os.getenv("ARGUS_URL", "http://localhost:7600"),
service_name=os.getenv("SERVICE_NAME", "payments-api"),
runtime_metrics=True,
auto_instrument=True,
)
install_exception_hook()
# Add Argus log handler
handler = ArgusHandler()
logging.getLogger().addHandler(handler)
app = FastAPI(title="Payments API")
app.add_middleware(ArgusMiddleware)
logger = logging.getLogger("payments-api")
PORT = int(os.getenv("PORT", "8000"))
# ---------------------------------------------------------------------------
# Merchant account data
# ---------------------------------------------------------------------------
MERCHANTS = {
"acct_1001": {
"account_id": "acct_1001",
"business_name": "Coastal Coffee Roasters",
"status": "active",
"currency": "USD",
"account_type": "merchant",
"created_at": "2024-08-14T09:22:00Z",
"risk_tier": "low",
},
"acct_1002": {
"account_id": "acct_1002",
"business_name": "Nimbus Cloud Hosting",
"status": "active",
"currency": "USD",
"account_type": "merchant",
"created_at": "2024-06-02T14:35:00Z",
"risk_tier": "low",
},
"acct_1003": {
"account_id": "acct_1003",
"business_name": "Verdant Meal Prep",
"status": "active",
"currency": "EUR",
"account_type": "merchant",
"created_at": "2024-11-20T11:10:00Z",
"risk_tier": "medium",
},
"acct_1004": {
"account_id": "acct_1004",
"business_name": "Atlas Freight Logistics",
"status": "active",
"currency": "USD",
"account_type": "enterprise",
"created_at": "2023-12-05T08:00:00Z",
"risk_tier": "low",
},
"acct_1005": {
"account_id": "acct_1005",
"business_name": "Pixel & Press Design Studio",
"status": "active",
"currency": "GBP",
"account_type": "merchant",
"created_at": "2025-01-18T16:45:00Z",
"risk_tier": "low",
},
}
# ---------------------------------------------------------------------------
# Chaos state — supports multiple active modes simultaneously
# ---------------------------------------------------------------------------
_chaos_modes: set[str] = set()
# ---------------------------------------------------------------------------
# Ops simulation endpoints (hidden from demo — triggered before recording)
# ---------------------------------------------------------------------------
@app.post("/_ops/simulate/db-failure")
async def ops_db_failure():
"""Activate database-down chaos mode. DB-dependent endpoints fail."""
_chaos_modes.add("down")
logger.warning("OPS: database failure simulation ACTIVATED")
return {"simulation": "db-failure", "active": sorted(_chaos_modes)}
@app.post("/_ops/simulate/degraded")
async def ops_degraded():
"""Activate degraded performance mode. Endpoints get extra latency."""
_chaos_modes.add("slow")
logger.warning("OPS: degraded performance simulation ACTIVATED")
return {"simulation": "degraded", "active": sorted(_chaos_modes)}
@app.post("/_ops/simulate/compromised")
async def ops_compromised():
"""Spawn a fake xmrig process for security demo."""
xmrig_path = "/tmp/xmrig"
try:
result = subprocess.run(
["pgrep", "-x", "xmrig"], capture_output=True, text=True,
)
if result.returncode == 0:
pids = result.stdout.strip()
return {"simulation": "compromised", "status": "already_running", "pids": pids}
except Exception:
pass
sleep_bin = shutil.which("sleep")
if not sleep_bin:
return JSONResponse(
status_code=500,
content={"error": "Cannot find 'sleep' binary to create fake xmrig"},
)
if not os.path.exists(xmrig_path):
shutil.copy2(sleep_bin, xmrig_path)
os.chmod(xmrig_path, 0o755)
proc = subprocess.Popen(
[xmrig_path, "infinity"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
_chaos_modes.add("vuln")
logger.warning("OPS: xmrig process spawned (PID %d)", proc.pid)
return {"simulation": "compromised", "pid": proc.pid, "active": sorted(_chaos_modes)}
@app.post("/_ops/simulate/recover")
async def ops_recover():
"""Deactivate all simulation modes."""
prev = sorted(_chaos_modes)
_chaos_modes.clear()
logger.info("OPS: all simulations cleared (were: %s)", prev)
return {"simulation": "recovered", "previous": prev}
@app.get("/_ops/simulate/status")
async def ops_status():
"""Return currently active simulation modes."""
return {"active": sorted(_chaos_modes)}
# ---------------------------------------------------------------------------
# Application endpoints
# ---------------------------------------------------------------------------
@app.get("/health")
async def health():
"""Health check — always responds, even during simulated failures."""
return {
"status": "ok",
"service": os.getenv("SERVICE_NAME", "payments-api"),
"version": "2.4.1",
}
@app.get("/v1/accounts/{account_id}")
@trace("get_account")
async def get_account(account_id: str):
"""Fetch a merchant account by ID."""
logger.info("Account lookup: %s", account_id)
# Chaos: degraded performance
if "slow" in _chaos_modes:
delay = random.uniform(2.0, 5.0)
logger.warning("Connection pool wait exceeded 2000ms (current active: 19/20)")
argus.add_breadcrumb("infra", f"Connection pool wait: {delay:.1f}s")
await asyncio.sleep(delay)
# Chaos: database down
if "down" in _chaos_modes:
err = ConnectionError("could not connect to pg-primary.internal:5432 — connection refused")
logger.error("DatabaseError: %s", err)
argus.capture_exception(err)
argus.event("dependency_error", {
"dependency": "pg-primary.internal:5432",
"type": "postgres",
"error": "connection refused",
"pool_active": 0,
"pool_max": 20,
})
return JSONResponse(
status_code=503,
content={"error": "DatabaseError: could not connect to pg-primary.internal:5432 — connection refused"},
)
# Lookup from merchant data
merchant = MERCHANTS.get(account_id)
if not merchant:
return JSONResponse(
status_code=404,
content={"error": f"Account {account_id} not found"},
)
await asyncio.sleep(random.uniform(0.01, 0.05))
argus.event("account_lookup", {"account_id": account_id, "business_name": merchant["business_name"], "lookup_ms": random.randint(8, 45)})
return merchant
@app.post("/v1/payments/refund")
@trace("process_refund")
async def process_refund(request: Request):
"""Process a payment refund — always fails (original txn not found)."""
body = {}
try:
body = await request.json()
except Exception:
pass
txn_id = body.get("transaction_id", f"txn_{random.choice(['a8f3k', 'b2m9p', 'c4x7q', 'd1n6r'])}")
amount = body.get("amount", round(random.uniform(10.0, 250.0), 2))
logger.info("Refund request: %s for $%.2f", txn_id, amount)
argus.add_breadcrumb("refund", "Looking up original transaction", {"transaction_id": txn_id})
try:
raise ValueError(f"Original transaction {txn_id} not found or already refunded")
except ValueError as e:
logger.error("Refund failed: %s", e)
argus.capture_exception(e)
return JSONResponse(status_code=400, content={"error": str(e), "code": "REFUND_NOT_FOUND"})
@app.get("/v1/compliance/screen")
@trace("compliance_screening")
async def compliance_screen():
"""AML/KYC compliance screening — naturally slow (1-3s)."""
logger.info("Compliance screening initiated")
# Chaos: degraded performance (becomes very slow)
if "slow" in _chaos_modes:
delay = random.uniform(8.0, 15.0)
logger.warning("Watchlist DB response time critical: %dms", int(delay * 1000))
argus.add_breadcrumb("infra", f"Watchlist DB latency spike: {delay:.1f}s")
await asyncio.sleep(delay)
# Chaos: database down
if "down" in _chaos_modes:
err = ConnectionError("cannot query watchlist database — all retries exhausted (3/3)")
logger.error("ComplianceError: %s", err)
argus.capture_exception(err)
return JSONResponse(
status_code=503,
content={"error": "ComplianceError: cannot query watchlist database — all retries exhausted (3/3)"},
)
# Natural latency for compliance checks
delay = random.uniform(1.0, 3.0)
await asyncio.sleep(delay)
screening_id = f"scr_{random.randbytes(3).hex()}"
risk_score = random.randint(2, 25)
argus.event("compliance_screened", {
"screening_id": screening_id,
"risk_score": risk_score,
"duration_ms": int(delay * 1000),
})
return {
"screening_id": screening_id,
"status": "clear",
"risk_score": risk_score,
"checks_completed": ["ofac", "pep", "adverse_media"],
"check_duration_ms": int(delay * 1000),
}
@app.post("/v1/transfers/initiate")
@trace("initiate_transfer")
async def initiate_transfer(request: Request):
"""Initiate a fund transfer — validates account, checks balance, initiates."""
body = {}
try:
body = await request.json()
except Exception:
pass
from_account = body.get("from_account", "acct_1001")
to_account = body.get("to_account", "acct_1002")
amount = body.get("amount", round(random.uniform(50.0, 5000.0), 2))
currency = body.get("currency", "USD")
logger.info("Transfer: %s -> %s, $%.2f %s", from_account, to_account, amount, currency)
# Chaos: degraded performance
if "slow" in _chaos_modes:
delay = random.uniform(4.0, 8.0)
logger.warning("Balance verification slow: replica lag detected (%dms)", int(delay * 1000))
argus.add_breadcrumb("infra", f"Read replica lag: {delay:.1f}s")
await asyncio.sleep(delay)
# Chaos: database down
if "down" in _chaos_modes:
err = ConnectionError("cannot verify available balance — read replica unreachable")
logger.error("BalanceLookupError: %s", err)
argus.capture_exception(err)
argus.event("dependency_error", {
"dependency": "pg-replica.internal:5432",
"type": "postgres",
"error": "read replica unreachable",
})
return JSONResponse(
status_code=503,
content={"error": "BalanceLookupError: cannot verify available balance — read replica unreachable"},
)
# Step 1: Validate sender account (internal call)
base = f"http://localhost:{PORT}"
async with httpx.AsyncClient() as client:
resp = await client.get(f"{base}/v1/accounts/{from_account}")
sender = resp.json()
# Step 2: Simulate balance check
await asyncio.sleep(random.uniform(0.02, 0.08))
transfer_id = f"tfr_{random.randbytes(4).hex()}"
argus.event("transfer_initiated", {
"transfer_id": transfer_id,
"from_account": from_account,
"to_account": to_account,
"amount": amount,
"currency": currency,
})
return {
"transfer_id": transfer_id,
"status": "pending",
"from_account": from_account,
"to_account": to_account,
"amount": amount,
"currency": currency,
"sender": sender.get("business_name", from_account),
}
@app.post("/v1/payments/charge")
@trace("process_charge")
async def process_charge(request: Request):
"""Process a payment charge — breadcrumbs through payment flow, fails at card network."""
body = {}
try:
body = await request.json()
except Exception:
pass
amount = body.get("amount", round(random.uniform(15.0, 500.0), 2))
currency = body.get("currency", "USD")
source = body.get("source", f"tok_visa_{random.randint(1000, 9999)}")
merchant_id = body.get("merchant_id", "mch_8291")
logger.info("Charge: $%.2f %s via %s for %s", amount, currency, source, merchant_id)
# Chaos: degraded performance
if "slow" in _chaos_modes:
delay = random.uniform(5.0, 12.0)
logger.warning("Card network response degraded: %dms", int(delay * 1000))
argus.add_breadcrumb("infra", f"Card network latency: {delay:.1f}s")
await asyncio.sleep(delay)
# Chaos: database down — gets partway through, fails at persistence
if "down" in _chaos_modes:
argus.add_breadcrumb("payment", "Validated merchant credentials (cached)", {"merchant_id": merchant_id})
await asyncio.sleep(0.01)
argus.add_breadcrumb("payment", "Fraud score computed", {"score": 12, "threshold": 75, "decision": "allow"})
await asyncio.sleep(0.01)
err = ConnectionError("failed to write to transactions table — database unavailable")
logger.error("TransactionPersistError: %s", err)
argus.add_breadcrumb("payment", "Persisting transaction record")
argus.capture_exception(err)
return JSONResponse(
status_code=503,
content={"error": "TransactionPersistError: failed to write to transactions table — database unavailable"},
)
argus.add_breadcrumb("payment", "Validated merchant credentials", {"merchant_id": merchant_id, "status": "active"})
await asyncio.sleep(0.01)
argus.add_breadcrumb("payment", "Fraud score computed", {"score": 12, "threshold": 75, "decision": "allow"})
await asyncio.sleep(0.01)
network = random.choice(["visa", "mastercard"])
argus.add_breadcrumb("payment", "Submitting to card network", {"network": network, "amount": amount, "currency": currency})
try:
raise RuntimeError(f"Card network ({network.title()}) did not respond within 30000ms")
except RuntimeError as e:
logger.error("GatewayTimeoutError: %s", e)
argus.capture_exception(e)
return JSONResponse(status_code=504, content={"error": f"GatewayTimeoutError: {e}", "code": "GATEWAY_TIMEOUT"})
@app.get("/v1/rates/convert")
@trace("fetch_exchange_rate")
async def fetch_exchange_rate():
"""Fetch live exchange rates from upstream FX provider."""
logger.info("Fetching exchange rates from upstream provider")
# Chaos: degraded performance (but still works — no DB needed)
if "slow" in _chaos_modes:
delay = random.uniform(3.0, 6.0)
logger.warning("Upstream FX provider latency spike: %dms", int(delay * 1000))
argus.add_breadcrumb("infra", f"FX provider latency: {delay:.1f}s")
await asyncio.sleep(delay)
# Rate conversion always works even when DB is down (no DB dependency)
async with httpx.AsyncClient() as client:
resp = await client.get("https://jsonplaceholder.typicode.com/todos/1")
data = resp.json()
rate = round(random.uniform(0.82, 0.95), 4)
argus.event("exchange_rate_fetched", {"pair": "USD/EUR", "rate": rate, "provider": "ecb"})
return {
"pair": "USD/EUR",
"rate": rate,
"provider": "ecb",
"timestamp": "2025-03-21T14:30:00Z",
"upstream_ref": data.get("id"),
}
@app.post("/v1/payments/authorize")
@trace("authorize_payment")
async def authorize_payment(request: Request):
"""Authorize a payment — generates varied realistic payment errors."""
body = {}
try:
body = await request.json()
except Exception:
pass
amount = body.get("amount", round(random.uniform(5.0, 500.0), 2))
card_last4 = body.get("card_last4", str(random.randint(1000, 9999)))
merchant_id = body.get("merchant_id", f"mch_{random.randint(1000, 9999)}")
logger.info("Authorization: $%.2f for %s (card ending %s)", amount, merchant_id, card_last4)
# Chaos: degraded performance
if "slow" in _chaos_modes:
delay = random.uniform(3.0, 7.0)
logger.warning("Issuing bank response degraded: %dms", int(delay * 1000))
argus.add_breadcrumb("infra", f"Issuing bank latency: {delay:.1f}s")
await asyncio.sleep(delay)
# Chaos: database down
if "down" in _chaos_modes:
err = ConnectionError("cannot record authorization hold — ledger database unavailable")
logger.error("LedgerWriteError: %s", err)
argus.capture_exception(err)
return JSONResponse(
status_code=503,
content={"error": "LedgerWriteError: cannot record authorization hold — ledger database unavailable"},
)
error_types = [
(ValueError, f"Card number failed Luhn check: invalid card ending in {card_last4}"),
(TimeoutError, "Issuing bank did not respond: timeout after 15000ms (BIN: 431940)"),
(PermissionError, f"Transaction declined: insufficient funds (available: $42.18, requested: ${amount:.2f})"),
(ConnectionError, "Payment processor connection refused: stripe-proxy.internal:8443"),
(RuntimeError, "Duplicate idempotency key: idem_9xk2m — original txn in terminal state"),
]
err_cls, msg = random.choice(error_types)
argus.add_breadcrumb("authorization", f"Processing card ending {card_last4}", {"merchant_id": merchant_id, "amount": amount})
argus.add_breadcrumb("authorization", f"Auth check: {err_cls.__name__}")
try:
raise err_cls(msg)
except Exception as e:
logger.error("Authorization failed: %s: %s", type(e).__name__, e)
argus.capture_exception(e)
return JSONResponse(status_code=400, content={"error": str(e), "type": type(e).__name__, "code": "AUTH_FAILED"})
# --- Background traffic simulator ---
async def _traffic_simulator():
"""Generate background traffic to keep telemetry flowing."""
endpoints = [
("GET", "/health", 8),
("GET", "/v1/accounts/acct_1001", 6),
("GET", "/v1/accounts/acct_1002", 4),
("POST", "/v1/payments/charge", 5),
("POST", "/v1/payments/authorize", 4),
("POST", "/v1/transfers/initiate", 3),
("GET", "/v1/rates/convert", 3),
("GET", "/v1/compliance/screen", 2),
("POST", "/v1/payments/refund", 2),
]
weighted = []
for method, path, weight in endpoints:
weighted.extend([(method, path)] * weight)
base = f"http://localhost:{PORT}"
await asyncio.sleep(5)
logger.info("Traffic simulator started")
while True:
try:
method, path = random.choice(weighted)
async with httpx.AsyncClient() as client:
if method == "GET":
await client.get(f"{base}{path}", timeout=30.0)
else:
await client.post(f"{base}{path}", timeout=30.0)
except Exception:
pass
delay = random.uniform(10, 45)
await asyncio.sleep(delay)
@app.on_event("startup")
async def startup():
asyncio.create_task(_traffic_simulator())
@app.on_event("shutdown")
async def shutdown():
argus.shutdown()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=PORT)