24

I have ":9100" showing up at the end of my metrics. I am using a prometheus datasource with my company and can't change it. Has anyone figured out how to remove this? I was thinking maybe templates or regex but I haven't been successful yet.

Configuration:

Legend Format = {{hostname}}

Result = myhostname.mydomain.com:9100

user3149615
  • 349
  • 1
  • 2
  • 5

3 Answers3

43

As far as I know, currently it is not possible to format the legend in Grafana (there is an open PR for it), however as you are using Prometheus, you can use its label_replace() function, eg.:

label_replace(my_vector, "short_hostname", "$1", "hostname", "(.*):.*")

This should give you:

Legend Format = {{short_hostname}}

Result = myhostname.mydomain.com

See the docs here - label_replace

fty4
  • 568
  • 9
  • 18
zsolt
  • 481
  • 5
  • 5
  • Where would I type this? I tried in the Legend Format edit box, and I tried defining a variable. I want to do this too, but I can't figure out from your comment how. Thanks. – Martin Del Vecchio Oct 10 '19 at 17:44
  • 8
    I figured out the answer to my above question . The label_replace() function wraps the original PromQL query, which is referred to as "my_vector" here. This example translates the "hostname" variable and creates a new one called "short_hostname". Then in the Legend Format, you can specify "{{short_hostname}}" to get the translated value, without the ":9100". Thanks! – Martin Del Vecchio Oct 22 '19 at 10:52
11

In Grafana 8.0, there is Transform menu that can be used to remove text. I hope it is still relevant to this question.

On Dashboard, select the panel > Edit. There are 3 tabs below chart, one of them is Transform. Select Transform > Add Transformation > Rename by Regex, fill the form with these values:

  • Match: ":9100" (without quotes)
  • Replace: keep it empty

Please refer to the link for detailed information.

opris
  • 131
  • 2
  • 4
0

I presume you are statically mentioning targets in the Prometheus config file as follows:

- job_name: "node_exporter"
  static_configs:
    - targets: ['localhost:9100']

In that case, if you see the {{instance_name}} in grafana, it will give you the same as your target name ['localhost:9100']. This is because it uses the static_configs to avail labels for your metrics.

In order to remove the :9100 from the labels, you can use service discovery to recognize targets.

For example: ec2_sd_config where the labels will be availed from labels mentioned in AWS EC2 dashboard, file_sd_config where you can provide the targets and their labels in json or yaml format, etc.

Please refer to the link for detailed information.

Hope this helps!

Akshay Shah
  • 323
  • 2
  • 16