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

Automated Monitoring With Grafana and Prometheus

DZone's Guide to

Automated Monitoring With Grafana and Prometheus

In this article, I would like to explain a Docker image that I created to keep Grafana dashboards in sync across environments.

Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Depending on the number of environments in which you have monitoring enabled, it may be cumbersome to keep all your Grafana dashboards synchronized across environments when changes occur.

In this article, I would like to show a Docker image I created to keep Grafana dashboards in sync across environments.

What's in the Image

  • Prometheus.
  • Grafana.
  • Supervisor.

How to Use It

With all options:

docker run --name automated-grafana -d -p 9090:9090 -p 6666:6666 \
       -e "ENVIRONMENT=prod" \
       -e "GF_SERVER_HTTP_PORT=6666" \
       -e "WAITING_TIME=20" \
       -v `pwd`/prometheus-config:/prometheus-config \
       -v `pwd`/dashboards:/dashboards \
       -v `pwd`/users:/users \
       -v `pwd`/sources:/sources \
       serragnoli/automated-grafana-prometheus
  • -p 9090:9090: Exposes the Prometheus default port to the host.

  • -p 6666:6666: Exposes a custom Grafana port that was determined by -e "GF_SERVER_HTTP_PORT=6666"; both of these parameter values need to match.

  • -e "ENVIRONMENT=prod": Determines which Prometheus config to use. Ensure that a prometheus-<env> .yml suffixed with the environment where you'll deploy to exists in the directory. In this example, the value is prod and /prometheus-config/prometheus-prod.yml is expected to exist.

  • -e "WAITING_TIME=20": By default, each JSON file is loaded 10 seconds apart from each other. There may be situations that 10 seconds may be too much or too little time. When that's the case, use this variable to control the interval of loading each JSON file.

  • -v `pwd`/dashboards:/dashboards
    -v `pwd`/users:/users
    -v `pwd`/sources:/sources
    : Map the directories containing the JSON files to be loaded by Grafana in the container. It's worth mentioning that the /dashboards directory must only contain dashboard JSON files and the same applies to the other directories.

After running the above command, you should have Prometheus running on localhost:9090 and Grafana running on localhost:6666.

Advantages

  • You can run the same image everywhere; just use a Prometheus config file that points to /metrics endpoints for the environment you're deploying to.

  • You can load as many dashboards, users, and data-source JSON files as you want by only mapping the directories that contain these files from the host to the container.

  • You get fewer moving parts in your monitoring system. Therefore, there are fewer things to worry about, i.e. lost/slow network connectivity between Prometheus and Grafana.

Disadvantages

Final Considerations

I've added sample JSON files to the image page in DockerHub: dashboard.json, user.json, and data-source.json.

I can't tell if I have tried to reinvent the wheel with this image, but the is fact that I couldn't find anything already automating Grafana dashboards in the same way.

As I mentioned above, this is currently my favorite solution and if I learn a better way of doing it, I'll update this article.

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:
grafana ,monitoring ,prometheus ,performance ,automated monitoring

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 }}