コード例 #1
0
def _requests_wrapper(func, instance, args, kwargs):
    """
    This is the wrapper of the function `requests.request`.
    This function is being wrapped specifically because it initializes the connection by itself and parses the response,
        which creates a gap from the traditional http.client wrapping.
    Moreover, these "extra" steps may raise exceptions. We should attach the error to the http span.
    """
    start_time = datetime.now()
    try:
        ret_val = func(*args, **kwargs)
    except Exception as exception:
        with lumigo_safe_execute("requests wrapper exception occurred"):
            method = safe_get_list(args, 0, kwargs.get("method", "")).upper()
            url = safe_get_list(args, 1, kwargs.get("url"))
            if Configuration.is_sync_tracer:
                if HttpState.previous_request:
                    span = SpansContainer.get_span().get_span_by_id(
                        HttpState.previous_span_id)
                else:
                    span = add_request_event(
                        None,
                        HttpRequest(
                            host=url,
                            method=method,
                            uri=url,
                            body=kwargs.get("data"),
                            headers=kwargs.get("headers"),
                            instance_id=id(instance),
                        ),
                    )
                    span_id = span["id"]
                    HttpState.request_id_to_span_id[get_lumigo_connection_id(
                        instance)] = span_id
                SpansContainer.add_exception_to_span(span, exception, [])
        raise
    with lumigo_safe_execute("requests wrapper time updates"):
        span_id = HttpState.response_id_to_span_id.get(
            get_lumigo_connection_id(ret_val.raw._original_response))
        SpansContainer.get_span().update_event_times(span_id,
                                                     start_time=start_time)
    return ret_val
コード例 #2
0
async def on_request_exception(session, trace_config_ctx, params):
    with lumigo_safe_execute("aiohttp on_request_exception"):
        span_id = getattr(trace_config_ctx, LUMIGO_SPAN_ID_KEY)
        span = SpansContainer.get_span().get_span_by_id(span_id)
        SpansContainer.add_exception_to_span(span, params.exception, [])