grafana-datasource icon indicating copy to clipboard operation
grafana-datasource copied to clipboard

Support metrics with special characters in query builder

Open rul-hydro opened this issue 1 year ago • 18 comments

As mentioned in #128, the query builder generates invalid code when the metric name contains special characters, such as slash (/) or dash (-), and quite possibly others.

When picking a metric with special characters (e.g. foo/bar) from the drop-down, the builder should write the query as {__name__="foo/bar"} instead of just foo/bar. When switching back from Code to Builder, the label filter __name__= should also be translated to the Metric drop-down.

I suppose the distinction for the metric names could be done by a regex. Prometheus docs state that a valid metric name should match [a-zA-Z_:][a-zA-Z0-9_:]*. The expression for compatible metric names could be a bit broader than that, like [a-z-A-Z0-9_:.] or something like that. Anything not matching this expression would be written as a label filter.

(Beware of quotes in metric names - if anyone should be crazy enough to have them.)

rul-hydro avatar Jan 11 '24 16:01 rul-hydro

Sorry guys ... I got it all wrong in #128 :D

Actually how it should be is to just escape the names from query builder:

Like for our case:

bts\-erp.sys_metrics_heap\-used

vs

{__name__="bts-erp.sys_metrics_heap-used"}

So just escape the names and take them into account when parsing or selecting the metrics from dropdown.

(based on idea from: https://stackoverflow.com/questions/52537029/escape-special-characters-in-prometheus-metric-names)

matejsp avatar Jan 11 '24 17:01 matejsp

I tried this in grafana too.

Entering the metrics without builder: image

But when trying to switch to builder: image

After accepting the risk and continue: image

matejsp avatar Jan 11 '24 17:01 matejsp

Support metrics with special characters has been added at datasource starting from v0.6.0. Closing the feature request as done.

Loori-R avatar Jan 31 '24 12:01 Loori-R

Looks great, thank you!

rul-hydro avatar Jan 31 '24 12:01 rul-hydro

@Loori-R I tried and it's actually a lot better. However for such metrics we are unable to select label filters in dropdown.

image

Raw query works (and we have 4 labels (color, host, ...):

bts\-erp.heartbeat_metrics_value{color="blue"}

When changing from raw query to builder it populates the fields correctly. Just dropdown does not resolve all the possible labels. image

Like when filtering labels or fetching them it does not take \ into account ?

matejsp avatar Jan 31 '24 13:01 matejsp

Hey @matejsp, thanks for pointing this out! You're right about the dropdown issue with the special characters. We've just opened Issue #140 to tackle this.

Loori-R avatar Jan 31 '24 14:01 Loori-R

hi @Loori-R , I found that the query builder doesn't support label names containing dot. In the following example, k8s.cluster.name is converted to k8s in builder

image image image

chenlujjj avatar May 11 '24 10:05 chenlujjj

Hello, @chenlujjj! Thanks for your message. It looks like we missed handling special characters in labels. I've reopened it.

Loori-R avatar May 11 '24 11:05 Loori-R

Hello @Loori-R , @hagen1778 , Thanks for your work, seems you have fixed it, cool!

May I know when the fix will be released so that we can install it into our Grafana ?

Does it also fix the auto-completion and syntax highlighting when the label names contain dots?

chenlujjj avatar May 24 '24 06:05 chenlujjj

Hi @chenlujjj ! Can you try to install the latest release? It contains the bug fix which should solve your issue.

dmitryk-dk avatar May 27 '24 12:05 dmitryk-dk

Hello @dmitryk-dk , @Loori-R , the new release do fix the issue of query builder, thanks.

However, there are still problems with auto-completion and syntax highlighting:

  1. auto-completion After I typed the metric name, the braces and the prefix of label name, it cannot auto complete the remaining part of the label name.

image NOTE: for the first time it can auto complete, but when I deleted the labels in braces and tried to input label names, it cannot auto complete.

  1. syntax highlighting image I think the k8s.cluster.name label should be of green color, but now only the name part is green.

chenlujjj avatar May 28 '24 05:05 chenlujjj

@chenlujjj thank you! We will take a look into this issue

dmitryk-dk avatar May 28 '24 06:05 dmitryk-dk

hi @dmitryk-dk wondering what's the progress of this issue ?

chenlujjj avatar Jul 11 '24 09:07 chenlujjj

hi @dmitryk-dk wondering what's the progress of this issue ?

Hi! Sorry for the delay. We will take a look as soon as possible, maybe by the end of next week.

dmitryk-dk avatar Jul 11 '24 09:07 dmitryk-dk

(grafana v10.4.14) (victoriametrics-datasource v0.10.3)

suffering from the same problem, any metric name with unicode characters is not properly handled by query builder

obraz

obraz

obraz

Query generated by query builder (not working):

µg\/m³_value{entity_id="ikea_of_sweden_vindstyrka_particulate_matter"}

This is working, but it's quite annoying since it requires switching to manual mode and escaping by hand every time

\µg\/m\³_value{entity_id="ikea_of_sweden_vindstyrka_particulate_matter"}

marekmarecki avatar Dec 27 '24 20:12 marekmarecki

(grafana v10.4.14) (victoriametrics-datasource v0.10.3)

suffering from the same problem, any metric name with unicode characters is not properly handled by query builder

obraz

obraz

obraz

Query generated by query builder (not working):

µg\/m³_value{entity_id="ikea_of_sweden_vindstyrka_particulate_matter"}

This is working, but it's quite annoying since it requires switching to manual mode and escaping by hand every time

\µg\/m\³_value{entity_id="ikea_of_sweden_vindstyrka_particulate_matter"}

Hi @Loori-R ! Could you take a look ?

dmitryk-dk avatar Dec 30 '24 11:12 dmitryk-dk

Hey @Loori-R @dmitryk-dk , any updates on it?

denisgolius avatar May 13 '25 15:05 denisgolius

No updates at this time; scheduled for this month.

Loori-R avatar May 13 '25 15:05 Loori-R