Repository navigation
feat: operation hooks to observe every query, mutation and subscription message (3.10.0) - #87
barucAlmaguer wants to merge 2 commits into
Conversation
…iption messages Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…message (3.10.0) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Overall Assessment
Operation hooks wrap execute/async_execute and each subscription payload, and the observe-only contract holds for the paths the tests cover. Two implementation bugs break that contract under retry and under concurrent async calls.
Findings
| # | Severity | File | Issue |
|---|---|---|---|
| 1 | High | pygqlc/hooks.py | _Outcome.response is a class attribute, so concurrent operations share one response and a retry can report the wrong kind. |
| 2 | Medium | pygqlc/hooks.py | The hook registry has no lock; add/remove during notify can skip a hook or raise. |
Notes
- The no-hooks fast path is real:
observed_operationandnotify_subscription_messagereturn before timing or regex work when_hooksis empty. The resend stays inside the context manager, so a successful retry is one event. notifycatchesExceptionand re-raises the operation'sBaseException. A hook cannot swallowGQLResponseException.query/mutatestill return(data, errors)as before.- Not flagged: connection-init and error subscription frames are not reported. The PR says "once per subscription message" and the call sits only on the payload path. Callers that want those frames need a different hook point; the payload path matches the test.
- Not flagged:
operation_ofcan matchquery/mutation/subscriptioninside a string or comment. That only mislabels the event; the POST body is unchanged. - Tests cover name selection, a 500, a raising hook, removal, and one subscription payload. They do not cover a retry or two overlapping async operations, which is where finding 1 shows up.
from pygqlc import hooksworks as a submodule import even thoughhooksis not re-exported in__init__.py. That matches the README.
🤖 PR Reviewer · Grok 4.7 (xAI)
|
|
||
|
|
||
| class _Outcome: | ||
| response: Optional[dict] = None |
There was a problem hiding this comment.
_Outcome.response is a class attribute, not an instance attribute. Every observed_operation writes the same slot.
Two failures follow:
- Concurrent
async_executecalls (the Temporal worker case this feature is for) share that slot. Operation A can finish, then operation B assignsoutcome.responsebefore A'sfinallybuilds the event. A's hook then sees B's response, orNoneif B already cleared it. - On a resend,
except BaseExceptionsetserrorand re-raises into thefinally, which notifies witherrorset. The retry then succeeds and assignsoutcome.response, but nothing setserror = None. The success event still carries the first attempt's exception, soevent.error or "ok"(the README example) reports a failure for a call that returned data.
Move response onto the instance (self.response = None in __init__), and clear error when the body returns normally:
class _Outcome:
def __init__(self):
self.response = None
try:
yield outcome
error = None
except BaseException as raised:
error = raised
raise|
|
||
|
|
||
| OperationHook = Callable[[OperationEvent], None] | ||
| _hooks: list = [] |
There was a problem hiding this comment.
_hooks is a process-wide list mutated by add_operation_hook / remove_operation_hook with no lock. notify snapshots with list(_hooks) and then each hook runs.
list.append and list.remove are not safe against another thread reading the list. A worker thread that registers or removes a hook while an operation thread is inside list(_hooks) or hook in _hooks can raise RuntimeError from the list resize, or skip a hook for that event.
Valiot workers register this around forked children and activity threads, which is exactly that overlap. A lock around add, remove, and the snapshot (same pattern as a process-wide registry) keeps notify from observing a half-updated list. The hook call itself should stay outside the lock so a slow hook does not block registration.
|
Superseded by #88: same commits on |
Description
Consumers that want to trace or measure their GraphQL traffic today have to wrap the client or monkeypatch it. Valiot's workers need exactly that: put every call a Temporal activity makes on a timeline, next to the screen that triggered the work. This adds a small, generic way to observe operations, with no change for anyone who doesn't use it.
pygqlc.hooks.add_operation_hook(fn)/remove_operation_hook(fn).fnreceives anOperationEvent(kind, name, document, variables, environment,started_at,duration,responseorerror):execute/async_executefinish. The resend from 3.9.2 counts as the same operation;query/mutatereturn the same thing as before (there is a test for both).set_logger: it survives resetting theGraphQLClientsingleton, which Valiot workers do in forked children.execute/async_executekeep their signatures. The POST moved to private_post_operation/_async_post_operationso the hook can wrap the whole operation, resend included.Not included (nothing needs it today): a middleware that modifies the request (headers, propagating
traceparent). It can be added the day there is a consumer.Version 3.10.0 (minor), with its CHANGELOG entry and a README section.
Type of change
How Has This Been Tested?
Tests first (
b6a5442, red becausepygqlc.hooksdid not exist:ImportError: cannot import name 'hooks' from 'pygqlc'). Hermetic, followingtest_operation_name.py: a real local HTTP server and the subscription loop fed the way the router feeds it.kind="query",name="Plans", environment,duration > 0, raw responseanonymousGQLResponseException,mutatestill returns(None, errors)remove_operation_hookkind="subscription", its variables and the message payloadAfter the red commit, one adjustment to the test itself: its subscription fixture lacked the
runskey that the realsubscribe()creates, and it compared the callback's flattened shape. Now it only checks that the callback still receives its message.The 25 errors are the tests that need a live backend (
API/WSS/TOKEN); they fail the same way onmainin this environment.uv.lockdoesn't change: the PR adds no dependencies (uv lock --upgradehere only re-marked dependencies because of the local uv version).On a real worker: valiot/python-tooling#272 uses this hook, and valiot/prolamsa#25 is the real run: "Resolver Empalmes" in prolamsa, 3 runs, with each of the worker's 69 GraphQL calls on the same timeline as the click, attributed to its activity. The hook also carried the error of the call that failed (
upsertPlans, complexity). The API held up as designed. Draft until it's reviewed and published.Checklist:
🤖 Generated with Claude Code