Add Leading Zeros to Values or "Natural" Sort

  • What Grafana version and what operating system are you using?
    Grafana v10.4.2, RHEL7 (a requirement from IT due to outside vendor reqs.)

  • What are you trying to achieve?
    Data fields are from an Elastic index and are strings with a mix of char and digits. I want to insert leading zeros ahead of the digits so sorting in a visualization is ordered. Alternatively, if there is a way to ‘natural sort’ the values, I would take that. The strings look similar to this:

    • host1
    • host2
    • host10
    • host11
    • host20

    But when each of those is a row in a status history visualization, they sort as host1, host10, host11, …, host2, host20, …, making them a nuisance to follow in the chart. I’d rather not go back to the logstash pipeline for the change which would then require a full reindex in Elastic.

  • How are you trying to achieve it?
    I haven’t been able to find a suitable method. I wanted to try a transform in Grafana to dynamically add the leading zeros, but haven’t found a way to do it. If a ‘natural sort’ option is available, that would also be a good solution. Could I put in a request for an additional option in sorting/order by: “Natrual”?

  • What happened?
    Nothing. Yet. No method has been found suitable.

  • What did you expect to happen?
    N/A.

  • Can you copy/paste the configuration(s) that you are having problems with?
    Not available.

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    N/A

  • Did you follow any online instructions? If so, what is the URL?
    There are no instructions anywhere that I’ve been able to find in any web searches I’ve performed (Kagi, DDG, I even tried the SEO nightmare of Google but found nothing).

strip the non numeric values and keep numeric fields only to use for sorting

Unfortunately, the data is an array below a key:

{
  hosts: [
    host1,
    host2,
    ...
    host10,
    host11,
    ....
  ]
}

To save my sanity, I’ve gone back to the logstash pipeline after all and just padded there. That way the data is stored in the index in the manner I need and rendering will be faster that way. Turns out I only need to re-index about two week’s worth of records.

well that is one way to hack it. not best approach though

Here I am using jsonata with uql with the infinity plugin which you can use with elastic search and open search etc. peruse in this forum for many great posts

So, accepting that my admittedly hack-ish method isn’t the best way, where would I perform the JSON manipulations to restructure the {hosts: } to that which is shown in the attached images? I haven’t been able to find how to do custom transforms on Elastic data like that.

A pointer to documentation that I haven’t yet been able to find would be fine.

Thanks!

nick

so here is what I have done, as you can see this is the Infinity plugin and you can use it to hit a rest api endpoint such as elasticsearch or opensearch at ip.address.host:9200

in my case I do not have an es or os server so I am using mock data you provided. But in your case you would put the url in the Source field.

then comes the juicy part, using UQL language with jsonata

so I am use parse-json because the data returned is json, then iterating over the hosts node then doing some mapping. read the docu for that $map function. Read the docu linked for the $map function

Another approach (if you’re using Elasticsearch datasource and don’t want to use Infinity Plugin) would be to go into transforms, trim the host part (Format string transformation should have substring option - if not, check out toggle features or bump Grafana), cast the rest to number and sort by that.

I’m giving the Infinity plugin a try first. I think that might be more powerful for my needs. But I don’t see a Format string transformation so perhaps that’s not available in the version of Grafana I’ve currently got running (via Docker) and as a result, I’m not sure that avenue is available to me yet. I will have to upgrade at some point in the near future.