fix: cover the BSON types the mongo span hook dropped - #33
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
response_hookserializes every command reply into a span attribute, but_parse_mongo_typeswas missing several BSON types. Each miss raises inside the listener — pymongo swallows that, so queries are unaffected, but the span losesdb.mongodb.server_replyand a traceback is logged.I enumerated every BSON type against orjson to find the real gaps rather than fixing only the one that surfaced:
RawBSONDocumentdict(obj)decimal.Decimalstr(obj)Regex{"$regex", "$options"}MinKey/MaxKey{"$minKey": 1}/{"$maxKey": 1}Code,Int64,datetime,UUIDRawBSONDocument
What write replies actually arrive as. On
services/aistaging this was 284 lines in 25 minutes, one per upsert — every failing payload carriedupdatedExisting. 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 → Decimaldecoder on the client, so replies echoing a stored decimal now surface asdecimal.Decimal, which orjson cannot serialize and this hook had no branch for. Reproduced against a real Mongo: insertDecimal128,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/financemost of all, wherePydanticDecimal128is 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:
Binaryprecedesbytes, which it subclasses.0.5.2→0.5.4, 111 tests pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01Tyg1MP35Bn36f9nyBfsfei