How to use time field in adding metrics data to the influx db?

tom picture tom · Aug 19, 2015 · Viewed 14k times · Source

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 ?

Answer

tom picture tom · Aug 20, 2015
 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.