supabase / supavisor

A cloud-native, multi-tenant Postgres connection pooler.
https://supabase.github.io/supavisor/
Apache License 2.0
1.7k stars 56 forks source link

Add initial Open Telemetry support #385

Open rlopzc opened 1 month ago

rlopzc commented 1 month ago

What kind of change does this PR introduce?

Feature

What is the current behavior?

No current behavior.

What is the new behavior?

This PR will start sending OpenTelemetry traces to the configured OTel vendor.

My goal with this PR is to set up OpenTelemetry in the repository + a development environment to send the traces. This will lower the bar for more contributors to explore this library and play with the traces (add more traces, trace other events, trace the different libraries this project uses, etc).

The OTel vendor can be configured with the following environment variables:

What's currently traced?

As the project is complex, I didn't have enough time to deep dive into each flow of queries + edge cases. That's why this PR just traces the query sent to the proxy, and when the ClientHandler responds to the caller

Next steps

Local environment producing traces

Pre-requisites:

To display the traces, I chose Grafana OTel because this project already uses the great library PromEx. I figured that Grafana had been already used in the stack. Of course, this is very easy to change :).

  1. Turn on Grafana OTel collector + WebUI, run: docker compose up grafana-otel.
  2. Go to http://localhost:4300 and login with admin/admin.
  3. Turn on the development environment, run: make dev.otel
  4. Connect via the proxy with: psql postgresql://postgres.dev_tenant:postgres@localhost:6543/postgres
  5. Execute a query in psql: select * from _supavisor.tenants;
  6. Explore the traces in Explore -> Choose Tempo -> Query type: search. image

I added dev.otel to the Makefile, which adds two environment variables to send the traces to http://localhost:4318.

Visualizing the traces:

As you can see in the image, the trace shows the duration of the executed query with additional information that'll help filter traces when making queries.

swappy-20240707_142332

Additional context

Related issue: #93

Let me know what can be improved in this PR. I'll address the reviews when I have free time :slightly_smiling_face:.