Ruby client for InfluxDB
The official Ruby client library for InfluxDB. Maintained by @toddboom and @dmke.
Support for InfluxDB v0.8.x is now deprecated. The final version of this library that will support the older InfluxDB interface is
v0.1.9, which is available as a gem and tagged on this repository.If you're reading this message, then you should only expect support for InfluxDB v0.9.1 and higher.
Since v0.7.0, this gem requires Ruby >= 2.3.0. MRI 2.2 should still work, however we are unable to test this properly, since our toolchain (Bundler) has dropped support for it. Support for MRI Attention: Please also read the
note about time precision below.
Allowed values for time_precision are:
"ns" or nil for nanosecond"u" for microsecond"ms" for millisecond"s" for second"m" for minute"h" for hourWrite data with a specific retention policy:
database = 'site_development'
name = 'foobar'
precision = 's'
retention = '1h.cpu'
influxdb = InfluxDB::Client.new database,
username: "foo",
password: "bar"
data = {
values: { value: 0 },
tags: { foo: 'bar', bar: 'baz' },
timestamp: Time.now.to_i
}
influxdb.write_point(name, data, precision, retention)Write data while choosing the database:
database = 'site_development'
name = 'foobar'
precision = 's'
retention = '1h.cpu'
influxdb = InfluxDB::Client.new {
username: "foo",
password: "bar"
}
data = {
values: { value: 0 },
tags: { foo: 'bar', bar: 'baz' },
timestamp: Time.now.to_i
}
influxdb.write_point(name, data, precision, retention, database)Write multiple points in a batch (performance boost):
data = [
{
series: 'cpu',
tags: { host: 'server_1', region: 'us' },
values: { internal: 5, external: 0.453345 }
},
{
series: 'gpu',
values: { value: 0.9999 },
}
]
influxdb.write_points(data)
# you can also specify precision in method call
precision = 'm'
influxdb.write_points(data, precision)Write multiple points in a batch with a specific retention policy:
data = [
{
series: 'cpu',
tags: { host: 'server_1', region: 'us' },
values: { internal: 5, external: 0.453345 }
},
{
series: 'gpu',
values: { value: 0.9999 },
}
]
precision = 'm'
retention = '1h.cpu'
influxdb.write_points(data, precision, retention)Write asynchronously:
database = 'site_development'
name = 'foobar'
influxdb = InfluxDB::Client.new database,
username: "foo",
password: "bar",
async: true
data = {
values: { value: 0 },
tags: { foo: 'bar', bar: 'baz' },
timestamp: Time.now.to_i
}
influxdb.write_point(name, data)Using async: true is a shortcut for the following:
…Write data via UDP (note that a retention policy cannot be specified for UDP writes):
influxdb = InfluxDB::Client.new udp: { host: "127.0.0.1", port: 4444 }
name = 'hitchhiker'
data = {
values: { value: 666 },
tags: { foo: 'bar', bar: 'baz' }
}
influxdb.write_point(name, data)Discard write errors:
influxdb = InfluxDB::Client.new(
udp: { host: "127.0.0.1", port: 4444 },
discard_write_errors: true
)
influxdb.write_point('hitchhiker', { values: { value: 666 } })The default precision in this gem is "s" (second), as Ruby's Time#to_i
operates on this resolution.
If you write data points with sub-second resolution, you have to configure
your client instance with a more granular time_precision option and you
need to provide timestamp values which reflect this precision. If you don't do
this, your points will be squashed!
A point is uniquely identified by the measurement name, tag set, and timestamp. If you submit a new point with the same measurement, tag set, and timestamp as an existing point, the field set becomes the union of the old field set and the new field set, where any ties go to the new field set. This is the intended behavior.
See How does InfluxDB handle duplicate points? for details.
For example, this is how to specify millisecond precision (which moves the pitfall from the second- to the millisecond barrier):
client = InfluxDB::Client.new(time_precision: "ms")
time = (Time.now.to_r * 1000).to_i
client.write_point("foobar", { values: { n: 42 }, timestamp: time })For convenience, InfluxDB provides a few helper methods:
# to get a timestamp with the precision configured in the client:
client.now
# to get a timestamp with the given precision:
InfluxDB.now(time_precision)
# to convert a Time into a timestamp with the given precision:
InfluxDB.convert_timestamp(Time.now, time_precision)As with client.write_point, allowed values for time_precision are:
"ns" or nil for nanosecond"u" for microsecond"ms" for millisecond"s" for second"m" for minute"h" for hour…If you would rather receive points with integer timestamp, it's possible to set
epoch parameter:
…Working with parameterized query strings works as expected:
…You can also pick the database to query from:
influxdb.query 'select * from time_series_1', database: 'database'If you expect large quantities of data in a response, you may want to enable
JSON streaming by setting a chunk_size:
influxdb = InfluxDB::Client.new database,
username: username,
password: password,
chunk_size: 10000See the [official documentation][docs-chunking] for more details.
By default, InfluxDB::Client will keep trying (with exponential fall-off) to
connect to the database until it gets a connection. If you want to retry only
a finite number of times (or disable retries altogether), you can pass the
:retry option.
:retry can be either true, false or an Integer to retry infinite times,
disable retries or retry a finite number of times, respectively. Passing 0 is
equivalent to false and -1 is equivalent to true.
…This index might be out of date. Please refer to InfluxDB::DEFAULT_CONFIG_OPTIONS,
found in lib/influxdb/config.rb for the source of truth.
| Category | Option | Default value | Notes |
|---|---|---|---|
| HTTP connection | :host or :hosts |
"localhost" | can be an array and can include port |
:port |
8086 | fallback port, unless provided by :host option |
|
:prefix |
"" | URL path prefix (e.g. server is behind reverse proxy) | |
:username |
"root" | user credentials | |
:password |
"root" | user credentials | |
:open_timeout |
5 | socket timeout | |
:read_timeout |
300 | socket timeout | |
:auth_method |
"params" | "params", "basic_auth" or "none" | |
| Retry | :retry |
-1 | max. number of retry attempts (reading and writing) |
:initial_delay |
0.01 | initial wait time (doubles every retry attempt) | |
:max_delay |
30 | max. wait time when retrying | |
| SSL/HTTPS | :use_ssl |
false | whether or not to use SSL (HTTPS) |
:verify_ssl |
true | verify vertificate when using SSL | |
:ssl_ca_cert |
false | path to or name of CA cert | |
| Database | :database |
empty | name of database |
:time_precision |
"s" | time resolution for data send to server | |
:epoch |
false | time resolution for server responses (false = server default) | |
| Writer | :async |
false | Async options hash, details here |
:udp |
false | UDP connection info, details here | |
:discard_write_errors |
false | suppress UDP socket errors | |
| Query | :chunk_size |
empty | details here |
:denormalize |
true | format of result |
git clone [email protected]:influxdata/influxdb-ruby.git
cd influxdb-ruby
bundle
bundle exec rakeCHANGELOG.md in the "unreleased" section on top.bundle exec rake.No open issues yet, or sync has not completed.