LogQL : How to get the last values? - grafana

I am parsing access logs from Squid (proxy) with Loki and Grafana.
I am trying to do something simple : display top 10 requested domains (or IP) in the last 24h.
To do so, I created a Loki label which extract the domain from the log line in the Loki pipeline with a regex.
So in the LogQL query I have access to a label named 'connection' which is the domain.
I don't manage to extract the top 10 domains. Here is what I tried :
LogQL query : topk(3, sum by(connection) (count_over_time({container_name="squid"} [1d])))
Data visualisation :
time series with the request
We can see that the last displayed line are the top 10 domains, but there is also many domains that were in the top 10 at some moment and they are still returned by the query.
I would like to keep only the top 10 domains at the and of the window of 24h...
In that way if I choose to display a bar gauge I will get only 10 entries. Currently I have this (with the same request) : Bar gauge with the request
Does someone have a solution ?

Ok I found out: In the query options, you have to change "range" to "instant"
and that works, it keeps the result of the query at the end of the time window !
Now I finally have my bar gauge, but it is not sorted... Next issue I guess ;)

Related

Remove Values from Grafana

I have a scenario like this pic, where I get the status from my hosts.
I have a few dozens of hosts so I would like to apply some filters, like showing only hosts that had problems during certain period.
I found one option, that I used in this example, that is the remove[below/above]value, but it only removes the value itself, not the entire column, the host itself.
Basically, considering this example, I would like this host(second bottom-up), that didn't have 1 value during this last week, did not appear on this screen.
Is there a way? I hope the question isn't very confusing.
The removeBelowValues function removes the values from the serie, but can't remove the serie.
Depending on your data, you can turn the series to rows, then remove rows using "Filter Data by Values" transformation. See https://grafana.com/docs/grafana/latest/panels-visualizations/query-transform-data/transform-data/#filter-data-by-value
Example:

Why does grafana display NaN for values that are clearly integers?

I created a SQL query that counts the number of servers running test jobs on a specific Jenkins server at a specified time. I'm trying to chart it on Grafana but for some reason, it's displaying the value as NaN.
The data source is a MySQL server. I'm running Grafana version 8.1.5. I went on the server (phpMyAdmin) to check the results of the query and I can see numbers.
When we look at the grafana chart/panel, the bars on the chart look like it matches the values, but the chart shows NaN instead of the value.
What setting do I need to change so that it can print the numeric value on the chart instead of displaying NaN?
EDIT: Looks like I didn't include enough info to my question.
Here's the query that's used, although, I don't know how feasible it is to include the tables with data to demo the issue:
SELECT COUNT(DISTINCT(tb_name)) as val, jenkins FROM (
SELECT
hw.collected_date AS mytime,
hw.jenkins AS jenkins,
hw.name AS tb_name
FROM hw_report as hw LEFT OUTER JOIN jenkins_owner jo
ON hw.jenkins = jo.jenkins
WHERE jo.org IN ('Enterprise Readiness')) as y
WHERE mytime = '2021-12-03 00:00:00'
GROUP BY jenkins
HAVING count(distinct(tb_name)) > '0'
Here are screenshots of my panel. The "Show values" setting is set to "Always", which II assume should show the values.
One thing I noticed is when I hover over the bar, it shows
COUNT(DISTINCT(name_cnt))
instead of a numeric value. Not sure if this is indicative of anything. I checked other charts in the dashboard that someone created and their bars either have a numeric value or it's just a column name (like name_cnt)
Sorry I'm a noob and didn't know what to configure, look up, or mention in my question. I accidentally came across the answer.
So it seems like I need to select something under the "Transform" tab for the panel. Calculation needs to be "Total" instead of "Count" and I need to hide the fields specified in my SQL query and show the "Total"

Grafana define Title value for chart

I am trying to create a dashboard. Able to generate Bar Gauge from the Prometheus data for simple query sum by (namespace) (kube_pod_container_status_running)
I want to display only Namespace rather than {namespace="kube-system"}, so was playing with Visualization > Field > Title for a while, but was not able to figure out.
Any Idea How can just display kube-system instead of {namespace="kube-system"} and make the list in sorted order, because everytime when i refresh the dashboard, it reshuffles the order.
What I need here ?
https://grafana.com/docs/grafana/latest/features/datasources/prometheus/
Legend format
Controls the name of the time series, using name or pattern. For example {{hostname}} will be replaced with label value for the label hostname.
=> configure Legend format in the Queries section and add {{namespace}} there.
I'm not sure about sort. You may try Prometheus sort/sort_desc function to sort query result.

Grafana Singlestat: select with timerange

I am trying to use the SingleStat Plugin of Grafana to add an online/offline indicator to one of my dashboards.
So what I have so far is this with an influxdb datasource:
What I am missing is the option to define a timerange for this query. Lets say I want the count() of the last 30min. If the count is 0 I know that the server is offline. If the count is > 0 he is online. (For example my server adds a new entry every 20min. So if I donĀ“t have an entry in the last 30min I know he must be offline)
So is it possible to get define a query with a timerange? When yes how ?
UPDATE
This is what I have so far now. But I get an error now which says a.form is undefined. Alos if I have a entry in the last 35min it doesnst switch to online.
The singlestat panel uses, by default, the timerange of the dashboard it is placed on.
For your case, make use of the 'override relative time' on the Time range tab and set it to "30m".
When using the count as you described, turn coloring on and set the threshold to 1. This will change the coloring when no entry is present (count is 0) in the last 30 minutes.

What's $interval mean in Grafana?

After installed Heapster in my kubernetes cluster, I can access Grafana but the graph are empty.
I can build a new graph with special value, e.g. "cpu/limits"; but if the pre-defined graph used $interval, the graph can not display; for example,
SELECT mean(value) FROM "cpu/limit_gauge" WHERE "container_name" = 'machine' AND $timeFilter GROUP BY time($interval), "hostname"
https://grafana.com/docs/grafana/latest/variables/variable-types/add-interval-variable/
$interval is a built in automatic variable in grafana , and is automatically set based on time range
the graph are empty? maybe your query is wrong
For future readers -
I want to add that sometimes the "group-by" "fill" option, which is NULL by default, can cause no values to be displayed depending on the resolution.
If you have a query you think should work, but still see no data, try changing the fill value to another setting that you think works (ie: none) and see if data shows up.
Grafana tries to use that configuration to fill in for missing data, and occasionally, depending on your interval and data collection rate, you might end up with oddness.