Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix idle/busy conn. pool metrics when using NpgsqlDataSource #5497

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

elafarge
Copy link

@elafarge elafarge commented Dec 12, 2023

I've recently tried to move to NpgsqlDataSource to create connection on my company's codebase (moving from new NpgsqlConnection(connectionString) to creating data sources at startup and then creating connections with _myDataSource.CreateConnection().

Incidentally, I realized that the "Idle Connections" and "Busy Connections" metrics we produce for each connection pool disappeared with my change.

For more context, we're relying on PollingCounters in NpgsqlEventSource to gather these metrics (link in Npgsql code).

After digging into the code, I realized that the only place where NpgsqlEventSource.DataSourceCreated() (which adds the data sources to the list of data sources to increment these polling counters for) was called from was NpgsqlConnection.SetupDataSource() which itself is only called in NpgsqlConnection.ConnectionString.set.

When going through the standard NpgsqlConnection(string) constructor, this setter is called accordingly... but not when building the connection with NpgsqlConnection.FromDataSource().

This PR proposes a naive fix: when instantiating a data source, register it against NpgsqlEventSource.

In order to prevent duplicates in NpgsqlEventSource._dataSources, I added a duplicate check (unit tests weren't passing without this check), in NpgsqlEventSource.DataSourceCreated.

In practice, if a user register multiple data sources against the exact same ConnectionString, only metrics for the first created data source will be reported. Given that this is kind of an anti-pattern, I voluntarily didn't try to come up with anything smarter for that particular edge case.

I came across a 1-year old issue which I think has been opened for the a similar problem.

Fixes #4798

@elafarge elafarge force-pushed the fix-idle-and-busy-connection-pool-metrics-when-using-data-sources-to-create-connections branch from d24cf08 to 8a15851 Compare December 12, 2023 17:24
I've recently tried to move to `NpgsqlDataSource` to create connection
on my company's codebase (moving from `new
NpgsqlConnection(connectionString)` to creating data sources at startup
and then creating connections with `_myDataSource.CreateConnection()`.

Incidentally, I realized that the "Idle Connections" and "Busy
Connections" metrics we produce for each connection pool disappeared
with my change.

For more context, we're relying on `PollingCounters` in
`NpgsqlEventSource` to gather these metrics ([link in Npgsql
code](https://github.com/npgsql/npgsql/blob/main/src/Npgsql/NpgsqlEventSource.cs#L217)).

After digging into the code, I realized that the only place where
`NpgsqlEventSource.DataSourceCreated()` (which adds the data sources to
the list of data sources to increment these polling counters for) was
called from was `NpgsqlConnection.SetupDataSource()` which itself is only
called in `NpgsqlConnection.ConnectionString.set`.

When going through the standard `NpgsqlConnection(string)` constructor,
this setter is called accordingly... but not when building the
connection with `NpgsqlConnection.FromDataSource()`.

This PR proposes a naive fix: when instantiating a data source, register
it against `NpgsqlEventSource`.

In order to prevent duplicates in `NpgsqlEventSource._dataSources`, I
added a duplicate check (unit tests weren't passing without this
check), in `NpgsqlEventSource.DataSourceCreated`.

In practice, if a user register multiple data sources against the exact
same ConnectionString, only metrics for the first created data source
will be reported. Given that this is kind of an anti-pattern, I
voluntarily didn't try to come up with anything smarter for that
particular edge case.

NOTE: I came across a 1-year old issue which I think has been opened
for the a similar problem which I believe may be sold by this PR.

Fixes npgsql#4798
@elafarge elafarge force-pushed the fix-idle-and-busy-connection-pool-metrics-when-using-data-sources-to-create-connections branch from 8a15851 to 459f5d1 Compare December 12, 2023 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing EventCounter when using Npgsql.DependencyInjection
1 participant