Compare metric values of two different metrics where the value of a label is the same as the value of the label in the other metric

Hi All,

I am very new to Grafana, I watched some tutorials but I didn’t find anywhere the answer that I needed. I hope you can help with the following question.

I have two Prometheus metrics, and both of the metrics have an “id” label, and many other labels. I don’t know the value of “id” beforehand, that is always automatically generated.
So, I have the following example metrics:
time_of_start{id=“123”, kubernetes_job=“job1”}, the value is a timestamp: 1698522956541
time_of_start{id=“123”, kubernetes_job=“job2”}, the value is the same timestamp as above: 1698522956541
time_of_start{id=“456”, kubernetes_job=“job1”}, the value is a timestamp: 1698524143839
time_of_start{id=“456”, kubernetes_job=“job2”}, the value is the same timestamp as above: 1698524143839
time_of_end{id=“123”, kubernetes_job=“job1”}, the value is a ts: 1698522980429
time_of_end{id=“123”, kubernetes_job=“job2”}, value is the same ts as above: 1698522980429
time_of_end{id=“456”, kubernetes_job=“job1”}, the value is a ts: 1698524143938
time_of_end{id=“456”, kubernetes_job=“job2”}, value is the same ts as above: 1698524143938

I would like to visualize (any kind of visualization would suffice) the start and end dates, or just their difference for each distinct ID once. And I don’t know beforehand what the values of the IDs are, so I can’t provide those values in the query.
Is it possible to do it with Grafana somehow?
And I am very novice when it comes to Grafana, so the more detailed your answer is the better. Thank you for your suggestions in advance.

Found the solution in the meantime:
((topk(1, time_of_end) by (id)) - on (id) (topk(1, time_of_start) by (id)))

1 Like