Skip to content

Add liquid template support for Prometheus variable substitution

Reuben Pereira requested to merge 35242-add-liquid-template into master

What does this MR do?

When defining Prometheus queries in GitLab (in custom metrics, custom dashboards, etc), it is often useful to use variables in the query, which will get replaced by the backend before the query is sent to Prometheus to compute.

The Prometheus proxy API (app/controllers/projects/environments/prometheus_api_controller.rb#proxy) does support variable substitution currently. However, only 3 predefined variables are currently supported: ci_environment_slug, kube_namespace, environment_filter.

This MR allows custom variables (using the Liquid template engine) to be used in the Prometheus query sent to the proxy API. The variable value has to be passed to the API in the variables parameter. Custom variables can only be used with the Liquid template format. The current ruby template format can continue to be used with the predefined variables only.

The variables parameter needs to be an array in the form of [variable1, value1, variable2, value2].

For example, if the query is up{pod="{{pod_name}}"}, the variables parameter needs to contain a value for the pod_name variable. So the request would contain:

query: "up{pod=\"{{pod_name}}\"}",
variables: ['pod_name', 'pod1']

In summary, after this MR:

  1. Custom dashboards can use the ruby template format to interpolate variables ("prefix_string_%{variable}_postfix_string") or the Liquid template format ("prefix_string_{{variable}}_postfix_string") in Prometheus queries.

    1. With the ruby template format, only predefined variables are supported.

    2. With the liquid template format, predefined variables and custom variables are supported.

    3. It is possible to use the Ruby template format and the Liquid template format in the same dashboard. However, if you try to use Liquid script tags ({% %}) in a dashboard, it will fail to render the dashboard.

  2. We will eventually (probably in 13.0) stop supporting the ruby template format in favor of the Liquid template format.

Issue: #35242 (closed)

Also solves #36550 (closed)

Screenshots

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by 🤖 GitLab Bot 🤖

Merge request reports