Search code examples
javaopentracingjaeger

Logging Exceptions with Opentracing and Jaeger


I've set up Jaeger with Opentracing in a Java environment and it works nicely with logging messages with spans and tracing. But I am a bit stuck when it comes to catching and logging exceptions.

    try
    {
        span.log(ImmutableMap.of("Exeption", "ex"));
        throw new IllegalArgumentException("Expecting one argument");
    }
    catch(Exception ex)
    {
       span.log(ImmutableMap.of("Error", ex));
       span.log(ImmutableMap.of("Event", "error", "Error-object", ex, "message", ex.getStackTrace()));
    }

But this way does not format error logging in a good readable way.

I have looked around for information about this as it feels pretty obvious there should be as this is one of its components for logging. But I have somehow never seen anything about this. It is mostly about building and structuring spans. Hope anyone can help me with this when it comes to capturing and logging exceptions.


Solution

  • This issue looks more to have to do with Java it self then either Opentracing and Jaeger. as ex.getStackTrace() is more of the problem. As it should be more like

    StringWriter errors = new StringWriter();
    ex.printStackTrace(new PrintWriter(errors));
    span.setTag("error", true);
    span.log(ImmutableMap.of("stack", errors));
    

    Problem solved.