Grafana Loki, LogQL, Datadog and Sentry
Loki quickly selects and counts entries by labels, Datadog links logs with metrics and traces, and Sentry groups exceptions by stack into issues. Loki indexes only stream labels, so speed depends on a precise selector, and labels must have low cardinality. A thousand identical exceptions in Sentry are one issue. Sentry does not replace backend logs.
Logs · Loki, Datadog, Sentry
One failure a thousand times or a thousand different failures? Which tool answers faster?
After a release the logs hold a thousand ERRORs. If it is one repeating exception, fixing one cause is enough. If they differ, it is more serious. Different tools help to tell: Loki quickly selects and counts entries with labels, Sentry groups exceptions by stack, Datadog links logs with metrics and traces in one place. This lesson covers how they differ and how to ask the first question in each.
Grafana Loki. This is a log system that indexes only the labels of a stream (service, environment, pod) and not the full text. This makes it cheaper than full-text indices: the rest is filtered at query time by scanning. Queries are written in LogQL in Grafana (Explore). A query starts with a stream selector in curly braces (at least one non-empty condition is required), followed by pipeline steps:
{app="checkout", env="staging"} |= "timeout"
{app="checkout"} |= "error" != "healthcheck"
{app="checkout"} |~ "order (8841|8842)"
{app="checkout"} | json | level="error" | durationMs > 1000
sum by (app) (count_over_time({env="staging"} |= "error" [5m]))
How a LogQL query is built
|=contains a string,!=does not contain,|~matches a regex,!~does not match.| json(or| logfmt) parses the line and creates labels from fields, which are then filtered (level="error").- Metric queries (
count_over_time,rate) turn logs into a series for a chart or an alert; the range is in square brackets.
Hence the main trap: only fields with a bounded number of values (service, environment, cluster) become labels. If a request or user id becomes a label, the number of streams explodes and Loki slows down. Such values stay in the text and are searched with |= "id" or a parser.
Datadog. This is a SaaS for logs, metrics and APM traces in one place. When set up correctly, logs are linked with traces (the dd.trace_id and dd.span_id fields are added by the tracer), so from a trace you go to its logs in one click. Search in the Log Explorer is written through facets and attributes:
service:checkout status:error env:staging
service:checkout @http.status_code:>=500
trace_id:4bf92f3577b34da6a3ce929d0e0e4736
Sentry. This is an error tracker: it collects exceptions from the frontend and backend, groups them into issues by stack and fingerprint, shows breadcrumbs (what happened before the error), the environment and the release version. For QA it is useful to see whether an error is new in this very release, how many users it affected, on which browsers or OSes. A thousand identical exceptions is one issue with a counter, and this answers the question from the intro.
Example: the error count grew after a release. In Sentry you open issues filtered by the new release: one new issue appears, "TypeError: undefined is not an object (evaluating 'cart.promo')" in CartPage, 4,200 events, all in Safari 17. That points to the frontend and a browser, and the breadcrumbs show that the error happens after clicking "Apply promo code". Meanwhile in Loki you check whether there is a parallel spike of server errors: sum by (app) (count_over_time({env="prod", app=~"checkout|cart|promo"} |= "error" [5m])) shows no change, so the backend is not involved. Two queries are enough to hand a developer a concrete scenario instead of "something broke".
Limits and alternatives. The choice of tool depends on the company, so the main thing is to state the sequence: time, service and environment, level, request identifier, root cause. Loki is cheap but slow on a broad scan without a precise selector. Datadog is convenient but billed by log volume. Sentry sees only what reached the SDK as an exception and does not replace backend logs: a failure without an exception (a slow response, wrong data) it will not show.
Typical mistakes.
- A broad selector (
{env="prod"}) and a text filter: the query is slow or aborted by a limit. - Making a request id a label: Loki does not like high cardinality.
- Looking in Sentry for a failure that throws no exception: it will not be there.
- Not checking the release: an old error seems new or the other way round.
Check yourself
The query {env="prod"} |= "timeout" runs very slowly. How do you speed it up without changing its meaning?
Answer
Narrow the stream selector by indexed labels: add a service or a cluster, for example {env="prod", app="checkout"}, and shorten the time range. Loki’s index works only on labels, while the |= "timeout" filter scans the text of all selected streams.
Why are 4,200 events in Sentry not 4,200 different bugs?
Answer
Sentry groups events by stack and fingerprint into issues. Identical exceptions merge into one issue with an event counter and a count of affected users. So look at issues: new ones, in the new release, with the widest reach.