Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ DATABASE_NAME=baserow

# BASEROW_TOTP_ISSUER_NAME=

# Set SENTRY_DSN to `fake` if you want to test sentry logging without sentry itself
# SENTRY_DSN=
# SENTRY_BACKEND_DSN=

Expand Down
12 changes: 12 additions & 0 deletions backend/src/baserow/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,10 +1439,12 @@ def __setitem__(self, key, value):
if SENTRY_DSN:
import sentry_sdk
import sentry_sdk.integrations as _sentry_integrations
from loguru import logger
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.scrubber import DEFAULT_DENYLIST, EventScrubber

from baserow.core.sentry import (
ConsoleSentryTransport,
drop_expected_asyncio_websocket_ping_timeout_events,
)

Expand All @@ -1457,6 +1459,15 @@ def __setitem__(self, key, value):
]

SENTRY_DENYLIST = DEFAULT_DENYLIST + ["username", "email", "name"]
sentry_transport = None

if SENTRY_DSN == "fake":
logger.info(
"[SENTRY] Using fake backend Sentry DSN, events will be logged to the "
"console."
)
SENTRY_DSN = "https://public@example.invalid/1"
sentry_transport = ConsoleSentryTransport()

sentry_sdk.init(
dsn=SENTRY_DSN,
Expand All @@ -1465,6 +1476,7 @@ def __setitem__(self, key, value):
before_send=drop_expected_asyncio_websocket_ping_timeout_events,
event_scrubber=EventScrubber(recursive=True, denylist=SENTRY_DENYLIST),
environment=os.getenv("SENTRY_ENVIRONMENT", ""),
transport=sentry_transport,
)
else:
BASEROW_LAZY_LOADED_LIBRARIES.append("sentry_sdk")
Expand Down
24 changes: 21 additions & 3 deletions backend/src/baserow/contrib/builder/application_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,24 +586,42 @@ def serialize_for_regression_testing(self, builder: Builder) -> dict:
keyed by page name. Used by snapshot tests to detect element-hierarchy
regressions across template changes.

Children are grouped by slot (place_in_container) and slots are ordered by
the minimum ``order`` value among their members, so the output is stable
regardless of cross-slot insertion history.

