DataDog / dd-trace-js

JavaScript APM Tracer
https://docs.datadoghq.com/tracing/
Other
640 stars 303 forks source link

Trace missing error message and stack trace #4452

Open radum opened 3 months ago

radum commented 3 months ago

I have a Node app that uses dd-trace for everything. Internally we use Pino to log (but it is wrapped inside our own custom library) json output. We also use custom Error types (basically we are extending the default Error object and we create others like APIError or something simmilar).

It works perfectly fine I can see traces and logs and errors and routes and everything.

The problem I am having is that every time I log an error when I open the trace in Datadog I can see the info and I can see the logs associated with it but under the errors tab, or even on the Info tab on the top it says Missing error message and stack trace.

image image

I initially thought that our custom Pino wrapper lib is doing something wrong and the output is not in the format it needs to be. But reading the docs it says:

To enable Error Tracking, logs must include both of the following: either an error.type or error.stack field a status level of ERROR, CRITICAL, ALERT, or EMERGENCY

Looking at my logs, I can see in the sent json the correct level (this is clear from the fact that even the trace is logged as an error), the err key that has inside a message a stack and a type key also. So all of the expected info is there. I tried to log only a default Error object not an extended one, but its the same issue.

I checked everything again, we have a service name, a version, the spans are attached all is working because I can see data everywhere, the one thing that is missing is the stack in the trace.

If I open the logs from the trace then that log items have the correct format:

image

(I cleared some private stuff from the screenshots)

First it shows the error as bunyan error, but I use pino, so not sure how that is determined. Then all the info is there for a stack to be shown.

What am I missing, I can't figure it out? How does a json output need to look like for it to be shown in a trace? Can we use custom error types that extend the global JS Error object?

akramarev commented 3 months ago

I spent some time today figuring out a similar issue. It turns out errors in this tab have nothing to do with logs, they must be provided as span tags (see here and here).

In my case, an application has ApiErrorFilter: ExceptionFilter, it's a handler that captures all uncaught errors, sorts them and wraps them into user-friendly HTTP responses. It makes them invisible for default dd-trace(r). To fix this I had to manually add error tag to the current scope:

 // Handle unknown exceptions
else if (exception instanceof Error) {
  ddTracer.scope().active()?.setTag('error', exception);
  this.logger.error(exception);
  detail = 'Internal Server Error';
}
dongweiming commented 2 months ago

I'm having the same problem. Please tell me where to find the problem, thanks a lot