Esempio n. 1
0
def create_client() -> InfluxDBClient:
    """
    Create a client for the Influx database.
    Includes connection test and database and retention policy creation.

    :return: an instance of InfluxDBClient.
    """
    client = InfluxDBClient(host=INFLUXDB_HOST,
                            port=INFLUXDB_PORT,
                            username=INFLUXDB_USERNAME,
                            password=INFLUXDB_PASSWORD,
                            database=INFLUXDB_DATABASE)

    # Test connection
    try:
        client.ping()
    except requests.exceptions.ConnectionError:
        _logger.error('Connection to InfluxDB was refused!')
        exit(code=1)  # TODO replace with backoff retry

    # Create database (will pass if already exists)
    client.create_database(INFLUXDB_DATABASE)

    # Set retention policy on database
    client.create_retention_policy(name=RETENTION_NAME,
                                   duration=RETENTION_DURATION,
                                   replication=RETENTION_REPLICATION,
                                   database=INFLUXDB_DATABASE,
                                   default=True)

    _logger.info("InfluxDB client created successfully")

    return client