Fork me on GitHub

Project Notes

Using InfluxDb 1.x with Ruby

Testing out the official ruby client calling a InfluxDb 1.7.9 server running in Docker.


The official influxdb-ruby gem does not support InfluxDb 2.x (yet?), so this test is running with InfluxDb 1.7.9.

Installing Ruby Gems

The Gemfile is setup for bundler:

$ gem install bundler
$ bundle install

Running InfluxDb in Docker

I’m using a Dockerized version of InfluxDb 1.7.9 for testing:

$ ./ start
Stopping/removing any previous docker container..
Error response from daemon: No such container: myinfluxdb17
Error: No such container: myinfluxdb17
Starting  container with web access on port 8086..

Exercising the Ruby Client

Creating a database:

$ ./create_database.rb
Creating database demo1..
Adding user admin/password..
$ ./list_databases.rb
List databases..

Stream Some Data

For some random data, I’m using the w command, that includes some processor stats in line 1:

$ w | head -1
23:21  up 18 days, 21:48, 15 users, load averages: 2.82 2.93 2.83

The stream_stats.rb example posts load averages every 5 seconds to a series called cpu with a bucket tag indicating past 1, 5, and 15 minute averages, so each data point will be like this:

  series: 'cpu',
  tags: { bucket: 'past1' },
  values: { load: 2.56 },
  timestamp: 1578414170

Running the stream…

$ ./stream_stats.rb
Streaming some stats to database:demo1 every 5 seconds..
Posting [2.01, 2.57, 2.62] at 1578417712..
Posting [2.17, 2.6, 2.63] at 1578417717..
Posting [2.23, 2.6, 2.63] at 1578417722..
Posting [2.13, 2.58, 2.62] at 1578417727..
Posting [2.04, 2.55, 2.61] at 1578417732..
Posting [2.04, 2.54, 2.6] at 1578417737..
Posting [2.2, 2.57, 2.61] at 1578417742..

Query Data

Doing a simple query for the last 6 points (select * from cpu WHERE bucket = 'past1' ORDER BY time DESC LIMIT 6)..

$ ./query_stats.rb
Querying the most recent cpu,bucket=past1 stats from database:demo1..
{"time"=>"2020-01-07T17:22:22Z", "bucket"=>"past1", "load"=>2.2}
{"time"=>"2020-01-07T17:22:17Z", "bucket"=>"past1", "load"=>2.04}
{"time"=>"2020-01-07T17:22:12Z", "bucket"=>"past1", "load"=>2.04}
{"time"=>"2020-01-07T17:22:07Z", "bucket"=>"past1", "load"=>2.13}
{"time"=>"2020-01-07T17:22:02Z", "bucket"=>"past1", "load"=>2.23}
{"time"=>"2020-01-07T17:21:57Z", "bucket"=>"past1", "load"=>2.17}

Credits and References

About LCK#117 databasesinfluxdbdockerruby
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.