TASK
Implementation
In production distributed systems, message tracing is critical for debugging. When something goes wrong, you need to answer: "What messages did this node send and receive, and when?"
Your task is to add a message envelope logger to your node:
- Every received message should be logged with: timestamp, direction (RECV), src, dest, body type
- Every sent message should be logged with: timestamp, direction (SENT), src, dest, body type
- Logs are stored in an in-memory buffer (most recent 100 entries)
- Implement a
get_logmessage type that returns the log entries
Request: {"type": "get_log", "msg_id": 1, "count": 5}
Response: {"type": "get_log_ok", "in_reply_to": 1, "entries": [
{"ts": "2024-01-01T00:00:00", "dir": "RECV", "src": "c0", "dest": "n1", "msg_type": "init"},
{"ts": "2024-01-01T00:00:00", "dir": "SENT", "src": "n1", "dest": "c0", "msg_type": "init_ok"}
]}The count field specifies how many recent entries to return. If fewer entries exist, return all of them.
Sample Test Cases
Init and echo produce correct outputTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"echo","msg_id":2,"echo":"log-test"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "echo_ok", "echo": "log-test", "in_reply_to": 2, "msg_id": 1}}
Get log returns entries after initTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"get_log","msg_id":2,"count":10}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Log each message as a single line with a timestamp prefix
Hint 2▾
Include direction (SENT or RECV), src, dest, and body type
Hint 3▾
Use ISO 8601 format for timestamps
Hint 4▾
Log to stderr so it does not interfere with stdout message passing
Hint 5▾
Implement a get_log message type that returns recent log entries
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
loggingobservabilitymessage tracingtimestamps
main.py
python
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
#!/usr/bin/env python3
import sys
import json
from datetime import datetime
from collections import deque
class Node:
def __init__(self):
self.node_id = None
self.node_ids = []
self.next_msg_id = 0
self.log_buffer = deque(maxlen=100)
def log_message(self, direction, src, dest, msg_type):
# TODO: Add a log entry to the buffer
# Include timestamp, direction, src, dest, msg_type
pass
def send(self, dest, body):
body["msg_id"] = self.next_msg_id
self.next_msg_id += 1
message = {"src": self.node_id, "dest": dest, "body": body}
# TODO: Log the sent message
print(json.dumps(message), flush=True)
def reply(self, request, body):
body["in_reply_to"] = request["body"]["msg_id"]
self.send(request["src"], body)
def get_recent_logs(self, count):
# TODO: Return the most recent 'count' log entries
pass
def main():
node = Node()
for line in sys.stdin:
line = line.strip()
if not line:
continue