:param builder: The builder application instance to serialize.
:return: A dict mapping page names to their element-tree representation.
"""
from collections import defaultdict

from baserow.contrib.builder.elements.handler import ElementHandler
from baserow.contrib.builder.pages.handler import PageHandler

result = {}
for page in PageHandler().get_pages(builder):
elements = list(ElementHandler().get_elements(page))
# Elements are already ordered by (order, id) via Element.Meta.ordering.

by_parent: dict[int | None, list] = {}
for el in elements:
by_parent.setdefault(el.parent_element_id, []).append(el)

def build_tree(parent_id):
children = by_parent.get(parent_id, [])
by_slot: dict[str, list] = defaultdict(list)
for el in children:
by_slot[el.place_in_container or ""].append(el)
ordered = []
for slot in sorted(
by_slot, key=lambda s: min(e.order for e in by_slot[s])
):
ordered.extend(sorted(by_slot[slot], key=lambda e: (e.order, e.id)))
return [
{"type": el.get_type().type, "children": build_tree(el.id)}
for el in by_parent.get(parent_id, [])
{
"type": element.get_type().type,
"children": build_tree(element.id),
}
for element in ordered
]

result[page.name] = build_tree(None)
Expand Down
59 changes: 59 additions & 0 deletions backend/src/baserow/core/sentry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,67 @@
import json
import logging
from typing import Any

from django.contrib.auth import get_user_model

from loguru import logger
from sentry_sdk.transport import Transport

SENTRY_LOG_PREFIX = "[SENTRY]"


def _get_sentry_event_message(event: dict[str, Any]) -> str:
exception_values = event.get("exception", {}).get("values", [])
if exception_values:
exception = exception_values[-1]
exception_type = exception.get("type", "UnknownError")
exception_value = exception.get("value", "")
if exception_value:
return f"{exception_type}: {exception_value}"
return exception_type

log_entry = event.get("logentry", {})
if log_entry.get("formatted"):
return log_entry["formatted"]
if log_entry.get("message"):
return log_entry["message"]
if event.get("message"):
return str(event["message"])

return "Sentry event captured without a message."


def log_sentry_event_to_console(event: dict[str, Any]) -> None:
level = str(event.get("level", "error")).upper()
event_id = event.get("event_id", "unknown")
message = _get_sentry_event_message(event)
logger.error(f"{SENTRY_LOG_PREFIX} [{level}] [{event_id}] {message}")


class ConsoleSentryTransport(Transport):
"""
A transport that logs Sentry events locally instead of sending them to Sentry.
"""

def capture_event(self, event):
log_sentry_event_to_console(event)

def capture_envelope(self, envelope):
for item in envelope.items:
item_type = item.headers.get("type", "unknown")
payload = item.get_bytes().decode("utf-8", errors="replace")

if item_type == "event":
try:
log_sentry_event_to_console(json.loads(payload))
continue
except json.JSONDecodeError:
pass

logger.error(
f"{SENTRY_LOG_PREFIX} [ENVELOPE] [{item_type.upper()}] {payload}"
)


def drop_expected_asyncio_websocket_ping_timeout_events(
event: dict[str, Any], hint: dict[str, Any]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down Expand Up @@ -125,7 +125,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
]),
'type': 'column',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
]),
'type': 'iframe',
}),
dict({
'children': list([
]),
'type': 'text',
}),
dict({
'children': list([
dict({
Expand All @@ -190,11 +195,6 @@
]),
'type': 'form_container',
}),
dict({
'children': list([
]),
'type': 'text',
}),
]),
'type': 'column',
}),
Expand Down Expand Up @@ -429,7 +429,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
dict({
'children': list([
Expand Down Expand Up @@ -459,7 +459,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
]),
'type': 'iframe',
}),
dict({
'children': list([
]),
'type': 'text',
}),
dict({
'children': list([
dict({
Expand All @@ -190,11 +195,6 @@
]),
'type': 'form_container',
}),
dict({
'children': list([
]),
'type': 'text',
}),
]),
'type': 'column',
}),
Expand Down Expand Up @@ -429,7 +429,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
dict({
'children': list([
Expand Down Expand Up @@ -459,7 +459,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
]),
'type': 'iframe',
}),
dict({
'children': list([
]),
'type': 'text',
}),
dict({
'children': list([
dict({
Expand All @@ -190,11 +195,6 @@
]),
'type': 'form_container',
}),
dict({
'children': list([
]),
'type': 'text',
}),
]),
'type': 'column',
}),
Expand Down Expand Up @@ -429,7 +429,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
dict({
'children': list([
Expand Down Expand Up @@ -459,7 +459,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
]),
'type': 'iframe',
}),
dict({
'children': list([
]),
'type': 'text',
}),
dict({
'children': list([
dict({
Expand All @@ -190,11 +195,6 @@
]),
'type': 'form_container',
}),
dict({
'children': list([
]),
'type': 'text',
}),
]),
'type': 'column',
}),
Expand Down Expand Up @@ -429,7 +429,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
dict({
'children': list([
Expand Down Expand Up @@ -459,7 +459,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
]),
'type': 'iframe',
}),
dict({
'children': list([
]),
'type': 'text',
}),
dict({
'children': list([
dict({
Expand All @@ -190,11 +195,6 @@
]),
'type': 'form_container',
}),
dict({
'children': list([
]),
'type': 'text',
}),
]),
'type': 'column',
}),
Expand Down Expand Up @@ -429,7 +429,7 @@
dict({
'children': list([
]),
'type': 'text',
'type': 'link',
}),
dict({
'children': list([
Expand Down Expand Up @@ -459,7 +459,7 @@
dict({
'children': list([
]),
'type': 'link',
'type': 'text',
}),
dict({
'children': list([
Expand Down
Loading
Loading