How to use the selected period of time in a query? - grafana

I'm using Grafana with Prometheus and I'd like to build a query that depends on the selected period of time selected in the upper right corner of the screen.
Is there any variable (or something like that) to use in the query field?
In other words, If I select 24hs I'd like to use that data in the query.

There are two ways that I know:
You can use the $__interval variable like this:
increase(http_requests_total[$__interval])
There is a drawback that the $__interval variable's value is adjusted by resolution of the graph, but this may also be helpful in some situations.
This approach should fit your case better:
Go to Dashboard's Templating settings, create new variable with the type of Interval. Enable "Auto Option", adjust "Step count" to be equal 1. Then ensure that the "auto" is selected in corresponding drop-down list at the top of the dashboard.
Let's assume you name it timeRange, then the query will look like this:
increase(http_requests_total[$timeRange])
This variable will not be adjusted by graph resolution and if you select "Last 10 hours" its value will be 10h.

If you are looking at using prometheus as data source, $__range supports your dashboard time is great.
increase(gin_total_requests[$__range])
Why create your variable when you can use inbuilt Global variable.
I have added links to documentations to back up my answer
Grafana 5.3+

count_over_time({job="gerrit-sshd"}[$__interval])
It works for me and what's more, you should set Max data point as 1 ,in query option.
Then the $__interval is the same Time-Range from the Panel on Grafana top right corner.

Related

In Grafana, how to build a data link to filter current dashboard when there is more than one variable?

I'm using Grafana v8.3.4 (a551d74b11)
I've a dashboard with many variables (filters). One of those is for exemple TransactionType.
I would like my charts to filter on click.
For example with a pie chart using TransactionType, I want to filter the TransactionType variable ,when I click in one of the element of the pie chart, by this element.
The closest feature I found is to use data link.
So far I tried in the pie chart the two data link there after.
The syntax is OK but the effect is not doing what i’m expecting.
a)
/d/${__dashboard.uid}/${__dashboard}?var-TransactionType=${__series.name}
For this one the data link apply the filter on TransactionType but clear all the other filters variables, even clear the All, so the dashboard is in error.
b)
/d/${__dashboard.uid}/${__dashboard}?${__all_variables}&var-TransactionType=${__series.name}
this one apply all filters first then add the TransactionType filter but, it keep the previous All value, so the filter become All + value, that don’t work, the dashboard is in error too.
==> Is there a way to keep all the existing variable filter unchanged and replace the value for one variable filter ?
move var-TransactionType=${__series.name} before ${__all_variables}

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"

How to plot horizontal line in Timeseries in Grafana

I use grafana to plot timeseries data. In a timeseries plot i want to add a constant line which comes from a monitoring level. The value of that level is dynamic (from a postgres database) the timeseries come from a ifluxdb Datasource.
The monitoring level have no timestamp. The result should look like this:
I have searched quite a while how to do this, but not found a good explanation.
You can now add thresholds (bottom of the edit screen).
Each threshold can be represented as a solid line and/or region with individual color
Another way to do it in a dirty way is to create panel with a mixed data source.
Create your variable in grafana - it can be a query, constant or custom. Just remember to keep it a single floating point.
Add your original query and add the prometheus data source to query your variable.
${net_ordered_storage}
You will have to play a little bit with the number of data points displayed (query options>max data points) and minimum step of data point in prometheus query to make grafana connect dots.
Green horizontal line from variable
To draw a line like that you have to "fake" a timeseries. (thresholds don't work since they can not be dynamic as far as I know)
First thing to keep in mind is that grafana needs timestamp to plot it, for this reason the global variables ${__to} and ${__from} come in handy.
Then, to draw a line, grafana needs at least two points. ([t0, t1][y0, y1])
So this is the sql (postgre) query that lead to the desired result:
SELECT
${__from} AS time,
level_1,
FROM my_table where display_name = '${my_grafana_var:raw}'
union all
SELECT
${__to} AS time,
level_1,
FROM my_table where display_name = '${my_grafana_var:raw}';
It is possible to add dynamic thresholds using the Config from query option.
Add a new query below your standard metric query. This will likely be called B.
Here you query the static reference value.
Eg SELECT expected_number_of_widgets FROM baseline
Open Transformation tab
Find Configuration from Query results
Choose Config query = B
At the dropdown for expected_number_of_widgets, choose Use as: Threshold1.
In the right panel, under Thresholds, make sure Show Tresholds is enabled and remove the default threshold of 80.
For more details, see https://grafana.com/docs/grafana/latest/panels-visualizations/query-transform-data/transform-data/#config-from-query-results

Create histograms in Grafana with alphabetical values as x-axis

I need to create a dashboard to be used in a control room, where a bunch of operators will need to monitor the number of tasks assigned to other employees (among other aspects).
Source data will be coming from a RDBMs (PostgreSQL, in this case). We have people with assigned and numbered tasks that also have a status, and the DB data is like this (purely fictional: but it resembles the real one)
Having to create and mantain a dashboard i was thinking to use tools like Grafana, Kibana or similars, to plot something like this
The problem is that Grafana, for example, doesn't let me use alphabetical values for the x-axis. It only allow numeric values, while i've names to plot (Mark, Luke, Brian).
Is there a best practice than i can follow? Am i trying to use the wrong tools?
Actually solution is easier then you think although it also took me some time to figure it out. I will place here an example for some unspecified shop data grouped over countries - you just need to change it for your task. Example was tested on Grafana 5.0.3
PostgreSQL query for metrics
SELECT
$__time( partition_date ),
country as metric,
sum(value) as value
FROM
aggregations.my_data_for_dashboard
WHERE
shop = 'myshopname' AND
$__timeFilter(partition_date )
group by 1, 2
Grafana will show usual metrics:
In "Axes" tab look at "X-Axis" section, item "Mode" - switch "Time" to "Series" and Grafana will show bar chart for countries.

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.