Module Review: Logs
[!NOTE] This module explores the core principles of Module Review: Logs, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. Key Takeaways
- Correlation is Key: Logs without trace IDs are disconnected silos. Injecting
trace_idandspan_idallows you to pivot instantly between logs and traces. - MDC Bridge: In Java, the Mapped Diagnostic Context (MDC) is the standard mechanism for passing context to logging frameworks (Logback, Log4j2).
- Go Context: In Go,
slogrequires explicitContextpassing (logger.InfoContext) to extract trace information. - Structured Logging: Always prefer JSON over plain text for production logs to enable efficient aggregation and querying in tools like ELK or Loki.
- Sampling: Be aware that if a trace is sampled out (dropped), the log will still have the
trace_id, but the trace will not exist in the backend.
2. Interactive Flashcards
Test your knowledge of logging concepts. Click a card to flip it.
What is MDC?
(Click to reveal)
Mapped Diagnostic Context
A thread-local map in Java logging frameworks used to store context (like trace_id) so it can be included in every log line.
Why use JSON logging?
(Click to reveal)
Machine Readability
JSON eliminates the need for complex regex parsing rules in log aggregators and handles multi-line exceptions cleanly.
How does Go's `slog` get the Trace ID?
(Click to reveal)
Explicit Context
Unlike Java's ThreadLocal, Go requires passing the `context.Context` object explicitly to methods like `InfoContext`.
What is the risk of Head Sampling with logs?
(Click to reveal)
Orphaned Logs
You might have error logs with a `trace_id`, but if the trace was sampled out at the start, you won't find the trace in your backend.
3. Cheat Sheet
| Feature | Java (Logback) | Go (Slog) |
|---|---|---|
| Context Storage | MDC (ThreadLocal) |
context.Context (Explicit) |
| Trace ID Key | trace_id |
trace_id (via OTel bridge) |
| Span ID Key | span_id |
span_id (via OTel bridge) |
| JSON Encoder | LogstashEncoder |
slog.NewJSONHandler |
| Log Method | log.info("msg") |
logger.InfoContext(ctx, "msg") |
| Attribute Format | kv("key", value) |
"key", value |
4. Quick Revision
- Inject: Ensure your OTel agent or SDK is injecting
trace_idinto the logging context. - Format: Update your logging pattern (XML/JSON) to output
%X{trace_id}. - Correlate: In your visualization tool (Grafana/Jaeger), use the
trace_idfrom the log to find the trace. - Structure: Move from text logs to JSON logs as soon as you have a centralized logging system.
5. Next Steps
Now that you have correlated logs, itβs time to set up the central nervous system of OpenTelemetry.
- Module 07: Collector - Learn how to aggregate, process, and export telemetry data using the OTel Collector.
- OpenTelemetry Glossary - Review definitions of key terms.