prometheus-api-client-python icon indicating copy to clipboard operation
prometheus-api-client-python copied to clipboard

Allow closing the connection/session

Open FRosner opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

We can open a client, but we cannot close the session. We have to work around it providing our own context manager which accesses a private variable in PrometheusConnect:

    def __enter__(self):
        return self._prometheus

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self._prometheus._session:
            self._prometheus._session.close()
        return False

Describe the solution you'd like

Would be nice to have a close method, or even also implement context manager.

Describe alternatives you've considered

Not sure if there are any. We need to be able to not leak connections.

FRosner avatar Feb 19 '24 16:02 FRosner

@FRosner with the latest release, you can pass a custom session to PrometheusConnect. Does that work for your usecase?

4n4nd avatar Feb 21 '24 21:02 4n4nd

@4n4nd thanks! I guess it still leaves it to us to close the session, but it's for sure better than accessing the private variable. Do you think there's value in providing a close method that closes the session (may it be provided or not), and maybe even the context manager methods?

FRosner avatar Feb 26 '24 10:02 FRosner