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

Reuse connections in postgresql metricsets #12603

Merged
merged 1 commit into from
Jun 20, 2019

Conversation

jsoriano
Copy link
Member

@jsoriano jsoriano commented Jun 18, 2019

Reuse connections in PostgreSQL metricsets instead of opening and
closing one on each fetch.

Fixes #12504

@jsoriano jsoriano requested a review from a team as a code owner June 18, 2019 16:03
@jsoriano jsoriano self-assigned this Jun 18, 2019
@jsoriano jsoriano force-pushed the postgresql-keep-connections branch from 8e1b3e1 to 801f9ad Compare June 18, 2019 16:04

results, err := postgresql.QueryStats(db, "SELECT * FROM pg_stat_statements")
ctx := context.Background()
results, err := m.QueryStats(ctx, "SELECT * FROM pg_stat_statements")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing metricset Close won't cancel these requests, I guess it would be unexpected for them to leak, but it could be worth it to handle ctx cancellation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, Close doesn't cancel running queries, they will be executed till they finish. I wouldn't expect leaks because of that.
Not sure if it would worth to hande cancelation here, we'd need to keep a thread safe list of cancelation functions available on close. Once we have #11981 or similar we will be able to cancel the parent context when calling the closers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ I thought about that PR too. That said, you don't really need to keep a list, isn't it? When creating the metricset you can create a Background context, use it in all fetch operations and cancel it on close.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having that that PR is moving (:crossed_fingers:) I'm ok with leaving this for update after it's merged

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, you don't really need to keep a list, isn't it? When creating the metricset you can create a Background context, use it in all fetch operations and cancel it on close.

Yes, this would be another option, but I think this is what should be done in a general what by #11981. Doing it this way here has some disadvantages, we'd be hiding the context inside the metricset and it would be a different one to the one received by Fetch if #11981 is implemented.

Having that that PR is moving (crossed_fingers) I'm ok with leaving this for update after it's merged

Yeah, let me move this forward 🙂

@exekias exekias added the needs_backport PR is waiting to be backported to other branches. label Jun 19, 2019
@jsoriano jsoriano removed the needs_backport PR is waiting to be backported to other branches. label Jun 20, 2019
@jsoriano jsoriano merged commit 400c67e into elastic:master Jun 20, 2019
@jsoriano jsoriano deleted the postgresql-keep-connections branch June 20, 2019 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Metricbeat Metricbeat module review Team:Integrations Label for the Integrations team v7.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keep connection open for metricbeat PostgresSQL modules
4 participants