jaegertracing / jaeger-client-python

🛑 This library is DEPRECATED!
https://jaegertracing.io/
Apache License 2.0
408 stars 155 forks source link

How to log (autolog) exceptions when using try/except ? #287

Closed kleysonr closed 4 years ago

kleysonr commented 4 years ago

Autologing works fine using the code below and I can see also the span set as a error in the Jaeger UI

    with tracer.start_active_span('resolve_getStatus', child_of=span_ctx, tags=span_tags) as scope:
        raise Exception('Error !!!')

But in case I use try/except clause, like the example below, jaeger is not reporting the span as error.

    with tracer.start_active_span('resolve_getStatusCnh', child_of=span_ctx, tags=span_tags) as scope:

        try:

            raise Exception('Error !!!')

        except Exception as e:
            return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)

How can I force the jaeger client report the span as error and get a error span with a log exception event in the Jaeger UI ?

Giohn commented 4 years ago

I also see curious behaviour. I have adapted the example code to demonstrate (I am using the all-in-one binary with this):

    tracer = config.initialize_tracer()

    do_stuff = lambda x: int(x)
    do_other_stuff = lambda x : print(x)

    with tracer.start_span('TestSpan') as span:
        span.log_kv({'event': 'test message', 'life': 42})
        with tracer.start_span('ChildSpan', child_of=span) as child_span:
            for c in 'xy2z4':
                with tracer.start_span('SubSpanExampleA', child_of=child_span) as a_span:
                    try:
                        do_stuff(c)
                    except ValueError:
                        do_other_stuff(c)
            for c in 'xy2z4':
                try:
                    with tracer.start_span('SubSpanExampleB', child_of=child_span) as b_span:
                        do_stuff(c)
                except ValueError:
                    do_other_stuff(c)

Notice where the span is created inside or outside of the try except block. Exceptions in python propagate and i would like to see that behaviour as an option on the created spans/tracer. I don't always want an error reported if it is handled by the code, even if the span is on the inside of a try except block; I may not always have control of the code I run inside the block but I can catch the errors. I am not sure what the correct strategy would be here or whether or not i am missing something that would answer this problem?

yurishkuro commented 4 years ago

@kleysonr if you're catching exception you need to log it to the span yourself.

I may not always have control of the code I run inside the block but I can catch the errors

@Giohn if you don't have control over code that creates b_span, then you can't affect anything on that span either, it's invisible to your code. But the code that starts a span and raises the error also has the option of logging that error to that span.

Closing this since none of these examples require changes to the tracer. There's no magic in how exceptions are handled.