Hasql-opentelemetry 0.1.0.0: OpenTelemetry spans for every hasql statement

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 = True uses the span names, span.type and resource.name that 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 .

13 Likes

Nice! Thank you!

It seems like this is in no way tied to hasql, but pqi instead. So any other driver that gets implemented on top of pqi in the future can benefit from your package.

1 Like

Is this a nice way of saying that maybe it should be called pqi-opentelemetry instead?

It looks like there’s some expectations about how the pqi library is being used though. From the hackage docs:


Where the SQL comes from

hasql 2 sends a statement twice over: once as sendPrepare name sql and from then on as sendQueryPrepared name params, which never names the SQL again. The prepare is therefore the only place a connection ever sees the text, and the attributes derived from it are cached per statement name for the life of the connection.


So it might only work quite right if you’re using prepared statements?