Monitor what developers write in your Rails console

Or how to tune Pry to send all user input to slack

Théo Carrive
Cheerz Engineering
3 min readFeb 20, 2019

--

Pry console to Slack channel

Why?

We try to avoid manual actions on our production console as much as we can.

However, it happens that for some reasons, we absolutely have to do something on our production console. Configuring something that is not accessible from the admin, doing an extract, debugging some weird problem.

It happens. But at some point, we want to:

  • Share knowledge about what other people do
  • Avoid an increasing amount of manual actions

The right thing to do then, is to monitor it. So, let’s do it!

What do we want exactly?

We want something:

  • Simple to set-up
  • Easily accessible by our development team
  • That centralizes information for many servers

We had many options in mind, like sending information to NewRelic, Datadog, or by email, but the one that seemed to be the best for us was something that every developer has access to and check multiple times per day, so, very simply… Slack.

OK great, so, let’s implement it!

Implementation

Pry hook

First of all, we need to think about how we are going to hook into Pry.

When pry starts, it checks for a pryrc file […] in the current directory(./.pryrc).

Neat! And what are we going to do with this?

Pry.config.input is an option determining the input object.
Pry accepts any object that implements the readline method.
When creating a custom object, you may also want to implement the completion_proc= and completion_proc methods.

OK that’s it. We have everything we need. We create a .pryrc file in our project, in which we will override the input by an instance of a class that we will create : ReadlineLogger.

The logger

Ok nice. So let’s implement that ReadlineLogger. What we want is a class that will implement 3 methods:

  • readline(value)
  • completion_proc
  • completion_proc=(value)

Let’s rely on the original Readline class to do its job, while we just send notification to Slack with another class that we will create soon:

The Slack notification service

Great! The only thing remaining is the Slack notification itself.

Easy peasy 🍋.

Et voilà!

Send Rails console input directly to slack

You can find the gist here: https://gist.github.com/rocketlobster/0ba9dbf14b5957e9bc3cab12d56608fb

Thoughts

  • We found it quite interesting to see what you can do when Pry loads.
  • Having everything is on Slack is great to monitor what should be automated when your team has more than a few developers
  • Having everything on Slack is a great way to share knowledge.

Do you want to see more than what we’ve shown here?

Don’t hesitate to contact us, it would be a pleasure to talk, or even to welcome for you for a coffee at our office (France 🇫🇷-> Paris 🏙-> Place de Clichy / Saint Lazare 🚊)!

Just so you know, we are hiring engineers! Don’t hesitate to contact us, or check our page here!

--

--