L1nk27
L1nk27

Reputation: 65

Grafana Prometheus merge two columns

I use the sflow-rt-exporter (https://github.com/sflow-rt/prometheus) to collect my traffic on my switches.

I then created a Grafana table where I see the traffic seperated in "source", "destination" and "traffic".

Now I would like to create a table in which the columns "source" and "destination" are in one column. So that it doesn't matter if the traffic went from or to this server.

Example:

Source    | Destination | Traffic
123.4.5.6 | 234.5.6.7   | 200B
234.5.6.7 | 123.4.5.6   | 500B

should become

IP        | Traffic
123.4.5.6 | 700B

After a week of trying I finally give up and hope that one of you can help me :)

Thanks in advance.

Greetings L1nk27

Upvotes: 2

Views: 3544

Answers (1)

Michael Doubez
Michael Doubez

Reputation: 6863

The key to your request is that you want to aggregate on distinct dimensions (labels) which is not possible with Prometheus.

Therefore, you must create another dimension (let call it IP) to collect the sides (source and destination). This can be done using the label_replace function:

  • This will copy source label into IP:

    label_replace(traffic, "IP", "$1", "source", "(.*)")
    
  • This will copy destination label into IP:

    label_replace(traffic, "IP", "$1", "destination", "(.*)")
    

Note that that you cannot have duplicates as the original values are kept (this is important). Then you can use the OR binary operator to concatenate them and then sum over the IP label:

sum by(IP) (
  label_replace(traffic, "IP", "$1", "source", "(.*)")
  OR
  label_replace(traffic, "IP", "$1", "destination", "(.*)"))

Upvotes: 2

Related Questions