OpenTelementry support for Datasette
OpenTelemetry is a collection of tools/APIs/libraries for reporting observabillty and metrics from your applications. Datasette is a complex application to run, where a single request can kick off dozens of SQLite queries across user and internal databases, which is hard to monitor from the outside. I propose that we integrate OTel into Datasette itself.
To do this, we only need to include the tiny opentelemetry-api dependency into Datasette itself. With that, we'd scatter a bunch of traces + spans (OTel vocab) across different hotpaths within Datasette, like so:
from opentelemetry import trace as otel_trace
tracer = otel_trace.get_tracer("datasette", __version__, schema_url="https://opentelemetry.io/schemas/1.29.0")
with tracer.start_as_current_span("custom-key"):
time.sleep(100) By default, this inclusion shouldn't slow down Datasette at all, besides tiny memory allocations. By itself this also isn't very useful. However, a person who's running a Datasette instance can use a whole suite of different OTel programs and services to hook into these traces, like so:
OTEL_SERVICE_NAME=datasette \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 \
OTEL_METRICS_EXPORTER=none \
OTEL_LOGS_EXPORTER=none \
uv run \
--with opentelemetry-distro \
--with opentelemetry-exporter-otlp \
opentelemetry-instrument datasette mydb.dbWith a few env vars, extra python tooling, and a separate OTLP received listening in on 4317 (ex Jaeger), now you can explore individual SQL queries and HTTP requests to investigate
What should we measure?
For one, SQL queries for sure. We can report out raw SQL queries that Datasette is running, both read and write queries, SQL parameters (maybe opt-in?). We can also measure how long a query had to wait in their thread queue before the SQL can be ran, which can be used to see how much load a Datasette instance is getting.
Additionally, HTTP queries themselves. Being able to say "this facet page times out because of these queries" is super powerful.
Also a bunch of other smaller featuresets can be useful to measure: plugin hook registration time, facet calculations, template rendering, startup times, etc.
Working with datasette.tracer
The datasette.tracer module is great, but it's something new that we have to maintain. It requires special URL parameters like _trace=1 and a few other non-standard features. I think the right OTel spans/traces should give us the same effective results, so I'm not 100% sure if we'd even need this anymore.
That being said, I don't think it really conflicts with OTel specifically, so it wouldn't hurt to keep it around. I'd say we can revisit once we've played with OTel for a bit.
Source: simonw/datasette