How to use time field in adding metrics data to the influx db?
17,448
Solution 1
from datetime import datetime
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
and use "time" like this in json_body-
"time": current_time
It works.
Solution 2
You need UNIX timestamp, so it should be:
"time": int(time.time())
Edit:
Time with nanosecond time precision:
"time": int(time.time() * 1000000000)
Eventually, second precision can be used "time": int(time.time())
and specified explicitly:
write_points(points, time_precision='s')
Author by
tom
Updated on June 07, 2022Comments
-
tom almost 2 years
I am using the following lines of code to add metrics data in influxDB.
def add_job_influx_db_metrics(tags_dict={}, values_dict={}, measurement='test'): influxdb_client = InfluxDB.get_connection() db_name = "mydb" influxdb_client.switch_database(database=db_name) current_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',time.localtime(time.time())) json_body = [ { "measurement": measurement, "tags": tags_dict, "time": current_time, "fields": values_dict } ] response = influxdb_client.write_points(points=json_body) print "write_operation response", response
When I am integrating it with Grafana,data does not come up but when I am checking it on 127.0.0.1:8083 ,it shows the time is 1970-01-01T00:00:00Z.Probably,it takes the start epoch time as default. I wanted to take the time of the form "2015-8-19T07:15:00Z". How to take the time field in influxdb(python client) and what is timePrecision ?