
Module 18 Exercises: Watching the Watchers
Practical exercises to challenge your understanding of structured logging, Prometheus metrics, and distributed tracing.
Module 18 Exercises: Watching the Watchers
Building an API is an act of creation; monitoring it is an act of maintenance. These exercises will help you turn "Invisible" code into "Observable" systems.
Exercise 1: The Log Level Strategy
You have a bug where a user intermittently fails to log in.
- Which log level should you use to record the "Raw JSON" of every login attempt during your investigation? (DEBUG, INFO, or ERROR?)
- Why is it important to turn this level back to
INFOonce the bug is fixed?
Exercise 2: Defining the RED Metrics
You are monitoring a Video Streaming API.
Define what the RED metrics would look like for the @app.get("/stream/{video_id}") endpoint:
- Rate: ?
- Errors: ?
- Duration: ?
Exercise 3: The Trace Interpreter
You are looking at a Trace for an endpoint that is failing with a 503 Service Unavailable.
- The "FastAPI Gate" span is green.
- The "Database Connect" span is red.
- The "Query Execution" span didn't start.
Goal: Based on these spans, what is the most likely cause of the failure?
Self-Correction / Discussion
Exercise 1 Answer:
- DEBUG. This level is for detailed diagnostic information.
- Logging raw JSON for every login on a high-traffic site would consume massive disk space and potentially leak sensitive user data into your logs.
Exercise 2 Answer:
- Rate: Number of streams started per minute.
- Errors: Number of 404s (video missing) or 500s (server error).
- Duration: Time to first byte (TTFB) or how long the handshake takes.
Exercise 3 Answer:
The Database Connection is failing. Because that span is red and the subsequent "Query" span didn't even start, you know the problem is in the connection string, the network, or the database server itself being down.
Summary of Module 18
You have given your API "Senses."
- Logging: You have a memory of past events.
- Metrics: You have a real-time pulse of health.
- Tracing: You can see through the fog of complex microservices.
In Module 19: FastAPI for AI and ML APIs, we will apply all of these "Enterprise" skills to the world of Artificial Intelligence, learning how to build fast, secure wrappers around LLMs and models.