-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_b.py
More file actions
121 lines (94 loc) · 3.23 KB
/
service_b.py
File metadata and controls
121 lines (94 loc) · 3.23 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
from flask import Flask, request, jsonify, g, has_request_context
import logging
from pythonjsonlogger import jsonlogger
import time
import sys
import uuid
from datetime import datetime, timezone
app = Flask(__name__)
SERVICE_NAME = "service-b"
class RequestContextFilter(logging.Filter):
def filter(self, record):
record.timestamp = datetime.now(timezone.utc).isoformat()
record.level = record.levelname
if not hasattr(record, "service"):
record.service = SERVICE_NAME
if not hasattr(record, "correlation_id"):
if has_request_context():
record.correlation_id = getattr(g, "correlation_id", "no-correlation-id")
else:
record.correlation_id = "no-request-context"
return True
def setup_logging():
"""Configure JSON logging for service B"""
log_handler = logging.StreamHandler(sys.stdout)
formatter = jsonlogger.JsonFormatter(
"%(timestamp)s %(level)s %(name)s %(service)s %(correlation_id)s %(message)s"
)
log_handler.setFormatter(formatter)
log_handler.addFilter(RequestContextFilter())
logger = logging.getLogger(SERVICE_NAME)
logger.handlers.clear()
logger.addHandler(log_handler)
logger.setLevel(logging.INFO)
logger.propagate = False
return logger
logger = setup_logging()
@app.before_request
def before_request():
"""Extract correlation ID from incoming requests"""
g.correlation_id = request.headers.get("X-Correlation-ID", str(uuid.uuid4()))
g.request_start_time = time.time()
@app.after_request
def after_request(response):
response.headers["X-Correlation-ID"] = getattr(g, "correlation_id", "")
return response
def log_with_context(level, message, **kwargs):
extra = {
"correlation_id": getattr(g, "correlation_id", "no-correlation-id"),
"timestamp": datetime.now(timezone.utc).isoformat(),
"service": SERVICE_NAME
}
extra.update(kwargs)
log_method = getattr(logger, level.lower(), logger.info)
log_method(message, extra=extra)
@app.route('/api/inventory/check', methods=['POST'])
def check_inventory():
"""Check inventory availability"""
data = request.get_json(silent=True) or {}
product_id = data.get('product_id')
log_with_context(
"info",
"Inventory check request received",
method=request.method,
path=request.path,
product_id=product_id
)
if not product_id:
log_with_context(
"warning",
"Inventory validation failed",
status_code=400,
missing_field="product_id"
)
return jsonify({"error": "product_id is required"}), 400
# Simulate inventory check
time.sleep(0.05)
result = {
"product_id": product_id,
"available": True,
"quantity": 50
}
duration_ms = round((time.time() - g.request_start_time) * 1000, 2)
log_with_context(
"info",
"Inventory check completed",
status_code=200,
product_id=product_id,
available=result["available"],
quantity=result["quantity"],
duration_ms=duration_ms
)
return jsonify(result)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=False)