Grafana is that possible to map prometheus label values according to another key-value pair variable? - grafana

I have a prometheus metric like this:
lines_added{project="xx",user="xx"}
the project label and user label are integer ids, not good for legend on graph.
and I have another variable members as query from postgresql datasource:
select u.id as __value,u.name as __text
from project_authorizations pa left join users u
on pa.user_id = u.id
where pa.project_id=[[project]]
now for the lines_added metric how can I make it display user's real name according "id"->"name" mapping in variable members
Is that possible with grafana?

Would it perhaps work if you stashed the SQL mappings into a variable and then perform some combination of label_replace regex magic on Prometheus query? At least that's how you get mappings into Prometheus world. But from there, I am yet to figure out how to pull it off in bulk. How many IDs you need to map?

Related

How to filter prometheus series based on the results of another query in grafana dashboard?

I am using Grafana 9.3.1 for monitoring of our system. Among other things, I am trying to monitor the remaining FUP of a phone number for each unit we operate.
Basically, we intend to use two data sources.
Database mapping of the unit ID to its phone number (e.g. "unit_id=123, phone_number="00 123456789")
Prometheus time series remaining_fup{phone_number="00 123456789"}. However, remaining_fup is a 3rd party data and does not include unit_id.
In my unit-detail dashboard I have unit_id variable which indicates which unit FUP should be displayed (among other things depending on unit_id)
My original approach was this:
Create a mixed datasource dashboard
Add database datasource as data A. SELECT phone_number FROM units WHERE unit_id='$unit_id'
Add prometheus datasource remaining_fup and filter it based on A.phone_number: remaining_fup{phone_number="${A.phone_number}"}
Unfortunatelly such use of A isn't supported. I used to hope for applying some transformation like Merge or Join by field and then Filter but with no success. After a lot of googling and trying I feel hopeless.
Could you help please? Is such filter even possible? Thanks!
TL;DR: In grafana dashboard I want to query one datasource in order to obtain a value which I subsequently want to use in another datasource query.
1.) Create variable - name phone_number, type: Query and query your database datasource SELECT phone_number FROM units WHERE unit_id='$unit_id'. You can hide this variable if you don't want it to be visible for the dashboard users.
2.) Variable phone_number may have multiple values, so use advance variable formatting to create valid regex query syntax for your prometheus datasource, e.g.
remaining_fup{phone_number=~"${phone_number:pipe}"}
Of course this queries are just examples and they may need some (syntax) tweaking for the use case. Main idea: don't use 2 queries, but one variable and one query (where you use that variable).

How to join 2 query results prometheus

I'm trying to query a Prometheus database to determine how many customers have recorded data for one metric with a specific label filter, but not another metric with a different label filter. I.e. all the customer_id's that show up in
sum(usage{usage_type="type_b"}) by (customer_id)
but not in
count(service_plan{plan_type=~".*plan_b.*"}) by (customer_id)
I could run each and just mash them together outside Prometheus, but I want to do this either in a single query in Prometheus, or with some fancy transformation tricks in Grafana.
You need unless operator - see these docs. The following query should return customer ids, which exist in the first query and are missing in the second query:
sum(usage{usage_type="type_b"}) by (customer_id)
unless
count(service_plan{plan_type=~".*plan_b.*"}) by (customer_id)

Microstrategy Lookup Table join

Lookup table (State)
In this table I have 4 Columns (Region_id,RegionName,State_id,State_name)
2.Fact table
In this I have couple of Columns like(customer,product,date,Region_id,State_id,Revenue, Profit)
In lookup table there are more number of states whereas in fact we have data only for few states.
My requirement is to display all the sates in report even though there is no corresponding data available in fact.i have set the state table as lookup for state attribute.
If I pull the state attribute and a metric from fact and set vldb properties preserve lookup table then I'm getting all the sates and it's metric values and null for the one which don't have metric value.
Now If I add some attribute like customer or product along with it I'm not getting all the sates..I'm getting only the states which have data in fact since on adding some attribute the mstr is not hitting state table at all it's bringing data from fact itself.
What could be done so tat I can get all the sates and null even for the customer and product..?
You could try forcing your metrics to outer-join on the dataset itself while preserving final pass elements to make a left join.
Within developer, navigate to your dataset/cube and open it up in editable mode.
Select "Data" within the top bar of the cube/dataset editor >> Report Data Options >> Metric Join Type.
Here you will see your Metrics. Change the join type from inner to outer. Rerun the report and it should bring back all the data you need.
Let me know how it goes.

How to define a tags / value query in Grafana 7.4?

I am having some trouble understanding the concept of Value groups/tags in Grafana 7.4.x with MySQL as a data source.
My main query gets the Countries
SELECT
NAME as __text,
id AS __value
from
countries
The tags query gets the Continents
SELECT
NAME as __text,
id AS __value
from
continents
That works so far, tags are shown in the list, but nothing happens once I click on them.
My tags query:
continents.$tag.*
The tags query seems to be the problem. Any help is greatly appreciated.
3 queries are involved:
The first one is under "Query Options" -> "Query": This one should list all the values (all the countries in your case).
The second query is the "Tags query" under "Value groups/tags": This query should list all the Tags you want (continents).
The third query is "Tag values query": This is where the magic happens, this query should return all the different values matching the selected tags, so, you must add a WHERE clause somewhere that Grafana will use to create a query to get the correct values, ...WHERE continent = '$tag'. <- $tag will automatically be replaced by a list of tags the user has chosen.
Be aware that the official documentation provides examples for InfluxDB datasource, since you are using MySQL, you must use SQL queries everywhere, so continents.$tag.* is invalid.

Grafana worldmap panel with Influxdb

I am struggling to get the worldmap panel working with influxdb.
Can someone explain what I am doing wrong?
I am assuming that you have saved geohash as a tag.
If I run your query in a table panel, then I get two fields in the result called geohash and one of them is empty.
I think you can remove the field(geohash) from the select and get the result you want. (Or remove the last selector or change the alias so that there is no conflict between it and the group by).