pjcalvo / locust-influxdb-listener

LocustIO base project with a custom influxDB listener.
MIT License
26 stars 20 forks source link

uncatchable exception #11

Closed obriat closed 3 years ago

obriat commented 3 years ago

I initiate InfluxDBListener on @events.init.add_listener and sometimes the InfluxDB is not up, so python throw me exceptions even if I try/catch the function call. I noticed that the call to influxdb_client in /locust_influxdb_listener/init.py", line 55 should be catched and return None. Am I missing something here ?

Traceback (most recent call last):
  File "~/.local/lib/python3.7/site-packages/urllib3/connection.py", line 170, in _new_conn
...
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/.local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
...
  File "~/.local/lib/python3.7/site-packages/urllib3/connection.py", line 182, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f56ba8acc88>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/.local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
...
  File "~/.local/lib/python3.7/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.5', port=8086): Max retries exceeded with url: /query?q=CREATE+DATABASE+%22locustidb%22&db=locustidb (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f56ba8acc88>: Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/.local/lib/python3.7/site-packages/locust_influxdb_listener/__init__.py", line 55, in __init__
     self.influxdb_client.create_database(influxDbSettings.database)
   File "/home/locust/.local/lib/python3.8/site-packages/influxdb/client.py", line 746, in create_database
pjcalvo commented 3 years ago

I will take a look.

pjcalvo commented 3 years ago

@obriat I just uploaded a fix. switched to .error instead of .exception to hide the extra information. https://pypi.org/project/locust-influxdb-listener/0.0.5/

obriat commented 3 years ago

It didn't fix my issue, I'm a python newbie so maybe my approach is not the right one I want to wait for influxdb to be ready and just log it if it can't be reach but without stopping my locsut test.

@events.init.add_listener
def locust_init(environment, **kwargs):
...
      influxDBSettings = InfluxDBSettings(
            influx_host=influx_host,
            influx_port='8086',
            user='****',
            pwd='****',
            database='locustidb'
        )
       # try 5 times for a total of 55s
        for i in range(5):
            try:
               InfluxDBListener(env=environment, influxDbSettings=influxDBSettings)
            except:
                wait = i * i 
                log("Try " + i + ": Could not connect to influxdb, wait for" + wait + " seconds and try again.")
                time.sleep(wait) 
                continue 
            else:
                log("Influxdb is available")
                break
        else:
            log("Influxdb is not available")
pjcalvo commented 3 years ago

@obriat The exception is currently handled by the listener. So you don't have to catch it in your end here.

Make you you update to version 0.0.5, which has the fix for the extra log information....

obriat commented 3 years ago

You're right, my package wasn't up to date. I also see my other mistake, I took the logger.exception for a real exception. So do you know how I can determine if InfluxDBListener is connected ? The object doesn't seem to return any data or state, it just logs the error.

pjcalvo commented 3 years ago

I will make it so it returns a true in case of success.. @obriat

obriat commented 3 years ago

Here a suggestion: #12.

It turns out that __init__ should not return anything, so I used a state parameter.

obriat commented 3 years ago

I think I find the right process by using the ping method and catching its exception:

            influxDBSettings = InfluxDBSettings(
                influx_host='influx_host',
                influx_port='8086',
                user='admin',
                pwd='pass',
                database='locustidb'
            )

            for i in range(1, 6):
                try:
                    # start listener with the given configuration
                    influx = InfluxDBListener(env=environment, influxDbSettings=influxDBSettings)
                    influx.influxdb_client.ping()
                except:
                   # InfluxDBListener does not throw exception but  influx.influxdb_client.ping() do.
                    wait = i * i
                    print("Try n°" + str(i) + ": Could not connect to influxdb, wait for " + str(
                        wait) + " seconds and try again.")
                    time.sleep(wait)  # sleep the script for x seconds and....#

                else:
                    print("Influx (version: " + influx.influxdb_client.ping() + ") is available")
                    break  # ...break out of the loop#
            else:
                print("Influx is not available!")
pjcalvo commented 3 years ago

Perfect.