gusutabopb / aioinflux

Asynchronous Python client for InfluxDB
MIT License
159 stars 31 forks source link

`path` support in constructor #24

Closed carlos-jenkins closed 5 years ago

carlos-jenkins commented 5 years ago

Hi, thanks again for this great package, been very helpful.

Sync InfluxDB client has in its constructor a path parameter:

https://influxdb-python.readthedocs.io/en/latest/api-documentation.html#influxdb.InfluxDBClient

path (str) – path of InfluxDB on the server to connect, defaults to ‘’

And the URL is built as follows:

https://github.com/influxdata/influxdb-python/blob/d5d12499f3755199d5eedd8b363450f1cf4073bd/influxdb/client.py#L123

        self.__baseurl = "{0}://{1}:{2}{3}".format(
            self._scheme,
            self._host,
            self._port,
            self._path)

Although in aioinflux there is no path parameter, and the URL is built as follows:

https://github.com/gusutabopb/aioinflux/blob/master/aioinflux/client.py#L163

        return f'{"https" if self.ssl else "http"}://{self.host}:{self.port}/{{endpoint}}'

So, it seems that I cannot connect with aioinflux to our Influx deployment, as for reasons unknown to me, it is under a path.

Currently, I created a quick monkey patch as follows:

class MonkeyPatchedInfluxDBClient(InfluxDBClient):
    def __init__(self, *args, path='/', **kwargs):
        super().__init__(*args, **kwargs)
        self._path = path

    @property
    def path(self):
        return self._path

    @property
    def url(self):
        return '{protocol}://{host}:{port}{path}{{endpoint}}'.format(
            protocol='https' if self.ssl else 'http',
            host=self.host,
            port=self.port,
            path=self.path,
        )

Thanks for placing the url in a property, that was useful.

gusutabopb commented 5 years ago

Thanks for the issue and sorry for the late reply.

The implementation seems trivial but I am not sure how to test such code.

What exactly is the environment you use InfluxDB so that you need such path? Do you use some sort of proxy in front of InfluxDB? If so, could you share any ideas (maybe some Nginx config?) of how to launch InfluxDB at localhost:8086/some/random/path so I could test the code.

carlos-jenkins commented 5 years ago

Yes, indeed, that's exactly the case.

We are using Kubernetes to deploy our services, and depending of the configuration of the ingress, many services can share one single domain, and what Kubernetes does is putting an Nginx as reverse proxy of those services, using a path to identify them.

I'll try to fiddle with a docker stack configuration that should do the same.

carlos-jenkins commented 5 years ago

aioinfluxdeploy.zip

Ok this deployment works to test the scenario.

Deploy stack with:

docker stack deploy --compose-file=stack.yml aioinflux

Access InfluxDB with:

curl -L -i http://127.0.0.1:8000/my/custom/path/ping?verbose=true

Connect using Sync Python client with:

from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8000, path='/my/custom/path/')
client.ping()
carlos-jenkins commented 5 years ago

Thanks a lot!

If anyone comes here, this was fix-released in 0.9.0.