Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATASETTE_TRACE_PLUGINS for tracing Pluggy calls #2274

Closed
simonw opened this issue Feb 16, 2024 · 3 comments
Closed

DATASETTE_TRACE_PLUGINS for tracing Pluggy calls #2274

simonw opened this issue Feb 16, 2024 · 3 comments

Comments

@simonw
Copy link
Owner

simonw commented Feb 16, 2024

Pluggy has a debugging mechanism that can be used to output information about plugin calls: https://pluggy.readthedocs.io/en/stable/api_reference.html#pluggy.PluginManager.add_hookcall_monitoring

I tried this just now and it's helping debug a plugins permissions issue. It should be a feature.

@simonw
Copy link
Owner Author

simonw commented Feb 16, 2024

Prototype:

diff --git a/datasette/plugins.py b/datasette/plugins.py
index f7a1905f..d7f73e7d 100644
--- a/datasette/plugins.py
+++ b/datasette/plugins.py
@@ -1,6 +1,7 @@
 import importlib
 import os
 import pluggy
+from pprint import pprint
 import sys
 from . import hookspecs
 
@@ -33,6 +34,29 @@ DEFAULT_PLUGINS = (
 pm = pluggy.PluginManager("datasette")
 pm.add_hookspecs(hookspecs)
 
+DATASETTE_PLUGGY_TRACING = os.environ.get("DATASETTE_PLUGGY_TRACING", None)
+
+
+def before(hook_name, hook_impls, kwargs):
+    print()
+    print(f"{hook_name}:")
+    pprint(kwargs, width=40, indent=4)
+    print("Hook implementations:")
+    pprint(hook_impls, width=40, indent=4)
+
+
+def after(outcome, hook_name, hook_impls, kwargs):
+    results = outcome.get_result()
+    if not isinstance(results, list):
+        results = [results]
+    print(f"Results:")
+    pprint(results, width=40, indent=4)
+
+
+if DATASETTE_PLUGGY_TRACING:
+    pm.add_hookcall_monitoring(before, after)
+
+
 DATASETTE_LOAD_PLUGINS = os.environ.get("DATASETTE_LOAD_PLUGINS", None)
 
 if not hasattr(sys, "_called_from_test") and DATASETTE_LOAD_PLUGINS is None:

Produces output like this:

actor_from_request:
{   'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'request': <asgi.Request method="GET" url="http://127.0.0.1:4433/fixtures/sortable">}
Hook implementations:
[   <HookImpl plugin_name='codespaces', plugin=<module 'datasette_codespaces' from '/Users/simon/.local/share/virtualenvs/datasette-AWNrQs95/lib/python3.10/site-packages/datasette_codespaces/__init__.py'>>,
    <HookImpl plugin_name='datasette.actor_auth_cookie', plugin=<module 'datasette.actor_auth_cookie' from '/Users/simon/Dropbox/Development/datasette/datasette/actor_auth_cookie.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[]

permission_allowed:
{   'action': 'view-table',
    'actor': None,
    'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'resource': (   'fixtures',
                    'sortable')}
Hook implementations:
[   <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[   <function permission_allowed_default.<locals>.inner at 0x1081332e0>]

permission_allowed:
{   'action': 'view-database',
    'actor': None,
    'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'resource': 'fixtures'}
Hook implementations:
[   <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[   <function permission_allowed_default.<locals>.inner at 0x108132cb0>]

@simonw
Copy link
Owner Author

simonw commented Feb 16, 2024

It's a bit annoying that the await_me_maybe pattern produces results like this:

Results:
[   <function permission_allowed_default.<locals>.inner at 0x1081332e0>]

I tried to display the result of await_me_maybe on them but the before and after hook functions aren't async and my attempts to execute that using loop.run_until_complete() didn't work.

@simonw simonw changed the title DATASETTE_PLUGGY_TRACING for tracing Pluggy calls DATASETTE_TRACE_PLUGINS for tracing Pluggy calls Feb 16, 2024
@simonw simonw closed this as completed in 232a304 Feb 16, 2024
@simonw
Copy link
Owner Author

simonw commented Feb 16, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant