
Logs Aren't Observability: Logs, Metrics and Traces Answer Different Questions
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
Observability means you can ask new questions
Metrics: is something wrong, and how much?
Traces: where did this request spend its time?
Logs: what exactly happened?
The trace ID ties the three together
The fix: give each question its signal
System.Diagnostics: OpenTelemetry repurposes "existing constructs like ActivitySource and Activity to be OpenTelemetry-compliant under the covers." Metrics use System.Diagnostics.Metrics. Here's the checkout, before and after:
public class Checkout(ILogger<Checkout> log, IPayments payments){ public async Task<bool> PayAsync(Order order) { log.LogInformation("Checkout started"); log.LogInformation("Calling payment service"); var ok = await payments.ChargeAsync(order); log.LogInformation("Payment finished, ok=" + ok); return ok; }}// Two checkouts at once, from the console logger:// info: Checkout[0] Checkout started// info: Checkout[0] Calling payment service// info: Checkout[0] Checkout started// info: Checkout[0] Calling payment service// info: Checkout[0] Payment finished, ok=True// info: Checkout[0] Payment finished, ok=Falseusing System.Diagnostics;using System.Diagnostics.Metrics;public class Checkout(ILogger<Checkout> log, IPayments payments){ static readonly ActivitySource Source = new("Shop.Checkout"); static readonly Meter Meter = new("Shop.Checkout"); static readonly Counter<long> Declines = Meter.CreateCounter<long>("checkout.declines"); public async Task<bool> PayAsync(Order order) { // Trace: a span for this one call, tagged with its order using var span = Source.StartActivity("ChargePayment"); span?.SetTag("order.id", order.Id); var ok = await payments.ChargeAsync(order); if (!ok) { Declines.Add(1); // Metric: a number to graph and alert on log.LogWarning("Payment declined for order {OrderId}", order.Id); } return ok; }}ok=False line doesn't say which order failed. For the second tab we registered the names with the OpenTelemetry .NET SDK 1.20.1 on .NET 10 (AddSource and AddMeter, plus the console exporter). The declined order produced a ChargePayment span tagged order.id: 81723, a checkout.declines sum of 1, and one log record with OrderId: 81723 and LogRecord.TraceId: 330be5addb80021ff67319cadebf59bf, the same value as the span's Activity.TraceId. The approved order produced a span and nothing else, because nothing worth a log line happened.
checkout.declines over time, error and request rates | ||
ChargePayment span inside the request's trace | ||
OrderId | ||
How to apply it in your project
Start with metrics for the golden signals For each user-facing service, make sure you can graph latency, traffic, errors and saturation, and alert on them. That's how you learn something is wrong before support does. Turn on tracing at the edges The OpenTelemetry docs point out that libraries your code depends on may already be instrumented, or have instrumentation libraries. Enable them for your HTTP server, HTTP client and database first, then add your own spans around important calls. Make logs structured and correlated Use message templates with named values, and check that a log record written inside a request carries its trace ID. If it doesn't, the SDK isn't wired to your logger yet. Replace step logs with spans Search for lines like "Calling X" and "Done X". Each one is a span waiting to be written, and deleting the log line makes the real events easier to find.
Key takeaways
Observability means being able to ask new questions of a running system, not having more log lines. Metrics answer "is something wrong, and how much?" cheaply and over time; start with the four golden signals. A trace answers "where did this request spend its time?" as a tree of spans across services. Logs answer "what exactly happened?", and they're most useful when they carry the trace ID. Use each signal for its question and link them through the trace ID instead of adding more log lines.
التعليقات (0)