Module 4 ยท Lecture 14

Grafana Fundamentals: Connecting Data to Dashboards

Install ยท Connect MongoDB Atlas ยท Build your first live panel

โฑ 90 minutes ๐Ÿ“ Prerequisites: Lectures 8โ€“9 (MongoDB Atlas data) ๐Ÿ”ง Lab: Docker + Grafana setup

Contents

  1. Learning Objectives
  2. Session Timeline
  3. What Is Grafana?
  4. The Grafana Mental Model
  5. Setup & Connection
  6. Hands-On Lab โ€” First Panel
  7. Panel Types Overview
  8. Key Takeaways

Learning Objectives

Session Timeline

TimeSegmentActivity
0โ€“10 minRecapModule 3 wrap-up โ€” from streaming to visualising
10โ€“25 minWhat Is Grafana?Architecture, who uses it, industry context
25โ€“40 minGrafana Mental ModelData sources, queries, panels, dashboards
40โ€“55 minSetup & ConnectionDocker install, MongoDB plugin, connect Atlas
55โ€“75 minHands-On LabBuild first panel: stock prices time series
75โ€“85 minPanel TypesTime Series, Stat, Bar Gauge, Table overview
85โ€“90 minA3 PreviewWhat students will build for Assignment 3

Section 1: What Is Grafana?

The Problem It Solves

In Modules 2 and 3, you built the full backend: Kafka producers, consumers, MongoDB storage, alert routing, and windowing. All that work produces data โ€” but the data lives in a database only engineers can query. Grafana closes this gap. It turns database queries into visual panels that refresh automatically, putting your streaming data in front of the people who need to act on it.

What It IsWhat It Isn't
A dashboard and visualisation platformA database
A query builder (UI on top of your data)A streaming system
An alerting layer with notification routingA replacement for MongoDB/Kafka
Open source + free for self-hosted useAn analytics/BI tool (that's Metabase, Tableau)

Who Uses Grafana

2014 โ€” Created by Torkel Odegaard as a fork of Kibana
2021 โ€” Grafana Labs valued at $3 billion
2024 โ€” ~20 million users, 200,000+ organisations
      Default monitoring dashboard at:
      Netflix, PayPal, Booking.com, Zepto, Swiggy, Paytm, HDFC Securities

Career context: When you walk into a data-forward company's operations centre and see a wall of live charts, there is a 70%+ chance you are looking at Grafana. Understanding this tool positions you to have meaningful conversations about data infrastructure โ€” as a manager, analyst, or product leader.

Section 2: The Grafana Mental Model

Four Building Blocks

  1. Data Source โ€” A connection to a database. Grafana supports 50+ natively (Prometheus, InfluxDB, MySQL) and 100+ via community plugins (MongoDB, BigQuery, Snowflake).
  2. Query โ€” The question you're asking of the data source. Written in the data source's native language (MongoDB aggregate pipeline, SQL, PromQL).
  3. Panel โ€” One visual unit: one chart, one number, one table. Each panel has exactly one query and one visualisation type.
  4. Dashboard โ€” A collection of panels on a grid. Has a time range selector and auto-refresh interval.

Time as a First-Class Citizen

Unlike static BI tools, Grafana treats time as the primary dimension. Every dashboard has a time range (last 5 min, last 1 hour, custom). This makes it ideal for streaming data โ€” you can zoom into the exact window when an alert fired.

Section 3: Setup & Connection

Install Grafana with Docker

docker run -d \
  --name grafana \
  -p 3000:3000 \
  -e GF_SECURITY_ADMIN_PASSWORD=admin \
  grafana/grafana-oss:latest

# Open: http://localhost:3000
# Login: admin / admin  (Grafana will prompt you to change it)

Install the MongoDB Community Plugin

docker exec -it grafana grafana-cli plugins install grafana-mongodb-datasource
docker restart grafana

Add MongoDB Atlas Data Source

1

Left sidebar โ†’ Connections โ†’ Data Sources โ†’ Add data source

2

Search "MongoDB" โ†’ select the community plugin

3

Connection string: mongodb+srv://<user>:<password>@cluster0.xxxxx.mongodb.net/
Database: sda_course

4

Click "Save & Test" โ€” should show โœ… "Data source is working"

Section 4: Hands-On Lab โ€” First Panel

Build a time series panel showing INFY stock price over the last 30 minutes, auto-refreshing every 10 seconds โ€” powered by the stock_prices collection from Lectures 8 & 9.

Step 1 โ€” New Dashboard

Left sidebar โ†’ Dashboards โ†’ New โ†’ New Dashboard โ†’ Add visualisation โ†’ Select MongoDB data source.

Step 2 โ€” Write the Query

{
  "collection": "stock_prices",
  "aggregate": [
    { "$match": { "symbol": "INFY" } },
    { "$sort": { "_saved_at": 1 } },
    { "$project": { "_id": 0, "time": "$_saved_at", "price": 1 } }
  ]
}

Map Time field โ†’ time | Value field โ†’ price

Step 3 โ€” Panel Settings

Alert Count Panel (Bonus)

Add a Stat panel for open alert count:

{
  "collection": "stock_alerts",
  "aggregate": [
    { "$match": { "status": "open" } },
    { "$count": "value" }
  ]
}

Thresholds: 0 = green | 5 = amber | 10 = red

Teaching moment: When a price spike fires an alert, students see both the time series move AND the alert counter increment in real time. This is the "aha" moment that makes the full pipeline visible โ€” from Kafka producer to live dashboard.

Section 5: Panel Types Reference

Panel TypeBest ForExample Use
Time SeriesTrends over timeStock price history, transaction volume per minute
StatSingle important numberCurrent price, total open alerts today
Bar GaugeProgress / comparisonAlert count per symbol (INFY vs TCS)
Bar ChartCategorical comparisonTransactions per city in last hour
TableRaw data / detail viewLatest 10 alerts from stock_alerts
GaugeSingle value with thresholdsRisk score: green/amber/red

Key Takeaways

โœ“ Key Takeaways โ€” Lecture 14

  1. Grafana is the visual layer โ€” it sits on top of databases, not inside them
  2. Four building blocks: Data Source โ†’ Query โ†’ Panel โ†’ Dashboard
  3. Time is central โ€” dashboards have a time range and auto-refresh, making them live
  4. MongoDB community plugin enables Atlas โ†’ Grafana; InfluxDB is the simpler native option
  5. Panel types serve different needs โ€” Time Series for trends, Stat for KPIs, Table for detail

Preview โ€” Lecture 15: Building Live Streaming Dashboards

Full hands-on build session: multi-panel stock monitor, variable dropdowns, the transactions dashboard, and Assignment 3 guidance. Keep Grafana running and your producer streaming before class.