# Loki 0.4.0 querying Metrics From Logs

**URL:** <https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998>\
**Category:** Grafana Loki\
**Tags:** loki\
**Created:** [November 18, 2019, 9:39am UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998 "2019-11-18T09:39:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dobostamas](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@dobostamas](https://community.grafana.com/u/dobostamas)\
**Post date:** [November 18, 2019, 9:39am UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/1 "2019-11-18T09:39:40Z")

</div>

Dear Grafana Community!  
I am quite new in the world of Grafana and I am looking for showing metrics from log content parsed with Promtail to labels.  
Did I get it right from the newsletter of the Loki 0.4.0 that via new Prometheus compatible API it is possible to do it?

> **[What’s New in Loki v0.4.0](https://grafana.com/blog/2019/10/28/whats-new-in-loki-v0.4.0/)**
>
> Loki v0.4.0 has just been released. Here’s a look at the new features.

  
If it is not necessary I would like to keep the tools I am using as minimum it is possible: Promtail, Loki, Grafana.

My use case would be to get out/parse metrics from log lines and show on Grafana. E.g. in every 10 seconds a new log line is appearing in log file showing some characteristics of the system like free memory, network usage, etc.  
I am already able to parse the log lines with Promtail as labels and send to Loki.  
Is is possible to reference only the labels by its name to show the values over time?

Thanks,  
Tamás

---

<div class="post-metadata">

**Author:** ![daniellee](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/daniellee/32/41_2.png) [@daniellee](https://community.grafana.com/u/daniellee)\
**Post date:** [November 18, 2019, 3:57pm UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/2 "2019-11-18T15:57:50Z")

</div>

It is possible with Grafana 6.4 - you add Loki as a prometheus data source. See this video for a [demo](https://youtu.be/J_nAt0XX0qw) - around 1:30.

Support in the UI in Grafana for metrics from Loki logs just got merged: [https://github.com/grafana/grafana/pull/20158](https://github.com/grafana/grafana/pull/20158) and will be included in the next release (6.6) in January.

There are packages and docker containers for the latest master build if you want to test it out now: [https://grafana.com/grafana/download/6.6.0-687351e1pre](https://grafana.com/grafana/download/6.6.0-687351e1pre)

---

<div class="post-metadata">

**Author:** ![dobostamas](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@dobostamas](https://community.grafana.com/u/dobostamas)\
**Post date:** [November 18, 2019, 4:51pm UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/3 "2019-11-18T16:51:51Z")

</div>

Hi Daniellee,  
Thanks for the response! I have also checked the video but as far as I have understood the example it is possible to count the number of logs/second matching on a query.  
My use case would be to show the value of the label I am choosing e.g.:  
2019-11-18 17:39:01.000 memory\_free 15  
2019-11-18 17:38:01.000 memory\_free 14

If I add to the Query section e.g. {memory\_free=“15”} it selects the right entry from the Loki but the return object will contain 2 entries:

- stream object with all of the labels using : “” form
- values array of timestamp and whole log line.

How can I add to the graph the labels from stream object? 🙂

Thanks and best regards,  
Tamás

---

<div class="post-metadata">

**Author:** ![daniellee](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/daniellee/32/41_2.png) [@daniellee](https://community.grafana.com/u/daniellee)\
**Post date:** [November 21, 2019, 12:20am UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/4 "2019-11-21T00:20:23Z")

</div>

Assuming that you created a prometheus data source that points to your Loki instance - have you tested with the rate function. Does something like this work:

`sum(rate({your_label="a_value"}[1m])) by (memory_free)`

---

<div class="post-metadata">

**Author:** ![dobostamas](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@dobostamas](https://community.grafana.com/u/dobostamas)\
**Post date:** [December 9, 2019, 3:32pm UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/5 "2019-12-09T15:32:27Z")

</div>

Thanks Danielle!  
It seems I have misunderstood something regarding Loki.  
I wanted to extract metrics from the logs and show it in Grafana while I am leaving out Prometheus from the game. As I have learnt the two tools shall work together efficiently. 🙂

Thanks a lot for your help again!

Best regards,  
Tamás

---

<div class="post-metadata">

**Author:** ![chenawi](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@chenawi](https://community.grafana.com/u/chenawi)\
**Post date:** [December 3, 2020, 3:19pm UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/6 "2020-12-03T15:19:07Z")

</div>

I have the exact same question but now on Loki 2.0.0. Is it possible to graph e.g. average cpu with Loki datasource and log lines such as:

2020-12-03 15:51:01 cpu\_usage=3.89

---

<div class="post-metadata">

**Author:** ![ewelch](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/ewelch/32/13744_2.png) [@ewelch](https://community.grafana.com/u/ewelch)\
**Post date:** [December 10, 2020, 4:44pm UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/7 "2020-12-10T16:44:09Z")

</div>

Yes!

```auto
avg(avg_over_time{app="foo"} | regexp `.*cpu_usage=(?P<cpu_usage>.*)` | unwrap cpu_usage[1m])

```

or to average by a label:

```auto
avg by (pick some labels) (avg_over_time{app="foo"} | regexp `.*cpu_usage=(?P<cpu_usage>.*)` | unwrap cpu_usage[1m])

```

- These queries may not be 100% correct but hopefully gives you the idea, you may need to adjust your regex to match better.

Take a look at the [logql docs](https://grafana.com/docs/loki/latest/logql/) or the [release blog post](https://grafana.com/blog/2020/10/28/loki-2.0-released-transform-logs-as-youre-querying-them-and-set-up-alerts-within-loki/) for some more info!

---

<div class="post-metadata">

**Author:** ![chenawi](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@chenawi](https://community.grafana.com/u/chenawi)\
**Post date:** [December 11, 2020, 8:31am UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/8 "2020-12-11T08:31:43Z")

</div>

Awesome @ewelch, thank you!

---

<div class="post-metadata">

**Author:** ![kdsumit04](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kdsumit04](https://community.grafana.com/u/kdsumit04)\
**Post date:** [May 15, 2021, 9:24am UTC](https://community.grafana.com/t/loki-0-4-0-querying-metrics-from-logs/22998/9 "2021-05-15T09:24:06Z")

</div>

How to plot a graph in grafana using a value which is present in my log.  
My log looks something like this.  
INFO 2021-05-15 09:15:30,752 - Greet-Client is Started , duration=0.003822803497314453s  
I want to keep duration as my Y-axis. Can someone show some light on how to do this.  
I have loki as my data source and these logs are coming from my Kubernetes app  
Thanks in advance!!  
@ewelch
