Skip to content

Commit 7fe4189

Browse files
author
AidanAbd
committed
fix: add span for acquired connection
1 parent 2fcaff6 commit 7fe4189

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

latch_postgres/postgres.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -500,47 +500,48 @@ async def inner(*args: P.args, **kwargs: P.kwargs):
500500
while True:
501501
try:
502502
async with pool.connection() as conn:
503-
assert isinstance(conn, LatchAsyncConnection)
503+
with tracer.start_as_current_span("connection acquired"):
504+
assert isinstance(conn, LatchAsyncConnection)
504505

505-
backoff = CABackoff(
506-
db_config.tx_retries.delay_quant,
507-
db_config.tx_retries.max_wait_time,
508-
)
509-
while True:
510-
try:
511-
res = await f(conn, *args, **kwargs)
512-
# Commit here so we can retry if it fails
513-
# Otherwise the context manager will commit and fail
514-
await conn.commit()
515-
return res
516-
except (
517-
# Class 40 - Transaction Rollback
518-
SerializationFailure,
519-
DeadlockDetected,
520-
) as e:
521-
# Retry with the same connection
522-
delay = backoff.retry()
523-
524-
if delay is None:
525-
raise e
526-
527-
s.add_event(
528-
"transaction retry",
529-
{
530-
"db.retry.count": retries,
531-
"db.retry.accum_retry_time": str(
532-
backoff.acc_wait_time
506+
backoff = CABackoff(
507+
db_config.tx_retries.delay_quant,
508+
db_config.tx_retries.max_wait_time,
509+
)
510+
while True:
511+
try:
512+
res = await f(conn, *args, **kwargs)
513+
# Commit here so we can retry if it fails
514+
# Otherwise the context manager will commit and fail
515+
await conn.commit()
516+
return res
517+
except (
518+
# Class 40 - Transaction Rollback
519+
SerializationFailure,
520+
DeadlockDetected,
521+
) as e:
522+
# Retry with the same connection
523+
delay = backoff.retry()
524+
525+
if delay is None:
526+
raise e
527+
528+
s.add_event(
529+
"transaction retry",
530+
{
531+
"db.retry.count": retries,
532+
"db.retry.accum_retry_time": str(
533+
backoff.acc_wait_time
534+
),
535+
"db.retry.delay": str(delay),
536+
}
537+
| dict_to_attrs(
538+
pg_error_to_dict(e, short=True),
539+
"db.retry.reason",
533540
),
534-
"db.retry.delay": str(delay),
535-
}
536-
| dict_to_attrs(
537-
pg_error_to_dict(e, short=True),
538-
"db.retry.reason",
539-
),
540-
)
541+
)
541542

542-
await conn.rollback()
543-
await asyncio.sleep(delay)
543+
await conn.rollback()
544+
await asyncio.sleep(delay)
544545
except (SerializationFailure, DeadlockDetected):
545546
# todo(maximsmol): should be unnecessary if the list below is precise enough
546547
raise

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "latch-postgres"
3-
version = "0.1.17"
3+
version = "0.1.18"
44
description = "Postges wrapper for latch python backend services"
55
authors = ["Max Smolin <[email protected]>"]
66
license = "CC0 1.0"

0 commit comments

Comments
 (0)