Skip to content

Commit 832de4d

Browse files
"Displaying geohash tags from a Loki datasource in a Grafana Worl… (#28)
* Initial draft * Optimised images with calibre/image-actions Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ef42bc1 commit 832de4d

6 files changed

Lines changed: 112 additions & 9 deletions

File tree

content/posts/2019/rename-kibana-index-patterns/rename-kibana-index-patterns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ If you want to know more about the structure of the documents that Kibana
146146
persists in ES (for its internal use) you should check [this blog
147147
post](https://www.elastic.co/blog/kibana-under-the-hood-object-persistence)
148148

149-
{{< /info >}}
149+
{{< /info >}}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.

layouts/shortcodes/picture.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{{ $width := default "100%" (.Get 2) }}
2+
13
<picture>
24
{{ if not .Site.IsServer }}
35
<source
@@ -14,5 +16,6 @@
1416
class="align-center"
1517
alt="{{ .Get 1 }}"
1618
loading="lazy"
19+
style="max-width:{{ $width }};"
1720
/>
1821
</picture>
50.4 KB
Loading
111 KB
Loading

themes/jorgelbg/static/css/style.css

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,6 @@ li {
266266
list-style-position: inside;
267267
}
268268

269-
#post-body,
270-
p {
271-
line-height: 1.7;
272-
}
273-
274269
b {
275270
font-weight: 500;
276271
color: var(--text-color);
@@ -763,8 +758,8 @@ h1 {
763758
font-weight: 400;
764759
font-style: normal;
765760
font-size: 1.22rem;
766-
line-height: 1.58;
767-
letter-spacing: -0.003em;
761+
line-height: 1.65;
762+
letter-spacing: 0.025em;
768763
}
769764

770765
.post h1 {
@@ -870,7 +865,7 @@ footer p.small {
870865

871866
.post header h1 svg {
872867
display: none;
873-
margin-left: -36px;
868+
margin-left: -24px;
874869
}
875870

876871
.post header h1 > a {
@@ -981,3 +976,9 @@ header .last-edited {
981976
text-transform: uppercase;
982977
margin-bottom: 30px;
983978
}
979+
980+
picture {
981+
display: flex;
982+
justify-content: center;
983+
margin-bottom: 20px;
984+
}

0 commit comments

Comments
 (0)