Create Amazing Stat Dashboards for Your App Locally Using Docker and Grafana

DZone 's Guide to

Create Amazing Stat Dashboards for Your App Locally Using Docker and Grafana

Learn how to whip out comprehensive dashboards of stats for apps with Docker and Grafana. Here's a look at what Grafana is, using Grafana with Docker, and how to instrument your code.

· Performance Zone ·
Free Resource

One of the most important but often forgotten facets when building a system is the ability to see what’s going on inside. Logging everything to the console is the usual worst-case scenario which involves writing scripts and scrapers to create CSVs to import into Excel. It’s all very manual and it’s horrible.

You’ve probably got an item on your backlog called “build admin/graphing system”. It’s been there since you first went live because it got pushed back by business when you were rushing to go live. It never gets prioritised which results in you spending hours each week on manual processes to get key stats and leads to you being purely reactive, not proactive.

Fortunately, there is a solution. It’s free and easy to integrate and you can have it up and running, at least locally, in the next 30 minutes. It’s called Grafana, and it rocks.

What is Grafana?

At a high level, it’s a beautiful, functioning graphing and dashboard system. When combined with Graphite (an uglier, less functional graphing system which does the grunt of the work) and Carbon, an underlying data storage mechanism, it forms the ELK stack. It exposes ports for you to fling time series data at which you can then turn into graphs and dashboards. These are great for throwing up on big monitors to be viewed by all or for drilling through for diagnosis.

Here’s a pretty picture to give you an idea:

Image title

Grafana With Docker

It’s pretty much impossible for you to have avoided hearing about Docker. You can read up about it here, but for our purposes it provides us with the ability to start a box locally which has the entire stack built for us already!

If you haven’t got docker installed already, head to here and follow the instructions for your relevant platform.

We can now use this great image on GitHub to fireup the entire ELK stack locally. Simply copy this into your command prompt:

docker run \
  --detach \
   --publish=80:80 \
   --publish=81:81 \
   --publish=8125:8125/udp \
   --publish=8126:8126 \
   --name kamon-grafana-dashboard \

On the first run, this will download the box from the docker repository and then run it on your machine. If you look through your logs you will see the IP address docker is using:

Image titlePop this it in your browser and you’ll be greeted with the login screen for Grafana:

Image title

It’s set to the default admin:admin.

Image title

Let’s park this for now and go over to our application.

Instrumenting Our Code

We can use the Metrics library (which I discussed in this post here) to quickly and simply instrument our application.

Drop this somewhere in your codebase to start the reporting, replacing the IP address with the one from your logs:

    final MetricRegistry metrics = new MetricRegistry();

        final Graphite graphite = new Graphite("", 2003);
        final GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
        reporter.start(10, TimeUnit.SECONDS);

This will publish the stats out every 10 seconds, but can obviously be configured to your needs.

You can now go through your application an instrument your code. For this example I’m just going to randomly output a number, and reporting it as a histogram.

   Histogram randomGen = metrics.histogram(name("dzone", "random"));

        Random random = new Random();

We can now run the application and it will start reporting data.


If we head over to the graphite on port :81 you can hopefully see the statistic in the tree. The stat will be named based on whatever you put in name(String… names) of your metric. For us, it will show under dzone.random.

Image title

As I used the histogram it automatically creates all of these statistics for us. We can now go back to grafana to create our dashboard.

Configuring Our Dashboard

Head back to grafana and click “Data Sources” then “Add new”. Give the data source any name, then put in your docker IP address at the port 81 and hit save.

Image title

Go to dashboards, and press the “home” drop down. Select “New” at the bottom. We now have a brand new blank dashboard. Click “Add Row” button, which should give you a new row, with a green mark on the left. We can now add a graph under “Add Panel”. Click the title of the graph and “edit” and we can put our stats up. Under metrics, we can switch the datasource to “graphite” and select our stat.

Image titlePoke around with the different options and build a dashboard with the stats. Here’s something I knocked up in 5 minutes:

Image title

graphite, java, monitoring, monitoring and performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}