0
0
Microservicessystem_design~10 mins

Correlation IDs in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to generate a unique correlation ID for each request.

Microservices
import uuid

def generate_correlation_id():
    return str([1].uuid4())
Drag options to blanks, or click blank then click option'
Auuid
Brandom
Ctime
Dhashlib
Attempts:
3 left
💡 Hint
Common Mistakes
Using random module which does not guarantee uniqueness.
Using time module which can cause collisions under high load.
2fill in blank
medium

Complete the code to attach the correlation ID to the HTTP request headers.

Microservices
def add_correlation_id_header(request, correlation_id):
    request.headers[[1]] = correlation_id
Drag options to blanks, or click blank then click option'
AContent-Type
BAuthorization
CUser-Agent
DX-Request-ID
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Authorization' header which is for security tokens.
Using 'Content-Type' which specifies data format.
3fill in blank
hard

Fix the error in the code that extracts the correlation ID from incoming request headers.

Microservices
def get_correlation_id(request):
    return request.headers.get([1], None)
Drag options to blanks, or click blank then click option'
AX-Request-ID
BX-Correlation-ID
Ccorrelation-id
DRequest-ID
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect header names causing missing IDs.
Using 'X-Correlation-ID' which is less common than 'X-Request-ID'.
4fill in blank
hard

Fill both blanks to create a middleware function that logs the correlation ID for each request.

Microservices
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
Drag options to blanks, or click blank then click option'
ACorrelation-ID
Bprint
Clogger.info
DCorrelationId
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of logger.info for logging.
Using incorrect label names that don't match correlation ID headers.
5fill in blank
hard

Fill all three blanks to propagate the correlation ID through multiple microservices calls.

Microservices
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
Drag options to blanks, or click blank then click option'
AX-Request-ID
Bheaders
Cdata
DX-Correlation-ID
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong header keys causing correlation ID loss.
Passing wrong variable names to HTTP client.
Extracting incorrect keys from response JSON.