VictoriaMetrics / grafana-datasource

Grafana Plugin for VictoriaMetrics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Annotations does not support templating variables (Prometheus datasource does)

dg-nvm opened this issue · comments

When using dynamic datasource for annotation like "${datasource}" the Datasource won't be found and query will fail

Also when query itself has templating like my_counter{db="$db"} the value is sent raw so it's not functional

The source of these problems are two:

  1. Datasource is always taken raw, I tested fix by adding to datasource.tsx:705:
if (datasource?.uid?.startsWith("$") === true){
      datasource.uid = this.templateSrv.replace(datasource.uid, {});
    };
  1. Variables templating is discarded in this section:
if (request.targets.every(t => t.refId === "Anno")) {
  const query = request.targets[0] as PromQueryRequest
  return this.performAnnotationQuery({ ...query, start, end })
}

this discards all query preparation done by prepareTargets / createQuery which has query.expr = this.templateSrv.replace(expr, scopedVars, this.interpolateQueryExpr); and takes raw input from target instead, fixed by:

       if (request.targets.every(t => t.refId === "Anno")) {
-        const query = request.targets[0] as PromQueryRequest
+        const query = {...request.targets[0], expr: queries[0].expr} as PromQueryRequest
         return this.performAnnotationQuery({ ...query, start, end })
       }

I am not javascript expert so these fixes can be raw, but generally after adding these both Datasource and Expression templating works as intended.

This issue has been fixed in datasource starting from v0.8.0. Closing the issue as fixed. Thanks @dg-nvm!