2017-11-16 19:51:44 +01:00
|
|
|
# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
|
2020-06-11 00:54:34 +02:00
|
|
|
import logging
|
2023-04-10 20:45:14 +02:00
|
|
|
from typing import Union
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2020-08-31 09:22:40 +02:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2016-06-04 21:47:59 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-04-11 05:50:08 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.http import require_POST
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-31 09:22:40 +02:00
|
|
|
from zerver.context_processors import get_valid_realm_from_request
|
2020-05-07 13:19:54 +02:00
|
|
|
from zerver.decorator import human_users_only
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.request import REQ, RequestNotes, has_request_variables
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.response import json_success
|
2015-11-23 17:29:37 +01:00
|
|
|
from zerver.lib.utils import statsd, statsd_key
|
2022-01-11 09:37:41 +01:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
WildValue,
|
|
|
|
check_bool,
|
|
|
|
check_string,
|
|
|
|
to_non_negative_int,
|
|
|
|
to_wild_value,
|
|
|
|
)
|
2016-06-04 21:47:59 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-28 00:16:13 +02:00
|
|
|
@human_users_only
|
2015-11-23 17:29:37 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def report_send_times(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
time: int = REQ(converter=to_non_negative_int),
|
|
|
|
received: int = REQ(converter=to_non_negative_int, default=-1),
|
|
|
|
displayed: int = REQ(converter=to_non_negative_int, default=-1),
|
2021-04-07 22:00:44 +02:00
|
|
|
locally_echoed: bool = REQ(json_validator=check_bool, default=False),
|
|
|
|
rendered_content_disparity: bool = REQ(json_validator=check_bool, default=False),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-11-03 05:13:04 +01:00
|
|
|
received_str = "(unknown)"
|
|
|
|
if received > 0:
|
|
|
|
received_str = str(received)
|
|
|
|
displayed_str = "(unknown)"
|
|
|
|
if displayed > 0:
|
|
|
|
displayed_str = str(displayed)
|
|
|
|
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data[
|
2021-02-12 08:19:30 +01:00
|
|
|
"extra"
|
|
|
|
] = f"[{time}ms/{received_str}ms/{displayed_str}ms/echo:{locally_echoed}/diff:{rendered_content_disparity}]"
|
2017-11-03 05:13:04 +01:00
|
|
|
|
2017-03-13 17:50:28 +01:00
|
|
|
base_key = statsd_key(user_profile.realm.string_id, clean_periods=True)
|
2020-06-10 06:41:04 +02:00
|
|
|
statsd.timing(f"endtoend.send_time.{base_key}", time)
|
2017-11-03 05:13:04 +01:00
|
|
|
if received > 0:
|
2020-06-10 06:41:04 +02:00
|
|
|
statsd.timing(f"endtoend.receive_time.{base_key}", received)
|
2017-11-03 05:13:04 +01:00
|
|
|
if displayed > 0:
|
2020-06-10 06:41:04 +02:00
|
|
|
statsd.timing(f"endtoend.displayed_time.{base_key}", displayed)
|
2015-11-23 17:29:37 +01:00
|
|
|
if locally_echoed:
|
2021-02-12 08:20:45 +01:00
|
|
|
statsd.incr("locally_echoed")
|
2015-11-23 17:29:37 +01:00
|
|
|
if rendered_content_disparity:
|
2021-02-12 08:20:45 +01:00
|
|
|
statsd.incr("render_disparity")
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2015-11-23 17:29:37 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def report_narrow_times(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: Union[UserProfile, AnonymousUser],
|
|
|
|
initial_core: int = REQ(converter=to_non_negative_int),
|
|
|
|
initial_free: int = REQ(converter=to_non_negative_int),
|
|
|
|
network: int = REQ(converter=to_non_negative_int),
|
|
|
|
) -> HttpResponse:
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data["extra"] = f"[{initial_core}ms/{initial_free}ms/{network}ms]"
|
2020-08-31 09:22:40 +02:00
|
|
|
realm = get_valid_realm_from_request(request)
|
|
|
|
base_key = statsd_key(realm.string_id, clean_periods=True)
|
2020-06-10 06:41:04 +02:00
|
|
|
statsd.timing(f"narrow.initial_core.{base_key}", initial_core)
|
|
|
|
statsd.timing(f"narrow.initial_free.{base_key}", initial_free)
|
|
|
|
statsd.timing(f"narrow.network.{base_key}", network)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2015-11-23 17:29:37 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def report_unnarrow_times(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: Union[UserProfile, AnonymousUser],
|
|
|
|
initial_core: int = REQ(converter=to_non_negative_int),
|
|
|
|
initial_free: int = REQ(converter=to_non_negative_int),
|
|
|
|
) -> HttpResponse:
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data["extra"] = f"[{initial_core}ms/{initial_free}ms]"
|
2020-08-31 09:22:40 +02:00
|
|
|
realm = get_valid_realm_from_request(request)
|
|
|
|
base_key = statsd_key(realm.string_id, clean_periods=True)
|
2020-06-10 06:41:04 +02:00
|
|
|
statsd.timing(f"unnarrow.initial_core.{base_key}", initial_core)
|
|
|
|
statsd.timing(f"unnarrow.initial_free.{base_key}", initial_free)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-11 05:50:08 +02:00
|
|
|
@csrf_exempt
|
|
|
|
@require_POST
|
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def report_csp_violations(
|
2022-01-11 09:37:41 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
csp_report: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-04-11 05:50:08 +02:00
|
|
|
def get_attr(csp_report_attr: str) -> str:
|
2022-01-11 09:37:41 +01:00
|
|
|
return csp_report.get(csp_report_attr, "").tame(check_string)
|
2018-04-11 05:50:08 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
logging.warning(
|
2021-05-10 07:02:14 +02:00
|
|
|
"CSP violation in document('%s'). "
|
|
|
|
"blocked URI('%s'), original policy('%s'), "
|
|
|
|
"violated directive('%s'), effective directive('%s'), "
|
|
|
|
"disposition('%s'), referrer('%s'), "
|
|
|
|
"status code('%s'), script sample('%s')",
|
2021-02-12 08:20:45 +01:00
|
|
|
get_attr("document-uri"),
|
|
|
|
get_attr("blocked-uri"),
|
|
|
|
get_attr("original-policy"),
|
|
|
|
get_attr("violated-directive"),
|
|
|
|
get_attr("effective-directive"),
|
|
|
|
get_attr("disposition"),
|
|
|
|
get_attr("referrer"),
|
|
|
|
get_attr("status-code"),
|
|
|
|
get_attr("script-sample"),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-04-11 05:50:08 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|