Yelish Giri
Back to Writing

How I Added a PostgreSQL Connection Hook to OpenTelemetry

What my merged PR changes, and why developers needed it.

· 4 min read · Open Source

The OpenTelemetry logo and the PostgreSQL elephant logo side by side, joined by a plus sign

OpenTelemetry's PostgreSQL package let developers add their own labels to database queries, but not to database connections. If you wanted to label a connection, there was no way to do it. I added one in this PR, and this post explains the problem and what I changed.

Why OpenTelemetry

I made this contribution for CSC 424, Software Engineering II, with Professor Jeremy Duckworth, where one of the biggest parts of our grade is contributing to an open source project of our own choosing. Getting familiar with a project and committing something of value was an amazing goal to be given.

I have read a lot about observability and why it matters. If you want to build stable, resilient software, you need to be able to monitor it for failures and get useful context when something goes wrong. OpenTelemetry gives you a standard way to collect that information, and most people then view it in a tool like Grafana.

Here is a quick introduction, using a Postgres query as the example:

  • A log is a record of one event, like an error when the query failed.
  • A metric is a number measured over time, like how long queries take on average.
  • A span is one operation with a start and an end, like that single query.
  • A trace is the story of a whole request, told through its spans. It shows what happened and in what order, which makes debugging a lot easier.

I have written JavaScript for most of my projects, so I was comfortable reading the code. The tests use Mocha, which is pretty similar to Jest with its describe and it blocks. That is why I chose the OpenTelemetry JavaScript contrib repository.

How I found the issue

I started by reading the OpenTelemetry documentation to understand signals, traces, spans and metrics, then the documentation for the codebase.

At first, I tried looking through the issues across the whole repository. I got better results by starting with something I used regularly and finding the package for it. instrumentation-pg fit perfectly because I was already familiar with Postgres.

From there, I found the issues that mentioned Postgres, read them, and figured out what was missing. I also used AI to help me understand the codebase. It worked like a quick knowledge transfer for the repository and that package, so I had a proper idea of the structure before changing anything.

What I changed

My change is in instrumentation-pg, the package that instruments the PostgreSQL client for Node.js. For a system to be observable, it has to be instrumented, which means its code has to emit signals like traces, metrics and logs. This package does that for Postgres, so developers don't have to write it themselves.

Developers can also add their own labels to spans, called attributes, through an option called requestHook. The problem was that it only ran for query spans. It did not run for the pg.connect and pg-pool.connect spans created when a connection opens.

Let's say John Doe wants to add a label called connection_joined whenever his connection pool connects. He could label his queries, but he had no way to label the connection. Issue #2118 discussed this feature, and it was still open.

The implementation was comparatively easy, because the same thing already existed for query spans. I just needed to do the same for connection spans and write tests for those scenarios.

My PR adds an optional connectionHook. Now John can write this:

new PgInstrumentation({
  connectionHook: (span, { connection }) => {
    span.setAttribute("connection_joined", connection.database ?? "unknown");
  },
});

The hook gets the span and the connection's database, host, port and user. It does not change requestHook, so existing code keeps working. If the hook throws an error, the error is caught and logged, so it can't break the app's database connection. I also added tests for pg.Client and pg-pool and documented the option in the README.

Getting it merged

While testing my change, I noticed that some tests were failing only some of the time. I wanted to know if I had caused it, so I cloned a fresh copy of the repository and ran the tests without my change. As I had predicted, the flakiness was already there: pg-pool (ESM) should work with ESM usage failed in 3 of 15 runs on main. I mentioned it in the PR so the reviewers would know it wasn't coming from my change.

A member of the project reviewed the PR and approved it without requesting any changes, which I am really grateful for. It was merged about a week after I opened it.

My main takeaway

Read issues with a specific goal in mind. Scanning the whole repository didn't get me far, but searching for Postgres issues led me to the one I worked on. If you can't get that specific yet, learn the overall architecture first, so you can spot what's missing.