2023-10-11 07:43:49 +02:00
|
|
|
import logging
|
2023-03-24 16:00:46 +01:00
|
|
|
import urllib
|
2023-04-21 00:07:13 +02:00
|
|
|
from contextlib import suppress
|
2023-03-24 16:00:46 +01:00
|
|
|
|
2023-04-21 00:07:13 +02:00
|
|
|
import orjson
|
2023-10-11 07:44:08 +02:00
|
|
|
from circuitbreaker import CircuitBreakerError, circuit
|
2023-03-24 16:00:46 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2023-10-11 07:44:08 +02:00
|
|
|
from requests.exceptions import ProxyError, RequestException, Timeout
|
2023-10-11 07:43:49 +02:00
|
|
|
from sentry_sdk.integrations.logging import ignore_logger
|
2023-03-24 16:00:46 +01:00
|
|
|
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.outgoing_http import OutgoingSession
|
2023-08-03 02:09:35 +02:00
|
|
|
from zerver.lib.validator import check_url, to_wild_value
|
2023-03-24 16:00:46 +01:00
|
|
|
|
2023-10-11 07:43:49 +02:00
|
|
|
# In order to not overload Sentry if it's having a bad day, we tell
|
|
|
|
# Sentry to ignore exceptions that we have when talking to Sentry.
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ignore_logger(logger.name)
|
|
|
|
|
2023-03-24 16:00:46 +01:00
|
|
|
|
|
|
|
class SentryTunnelSession(OutgoingSession):
|
|
|
|
def __init__(self) -> None:
|
2023-07-10 18:03:13 +02:00
|
|
|
super().__init__(role="sentry_tunnel", timeout=1)
|
2023-03-24 16:00:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def sentry_tunnel(
|
|
|
|
request: HttpRequest,
|
|
|
|
) -> HttpResponse:
|
|
|
|
try:
|
2023-04-21 00:07:13 +02:00
|
|
|
envelope_header_line, envelope_items = request.body.split(b"\n", 1)
|
|
|
|
envelope_header = to_wild_value("envelope_header", envelope_header_line.decode("utf-8"))
|
|
|
|
dsn = urllib.parse.urlparse(envelope_header["dsn"].tame(check_url))
|
2023-03-24 16:00:46 +01:00
|
|
|
except Exception:
|
|
|
|
raise JsonableError(_("Invalid request format"))
|
|
|
|
|
|
|
|
if dsn.geturl() != settings.SENTRY_FRONTEND_DSN:
|
|
|
|
raise JsonableError(_("Invalid DSN"))
|
|
|
|
|
|
|
|
assert dsn.hostname
|
|
|
|
project_id = dsn.path.strip("/")
|
|
|
|
url = dsn._replace(netloc=dsn.hostname, path=f"/api/{project_id}/envelope/").geturl()
|
2023-04-21 00:07:13 +02:00
|
|
|
|
|
|
|
# Adjust the payload to explicitly contain the IP address of the
|
|
|
|
# user we see. If left blank, Sentry will assume the IP it
|
|
|
|
# received the request from, which is Zulip's, which can make
|
|
|
|
# debugging more complicated.
|
|
|
|
updated_body = request.body
|
|
|
|
# If we fail to update the body for any reason, leave it as-is; it
|
2023-10-09 20:54:10 +02:00
|
|
|
# is better to misreport the IP than to drop the report entirely.
|
2023-04-21 00:07:13 +02:00
|
|
|
with suppress(Exception):
|
|
|
|
# This parses the Sentry ingestion format, known as an
|
|
|
|
# Envelope. See https://develop.sentry.dev/sdk/envelopes/ for
|
|
|
|
# spec.
|
|
|
|
parts = [envelope_header_line, b"\n"]
|
|
|
|
while envelope_items != b"":
|
|
|
|
item_header_line, rest = envelope_items.split(b"\n", 1)
|
|
|
|
parts.append(item_header_line)
|
|
|
|
parts.append(b"\n")
|
|
|
|
item_header = orjson.loads(item_header_line.decode("utf-8"))
|
|
|
|
length = item_header.get("length")
|
|
|
|
if length is None:
|
2023-09-12 21:10:57 +02:00
|
|
|
item_body, envelope_items = [*rest.split(b"\n", 1), b""][:2]
|
2023-04-21 00:07:13 +02:00
|
|
|
else:
|
|
|
|
item_body, envelope_items = rest[0:length], rest[length:]
|
|
|
|
if item_header.get("type") in ("transaction", "event"):
|
|
|
|
# Event schema:
|
|
|
|
# https://develop.sentry.dev/sdk/event-payloads/#core-interfaces
|
|
|
|
# https://develop.sentry.dev/sdk/event-payloads/user/
|
|
|
|
#
|
|
|
|
# Transaction schema:
|
|
|
|
# https://develop.sentry.dev/sdk/event-payloads/transaction/#anatomy
|
|
|
|
# Note that "Transactions are Events enriched with Span data."
|
|
|
|
payload_data = orjson.loads(item_body)
|
|
|
|
if "user" in payload_data:
|
|
|
|
payload_data["user"]["ip_address"] = request.META.get("REMOTE_ADDR")
|
|
|
|
item_body = orjson.dumps(payload_data)
|
|
|
|
parts.append(item_body)
|
|
|
|
if length is None:
|
|
|
|
parts.append(b"\n")
|
|
|
|
updated_body = b"".join(parts)
|
|
|
|
|
2023-10-11 07:43:49 +02:00
|
|
|
try:
|
2023-10-11 07:44:08 +02:00
|
|
|
sentry_request(url, updated_body)
|
|
|
|
except CircuitBreakerError:
|
|
|
|
logger.warning("Dropped a client exception due to circuit-breaking")
|
2023-10-11 07:43:49 +02:00
|
|
|
except RequestException as e:
|
|
|
|
# This logger has been configured, above, to not report to Sentry
|
|
|
|
logger.exception(e)
|
2023-03-24 16:00:46 +01:00
|
|
|
return HttpResponse(status=200)
|
2023-10-11 07:44:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Circuit-break and temporarily stop trying to report to
|
|
|
|
# Sentry if it keeps timing out. We include ProxyError in
|
|
|
|
# here because we are likely making our requests through
|
|
|
|
# Smokescreen as a CONNECT proxy, so failures from Smokescreen
|
|
|
|
# failing to connect at the TCP level will report as
|
|
|
|
# ProxyErrors.
|
|
|
|
@circuit(
|
|
|
|
failure_threshold=2,
|
|
|
|
recovery_timeout=30,
|
|
|
|
name="Sentry tunnel",
|
|
|
|
expected_exception=(ProxyError, Timeout),
|
|
|
|
)
|
|
|
|
def sentry_request(url: str, data: bytes) -> None:
|
|
|
|
SentryTunnelSession().post(
|
|
|
|
url=url,
|
|
|
|
data=data,
|
|
|
|
headers={"Content-Type": "application/x-sentry-envelope"},
|
|
|
|
).raise_for_status()
|