docs: def generic_xray_wrapper(wrapped, instance, args, kwargs, name, namespace,
                           metadata_extractor,
                           error_handling_type=ERROR_HANDLING_GENERIC):
      """Wrapper function around existing calls to send traces to X-Ray.
  
      `wrapped` is the original function, `instance` is the original instance,
      if the function is an instance method (otherwise it'll be `None`), `args`
      and `kwargs` are the positional and keyword arguments the original function
      was called with.
  
      The `name` argument can either be a `string` or a callable. In the latter
      case the function must accept the following arguments:
  
          def callback(wrapped, instance, args, kwargs):
              pass
  
      The `metadata_extractor` argument has to be a function with the following
      definition:
  
          def callback(wrapped, instance, args, kwargs, return_value):
              pass
  
      It has to return a `dict` that will be used to extend the segment document.
  
      The `error_handling_type` determines how exceptions raised by the wrapped
      function are handled. Currently `botocore` requires some special care.
      """
      if not get_trace_id().sampled:
          # Request not sampled by X-Ray, let's get to the call
          # immediately.
          LOGGER.debug('Request not sampled by X-Ray, skipping trace')
          return wrapped(*args, **kwargs)
  
      start_time = time.time()
      error = False
      cause = None
  
      # Fetch the parent ID from the current thread, and set it to the current
      # subsegment, so that downstream subsegments will be correctly nested.
      original_parent_id = get_parent_id()
      # If not parent ID exists in the thread-local storage, it means we're at
      # the topmost level, so we have to retrieve the parent ID from the trace ID
      # environment variable.
      parent_id = original_parent_id or get_parent_id_from_trace_id()
      subsegment_id = generate_subsegment_id()
      set_parent_id(subsegment_id)
      # Send partial subsegment to X-Ray, so that it'll know about the relations
      # upfront (otherwise we'll lose data, since downstream subsegments will
      # have invalid parent IDs).
      send_subsegment_to_xray_daemon(
          subsegment_id=subsegment_id,
          parent_id=parent_id,
          start_time=start_time,
      )
      try:
          return_value = wrapped(*args, **kwargs)
      except Exception as exc:
          error = True
          cause = {
              'exceptions': [
                  {
                      'message': str(exc),
                      'type': '{}.{}'.format(
                          type(exc).__module__,
                          type(exc).__name__,
                      ),
                  }
              ]
          }
          if error_handling_type == ERROR_HANDLING_GENERIC:
              return_value = None
          elif error_handling_type == ERROR_HANDLING_BOTOCORE:
              if isinstance(exc, ClientError):
                  return_value = exc.response
              else:
                  return_value = {}
          raise
      finally:
          end_time = time.time()
          extra_data = metadata_extractor(
              wrapped=wrapped,
              instance=instance,
              args=args,
              kwargs=kwargs,
              return_value=return_value,
          )
          extra_data['error'] = error
          if error:
              extra_data['cause'] = cause
          # We allow the name to be determined dynamically when a function is
          # passed in as the `name` argument.
          if callable(name):
              name = name(wrapped, instance, args, kwargs)
          send_subsegment_to_xray_daemon(
              subsegment_id=subsegment_id,
              parent_id=parent_id,
              start_time=start_time,
              end_time=end_time,
              name=name,
              namespace=namespace,
              extra_data=extra_data,
          )
          # After done with reporting the current subsegment, reset parent
          # ID to the original one.
          set_parent_id(original_parent_id)
  
      return return_value