|
| 1 | +""" |
| 2 | +Illustrates getting header values from Errors that may occur on write. |
| 3 | +
|
| 4 | +To test against cloud set the following environment variables: |
| 5 | + INFLUX_URL |
| 6 | + INFLUX_TOKEN |
| 7 | + INFLUX_DATABASE |
| 8 | + INFLUX_ORG |
| 9 | +
|
| 10 | +...otherwise will run against a standard OSS endpoint. |
| 11 | +""" |
| 12 | +import asyncio |
| 13 | +import os |
| 14 | +from typing import MutableMapping |
| 15 | + |
| 16 | +from influxdb_client import InfluxDBClient |
| 17 | +from influxdb_client.client.exceptions import InfluxDBError |
| 18 | +from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync |
| 19 | +from influxdb_client.client.write_api import SYNCHRONOUS |
| 20 | +from influxdb_client.rest import ApiException |
| 21 | + |
| 22 | + |
| 23 | +def get_envar(key, default): |
| 24 | + try: |
| 25 | + return os.environ[key] |
| 26 | + except: |
| 27 | + return default |
| 28 | + |
| 29 | + |
| 30 | +class Config(object): |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + self.url = get_envar("INFLUX_URL", "http://localhost:8086") |
| 34 | + self.token = get_envar("INFLUX_TOKEN", "my-token") |
| 35 | + self.bucket = get_envar("INFLUX_DATABASE", "my-bucket") |
| 36 | + self.org = get_envar("INFLUX_ORG", "my-org") |
| 37 | + |
| 38 | + def __str__(self): |
| 39 | + return (f"config:\n" |
| 40 | + f" url: {self.url}\n" |
| 41 | + f" token: ****redacted*****\n" |
| 42 | + f" bucket: {self.bucket}\n" |
| 43 | + f" org: {self.org}\n" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +# To encapsulate functions used in batch writing |
| 48 | +class BatchCB(object): |
| 49 | + |
| 50 | + def success(self, conf: (str, str, str), data: str): |
| 51 | + print(f"Write success: {conf}, data: {data}") |
| 52 | + |
| 53 | + def error(self, conf: (str, str, str), data: str, exception: InfluxDBError): |
| 54 | + print(f"\nBatch -> Write failed: {conf}, data: {data}, error: {exception.message}") |
| 55 | + report_headers(exception.headers) |
| 56 | + |
| 57 | + def retry(self, conf: (str, str, str), data: str, exception: InfluxDBError): |
| 58 | + print(f"Write failed but retryable: {conf}, data: {data}, error: {exception}") |
| 59 | + |
| 60 | + |
| 61 | +# simple reporter that server is available |
| 62 | +def report_ping(ping: bool): |
| 63 | + if not ping: |
| 64 | + raise ValueError("InfluxDB: Failed to ping server") |
| 65 | + else: |
| 66 | + print("InfluxDB: ready") |
| 67 | + |
| 68 | + |
| 69 | +# report some useful expected header fields |
| 70 | +def report_headers(headers: MutableMapping[str, str]): |
| 71 | + print(" Date: ", headers.get("Date")) |
| 72 | + print(" X-Influxdb-Build: ", headers.get("X-Influxdb-Build")) |
| 73 | + print(" X-Influxdb-Version: ", headers.get("X-Influxdb-Version")) # OSS version, Cloud should be None |
| 74 | + print(" X-Platform-Error-Code: ", headers.get("X-Platform-Error-Code")) # OSS invalid, Cloud should be None |
| 75 | + print(" Retry-After: ", headers.get("Retry-After")) # Should be None |
| 76 | + print(" Trace-Id: ", headers.get("Trace-Id")) # OSS should be None, Cloud should return value |
| 77 | + |
| 78 | + |
| 79 | +# try a write using a synchronous call |
| 80 | +def use_sync(conf: Config): |
| 81 | + print("Using sync") |
| 82 | + with InfluxDBClient(url=conf.url, token=conf.token, org=conf.org) as client: |
| 83 | + report_ping(client.ping()) |
| 84 | + try: |
| 85 | + client.write_api(write_options=SYNCHRONOUS).write(bucket=conf.bucket, record="cpu,location=G4 usage=") |
| 86 | + except ApiException as ae: |
| 87 | + print("\nSync -> Caught ApiException: ", ae.message) |
| 88 | + report_headers(ae.headers) |
| 89 | + |
| 90 | + print("Sync write done") |
| 91 | + |
| 92 | + |
| 93 | +# try a write using batch API |
| 94 | +def use_batch(conf: Config): |
| 95 | + print("Using batch") |
| 96 | + with InfluxDBClient(url=conf.url, token=conf.token, org=conf.org) as client: |
| 97 | + cb = BatchCB() |
| 98 | + with client.write_api(success_callback=cb.success, |
| 99 | + error_callback=cb.error, |
| 100 | + retry_callback=cb.retry) as write_api: |
| 101 | + write_api.write(bucket=conf.bucket, record="cpu,location=G9 usage=") |
| 102 | + print("Batch write sent") |
| 103 | + print("Batch write done") |
| 104 | + |
| 105 | + |
| 106 | +# try a write using async.io |
| 107 | +async def use_async(conf: Config): |
| 108 | + print("Using async") |
| 109 | + async with InfluxDBClientAsync(url=conf.url, token=conf.token, org=conf.org) as client: |
| 110 | + report_ping(await client.ping()) |
| 111 | + try: |
| 112 | + await client.write_api().write(bucket=conf.bucket, record="cpu,location=G7 usage=") |
| 113 | + except InfluxDBError as ie: |
| 114 | + print("\nAsync -> Caught InfluxDBError: ", ie.message) |
| 115 | + report_headers(ie.headers) |
| 116 | + print("Async write done") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + conf = Config() |
| 121 | + print(conf) |
| 122 | + use_sync(conf) |
| 123 | + print("\n Continuing...\n") |
| 124 | + use_batch(conf) |
| 125 | + print("\n Continuing...\n") |
| 126 | + asyncio.run(use_async(conf)) |
0 commit comments