Skip to content

How to determine possible failures during write api #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
witanswer opened this issue Jun 29, 2021 · 6 comments
Closed

How to determine possible failures during write api #279

witanswer opened this issue Jun 29, 2021 · 6 comments
Labels
question Further information is requested wontfix This will not be worked on
Milestone

Comments

@witanswer
Copy link

Hello, I am working on a piece of code to send influxdb formatted files on the local filesystem to our influxdb server.
I would like to be able to assure that a file's data has been successfully sent to the server before removing the local file.

In example code, It seems that the return values of write are not checked (as far as i can tell):

https://github.com/influxdata/influxdb-client-python/blob/master/examples/import_data_set.py#L73

And from reading the api, It seems that if the request is synchronus, it returns None

https://github.com/influxdata/influxdb-client-python/blob/master/influxdb_client/client/write_api.py#L257

I would like to have the option to take action in certain conditions, IE:

-A badly formatted line among a large number of well formed lines
-Timeout longer than configured has occured
-Incorrect authentication has been given

How would I go about detecting this in code?

Thank you!

@bednar
Copy link
Contributor

bednar commented Jul 9, 2021

Hi @witanswer,

thanks for using our client.

Hello, I am working on a piece of code to send influxdb formatted files on the local filesystem to our influxdb server.
I would like to be able to assure that a file's data has been successfully sent to the server before removing the local file.

In example code, It seems that the return values of write are not checked (as far as i can tell):

https://github.com/influxdata/influxdb-client-python/blob/master/examples/import_data_set.py#L73

And from reading the api, It seems that if the request is synchronus, it returns None

https://github.com/influxdata/influxdb-client-python/blob/master/influxdb_client/client/write_api.py#L257

The synchronous version of api produces error if something fails. You can catch this error and determine what do you want to do.

I would like to have the option to take action in certain conditions, IE:

-A badly formatted line among a large number of well formed lines
-Timeout longer than configured has occured
-Incorrect authentication has been given

You can use something like:

import json

import urllib3

from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
from influxdb_client.rest import ApiException

url = "http://localhost:8086"
token = "my-token"
bucket = "my-bucket"
org = "my-org"

with InfluxDBClient(url=url, token=token, org=org) as client:

    write_api = client.write_api(write_options=SYNCHRONOUS)

    """
    Write data
    """
    try:
        write_api.write(bucket=bucket, record="mem,location=aws-va523 value=1")
    except ApiException as e:
        # missing credentials
        if e.status == 401:
            raise Exception(f"Incorrect authentication...") from e
        # badly formatted
        if e.status == 400:
            message = json.loads(e.body)['message']
            raise Exception(f"Badly formatted... '{message}'.") from e
        raise
    except urllib3.exceptions.TimeoutError as e:
        raise Exception(f"Timeout...") from e

Regards

@bednar bednar added the question Further information is requested label Jul 9, 2021
@witanswer
Copy link
Author

Sounds good, thanks for the assistance.

@bednar bednar added this to the 1.20.0 milestone Jul 11, 2021
@bednar bednar added the wontfix This will not be worked on label Jul 11, 2021
@bednar bednar closed this as completed Jul 11, 2021
@ronytesler
Copy link

But what if the write is not synchronous, for example when using batch write, how can I know if an error has occurred?

@bednar
Copy link
Contributor

bednar commented Dec 10, 2021

Hi @ronytesler,

thanks for using our client.

The version 1.22.0 introduces possibility to handle batch events - #341.

For more info see docs:

Regards

@ronytesler
Copy link

@bednar Thanks. So I can use:
try: client.write_api(write_options=ASYNCHRONOUS).write("my-bucket", record="mem,tag=a value=86") except InfluxDBError as e: if e.response.status == 401: raise Exception(f"Insufficient write permissions to 'my-bucket'.") from e raise

I thought that if the call is ASYNCHRONOUS, I can't just catch the error like this.

@bednar
Copy link
Contributor

bednar commented Dec 10, 2021

The ASYNCHRONOUS write is suppose to run with AsyncIO. If you would like to get the result of write you have to call get():

https://github.com/influxdata/influxdb-client-python#asynchronous-client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants