InfluxCommunity / influxdb-ruby

Ruby client for InfluxDB
MIT License
370 stars 133 forks source link

Not all points are written when using write_point inside a loop without sleep #189

Closed navaneeth closed 7 years ago

navaneeth commented 7 years ago

I am using the following code:

require 'influxdb'
database = 'tt'
name     = 'foobar3'
influxdb = InfluxDB::Client.new database
5.times do
    item = {
    values: { count: rand(10...42).to_f },
    tags:   { type: 'Draft', subtype: 'dummy', state: 'Live' } # tags are optional
    }
    influxdb.write_point(name, item)
    sleep 1 # Removing this will cause the issue
end

Above code inserts 5 values into influx. If I remove the sleep 1, then only one point gets inserted into the DB. When I have sleep in the loop, all 5 values gets inserted.

Is this expected?

Influx version: 1.2.4 Influx ruby: 0.3.14

dmke commented 7 years ago

Yeah, this is a common pitfall when using the Ruby gem:

So if you don't specify a precision, we'll fall back to second-precision. When you write five points with the same tags (within the same second), the first four points are overwritten by the last point (i.e. each subsequent point overwrites the last one).

You can find the corresponding note in the README in the Writing data section (scroll to the "Note:" under the second code sample):

The easiest soultion is to explicitly define a time_precision (on the InfluxDB::Client) and the timestamp option (when writing points):

influxdb = InfluxDB::Client.new "tt",  time_precision: "ns"
tags = { type: 'Draft', subtype: 'dummy', state: 'Live' }

def now_in_nanoseconds
  (Time.now.to_r * 10**9).to_i
end

5.times do
  item = {
    values:    { count: rand(10...42).to_f },
    tags:      tags,
    timestamp: now_in_nanoseconds,
  }
  influxdb.write_point(name, item)
end
dmke commented 7 years ago

I've added a link to the readme: https://github.com/influxdata/influxdb-ruby#a-note-about-time-precision

dmke commented 7 years ago

FYI: If you're feeling adventurous, you could try the timestamps branch, whilch might be included in the next release:

# Gemfile
gem "influxdb", github: "influxdata/influxdb-ruby", branch: "timestamps"

# your code
influxdb = InfluxDB::Client.new "tt",  time_precision: "ns"
# item = { values: {...}, tags: {...} }
influxdb.write_point(name, item, Time.now)

As you can see, you still need to define the time_precision option on the client and explicitly provide a timestamp on the data point, but you'll save the second→whatever conversion.