Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Discover 50 of the latest mobile performance statistics with the Ultimate Guide to Digital Experience Monitoring, brought to you in partnership with Catchpoint.

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 \
   kamon/grafana_graphite

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("192.168.99.100", 2003);
        final GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .filter(MetricFilter.ALL)
            .build(graphite);
        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();
        while(true){
            randomGen.update(random.nextInt(100));
            Thread.sleep(random.nextInt(5000));
        }

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

Graphite

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

Is your APM strategy broken? This ebook explores the latest in Gartner research to help you learn how to close the end-user experience gap in APM, brought to you in partnership with Catchpoint.

Topics:
monitoring ,graphite ,java ,monitoring and performance

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}