Skip to content

fix: cover the BSON types the mongo span hook dropped - #33

Merged
aradng merged 2 commits into
mainfrom
fix/serialize-raw-bson-replies
Aug 13, 2026
Merged

fix: cover the BSON types the mongo span hook dropped#33
aradng merged 2 commits into
mainfrom
fix/serialize-raw-bson-replies

Conversation

@aradng

@aradng aradng commented Aug 13, 2026

Copy link
Copy Markdown
Owner

response_hook serializes every command reply into a span attribute, but _parse_mongo_types was missing several BSON types. Each miss raises inside the listener — pymongo swallows that, so queries are unaffected, but the span loses db.mongodb.server_reply and a traceback is logged.

I enumerated every BSON type against orjson to find the real gaps rather than fixing only the one that surfaced:

type before now
RawBSONDocument ✗ raised dict(obj)
decimal.Decimal ✗ raised str(obj)
Regex ✗ raised {"$regex", "$options"}
MinKey / MaxKey ✗ raised {"$minKey": 1} / {"$maxKey": 1}
Code, Int64, datetime, UUID orjson native unchanged

RawBSONDocument

What write replies actually arrive as. On services/ai staging this was 284 lines in 25 minutes, one per upsert — every failing payload carried updatedExisting. The first test reproduces it byte-for-byte:

RawBSONDocument(b'\x1e\x00\x00\x00\x10n\x00\x01\x00\x00\x00\x08updatedExisting\x00\x01\x00')

Decimal — a regression from #32

#32 registered a Decimal128 → Decimal decoder on the client, so replies echoing a stored decimal now surface as decimal.Decimal, which orjson cannot serialize and this hook had no branch for. Reproduced against a real Mongo: insert Decimal128, find_one, then serialize the captured reply — Type is not JSON serializable: decimal.Decimal. It passes now, with the same 5 replies checked and zero failures.

Anything on 0.5.2 storing decimals is affected — bluprint/finance most of all, where PydanticDecimal128 is used across wallets, invoices and commissions.

Dispatch table

Adding four branches pushed the if-chain past ruff's complexity limit (14 > 10), so it is a table now. Order is preserved where it matters: Binary precedes bytes, which it subclasses.

0.5.20.5.4, 111 tests pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Tyg1MP35Bn36f9nyBfsfei

aradng added 2 commits August 13, 2026 18:56
Write replies arrive as RawBSONDocument, which the span serializer had no
branch for, so every upsert logged a TypeError and lost its attribute.
Decimal is the urgent one: 0.5.2 decodes Decimal128 into Decimal on read,
so every reply echoing a stored decimal started failing to serialize.
Regex, MinKey and MaxKey show up in explain and index metadata.

The if-chain tripped the complexity limit once extended, so it is a
dispatch table now — Binary still precedes bytes, which it subclasses.
@aradng aradng changed the title fix: serialize raw BSON replies in the mongo span hook fix: cover the BSON types the mongo span hook dropped Aug 13, 2026
@aradng
aradng merged commit ec7277c into main Aug 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant