Rob Sears bio photo

Rob Sears

       

Rocket scientist. Computer hacker. Geek before it was cool.

BTC Donations:
1AU9qGkSubhR24r8Y4WEoV8bccZjeT2dKg

A few years go when we were first getting Bonsai off the ground, we were fielding a ton of questions about how to set up a Rails app to integrate with Bonsai on Heroku. After answering the umpteenth email about it in a day, I mused that it would probably be easier to simply create a gem to do the (minimal) work needed. I went ahead and simply whipped one up and put it on RubyGems.org, and linked it out to anyone who asked about it.

Flash forward to today, and we had a question about the gem. I had actually forgotten that I’d made it. I checked the stats and saw that it had been installed 16,058 times! Holy crap! Granted, that is definitely not unique apps. A lot of users have CI processes that involve spinning up free staging apps on Heroku with a set configuration for testing purposes. The gem was probably in the Gemfile of a lot of these kinds of automated builds, and hence inflated the stats. But damn, though.

I figured that it was probably worth performing some cleanup on the gem. I only ever expected maybe a few dozen users, a couple hundred at the most, and the code I posted was quick and dirty. It’s just a shim, after all. Scaling and performance aren’t really a concern for a piece of code for which the sole purpose is to read an ENV variable on initialization.

Anyway, I was able to find the old code and do some work to bring it up to at least the RubyGems code standards. There isn’t a test suite for it (yet), but it’s so stupidly easy I had trouble justifying the extra time. Maybe later. At least now it’s cleaner and on GitHub where users can create proper issues and pull requests.

How to use it

The first question is: are you a customer of Bonsai Elasticsearch? If not, then this gem isn’t really built for you. You can still use it, but it’s sort of pointless.

Basically, when a user adds Bonsai to their Heroku app, we create an environment variable called BONSAI_URL that is populated with a link to a new Elasticsearch cluster. This gem simply reads that variable and instantiates the Elasticsearch client with the URL. This effectively allows the user to avoid needing to set up an initializer or create and destroy clients within the app.

Like I said, it’s dead simple. Here’s the entirety of the code:

require 'elasticsearch/model'
require 'logger'

url    = ENV['BONSAI_URL']
logger = Logger.new(STDOUT)

unless url.nil?
  url_safe = url.gsub(/[a-z0-9]{1,}\:[a-z0-9]{1,}@/, "redacted:redacted@")
  logger.info("Starting up a new Bonsai Elasticsearch client with url: #{url_safe}")
  Elasticsearch::Model.client = Elasticsearch::Client.new url: url
else
  logger.warn('Could not find a `BONSAI_URL` environment variable with a cluster URL. The bonsai-elasticsearch-rails can not create an Elasticsearch client. Please contact support at: [email protected]')
end

Users on Heroku

Simply add this to your Gemfile:

gem 'bonsai-elasticsearch-rails'

Users not on Heroku

Add the gem to your Gemfile:

gem 'bonsai-elasticsearch-rails'

Then make sure to set the BONSAI_URL environment variable:

$ export BONSAI_URL="<whatever your URL happens to be>"

Incidentally, this will also work for non-Bonsai customers. Just provide a link to your own cluster. As long as the BONSAI_URL variable points to a legit cluster, the gem will save you a little bit of work.