Complete the code to generate a unique correlation ID for each request.
import uuid def generate_correlation_id(): return str([1].uuid4())
The uuid module provides a simple way to generate unique IDs using uuid4().
Complete the code to attach the correlation ID to the HTTP request headers.
def add_correlation_id_header(request, correlation_id): request.headers[[1]] = correlation_id
The X-Request-ID header is commonly used to pass correlation IDs in HTTP requests.
Fix the error in the code that extracts the correlation ID from incoming request headers.
def get_correlation_id(request): return request.headers.get([1], None)
The standard header to extract correlation IDs is X-Request-ID, matching what was set earlier.
Fill both blanks to create a middleware function that logs the correlation ID for each request.
def middleware(request): correlation_id = get_correlation_id(request) or generate_correlation_id() log_message = f"Processing request with [1]: {correlation_id}" [2](log_message) # continue processing request
The log message should mention Correlation-ID and use logger.info to log it properly.
Fill all three blanks to propagate the correlation ID through multiple microservices calls.
def call_service(service_url, correlation_id): headers = { [1]: correlation_id } response = http_client.get(service_url, headers=[2]) if response.status_code == 200: return response.json().get([3]) return None
Use X-Request-ID as the header key, pass the headers dictionary to the HTTP client, and extract data from the JSON response.