Hi all,
I’ve released hasql-opentelemetry, which makes every statement hasql 2 sends to Postgres an OpenTelemetry client span.
This was made possible thanks to the changes in hasql 2, which added the pqi adapter. It makes this package as simple to add as doing this:
import Hasql.OpenTelemetry qualified as HasqlOtel
import Pqi.Ffi qualified
Connection.acquire (HasqlOtel.traced Pqi.Ffi.adapter) settings
Your statements and sessions stay as they are, and the same adapter works with hasql-pool. Both pqi-ffi and pqi-native are supported.
What’s included:
- Inspect the SQL that is actually executed. Each span carries the statement text, the operation, the database and server, rows returned or affected, and on failure the SQLSTATE. Attribute names follow the current OTel database conventions.
- Accurate timing in pipeline mode. hasql pipelines every roundtrip, so a send returns long before Postgres answers. Spans run from the send until that command’s last result is read, not just around the send.
- No sensitive data. Parameter values are never recorded, and neither is any text from a server error report. Postgres often puts the offending value in the error message, e.g.
invalid input syntax for type uuid: "…", so a failure records only its SQLSTATE. - Low noise by default. Spans are only started under an existing parent span, so pool warm-up and health checks don’t create traces of their own.
- Optional Datadog shape.
datadogCompat = Trueuses the span names,span.typeandresource.namethat make Datadog APM show the spans as database queries.
We’ve been running this in production at my work for a while and I decided to extract it into a package for the wider community to use.
Source: GitHub - kronor-io/hasql-opentelemetry: OpenTelemetry tracing for hasql · GitHub .
