datasette/tests
Alex Garcia 8b35d4a80d Add OpenTelemetry metrics for SQL thread pool saturation and query latency
Spans describe requests that have finished. They structurally cannot answer
"am I saturating my 3 SQL threads right now", because that is a level rather
than an event - and with num_sql_threads defaulting to 3, it is usually the
first thing worth knowing about a busy Datasette. This adds the metrics that
answer it.

Five observable gauges, computed only when something is collecting, so an
instance with no MeterProvider installed does no work for them at all:

  datasette.sql.threads.limit         num_sql_threads
  datasette.sql.threads.queue_depth   queries waiting for a free thread
  datasette.sql.queries.pending       in-flight reads, by db.namespace
  datasette.write.queue_depth         writes behind the single write thread
  datasette.connections.open          tracked file connections

Three instruments recorded inline, which matters because metrics survive
trace sampling and spans do not - an operator sampling 1% of traces still
gets 100% of the latency distribution:

  db.client.operation.duration        semconv histogram, with error.type
  datasette.write.queue_wait          the metric twin of the existing span
  datasette.sql.queries.interrupted   sql_time_limit_ms kills

The interrupted counter closes a gap the plan called out as unanswerable:
"how often are we killing queries at the limit" is a rate, and a rate cannot
be recovered from sampled spans.

Core still creates no provider of any kind, so the architecture is unchanged;
`grep -rn 'opentelemetry.sdk' datasette/` stays empty. One real difference
from tracing is worth recording: _ProxyMeter and its instruments forward to a
provider installed after they were created, whereas ProxyTracer permanently
caches the first concrete tracer it resolves. Module-level instruments are
therefore safe and the test fixture has no ordering constraint.

Live instances are tracked in a lock-guarded WeakSet so instrumenting an
instance never keeps it alive. The pool gauges carry no attribute saying
which Datasette produced them: production runs one instance per process, and
adding an id to disambiguate the test suite's hundreds of instances would buy
unbounded attribute cardinality to fix a case that does not occur. The
collision is documented instead, and the gauge callbacks are plain generator
functions so tests can assert exact values by calling them directly rather
than through the SDK's last-value aggregation.

demos/otel/metrics_demo.py fires 12 concurrent 40ms queries at a 3-thread
pool and samples the gauges mid-flight: queue_depth peaks at exactly 9, and
the duration histogram reads max=0.1695s for a query whose work is 40ms. That
gap is the queue, and it is the thing traces alone will not show you.

Also corrects the demo README's privacy section, which still claimed
parameter values are never recorded - that stopped being unconditionally true
when trace_sql_parameters landed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

(cherry picked from 6ef0dd8c and adapted to the rebuilt phase-1 stack:
attribute names now come from telemetry_registry where entries exist, the
meter carries the instrumentation-scope version and schema URL, and the
interrupted-queries counter skips expected timeouts - callers that opted
into a deliberately short budget, like facet suggestion - matching how
those are excluded from span error status. The internals.rst reference
lands with the registry commit that follows.)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F2h9ANGZ7paWSpqs5DUAcG
2026-09-01 17:09:35 -07:00
..
plugins Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_templates Fix handling of nested custom page wildcard paths, closes #996 2020-10-07 15:51:11 -07:00
__init__.py Broke up test_app into test_api and test_html 2017-12-15 04:08:24 -08:00
build_small_spatialite_db.py New run_sanity_checks mechanism, for SpatiLite 2019-05-11 15:55:30 -07:00
conftest.py Add OpenTelemetry metrics for SQL thread pool saturation and query latency 2026-09-01 17:09:35 -07:00
ext.c Add new entrypoint option to --load-extensions. (#1789) 2022-08-23 11:34:30 -07:00
fixtures.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
spatialite.db New run_sanity_checks mechanism, for SpatiLite 2019-05-11 15:55:30 -07:00
test-datasette-load-plugins.sh fix (typo): Corrected spelling of 'environments' (#2268) 2024-02-19 14:41:32 -08:00
test_actions_sql.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_actor_restriction_bug.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_allowed_many.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_allowed_resources.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_api.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_api_write.py disable_fts() before dropping table 2026-08-10 15:03:29 -07:00
test_auth.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_autocomplete.py Add "ok": true to every JSON object success response 2026-07-04 13:40:05 +00:00
test_base_view.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_cli.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_cli_serve_get.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_cli_serve_server.py Run datasette serve startup and uvicorn on a single event loop (#2886) 2026-09-01 09:32:37 -07:00
test_column_types.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_config_dir.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_config_permission_rules.py Run black formatter 2025-10-25 15:38:07 -07:00
test_crossdb.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_csrf_middleware.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_csv.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_custom_pages.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_datasette_https_server.sh More robust test_datasette_https_server.sh test 2026-06-22 10:11:56 -07:00
test_debug_autocomplete.py Autocomplete widget and /-/debug/autocomplete test page 2026-06-13 22:59:37 -07:00
test_default_deny.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_docs.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_docs_plugins.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_error_shape.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_extras.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_facets.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_fd_leak.py FD-leak regression test for Datasette.close() 2026-04-16 20:18:05 -07:00
test_filters.py Fix for SQL injection issue in table filters, refs #2868 2026-08-06 11:19:50 -07:00
test_fixtures.py datasette.fixtures module, closes #2733 2026-05-21 23:05:37 -07:00
test_html.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_http_span.py Name the request span after the route it matched 2026-09-01 16:24:35 -07:00
test_internal_db.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_internals_database.py Propagate otel context across the thread boundaries 2026-09-01 16:24:35 -07:00
test_internals_datasette.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_internals_datasette_client.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_internals_request.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_internals_response.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_internals_urls.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_jump.py Autocomplete widget and /-/debug/autocomplete test page 2026-06-13 22:59:37 -07:00
test_label_column_for_table.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_lifespan.py Run startup via ASGI lifespan instead of waiting for the first request (#2887) 2026-09-01 09:39:25 -07:00
test_load_extensions.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_messages.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_multipart.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_package.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_permission_endpoints.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_permissions.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_playwright.py Make binary playwright test more robust 2026-07-05 22:46:00 -07:00
test_plugins.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_publish_cloudrun.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_publish_heroku.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_pytest_autoclose_plugin.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_queries.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_restriction_sql.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_routes.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_schema_endpoints.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_search_tables.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_spatialite.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_stored_queries.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_success_envelope.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_table_api.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_table_html.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_telemetry.py Stop marking a deliberately-short query budget as a span error 2026-09-01 16:24:35 -07:00
test_telemetry_metrics.py Add OpenTelemetry metrics for SQL thread pool saturation and query latency 2026-09-01 17:09:35 -07:00
test_telemetry_registry.py Name the request span after the route it matched 2026-09-01 16:24:35 -07:00
test_template_context.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_token_handler.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_utils.py Remove the hand-rolled tracer now that OpenTelemetry covers the same ground 2026-09-01 16:24:35 -07:00
test_utils_check_callable.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_utils_permissions.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_utils_sql_analysis.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
test_write_sql_operation_decisions.py Refactored write decision tests 2026-05-28 12:09:20 -07:00
test_write_wrapper.py Upgrade to ruff>=0.16.0 (#2857) 2026-07-25 15:47:08 -07:00
utils.py From 409 warnings down to 52 warnings. 2026-04-14 18:46:47 -07:00