VictoriaMetrics / grafana-datasource

Grafana Plugin for VictoriaMetrics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support metrics with special characters in query builder

rul-hydro opened this issue · 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.)

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)

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

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

Looks great, thank you!

@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 ?

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.

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

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