def create_filtered_path(path, query_params):
    if scout_config.value("uri_reporting") == "path":
        return path
    filtered_params = sorted(
        ((k, "[FILTERED]" if k.lower() in FILTER_PARAMETERS else v)
         for k, v in query_params))
    if not filtered_params:
        return path
    return path + "?" + urlencode(filtered_params)
Esempio n. 2
0
def create_filtered_path(path, query_params):
    if scout_config.value("uri_reporting") == "path":
        return path
    # Python 2 unicode compatibility: force all keys and values to bytes
    filtered_params = sorted(((
        key.encode("utf-8"),
        (b"[FILTERED]"
         if key.lower() in FILTER_PARAMETERS else value.encode("utf-8")),
    ) for key, value in query_params))
    if not filtered_params:
        return path
    return path + "?" + urlencode(filtered_params)
Esempio n. 3
0
def create_filtered_path(path, query_params):
    if scout_config.value("uri_reporting") == "path":
        return path
    filtered_params = sorted([
        (
            text_type(key).encode("utf-8"),
            # Apply text_type again to cover the None case.
            text_type(filter_element(key, value)).encode("utf-8"),
        ) for key, value in query_params
    ])
    if not filtered_params:
        return path
    return path + "?" + urlencode(filtered_params)
def create_filtered_path(path, query_params):
    if scout_config.value("uri_reporting") == "path":
        return path
    # We expect query_params to have keys and values both of strings, because
    # that's how frameworks build it. However sometimes application code
    # mutates this structure to use incorrect types before we read it, so we
    # have to cautiously make everything a string again. Ignoring the
    # possibilities of bytes or objects with bad __str__ methods because they
    # seem very unlikely.
    string_query_params = ((text_type(key), text_type(value))
                           for key, value in query_params)
    # Python 2 unicode compatibility: force all keys and values to bytes
    filtered_params = sorted(((
        key.encode("utf-8"),
        (b"[FILTERED]"
         if key.lower() in FILTER_PARAMETERS else value.encode("utf-8")),
    ) for key, value in string_query_params))
    if not filtered_params:
        return path
    return path + "?" + urlencode(filtered_params)
Esempio n. 5
0
    def _send(self, http, errors):
        try:
            data = json.dumps({
                "notifier": "scout_apm_python",
                "environment": scout_config.value("environment"),
                "root": scout_config.value("application_root"),
                "problems": errors,
            }).encode("utf-8")
        except (ValueError, TypeError) as exc:
            logger.debug("Exception when serializing error message: %r",
                         exc,
                         exc_info=exc)
            return False

        params = {
            "key": scout_config.value("key"),
            "name": escape(scout_config.value("name"), quote=False),
        }
        headers = {
            "Agent-Hostname": scout_config.value("hostname") or "",
            "Content-Encoding": "gzip",
            "Content-Type": "application/json",
            "X-Error-Count": "{}".format(len(errors)),
        }

        encoded_args = urlencode(params)
        full_url = urljoin(scout_config.value("errors_host"),
                           "apps/error.scout") + "?{}".format(encoded_args)

        try:
            # urllib3 requires all parameters to be the same type for
            # python 2.7.
            # Since gzip can only return a str, convert all unicode instances
            # to str.
            response = http.request(
                str("POST"),
                str(full_url),
                body=gzip_compress(data),
                headers={
                    str(key): str(value)
                    for key, value in headers.items()
                },
            )
            if response.status >= 400:
                logger.debug(
                    ("ErrorServiceThread %r response error on _send:" +
                     " %r on PID: %s on thread: %s"),
                    response.status,
                    response.data,
                    os.getpid(),
                    threading.current_thread(),
                )
                return False
        except Exception as exc:
            logger.debug(
                ("ErrorServiceThread exception on _send:" +
                 " %r on PID: %s on thread: %s"),
                exc,
                os.getpid(),
                threading.current_thread(),
                exc_info=exc,
            )
            return False
        return True