Monitoring a Rails app using Prometheus and Grafana — DevOps Project 08

This is me learning and building in public, I have complete 8 DevOps Projects by far, see them all here.

Bhavyansh @ DiversePixel
4 min readJun 28, 2024

In this one, we will be using Prometheus and Grafana to monitor our rails application, Geeky.

First: key pair

2. security groups

Ensure that your security groups allow the necessary traffic between these instances. For example:

  • Rails instance: Allow traffic on port 3000 (or whichever port your app is running on).
  • Prometheus instance: Allow traffic on port 9090.
  • Grafana instance: Allow traffic on port 3000.
  • Allow internal communication between these instances on necessary ports.

We will create two security groups:

  • app-sg: for rails
  • monitoring-sg: for prometheus and grafana

Create instances

Application prereqs

On a temporary instance, installed ansible, now will run the playbook (this is me taking help from a previous project of mine, in which I wrote ansible playbook that deploys an app to production using just a few commands, here is the walkthrough)

Our app is up

Next we will be setting up prometheus and grafana

The prometheus scraping itself
Endpoint configured to show metrics
our target is up and running

Install Grafana on Debian or Ubuntu | Grafana documentation

Access it on port 3000 of your grafana instance

Add data source

Give the details

Now create a dashboard

And we can see our metrics on grafana

And on our application /metrics endpoint

/metrics endpoint

So we used monitoring tools — Prometheus and Grafana, to set up monitoring for our rails app.

With the stack of Prometheus and Grafana, we can do a lot more to monitor, analyze, and improve our application and infrastructure. Here are some advanced use cases and capabilities:

Advanced Monitoring and Alerting

  1. Advanced Metrics Collection
  • Custom Instrumentation: Besides basic metrics, you can instrument custom application-specific metrics to track key performance indicators (KPIs) specific to your business logic.
  • Middleware Metrics: Collect metrics from middleware layers such as caching (Redis, Memcached), message queues (RabbitMQ, Kafka), and more.
  • External Service Monitoring: Monitor the performance and availability of external services and third-party APIs your application depends on.

2. Alerting with Prometheus

  • Alertmanager: Set up Prometheus Alertmanager to handle alerts. You can define alerting rules in Prometheus and configure Alertmanager to send notifications via email, Slack, PagerDuty, etc.
  • Anomaly Detection: Use Prometheus to detect anomalies in your metrics, such as spikes in latency or error rates, and trigger alerts based on these anomalies.

3. Infrastructure Monitoring

  • Node Exporter: Use the Prometheus Node Exporter to collect hardware and OS metrics from your servers. This includes CPU, memory, disk, and network metrics.

4. Security Monitoring

  • Security Metrics: Collect and monitor security-related metrics such as authentication attempts, authorization failures, and other security events.

5. Webhooks and APIs

  • Webhooks: Use Prometheus Alertmanager webhooks to integrate with other systems and automate responses to alerts.

Questions, suggestions always welcome.

Connect with me on X @ bhavyansh001, I am learning and building in public!

--

--

Bhavyansh @ DiversePixel

Hey I write about Tech. Join me as I share my tech learnings and insights. 🚀