Esempio n. 1
0
def get_request_data_from_django():
    """Get http_request and trace data from django request headers.

    Returns:
        Tuple[Optional[dict], Optional[str], Optional[str], bool]:
            Data related to the current http request, trace_id, span_id, and trace_sampled
            for the request. All fields will be None if a django request isn't found.
    """
    request = _get_django_request()

    if request is None:
        return None, None, None, False

    # build http_request
    http_request = {
        "requestMethod": request.method,
        "requestUrl": request.build_absolute_uri(),
        "userAgent": request.META.get(_DJANGO_USERAGENT_HEADER),
        "protocol": request.META.get(_PROTOCOL_HEADER),
    }

    # find trace id and span id
    # first check for w3c traceparent header
    header = request.META.get(_DJANGO_TRACEPARENT)
    trace_id, span_id, trace_sampled = _parse_trace_parent(header)
    if trace_id is None:
        # traceparent not found. look for xcloud_trace_context header
        header = request.META.get(_DJANGO_XCLOUD_TRACE_HEADER)
        trace_id, span_id, trace_sampled = _parse_xcloud_trace(header)

    return http_request, trace_id, span_id, trace_sampled
Esempio n. 2
0
def get_request_data_from_django():
    """Get http_request and trace data from django request headers.

    Returns:
        Tuple[Optional[dict], Optional[str]]:
            Data related to the current http request and the trace_id for the
            request. Both fields will be None if a django request isn't found.
    """
    request = _get_django_request()

    if request is None:
        return None, None
    # build http_request
    http_request = {
        "requestMethod": request.method,
        "requestUrl": request.build_absolute_uri(),
        "requestSize": len(request.body),
        "userAgent": request.META.get(_DJANGO_USERAGENT_HEADER),
        "remoteIp": request.META.get(_DJANGO_REMOTE_ADDR_HEADER),
        "referer": request.META.get(_DJANGO_REFERER_HEADER),
        "protocol": request.META.get(_PROTOCOL_HEADER),
    }

    # find trace id
    trace_id = None
    header = request.META.get(_DJANGO_TRACE_HEADER)
    if header:
        trace_id = header.split("/", 1)[0]

    return http_request, trace_id
Esempio n. 3
0
    def test_process_request(self):
        from django.test import RequestFactory
        from google.cloud.logging_v2.handlers.middleware import request

        middleware = self._make_one()
        mock_request = RequestFactory().get("/")
        middleware.process_request(mock_request)

        django_request = request._get_django_request()
        self.assertEqual(django_request, mock_request)
Esempio n. 4
0
def get_trace_id_from_django():
    """Get trace_id from django request headers.

    Returns:
        str: TraceID in HTTP request headers.
    """
    request = _get_django_request()

    if request is None:
        return None

    header = request.META.get(_DJANGO_TRACE_HEADER)
    if header is None:
        return None

    trace_id = header.split("/", 1)[0]

    return trace_id
Esempio n. 5
0
def get_request_data_from_django():
    """Get http_request and trace data from django request headers.

    Returns:
        Tuple[Optional[dict], Optional[str], Optional[str]]:
            Data related to the current http request, trace_id, and span_id for
            the request. All fields will be None if a django request isn't
            found.
    """
    request = _get_django_request()

    if request is None:
        return None, None, None

    # convert content_length to int if it exists
    content_length = None
    try:
        content_length = int(request.META.get(_DJANGO_CONTENT_LENGTH))
    except (ValueError, TypeError):
        content_length = None

    # build http_request
    http_request = {
        "requestMethod": request.method,
        "requestUrl": request.build_absolute_uri(),
        "requestSize": content_length,
        "userAgent": request.META.get(_DJANGO_USERAGENT_HEADER),
        "remoteIp": request.META.get(_DJANGO_REMOTE_ADDR_HEADER),
        "referer": request.META.get(_DJANGO_REFERER_HEADER),
        "protocol": request.META.get(_PROTOCOL_HEADER),
    }

    # find trace id and span id
    header = request.META.get(_DJANGO_TRACE_HEADER)
    trace_id, span_id = _parse_trace_span(header)

    return http_request, trace_id, span_id
Esempio n. 6
0
    def _call_fut():
        from google.cloud.logging_v2.handlers.middleware import request

        return request._get_django_request()