?_trace=1 now adds SQL trace info to JSON/HTML response

Also added documentation for it. Refs #435
pull/437/head
Simon Willison 2019-04-21 10:41:16 -07:00
rodzic 7d01ca34a1
commit 58a862cee4
2 zmienionych plików z 23 dodań i 5 usunięć

Wyświetl plik

@ -624,7 +624,7 @@ class Datasette:
else:
return Results(rows, False, cursor.description)
with trace("sql", (db_name, sql, params)):
with trace("sql", (db_name, sql.strip(), params)):
results = await self.execute_against_connection_in_thread(
db_name, sql_operation_in_thread
)
@ -729,10 +729,23 @@ class Datasette:
return response.redirect(path)
@app.middleware("response")
async def print_traces(request, response):
if request.get("traces") is not None:
print(json.dumps(request["traces"], indent=2))
print("Num traces: {}".format(len(request["traces"])))
async def add_traces_to_response(request, response):
if request.get("traces") is None:
return
traces = request["traces"]
if "text/html" in response.content_type and b'</body>' in response.body:
extra = json.dumps(traces, indent=2)
extra_html = "<pre>{}</pre></body>".format(extra).encode("utf8")
response.body = response.body.replace(b"</body>", extra_html)
elif "json" in response.content_type and response.body.startswith(b"{"):
data = json.loads(response.body)
if "_traces" not in data:
data["_traces"] = {
"num_traces": len(traces),
"traces": traces,
"duration_sum_ms": sum(t[-1] for t in traces),
}
response.body = json.dumps(data).encode("utf8")
@app.exception(Exception)
def on_exception(request, exception):

Wyświetl plik

@ -303,6 +303,11 @@ Special table arguments
Pagination by continuation token - pass the token that was returned in the
``"next"`` property by the previous page.
``?_trace=1``
Turns on tracing for this page: SQL queries executed during the request will
be gathered and included in the response, either in a new ``"_traces"`` key
for JSON responses or at the bottom of the page if the response is in HTML.
.. _expand_foreign_keys:
Expanding foreign key references