zulip/zerver/views/report.py

138 lines
5.9 KiB
Python
Raw Normal View History

# System documented in https://zulip.readthedocs.io/en/latest/logging.html
from typing import Any, Dict, Optional, Text
from django.conf import settings
2016-06-04 21:47:59 +02:00
from django.http import HttpRequest, HttpResponse
from zerver.decorator import human_users_only, \
to_non_negative_int
from zerver.lib.bugdown import privacy_clean_markdown
from zerver.lib.request import has_request_variables, REQ
from zerver.lib.response import json_success
from zerver.lib.queue import queue_json_publish
from zerver.lib.unminify import SourceMap
from zerver.lib.utils import statsd, statsd_key
from zerver.lib.validator import check_bool, check_dict
2016-06-04 21:47:59 +02:00
from zerver.models import UserProfile
import subprocess
import os
js_source_map = None
# Read the source map information for decoding JavaScript backtraces.
def get_js_source_map() -> Optional[SourceMap]:
global js_source_map
if not js_source_map and not (settings.DEVELOPMENT or settings.TEST_SUITE):
js_source_map = SourceMap([
os.path.join(settings.DEPLOY_ROOT, 'prod-static/source-map'),
os.path.join(settings.STATIC_ROOT, 'webpack-bundles')
])
return js_source_map
@human_users_only
@has_request_variables
def report_send_times(request, user_profile,
time=REQ(converter=to_non_negative_int),
received=REQ(converter=to_non_negative_int, default="(unknown)"),
displayed=REQ(converter=to_non_negative_int, default="(unknown)"),
locally_echoed=REQ(validator=check_bool, default=False),
rendered_content_disparity=REQ(validator=check_bool, default=False)):
2016-06-04 21:47:59 +02:00
# type: (HttpRequest, UserProfile, int, int, int, bool, bool) -> HttpResponse
request._log_data["extra"] = "[%sms/%sms/%sms/echo:%s/diff:%s]" \
% (time, received, displayed, locally_echoed, rendered_content_disparity)
base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)
statsd.timing("endtoend.send_time.%s" % (base_key,), time)
if received != "(unknown)":
statsd.timing("endtoend.receive_time.%s" % (base_key,), received)
if displayed != "(unknown)":
statsd.timing("endtoend.displayed_time.%s" % (base_key,), displayed)
if locally_echoed:
statsd.incr('locally_echoed')
if rendered_content_disparity:
statsd.incr('render_disparity')
return json_success()
@human_users_only
@has_request_variables
def report_narrow_times(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int),
network=REQ(converter=to_non_negative_int)):
2016-06-04 21:47:59 +02:00
# type: (HttpRequest, UserProfile, int, int, int) -> HttpResponse
request._log_data["extra"] = "[%sms/%sms/%sms]" % (initial_core, initial_free, network)
base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)
statsd.timing("narrow.initial_core.%s" % (base_key,), initial_core)
statsd.timing("narrow.initial_free.%s" % (base_key,), initial_free)
statsd.timing("narrow.network.%s" % (base_key,), network)
return json_success()
@human_users_only
@has_request_variables
def report_unnarrow_times(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int)):
2016-06-04 21:47:59 +02:00
# type: (HttpRequest, UserProfile, int, int) -> HttpResponse
request._log_data["extra"] = "[%sms/%sms]" % (initial_core, initial_free)
base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)
statsd.timing("unnarrow.initial_core.%s" % (base_key,), initial_core)
statsd.timing("unnarrow.initial_free.%s" % (base_key,), initial_free)
return json_success()
@human_users_only
@has_request_variables
def report_error(request, user_profile, message=REQ(), stacktrace=REQ(),
ui_message=REQ(validator=check_bool), user_agent=REQ(),
href=REQ(), log=REQ(),
more_info=REQ(validator=check_dict([]), default=None)):
# type: (HttpRequest, UserProfile, Text, Text, bool, Text, Text, Text, Optional[Dict[str, Any]]) -> HttpResponse
"""Accepts an error report and stores in a queue for processing. The
actual error reports are later handled by do_report_error (below)"""
if not settings.BROWSER_ERROR_REPORTING:
return json_success()
if more_info is None:
more_info = {}
js_source_map = get_js_source_map()
if js_source_map:
stacktrace = js_source_map.annotate_stacktrace(stacktrace)
try:
2017-02-11 05:45:39 +01:00
version = subprocess.check_output(["git", "log", "HEAD^..HEAD", "--oneline"],
universal_newlines=True) # type: Optional[Text]
except Exception:
version = None
# Get the IP address of the request
remote_ip = request.META.get('HTTP_X_REAL_IP')
if remote_ip is None:
remote_ip = request.META['REMOTE_ADDR']
# For the privacy of our users, we remove any actual text content
# in draft_content (from drafts rendering exceptions). See the
# comment on privacy_clean_markdown for more details.
if more_info.get('draft_content'):
more_info['draft_content'] = privacy_clean_markdown(more_info['draft_content'])
queue_json_publish('error_reports', dict(
type = "browser",
report = dict(
host = request.get_host().split(":")[0],
ip_address = remote_ip,
user_email = user_profile.email,
user_full_name = user_profile.full_name,
user_visible = ui_message,
server_path = settings.DEPLOY_ROOT,
version = version,
user_agent = user_agent,
href = href,
message = message,
stacktrace = stacktrace,
log = log,
more_info = more_info,
)
), lambda x: None, call_consume_in_tests=True)
return json_success()