Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
I

influxdb-ruby

> 开发工具
Open source

Ruby client for InfluxDB

371 stars0 likes2 views
WebsiteGitHub

About

Ruby client for InfluxDB

influxdb-ruby

The official Ruby client library for InfluxDB. Maintained by @toddboom and @dmke.

Note: This library is for use with InfluxDB 1.x. For connecting to InfluxDB 2.x instances, please use the influxdb-client-ruby client.

Contents

  • Platform support
  • Ruby support
  • Installation
  • Usage
    • Creating a client
    • Writing data
    • A Note About Time Precision
    • Querying
  • Advanced Topics
    • Administrative tasks
    • Continuous queries
    • Retention policies
    • Reading data
      • De-normalization
      • Streaming response
      • Retry
  • List of configuration options
  • Testing
  • Contributing

Platform support

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.

Ruby support

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 hour

Write data with a specific retention policy:

ruby
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:

ruby
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):

ruby
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:

ruby
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:

ruby
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):

ruby
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:

ruby
influxdb = InfluxDB::Client.new(
  udp: { host: "127.0.0.1", port: 4444 },
  discard_write_errors: true
)

influxdb.write_point('hitchhiker', { values: { value: 666 } })

A Note About Time Precision

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):

ruby
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:

ruby
# 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

Querying

…

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'

Streaming response

If you expect large quantities of data in a response, you may want to enable JSON streaming by setting a chunk_size:

ruby
influxdb = InfluxDB::Client.new database,
  username:   username,
  password:   password,
  chunk_size: 10000

See the [official documentation][docs-chunking] for more details.

Retry

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.

…

List of configuration options

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

Testing

bash
git clone [email protected]:influxdata/influxdb-ruby.git
cd influxdb-ruby
bundle
bundle exec rake

Contributing

  • Fork this repository on GitHub.
  • Make your changes.
    • Add tests.
    • Add an entry in the CHANGELOG.md in the "unreleased" section on top.
  • Run the tests: bundle exec rake.
  • Send a pull request.
    • Please rebase against the master branch.
  • If your changes look good, we'll merge them.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyclient-libinfluxdbrubytime-series

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具