Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Run the two reads behind every delivery on asyncpg directly (CHOO-3667)

Abel Dantas6a3f051ee521
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 512bca153..c9c304786 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -142,6 +142,13 @@ version of their own to them without also giving them a release of their own.
compares it only when someone signs in.
#### Changed
+- **The two reads behind every delivery run on asyncpg directly.** A
+ stream reads an agent's events, and counts its unread messages, for every
+ event it delivers, and under load SQLAlchemy's work around each statement
+ cost more than Postgres took to run it. `db/raw.py` runs a statement
+ SQLAlchemy compiled once on the session's own asyncpg connection, in its
+ transaction and under its tenant, with SQLAlchemy's type conversions and
+ timed into `db.query.duration` as every other statement is.
- **Pods start in seconds again with many rooms.** Every process checks at
startup that each room has its clients, under a lock that lets one
process at a time through, and read each room on its own to do it. With
diff --git a/core/switch_core/bridges/agent/protocol/event_buffer.py b/core/switch_core/bridges/agent/protocol/event_buffer.py
index d2620e661..e722e076f 100644
--- a/core/switch_core/bridges/agent/protocol/event_buffer.py
+++ b/core/switch_core/bridges/agent/protocol/event_buffer.py
@@ -64,6 +64,8 @@ from sqlalchemy import (
BigInteger,
Boolean,
Text,
+ all_,
+ any_,
bindparam,
delete,
func,
@@ -106,6 +108,7 @@ from switch_core.db.models import (
PendingAttachmentGroup,
Room,
)
+from switch_core.db.raw import RawStatement
from switch_core.db.session_scope import (
holds_shared_session,
shared_tenant_session,
@@ -412,6 +415,9 @@ _UNREAD_IN_ROOM_SQL = text(
LEFT JOIN base b ON true
"""
).bindparams(bindparam("throughs", type_=ARRAY(BigInteger)))
+# Run by asyncpg directly (`db/raw.py`): a stream counts an agent's unread
+# messages for every event it delivers.
+_UNREAD_IN_ROOM = RawStatement(_UNREAD_IN_ROOM_SQL)
# Locks the rows in `agent_event_heads` of the agents named by `:agent_ids`
@@ -989,7 +995,7 @@ class EventBuffer:
if limit is not None:
parameters["limit"] = limit
async with tenant_session(self._session_factory, tenant_id) as session:
- found = (await session.execute(query, parameters)).all()
+ found = await query.all(session, parameters)
# `expire` deletes rows and records what it deleted in one
# transaction, and the rooms it dropped after `after_seq` are read in
# the statement that reads the events, so the two are one snapshot: a
@@ -999,12 +1005,11 @@ class EventBuffer:
# then, and one landing after it is reported too: a spare gap, never a
# silent one.
if found:
- dropped = found[0][1]
+ dropped = found[0].dropped
if dropped:
await self._expired(tenant_id, agent_id, after_seq, dropped)
else:
await self.check_cursor(tenant_id, agent_id, after_seq)
- rows = [row for row, _ in found]
out = [
BufferedEvent(
seq=row.seq,
@@ -1015,7 +1020,7 @@ class EventBuffer:
room_seq=row.room_seq,
at=_epoch(row.created_at),
)
- for row in rows
+ for row in found
]
self._record_scan(_READ, len(out), started)
return out
@@ -1975,18 +1980,16 @@ class EventBuffer:
for room_id, room_throughs in wanted.items():
throughs = sorted(room_throughs)
started = time.perf_counter()
- rows = (
- await session.execute(
- _UNREAD_IN_ROOM_SQL,
- {
- "room_id": room_id,
- "agent_id": agent_id,
- "cutoff": cutoff,
- "through": throughs[-1],
- "throughs": throughs,
- },
- )
- ).all()
+ rows = await _UNREAD_IN_ROOM.all(
+ session,
+ {
+ "room_id": room_id,
+ "agent_id": agent_id,
+ "cutoff": cutoff,
+ "through": throughs[-1],
+ "throughs": throughs,
+ },
+ )
caught_up = rows[0][0] if rows else None
if caught_up is None:
for through_seq in throughs:
@@ -2524,14 +2527,15 @@ _ROOMS_DROPPED_AFTER = select(AgentEventDrop.room_id).where(
@functools.cache
def _own_events_read(
*, rooms: bool, notifiable_only: bool, through: bool, limit: bool
-) -> Any:
+) -> RawStatement:
"""The statement `EventBuffer.read_from` runs for one shape of read, built
- once per shape (there are sixteen) and run with bind parameters: the
- agent's own events after `:after_seq`, oldest first, each with the rooms
- in which `expire` deleted own events of the agent numbered after
- `:after_seq` (NULL for none), so the two are read in one statement. A
- stream reads an agent's events for every event it delivers, and building
- the statement each time cost more than running it.
+ once per shape (there are sixteen) and run with bind parameters by asyncpg
+ directly (`db/raw.py`): the agent's own events after `:after_seq`, oldest
+ first, each with the rooms in which `expire` deleted own events of the
+ agent numbered after `:after_seq` (NULL for none), so the two are read in
+ one statement. A stream reads an agent's events for every event it
+ delivers, and building the statement each time, or running it through
+ SQLAlchemy's execution, cost more than Postgres takes to run it.
Optional parts, by flag: only the rooms in `:rooms`, only notifiable
events, none numbered after `:through_seq`, at most `:limit` of them."""
@@ -2542,19 +2546,30 @@ def _own_events_read(
AgentEventDrop.dropped_through > bindparam("after_seq"),
)
.scalar_subquery()
+ .label("dropped")
)
query = (
- select(AgentEventRow, dropped)
+ select(
+ AgentEventRow.seq,
+ AgentEventRow.room_id,
+ AgentEventRow.room_seq,
+ AgentEventRow.payload,
+ AgentEventRow.notifiable,
+ AgentEventRow.created_at,
+ dropped,
+ )
.where(
AgentEventRow.agent_id == bindparam("agent_id"),
AgentEventRow.seq > bindparam("after_seq"),
- AgentEventRow.type.not_in(RETIRED_EVENT_TYPES),
+ # `NOT IN`, in the form that compiles once.
+ AgentEventRow.type
+ != all_(literal(sorted(RETIRED_EVENT_TYPES), ARRAY(Text))),
)
.order_by(AgentEventRow.seq)
)
if rooms:
query = query.where(
- AgentEventRow.room_id.in_(bindparam("rooms", expanding=True))
+ AgentEventRow.room_id == any_(bindparam("rooms", type_=ARRAY(Text)))
)
if notifiable_only:
query = query.where(AgentEventRow.notifiable.is_(True))
@@ -2562,7 +2577,7 @@ def _own_events_read(
query = query.where(AgentEventRow.seq <= bindparam("through_seq"))
if limit:
query = query.limit(bindparam("limit"))
- return query
+ return RawStatement(query)
def _room_head(room_id: str) -> Any:
diff --git a/core/switch_core/db/raw.py b/core/switch_core/db/raw.py
new file mode 100644
index 000000000..38e01a866
--- /dev/null
+++ b/core/switch_core/db/raw.py
@@ -0,0 +1,155 @@
+"""Statements run by asyncpg directly, for the reads every message costs.
+
+Profiled under load, SQLAlchemy is about half of what switch-core spends its
+CPU on, and most of that is per statement rather than per row: the execution
+context, its events, the cursor adapter, the result and its rows. For a
+statement every delivered message runs, that costs more than Postgres takes to
+run it.
+
+A `RawStatement` is still written in SQLAlchemy and compiled by it, once. It is
+then run by asyncpg on the session's own connection, in the session's
+transaction, so the tenant that transaction was opened with and row-level
+security apply to it as to any other statement. What SQLAlchemy would do with
+its parameters and columns is done here too: each goes through its type's
+processor for the dialect, the connection's codecs are the ones SQLAlchemy set
+up, the tenant binding is checked against the transaction's
+(`refuse_drift`), and the statement is timed into `db.query.duration` as
+`observability/query.py` times the others.
+
+What is different: a row is a named tuple of the selected columns, not an ORM
+object, and nothing is added to the session. A statement whose parameters
+expand when it runs (`in_` over a list) cannot be compiled once, so it is
+refused; `= any(:array)` is the form that can.
+"""
+
+from __future__ import annotations
+
+import time
+from collections import namedtuple
+from collections.abc import Callable, Iterable, Mapping
+from dataclasses import dataclass
+from typing import Any, cast
+
+from sqlalchemy.engine import Dialect
+from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.sql import ClauseElement
+from sqlalchemy.sql.compiler import SQLCompiler
+from sqlalchemy.sql.selectable import Select
+
+from switch_core.db.tenant_session import refuse_drift
+from switch_core.observability.catalogue import DB_QUERY_DURATION
+from switch_core.observability.metrics import metrics
+from switch_core.observability.query import query_operation
+from switch_core.tenant_context import current_tenant_id
+
+_Processor = Callable[[Any], Any]
+
+
+@dataclass(frozen=True)
+class _Compiled:
+ sql: str
+ operation: str
+ # The parameters in the order the statement numbers them, each with its
+ # type's bind processor, whether a value must be passed for it, and the
+ # value it was given when built otherwise.
+ parameters: tuple[tuple[str, _Processor | None, bool, Any], ...]
+ # One per selected column, None for a statement that declares none.
+ columns: tuple[_Processor | None, ...] | None
+ row: Callable[[Iterable[Any]], tuple[Any, ...]] | None
+
+
+class RawStatement:
+ def __init__(self, statement: ClauseElement) -> None:
+ self._statement = statement
+ self._compiled: _Compiled | None = None
+
+ async def all(
+ self, session: AsyncSession, parameters: Mapping[str, Any]
+ ) -> list[Any]:
+ """Every row the statement returns: a named tuple per row for a
+ `select`, asyncpg's own record for a statement that names no columns."""
+ connection = await session.connection()
+ # The transaction was begun, and its tenant set, with the tenant bound
+ # now; a raw statement is never run with none.
+ refuse_drift(session.sync_session)
+ if current_tenant_id() is None:
+ raise RuntimeError("a raw statement runs with a tenant bound")
+ compiled = self._compiled or self._compile(connection.dialect)
+ arguments = []
+ for name, process, required, default in compiled.parameters:
+ if name in parameters:
+ value = parameters[name]
+ elif required:
+ raise KeyError(f"no value for the statement's parameter {name!r}")
+ else:
+ value = default
+ arguments.append(value if process is None else process(value))
+ driver: Any = (await connection.get_raw_connection()).driver_connection
+ if driver is None or not driver.is_in_transaction():
+ raise RuntimeError("a raw statement runs in its session's transaction")
+ started = time.perf_counter()
+ records = await driver.fetch(compiled.sql, *arguments)
+ metrics().observe(
+ DB_QUERY_DURATION,
+ {"operation": compiled.operation},
+ (time.perf_counter() - started) * 1000.0,
+ )
+ if compiled.row is None or compiled.columns is None:
+ return list(records)
+ make = compiled.row
+ columns = compiled.columns
+ if not any(columns):
+ return [make(record) for record in records]
+ return [
+ make(
+ process(value) if process is not None else value
+ for process, value in zip(columns, record, strict=True)
+ )
+ for record in records
+ ]
+
+ def _compile(self, dialect: Dialect) -> _Compiled:
+ compiled = cast(SQLCompiler, self._statement.compile(dialect=dialect))
+ for bind in compiled.binds.values():
+ if bind.expanding:
+ raise ValueError(
+ f"{bind.key!r} expands when the statement runs, so it cannot "
+ "be compiled once; compare with `= any(:array)` instead"
+ )
+ parameters = tuple(
+ (
+ name,
+ compiled.binds[name].type.dialect_impl(dialect).bind_processor(dialect),
+ compiled.binds[name].required,
+ compiled.binds[name].value,
+ )
+ for name in compiled.positiontup or ()
+ )
+ columns: tuple[_Processor | None, ...] | None = None
+ row: Callable[[Iterable[Any]], tuple[Any, ...]] | None = None
+ if isinstance(self._statement, Select):
+ selected = list(self._statement.selected_columns)
+ columns = tuple(
+ column.type.dialect_impl(dialect).result_processor(dialect, None)
+ for column in selected
+ )
+ names = [column.key for column in selected]
+ if None in names:
+ raise ValueError("every column a raw statement selects is named")
+ row = _row_type([name for name in names if name is not None])
+ sql = str(compiled)
+ self._compiled = _Compiled(
+ sql=sql,
+ operation=query_operation(sql),
+ parameters=parameters,
+ columns=columns,
+ row=row,
+ )
+ return self._compiled
+
+
+def _row_type(names: list[str]) -> Callable[[Iterable[Any]], tuple[Any, ...]]:
+ """What makes a row of the selected columns, read by name or by position
+ as SQLAlchemy's own rows are."""
+ row = namedtuple("Row", names) # type: ignore[misc]
+ return row._make # type: ignore[no-any-return]
diff --git a/core/switch_core/db/tenant_session.py b/core/switch_core/db/tenant_session.py
index cb41dad8e..cdc7c65e9 100644
--- a/core/switch_core/db/tenant_session.py
+++ b/core/switch_core/db/tenant_session.py
@@ -185,7 +185,7 @@ def _refuse_a_binding_that_came_too_late(state: ORMExecuteState) -> None:
nothing to compare and nothing to refuse. From the second onwards the two
must agree.
"""
- _refuse_drift(state.session)
+ refuse_drift(state.session)
def _note_that_a_statement_was_issued(state: ORMExecuteState) -> None:
@@ -197,10 +197,10 @@ def _refuse_a_flush_that_drifted(
session: Session, flush_context: object, instances: object
) -> None:
"""The same check, on the path `do_orm_execute` does not see."""
- _refuse_drift(session)
+ refuse_drift(session)
-def _refuse_drift(session: Session) -> None:
+def refuse_drift(session: Session) -> None:
if _STAMPED not in session.info:
return
stamped = session.info[_STAMPED]
@@ -326,7 +326,7 @@ class TenantCheckedSession(Session):
"""
def get(self, *args: Any, **kwargs: Any) -> Any:
- _refuse_drift(self)
+ refuse_drift(self)
self.info[_ISSUED_A_STATEMENT] = False
instance = super().get(*args, **kwargs)
if instance is not None and not self.info[_ISSUED_A_STATEMENT]:
@@ -334,7 +334,7 @@ class TenantCheckedSession(Session):
return instance
def get_one(self, *args: Any, **kwargs: Any) -> Any:
- _refuse_drift(self)
+ refuse_drift(self)
self.info[_ISSUED_A_STATEMENT] = False
instance = super().get_one(*args, **kwargs)
if not self.info[_ISSUED_A_STATEMENT]:
diff --git a/core/switch_core/observability/query.py b/core/switch_core/observability/query.py
index 28cbe8f65..444375cb1 100644
--- a/core/switch_core/observability/query.py
+++ b/core/switch_core/observability/query.py
@@ -49,7 +49,7 @@ _TENANT_BIND = "tenant_bind"
_TIMER_KEY = "_switch_query_started"
-def _operation(statement: str) -> str:
+def query_operation(statement: str) -> str:
"""The leading keyword, or `other`.
Deliberately crude. A statement whose first word is not one of the four is
@@ -104,7 +104,7 @@ def _after(
return
elapsed_ms = (time.perf_counter() - started) * 1000.0
metrics().observe(
- DB_QUERY_DURATION, {"operation": _operation(statement)}, elapsed_ms
+ DB_QUERY_DURATION, {"operation": query_operation(statement)}, elapsed_ms
)
diff --git a/core/tests/switch_core/db/test_raw.py b/core/tests/switch_core/db/test_raw.py
new file mode 100644
index 000000000..8c5d6b923
--- /dev/null
+++ b/core/tests/switch_core/db/test_raw.py
@@ -0,0 +1,162 @@
+"""`RawStatement` (`db/raw.py`): a statement compiled by SQLAlchemy once and run
+by asyncpg on the session's own connection.
+
+What has to hold for it to stand in for `session.execute`: it runs in the
+session's transaction, under its tenant, with row-level security applying; its
+parameters and columns are converted as SQLAlchemy converts them; it is timed
+as every other statement is; and what it cannot do the same way is refused
+rather than done differently.
+"""
+
+from __future__ import annotations
+
+import uuid
+from datetime import datetime
+
+import pytest
+from sqlalchemy import Boolean, Integer, bindparam, func, literal, select
+from sqlalchemy.dialects.postgresql import JSONB
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.models import TENANT_ZERO_ID, RoomGroup, Tenant
+from switch_core.db.raw import RawStatement
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.room_group_store import RoomGroupStore
+from switch_core.db.tenant_session import TenantBindingDriftError
+from switch_core.observability.catalogue import DB_QUERY_DURATION
+from switch_core.observability.metrics import MetricsRegistry, install, uninstall
+from switch_core.tenant_context import tenant_scope
+from tests.conftest import RLSHarness
+
+TENANT = TENANT_ZERO_ID
+
+_GROUP_BY_ID = RawStatement(
+ select(RoomGroup.id, RoomGroup.tenant_id, RoomGroup.name).where(
+ RoomGroup.id == bindparam("id")
+ )
+)
+_GROUPS = RawStatement(select(RoomGroup.id, RoomGroup.tenant_id))
+
+
+async def _make_tenant(owner: async_sessionmaker[AsyncSession], tenant_id: str) -> None:
+ async with owner() as session:
+ session.add(Tenant(id=tenant_id, slug=tenant_id, name=tenant_id))
+ await session.commit()
+
+
+async def _group(session: AsyncSession, name: str) -> str:
+ group = await RoomGroupStore().create(
+ session, name=name, description=None, color=None, parent_group_id=None
+ )
+ return group.id
+
+
+async def test_it_reads_what_its_transaction_wrote(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ async with tenant_session(session_factory, TENANT) as session:
+ group_id = await _group(session, "uncommitted")
+ rows = await _GROUP_BY_ID.all(session, {"id": group_id})
+ await session.rollback()
+
+ assert [(row.id, row.tenant_id, row.name) for row in rows] == [
+ (group_id, TENANT, "uncommitted")
+ ]
+ # Read by position as well as by name, as SQLAlchemy's rows are.
+ assert rows[0][2] == "uncommitted"
+
+
+async def test_row_level_security_applies(rls_harness: RLSHarness) -> None:
+ tenant_a, tenant_b = (f"tenant-{uuid.uuid4().hex[:8]}" for _ in range(2))
+ for tenant_id in (tenant_a, tenant_b):
+ await _make_tenant(rls_harness.owner, tenant_id)
+ async with tenant_session(rls_harness.restricted, tenant_id) as session:
+ await _group(session, tenant_id)
+ await session.commit()
+
+ async with tenant_session(rls_harness.restricted, tenant_a) as session:
+ rows = await _GROUPS.all(session, {})
+
+ assert {row.tenant_id for row in rows} == {tenant_a}
+
+
+async def test_parameters_and_columns_are_converted_as_sqlalchemy_converts_them(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ statement = select(
+ bindparam("document", type_=JSONB).label("document"),
+ literal({"nested": [1, "two", None]}, JSONB).label("constant"),
+ bindparam("flag", type_=Boolean).label("flag"),
+ func.now().label("at"),
+ literal(7, Integer).label("seven"),
+ )
+ values = {"document": {"a": [1, 2], "b": {"c": True}}, "flag": True}
+
+ async with tenant_session(session_factory, TENANT) as session:
+ raw = await RawStatement(statement).all(session, values)
+ through_sqlalchemy = (await session.execute(statement, values)).all()
+
+ assert [tuple(row) for row in raw] == [tuple(row) for row in through_sqlalchemy]
+ assert raw[0].document == {"a": [1, 2], "b": {"c": True}}
+ assert isinstance(raw[0].at, datetime)
+ assert raw[0].at.tzinfo is not None
+
+
+async def test_it_is_timed_as_every_statement_is(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ registry = MetricsRegistry()
+ install(registry)
+ try:
+ async with tenant_session(session_factory, TENANT) as session:
+ await _GROUPS.all(session, {})
+ finally:
+ uninstall()
+
+ timed = {
+ point.attributes["operation"]: point.count
+ for payload in registry.collect()
+ if payload.name == DB_QUERY_DURATION.name
+ for point in payload.histograms
+ }
+ assert timed.get("select") == 1
+
+
[email protected]_ambient_tenant
+async def test_it_refuses_to_run_with_no_tenant_bound(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ async with session_factory() as session:
+ with pytest.raises(RuntimeError, match="tenant bound"):
+ await _GROUPS.all(session, {})
+
+
+async def test_it_refuses_a_tenant_bound_after_the_transaction_began(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ async with tenant_session(session_factory, TENANT) as session:
+ await _GROUPS.all(session, {})
+ with tenant_scope("another-tenant"), pytest.raises(TenantBindingDriftError):
+ await _GROUPS.all(session, {})
+
+
+async def test_it_refuses_a_missing_parameter(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ async with tenant_session(session_factory, TENANT) as session:
+ with pytest.raises(KeyError, match="'id'"):
+ await _GROUP_BY_ID.all(session, {})
+
+
+async def test_it_refuses_a_statement_that_cannot_be_compiled_once(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ expanding = RawStatement(
+ select(RoomGroup.id).where(RoomGroup.id.in_(bindparam("ids", expanding=True)))
+ )
+ unnamed = RawStatement(select(func.count()))
+ async with tenant_session(session_factory, TENANT) as session:
+ with pytest.raises(ValueError, match="any"):
+ await expanding.all(session, {"ids": ["x"]})
+ with pytest.raises(ValueError, match="named"):
+ await unnamed.all(session, {})
diff --git a/core/tests/switch_core/management/test_stream.py b/core/tests/switch_core/management/test_stream.py
index 010e11ac0..0625d528f 100644
--- a/core/tests/switch_core/management/test_stream.py
+++ b/core/tests/switch_core/management/test_stream.py
@@ -13,7 +13,6 @@ import time
from typing import Any
import pytest
-from sqlalchemy import event
from sqlalchemy.exc import TimeoutError as PoolTimeoutError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
@@ -42,6 +41,9 @@ from switch_core.management.notifier import (
OPERATION_PENDING,
ControllerNotifier,
)
+from switch_core.observability.catalogue import DB_QUERY_DURATION
+from switch_core.observability.metrics import MetricsRegistry, install, uninstall
+from switch_core.observability.query import instrument_queries
from tests.db_buffer import age_events, say
from tests.session_counting import CountingSessionFactory
from tests.switch_core.management.harness import (
@@ -1112,22 +1114,27 @@ async def test_a_delivery_told_its_head_costs_three_statements(
delivered = asyncio.ensure_future(take(stream, 1))
await asyncio.sleep(0.2)
await _ask(harness, agent_id, room_id, "@you")
- statements: list[str] = []
- engine = buffer._session_factory.kw["bind"].sync_engine
-
- def record(_conn: object, _cursor: object, statement: str, *_args: object) -> None:
- statements.append(statement)
-
- event.listen(engine, "before_cursor_execute", record)
+ # Counted as the query timing counts them, which sees the statements run
+ # on asyncpg directly (`db/raw.py`) as well as those run through SQLAlchemy.
+ instrument_queries(buffer._session_factory.kw["bind"])
+ registry = MetricsRegistry()
+ install(registry)
try:
[(name, data)] = await delivered
finally:
- event.remove(engine, "before_cursor_execute", record)
+ uninstall()
await stream.aclose()
assert name == "agent.event"
assert data["event"]["missed"] == {"count": 0, "reason": None}
- assert len(statements) == 3, statements
+ timed = {
+ point.attributes["operation"]: point.count
+ for payload in registry.collect()
+ if payload.name == DB_QUERY_DURATION.name
+ for point in payload.histograms
+ }
+ # The unread count opens with `WITH`, which the timing labels `other`.
+ assert timed == {"tenant_bind": 1, "select": 1, "other": 1}
async def test_a_wait_watches_one_bell_whatever_the_number_of_agents(
diff --git a/core/tests/switch_core/observability/test_query_timing.py b/core/tests/switch_core/observability/test_query_timing.py
index 81f3b241a..5296e68ec 100644
--- a/core/tests/switch_core/observability/test_query_timing.py
+++ b/core/tests/switch_core/observability/test_query_timing.py
@@ -20,7 +20,7 @@ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from switch_core.observability.catalogue import DB_QUERY_DURATION
from switch_core.observability.metrics import MetricsRegistry, install, uninstall
-from switch_core.observability.query import _operation, instrument_queries
+from switch_core.observability.query import instrument_queries, query_operation
@pytest.fixture
@@ -66,7 +66,7 @@ class TestTheOperationLabel:
def test_it_is_the_leading_keyword_or_other(
self, statement: str, expected: str
) -> None:
- assert _operation(statement) == expected
+ assert query_operation(statement) == expected
def test_a_statement_never_becomes_the_label(self) -> None:
"""The whole point. A room id or an email in a literal would otherwise
@@ -74,12 +74,12 @@ class TestTheOperationLabel:
not entitled to that tenant's rows."""
statement = "SELECT * FROM users WHERE email = '[email protected]'"
- assert _operation(statement) == "select"
- assert "example.test" not in _operation(statement)
+ assert query_operation(statement) == "select"
+ assert "example.test" not in query_operation(statement)
def test_every_label_it_can_produce_is_a_small_fixed_set(self) -> None:
produced = {
- _operation(s)
+ query_operation(s)
for s in [
"SELECT",
"INSERT",