|
| 1 | +--- |
| 2 | +title: "Displaying geohash tags from a Loki datasource in a Grafana Worldmap Panel" |
| 3 | +date: 2020-03-18T00:00:00+01:00 |
| 4 | +draft: true |
| 5 | +description: > |
| 6 | + How to use Loki and the Worldmap panel plugin to show geohashes or latitude/longitude pairs in a map. |
| 7 | +tags: ["grafana", "loki", "worldmap", "geohash"] |
| 8 | +--- |
| 9 | + |
| 10 | +[Loki](https://grafana.com/oss/loki/) is a new~ish project from [Grafana](https://grafana.com), yes |
| 11 | +the same company behind the popular open-source observability platform. Loki itself is a |
| 12 | +horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus. |
| 13 | + |
| 14 | +On fewer words, Loki is like Prometheus but for your logs. This means that although it doesn't |
| 15 | +provide all the full-text search capabilities of Elasticsearch, it allows filtering by a set of |
| 16 | +labels for each log stream. This translates into a more cost-effective solution. |
| 17 | + |
| 18 | +Even though Loki was announced on [KubeCon |
| 19 | +18'](https://kccna18.sched.com/event/GrXC/on-the-oss-path-to-full-observability-with-grafana-david-kaltschmidt-grafana-labs). |
| 20 | +It is still under active development. As expected the team is building in parallel the internal Loki |
| 21 | +components: ingestion, storage, and query layer and the visualization UI (Grafana). It was not until recently that I've started to play with Loki. My main experience is about |
| 22 | +running large [Solr](https://lucene.apache.org/solr/) and/or |
| 23 | +[Elasticsearch](https://www.elastic.co/de/elasticsearch) clusters, but I was looking for something |
| 24 | +a bit easier and cheaper to host by myself. After all, I was not interested in all |
| 25 | +the features from the ELK stack. |
| 26 | + |
| 27 | +At the moment my data consist of a few labels and no actual logline, which is ok. I'm |
| 28 | +more interested only in the labels. Some of those labels (in my case) contained the latitude (`lat`), |
| 29 | +longitude (`long`) and geohash (`geohash`) of a given location. I also included some descriptive |
| 30 | +information like `place` and `country`. |
| 31 | + |
| 32 | +I wanted to plot this into a map using the [Worldmap |
| 33 | +Panel](https://grafana.com/grafana/plugins/grafana-worldmap-panel/installation). Yet, it was a bit |
| 34 | +tricky. I had already run a container with the latest version of Grafana since I was using |
| 35 | +the Explore UI from Grafana with the Loki datasource to check the incoming data. I ran the |
| 36 | +following command to add the world map panel: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ docker run -d -p 3000:3000 -e "GF_INSTALL_PLUGINS=grafana-worldmap-panel" grafana/grafana |
| 40 | +``` |
| 41 | + |
| 42 | +My initial thought was to point the Worldmap Panel to the Loki datasource and run one of the |
| 43 | +[LogQL](https://github.com/grafana/loki/blob/master/docs/logql.md) queries against it. The query |
| 44 | +ended up looking like: |
| 45 | + |
| 46 | +```js |
| 47 | +sum(count_over_time({geohash=~".+"}[1h])) by (geohash,lat,long,country,place) |
| 48 | +``` |
| 49 | + |
| 50 | +My initial thought was to point the Worldmap Panel to the Loki datasource and run one of the |
| 51 | +[LogQL](https://github.com/grafana/loki/blob/master/docs/logql.md) queries against it. The query |
| 52 | +ended up looking like: |
| 53 | + |
| 54 | +This didn't work. Using the query inspector from Grafana I noticed that the response payload |
| 55 | +from Loki was almost identical to the output of a Prometheus query. The next step was |
| 56 | +to try and replicate the setup shown in [this |
| 57 | +post](https://www.robustperception.io/using-geohashes-with-the-worldmap-panel-and-prometheus). |
| 58 | + |
| 59 | +Trying to show the data setting the Location Data as a geohash yielded this error: |
| 60 | +``` |
| 61 | +Error: Missing geohash value |
| 62 | +``` |
| 63 | +Which made a bit of sense, since we need to set the `{{ geohash }}` as the legend of our query. This |
| 64 | +makes the `geohash` label (when using a Prometheus datasource) available to the Worldmap panel. Since |
| 65 | +the legend input is missing from the Loki datasource it is not possible to do set it up this way. |
| 66 | +Since I already have the `lat` and `long` available from the query, I also tried to use the |
| 67 | +Location Data as a table, the setup ended up looking like: |
| 68 | + |
| 69 | +{{< picture "loki-table-fail" "Setup of the Loki datasource as a table" "50%" >}} |
| 70 | + |
| 71 | +Although this setup didn't produce any error it didn't visualize anything on the map either 🤷♂️. |
| 72 | +The missing **piece of the puzzle** is that we can configure the Loki datasource as a Prometheus |
| 73 | +datasource in Grafana 🤯. |
| 74 | + |
| 75 | +To do this we need to create a Prometheus datasource in Grafana but point it out to the Loki |
| 76 | +endpoint: |
| 77 | + |
| 78 | +{{< picture "loki-and-prometheus-datasources" "Both datasources configured in the Grafana instance" >}} |
| 79 | + |
| 80 | +As you can see we use the same URL, but we need to add the `/loki` path to the Prometheus datasource. |
| 81 | + |
| 82 | +Using Loki as a Prometheus datasources allows us to use the same query as before, but have |
| 83 | +access to the configuration options only available for a Prometheus datasource. We can now follow the |
| 84 | +instructions in the [mentioned |
| 85 | +post](https://www.robustperception.io/using-geohashes-with-the-worldmap-panel-and-prometheus) to |
| 86 | +visualize the labels stored in a Loki datasource in the Worldmap panel. |
| 87 | + |
| 88 | +## Summary |
| 89 | + |
| 90 | +TL;DR you can configure a Loki datasource as a Prometheus datasource |
| 91 | +in Grafana, which will provide the same bells and whistles of a normal Prometheus server. Keep in mind that |
| 92 | +LogQL, the query language implemented by Loki, is a subset of PromQL, which means that not all |
| 93 | +functions, aggregations or operators will be available. |
| 94 | + |
| 95 | +This step of configuring Loki as a Prometheus datasource is mandatory (until the publishing date of |
| 96 | +this post) to get the Worldmap Panel plugin playing nicely with your Loki datasource. I imagine that |
| 97 | +in the no so far future Grafana's Loki support will be easier to use and it will change its behavior |
| 98 | +depending on how you want to use the data, which will make things (like using a 3rd party plugin) |
| 99 | +easier. |
0 commit comments