A stack trace is a report of the active stack frames at a specific moment during program execution, typically when an exception is thrown. It lists the chain of function or method calls that led to that point, usually with class, function name, file and line number, and is the primary starting point for debugging errors.
What is a stack trace?
Whenever a program calls a function, the runtime puts a frame on the call stack that remembers where to return to. A stack trace (also stacktrace, backtrace or, in Python, traceback) is a snapshot of these frames. Most developers meet it when an unhandled exception is printed, but runtimes can produce stack traces at any time, for example in a profiler sample or a thread dump.
A stack trace answers two questions: where exactly did the problem happen, and how did the program get there? The second part is often more valuable, because the line that throws is frequently library code, while the bug lives in the application code that called it.
Anatomy of a stack trace
A typical Java stack trace looks like this:
java.lang.IllegalStateException: Cart is empty
at com.shop.checkout.OrderService.placeOrder(OrderService.java:87)
at com.shop.checkout.CheckoutController.submit(CheckoutController.java:42)
at org.springframework.web.servlet.FrameworkServlet.service(...)
...
Caused by: java.sql.SQLTimeoutException: Query timed out
at org.postgresql.jdbc.PgStatement.executeQuery(...)
... 23 more
- Exception type and message: the first line names the error class and a human-readable message.
- Frames: each
atline is one frame with class, method, file and line number. In Java and JavaScript the most recent call is at the top. - Cause chain:
Caused byshows a wrapped, underlying exception. The root cause is usually the last one in the chain. - Elided frames:
... 23 moremeans these frames are identical to the enclosing trace and were left out.
Differences between languages
The order is not the same everywhere. Python prints Traceback (most recent call last), so the line that failed is at the bottom, directly above the exception message. Python also chains exceptions with "During handling of the above exception, another exception occurred". Go prints goroutine stacks on panic, .NET shows at Namespace.Class.Method() in File.cs:line 12, and JavaScript in browsers often contains minified file names that only become readable with source maps.
Some languages also distinguish between the stack trace of an exception and the stack of a thread. A Java exception records the stack at the moment the exception object is created, not where it is caught or logged. Re-throwing a new exception without passing the original as cause therefore loses the most important information. The same applies to Python: use raise NewError(...) from err so the original traceback is preserved, and in JavaScript pass { cause: err } to the Error constructor.
How to read a stack trace efficiently
- Read the exception type and message first. They often already explain the problem.
- Follow the cause chain to the root. The top-level exception is often just a wrapper.
- Find the first frame in your own code. Skip framework and library frames until you reach your package names. That is where you usually need to look.
- Check the context. A stack trace shows the code path, not the data. Combine it with request parameters, the user, the release and logs from the same request to reproduce the issue.
Asynchronous code makes this harder: after an await or a callback, the original caller may no longer be on the stack. Distributed systems add another layer, because a stack trace ends at the process boundary. A span in a distributed trace connects the error to the upstream services that called it.
Stack traces in observability
For production systems, stack traces should be captured in a structured way rather than as many separate log lines, which log collectors may split apart. With structured logging, the trace goes into a single field. OpenTelemetry records exceptions as span events named exception with the attributes exception.type, exception.message and exception.stacktrace. Error tracking tools then group occurrences by a fingerprint derived from the stack frames, so a thousand identical errors become one issue with a count instead of a thousand alerts. Rising issue counts feed directly into the error rate of a service.
Be careful with sensitive data: exception messages can contain user input, e-mail addresses or tokens, so mask them before storage.
How Obseria helps
Obseria error tracking groups exceptions by stack trace, shows the first and last occurrence per release and links every occurrence to its trace and logs. Obseria Intelligence uses this context in root cause analysis, and the Obseria Pipeline can mask personal data in exception messages before they are stored.