Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 398
tom Avatar asked Aug 19 '15 08:08

tom


People also ask

What is timestamp in InfluxDB?

Timestamps are UNIX timestamps. The minimum valid timestamp is -9223372036854775806 or 1677-09-21T00:12:43.145224194Z . The maximum valid timestamp is 9223372036854775806 or 2262-04-11T23:47:16.854775806Z . As mentioned above, by default, InfluxDB assumes that timestamps have nanosecond precision.

Is InfluxDB real-time?

InfluxDB. Build real-time applications for analytics, IoT and cloud-native services in less time with less code using InfluxDB. Fast, elastic, serverless real-time monitoring platform, dashboarding engine, analytics service and event and metrics processor.

How does Python write data to InfluxDB?

Write data to InfluxDB with Python In your Python program, import the InfluxDB client library and use it to write data to InfluxDB. Define a few variables with the name of your bucket, organization, and token. Instantiate the client. The InfluxDBClient object takes three named parameters: url , org , and token .

What is difference between tag and field in InfluxDB?

Fields are a necessary entity of building the database which are “non-indexed” hence they get scanned for all values when queried. Tags on the other hand are not a necessary but indexed entities that make the queries more performant.


1 Answers

 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.

like image 98
tom Avatar answered Sep 26 '22 08:09

tom