Grafana - Is it possible to use variables in Loki-based dashboard query? - grafana

I am working on a Loki-based Dashboard on Grafana. I have one panel for searching text in the Loki trace logs, the current query is like:
{job="abc-service"}
|~ "searchTrace"
|json
|line_format "{if .trace_message}} Message: \t{{.trace_message}} {{end}}"
Where searchTrace is a variable of type "Text box" for the user to input search text.
I want to include another variable skipTestLog to skip logs created by some test cron tasks. skipTestLog is a custom variable of two options: Yes,No.
Suppose the logs created by test cron tasks contain the text CronTest in the field trace_message after the json parser, are there any ways to filter them out based on the selected value of skipTestLog?

Create a key/value custom variable like in the following example:
Use the variable like in the following example:

Related

grafana logql query using line_format and regexReplaceAll dynamically

I need to read my logs just like they were displayed in the application console.
My grafana LogQL query looks like this :
{job="myjob", host="myserver"}
| json
| line_format `{{ regexReplaceAll "{(.*?)}" .MessageTemplate "{{.Properties_${1}}}" }}`
It ouputs a json object, I would like to use the "MessageTemplate" property and replace all the bracketed variables back where they belong so the log is readable in grafana log dashboards.
My problem is when using regexReplaceAll("<regexp>" <String_To_search> <output>) you cannot parse the output as a variable. Is there a way to do this that I missed ? I've tried hundreds of combinations and parsed through the official documentation.
Please let me know if it's impossible !

Fill Grafana Dashboard Variable From LoqQL

I have a Grafana dashboard and I'd like to define a variable for this dashboard. I'd like the values of this variable will come from LogQL query. To be more specific - in each log I have a field called "site_ids", and I want the values of the variable to be all the different "site_ids" (longs).
So I wrote this query:
{_namespace_="namespace",_schema_="schema"} | logfmt | line_format "{{.site_ids}}"
Which seems to work when I just run it in the query executor, this is the output (the actual site_ids):
0
-1
196
2
3
...
But when putting it as a query when I try to configure a new variable, I see nothing in the Preview of values:
Unfortunately I can barely find documentation about this..
Any suggestions?
Thanks!
Use label_values like this Query Variable section

Grafana dashboard to display a metric for a key in JSON Loki record

I'm having trouble understanding how to create a dashboard time series plot to display a single key/value from a Loki log which is in JSON format.
eg:
here is my query in the Explorer:
{job="railsdevlogs"}|json
which returns log lines such as:
{"date":"2022-01-05T21:27:21.895Z","pool":{"Pool Size":50,"Current":5,"Active":1,"Idle":4,"Dead":0,"Timeout":"5 sec"},"puma":{"Started At":"2022-01-05T20:35:26Z","Max Threads":16,"Pool Capacity":16,"Running":1,"Backlog":0,"IO Handles":15,"File Handles":2,"Socket Handles":4,"Server Log Size":46750072},"process":[{"Name":"ruby.exe","Process ID":656,"Threads":11,"Working Set":150728704,"Virtual Size":288079872},{"Name":"mysqld.exe","Process ID":4836,"Threads":3,"Working Set":360448,"Virtual Size":4445065216},{"Name":"mysqld.exe","Process ID":5808,"Threads":49,"Working Set":69906432,"Virtual Size":4924059648},{"Name":"aaaaa.exe","Process ID":14460,"Threads":18,"Working Set":49565696,"Virtual Size":5478469632},{"Name":"bbbbb.exe","Process ID":9584,"Threads":14,"Working Set":35012608,"Virtual Size":4496551936},{"Name":"ccccc.exe","Process ID":11944,"Threads":14,"Working Set":29609984,"Virtual Size":4481880064}],"gc":{"count":242,"heap_allocated_pages":1277,"heap_sorted_length":1279,"heap_allocatable_pages":9,"heap_available_slots":869213,"heap_live_slots":464541,"heap_free_slots":404672,"heap_final_slots":0,"heap_marked_slots":411311,"heap_swept_slots":457903,"heap_eden_pages":1268,"heap_tomb_pages":9,"total_allocated_pages":1278,"total_freed_pages":1,"total_allocated_objects":74364715,"total_freed_objects":73900174,"malloc_increase_bytes":640096,"malloc_increase_bytes_limit":16777216,"minor_gc_count":131,"major_gc_count":111,"remembered_wb_unprotected_objects":57031,"remembered_wb_unprotected_objects_limit":114062,"old_objects":349257,"old_objects_limit":698512,"oldmalloc_increase_bytes":640288,"oldmalloc_increase_bytes_limit":16777216},"os":{"System Name":"xxxxx","Description":"","Organization":"","Operating System":"Microsoft Windows 10 Enterprise LTSC","OS Version":"10.0.17763","OS Serial Number":"xxxxx-xxxxx-xxxxx-xxxxx","System Time":"2022-01-05T16:27:22.000-05:00","System Time Zone":-300,"Last Boot Time":"2021-12-15T23:26:38.000-05:00","System Drive":"C:","Total Physical Memory":34204393472,"Free Physical Memory":20056260608,"Total Virtual Memory":39304667136,"Free Virtual Memory":13915041792,"Number of Processes":307,"Number of Users":2,"volumes":[{"Drive":"C:\\","Type":"NTFS","Total Space":1023563264000,"Free Space":681182343168,"Block Size":4096}]},"symbol":{"size":28106},"stats_collection_time":387}
using |json will automatically create dynamic labels for all the key/values in the json log line:
gc_count = 123
os_Free_Virtual_Memory = 456789
etc.
Now I would like to plot one of these values in a grafana time series plot, but I am struggling to understand how to isolate one dynamic label and plot it.
Perhaps I'm using |json incorrectly. The documentation and examples I have read so far shows how to filter the logs using the dynamic labels, but I dont need that since I want to plot every log line.
thanks
I think this should help https://grafana.com/go/observabilitycon/2020/keynote-what-is-observability/ if you go to minute 41.
There's an example which is very similar to what you're trying to achieve.
Your query should look something like:
quantile_over_time(0,99, {job="railsdevlogs"}
| json
| unwrap gc_count [1m]}
by (job)

