Skip to content

Instantly share code, notes, and snippets.

@scottlepp
Created October 28, 2020 16:45
Show Gist options
  • Save scottlepp/b368c140c054c7f1ee63a9f45d8b333b to your computer and use it in GitHub Desktop.
Save scottlepp/b368c140c054c7f1ee63a9f45d8b333b to your computer and use it in GitHub Desktop.
Mongo

Observing and visualizing MongoDB with Grafana The official MongoDB Plugin for Grafana


The Grafana MongoDB plugin allows observation and visualization of not only your MongoDB data, but also allows capturing valuable diagnostic metrics so you can monitor your MongoDB database/cluster.

Observing and Visualizing your MongoDB Data

Lets take a look at some sample data, from a database called sample_mflix, provided by MongoDB Atlas, a cloud provider of MongoDB (you can try this out for free).

The MongoDB plugin provides an editor where you can write/paste your MongoDB queries. We have augmented the standard MongoDB query syntax to allow for one line queries.

Instead of:

use sample_flix
db.movies.find()

You can simply do:

sample_mflix.movies.find()

OK. Lets look at some time series data! Using the following query, we can look at movie production over the last 2 decades. Sorting is typically part of the aggregate pipeline. The additional sort function here is another augmentation to get around the Atlas free tier sort limitation.

sample_mflix.movies.aggregate([
{"$match": { "year": {"$gt" : 2000} }},
{"$group": { "_id": "$year", "count": { "$sum": 1 }}},
{"$project": { "_id": 0, "count": 1, "time": { "$dateFromParts": {"year": "$_id", "month": 2}}}}
]).sort({"time": 1})

Movies Produced Per Year

Right away you might have noticed movie production steadily increased then dropped off after 2014. Seems strange? We can do some further investigation to see what is going on here. Let's run a query to see some more recent movies.

sample_mflix.movies.find({year: {$gt:2010}, released: {$ne: null}},{"released":1,"_id":0,title: 1, year: 1, rated: 1}).limit(1000).sort({"year": -1})

We can see that this dataset does not contain any movies after 2015. So that explains it. So with this plugin we can quickly identify anomalies in data, and even add alerting so we get notifications when something strange occurs!

Wouldn't it be cool to quickly see everything you ever wanted to know about a certain movie? The poster, ratings, awards, reviews, plot, writers, directors, etc? We can do that! Below is a dashboard using standard panels provided in Grafana.

You may have noticed the drop down for selecting the movie. The plugin supports template variables, which allows that feature. For more on template variables with MongoDb, see Grafana MongoDB Plugin.

We've really just scratched the surface here on how we can visualize and observe our data. The possibilities are endless. Now let's now move on to diagnostics.

MongoDB Diagnostics

Wouldn't it be great to monitor your MongoDB so you can quickly see, at a glance, everything that is happening under the covers? And set up alerts so you know ahead of time when something might fail?

What are some things we might want to monitor?

  • Server connections: available, current, and total connections
  • Network: Bytes in and out, and number of requests
  • Memory usage
  • Authenticated users
  • Connection pool stats: status, created, available, in use
  • Database stats: collections, data size, indexes, etc

You can capture all of that. Let's look at a diagnostics kitchen sink dashboard, just to get an idea of the capabilities here.

We've thrown a lot in here. What is great about Grafana is that you can build your own dashboard to your liking.

Conclusion

So with the Grafana MongoDB plugin we've shown that its possible to quickly visualize and observe not only MongoDB data, but also get diagnostic metrics so we can diagnose problems, and setup alerting so we know ahead of time when we need to make adjustments to not only keep our MongoDB up and running, but running efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment