from __future__ import absolute_import
import opentracing.ext.tags as ext
import instana
import opentracing
import wrapt


@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
def urlopen_with_instana(wrapped, instance, args, kwargs):
    try:
        span = instana.internal_tracer.start_span("urllib3")
        span.set_tag(ext.HTTP_URL, args[1])
        span.set_tag(ext.HTTP_METHOD, args[0])

        instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
        rv = wrapped(*args, **kwargs)

        span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
        if 500 <= rv.status <= 599:
            span.set_tag("error", True)
            ec = span.tags.get('ec', 0)
            span.set_tag("ec", ec+1)

    except Exception as e:
        span.log_kv({'message': e})
        span.set_tag("error", True)
        ec = span.tags.get('ec', 0)
        span.set_tag("ec", ec+1)
        raise
    else:
        span.finish()
        return rv

from __future__ import absolute_import
import opentracing.ext.tags as ext
import instana
import opentracing
import wrapt


@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
def urlopen_with_instana(wrapped, instance, args, kwargs):
    try:
        span = instana.internal_tracer.start_span("urllib3")
        span.set_tag(ext.HTTP_URL, args[1])
        span.set_tag(ext.HTTP_METHOD, args[0])

        instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
        rv = wrapped(*args, **kwargs)

        span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
        if 500 <= rv.status <= 599:
            span.set_tag("error", True)
            ec = span.tags.get('ec', 0)
            span.set_tag("ec", ec+1)

    except Exception as e:
        span.log_kv({'message': e})
        span.set_tag("error", True)
        ec = span.tags.get('ec', 0)
        span.set_tag("ec", ec+1)
        span.finish()
        raise
    else:
        span.finish()
        return rv

Make sure to finish span when there is an exception
Make sure to finish span when there is an exception

  from __future__ import absolute_import
  import opentracing.ext.tags as ext
  import instana
  import opentracing
  import wrapt
  
  
  @wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
  def urlopen_with_instana(wrapped, instance, args, kwargs):
      try:
          span = instana.internal_tracer.start_span("urllib3")
          span.set_tag(ext.HTTP_URL, args[1])
          span.set_tag(ext.HTTP_METHOD, args[0])
  
          instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
          rv = wrapped(*args, **kwargs)
  
          span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
          if 500 <= rv.status <= 599:
              span.set_tag("error", True)
              ec = span.tags.get('ec', 0)
              span.set_tag("ec", ec+1)
  
      except Exception as e:
          span.log_kv({'message': e})
          span.set_tag("error", True)
          ec = span.tags.get('ec', 0)
          span.set_tag("ec", ec+1)
+         span.finish()
          raise
      else:
          span.finish()
          return rv
  
Make sure to finish span when there is an exception
## Code Before:
from __future__ import absolute_import
import opentracing.ext.tags as ext
import instana
import opentracing
import wrapt


@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
def urlopen_with_instana(wrapped, instance, args, kwargs):
    try:
        span = instana.internal_tracer.start_span("urllib3")
        span.set_tag(ext.HTTP_URL, args[1])
        span.set_tag(ext.HTTP_METHOD, args[0])

        instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
        rv = wrapped(*args, **kwargs)

        span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
        if 500 <= rv.status <= 599:
            span.set_tag("error", True)
            ec = span.tags.get('ec', 0)
            span.set_tag("ec", ec+1)

    except Exception as e:
        span.log_kv({'message': e})
        span.set_tag("error", True)
        ec = span.tags.get('ec', 0)
        span.set_tag("ec", ec+1)
        raise
    else:
        span.finish()
        return rv

## Instruction:
Make sure to finish span when there is an exception
## Code After:
from __future__ import absolute_import
import opentracing.ext.tags as ext
import instana
import opentracing
import wrapt


@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
def urlopen_with_instana(wrapped, instance, args, kwargs):
    try:
        span = instana.internal_tracer.start_span("urllib3")
        span.set_tag(ext.HTTP_URL, args[1])
        span.set_tag(ext.HTTP_METHOD, args[0])

        instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
        rv = wrapped(*args, **kwargs)

        span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
        if 500 <= rv.status <= 599:
            span.set_tag("error", True)
            ec = span.tags.get('ec', 0)
            span.set_tag("ec", ec+1)

    except Exception as e:
        span.log_kv({'message': e})
        span.set_tag("error", True)
        ec = span.tags.get('ec', 0)
        span.set_tag("ec", ec+1)
        span.finish()
        raise
    else:
        span.finish()
        return rv