How do I map one variable's values to another variable in Grafana?

In my Grafana dashboard (with Prometheus as a data source), I have a custom $tier variable, which allows the user to pick the tier from a dropdown. It's defined as:
Values separated by comma: production, stage, development
I need to filter a Prometheus metric by a label which contains a shortened version of the tier name:
"foo-dev"
"foo-stage"
"foo-prod"
I was thinking that I'd create a hidden variable $shortened_tier so I could use that in my query filter, like this:
my_label=~"foo-$shortened_tier"
I'd like to define it based on the value of $tier:
"development" -> "dev"
"stage" -> "stage"
"production" -> "prod"
How do I do that?
I figured out a workaround for this, but it is suuuuper hacky:
Name: shortened_tier
Type: Query
Data Source: Prometheus
Query: label_values(up{env="$tier"}, env)
Regex: (dev|stage|prod).*
What I wanted to do was simply Query: $tier, but since Grafana wouldn't let me do that, I had to use a completely different metric (up) where I could pass in $tier and get back the same exact value as a string. Then I use regex to just look for dev|stage|prod at the beginning of the string, capture that part, and throw away the rest.
This has the result that I'm looking for, with the value of $shortened_tier dynamically changing based on the value that's selected and assigned to $tier. But man I wish Grafana had a less hacky way to do this.

Use CSV values in JMeter as request path

I have one of jmeter User defined variable as a "comma separated value" - ${countries} = IN,US,CA,ALL .
(I was first trying to get it as a list/array - [IN,US,CA,ALL] )
I want to use the variable to test a web service - GET /${country}/info . IS it possible using ForEach controller or Loop controller ?
Only thing is that I want to save it or read it as IN,US,..,ALL and use it in the request path.
Thanks
The CSV should be as per the format mentioned in the image attached.
Refer to the link on how to use CSV in Jmeter: http://ivetetecedor.com/how-to-use-a-csv-file-with-jmeter/
Thread Group Settings
No. of threads: 1
Ramp-up period: 1
Loop Count: 4
Hope this will help.
CSV config is a red herring, you don't need it.
You can use a regular expression extractor to split up the variable into another variable (eg MyVar), using something like:
(.+?)[,\n]
This is trying to match each item before a , or newline. It will place the values in variables like MyVar_1, MyVar_2, etc. This is as close to an array as JMeter understands natively.
You can then loop on the contents of the matches using MyVar_matchNr, and MyVar_1 to MyVar_n (you will need to use __V() function to access the 'array' contents.