0

I am running the script example.py from the influxdb-client-python code base. It runs and writes a data point to a local influxDB instance, for tests and learning purposes only. From what I understand, this script is supposed to write a single point in 2 different ways: using line_protocol (a string) and the Point data structure. I have no problem with the Point data structure; however, the line_protocol writes a point somewhere in February 1970. I am afraid this is a problem of WritePrecision. Maybe it should not be specified when using a Datetime object that is converted to a line_protocol? Can someone please confirm this behaviour (if this is not an error on my side) and also that this is expected ? Many thanks.

2 Answers 2

1
+50

So, what you're seeing with the February 1970 timestamp is likely due to the precision mismatch when you're using line protocol to write data. InfluxDB expects the timestamp in nanoseconds by default. If you don’t specify the precision, it assumes nanoseconds, and if your timestamp is off, it defaults to something like the Unix epoch start — hence, the Feb 1970 date.

You can do 2 things:

  1. If you're using line protocol, make sure you're giving the timestamp in the right precision.

  2. If you're using Point (like you mentioned), it handles precision for you, so you don’t have to worry about that part.

With Line Protocol:

from influxdb_client import InfluxDBClient
import datetime

client = InfluxDBClient(url="http://localhost:8086", token="your-token", org="your-org")
write_api = client.write_api()

# Timestamp in nanoseconds
timestamp = datetime.datetime.utcnow()
line_protocol = f"temperature value=23.5 {int(timestamp.timestamp() * 1e9)}"

# Write with nanosecond precision
write_api.write(bucket="your-bucket", record=line_protocol, write_precision="ns")

With Point:

from influxdb_client import InfluxDBClient, Point
import datetime

client = InfluxDBClient(url="http://localhost:8086", token="your-token", org="your-org")
write_api = client.write_api()

# Create a datetime object (UTC)
timestamp = datetime.datetime.utcnow()

# InfluxDB handles precision for you with Point
point = Point("temperature").field("value", 23.5).time(timestamp)
write_api.write(bucket="your-bucket", record=point)

TL;DR:

  • Line Protocol: Make sure to set the timestamp in the right precision (nanoseconds or milliseconds).
  • Point: Let it do the work for you, it handles precision automatically.

Hopefully this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

You're probably right. I also arrived to the same workaround. What seems strange to me is the example given in the source code example.py which contains this point written in Feb 1970 and i'm pretty sure it was not intended... Anyway I made a MR and asked the question on github, we'll see. Thanks for your comprehensive answer. If you can just confirm by comment that you get the same result running the example.py that would be perfect.
@matleg I ran the code. I am attaching the output here. anotepad.com/notes/ss3rn4kw#google_vignette
Thanks @sarthak-bhan, can you increase the custom time range and go back to 1970, see if you get another record ?
0

Hi I have several ideas:

Point Object Behavior: When using the Point data structure, you specify the timestamp using datetime.now(tz=timezone.utc) along with WritePrecision.MS. The Point object ensures the timestamp is formatted correctly in milliseconds (since the Unix epoch), as required by InfluxDB.

line_protocol Behavior: When you call p.to_line_protocol(), the timestamp in the Point object is already converted to milliseconds. However, when you write this string directly using write_api.write(), InfluxDB assumes the timestamp is in nanoseconds (default precision) unless you specify otherwise.

If you do not specify the WritePrecision during this write operation, the timestamp will be misinterpreted, resulting in a date in February 1970 (a typical symptom of a Unix epoch timestamp mismatch).

solution: When writing a line_protocol string, you need to explicitly specify the WritePrecision to match the timestamp format used in the line_protocol.

Please test this:

enter code here
  write_api.write(bucket="my-bucket", record=line_protocol, 
  write_precision=WritePrecision.MS)

This ensures that InfluxDB interprets the timestamp in the line_protocol string correctly as milliseconds. Additionaly, when using the Point object, the WritePrecision is managed internally, so no issues occur. For direct line_protocol writes, you must always ensure the write_precision parameter matches the format of your timestamp. This behavior is expected and not an error in the InfluxDB client library. It's just a matter of correctly specifying WritePrecision when working with line_protocol strings. Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.