Skip to content

Prometheus: When there is an empty label mather in recording rule expanding is not working #108600

@itsmylife

Description

@itsmylife

If we have a query that contains a recording rule with an empty label matcher, expanding it fails. Because addLabelToQuery functions is not expecting empty values.

See:

if (!key || !value) {
throw new Error('Need label to add to query.');
}

If we ignore that by simply removing that lines it works as expected. But this might affect other parts of the data source UI.

So before making any further changes we have to make sure everything is fine.

The error message when we try to expand

Uncaught Error: Need label to add to query.
Sa	@	add_label_to_query.ts:25
(anonymous)	@	language_utils.ts:191
Hl	@	language_utils.ts:188
(anonymous)	@	language_utils.ts:146
Wl	@	language_utils.ts:129
modifyQuery	@	datasource.ts:715
ie	@	PromQueryField.tsx:135

A unit test to verify. Put it in language_utils.test.ts under expandRecordingRules() section

it('when there is an empty label value it should still be able to expand the rule', () => {
  const query = `sum(max by (cluster, container) (pod_cpu:active:kube_limits{container!="", cluster=~"pink"}))`;
  const mapping = {
    'pod_cpu:active:kube_limits': {
      expandedQuery: `kube_limits{job!="", resource="cpu"} * on (namespace, pod, cluster) group_left () max by (namespace, pod, cluster) ((kube_pod_status_phase{phase=~"Pending|Running"} == 1))`,
    },
  };
  const expected = `sum(max by (cluster, container) (kube_limits{job!="", resource="cpu", container!="", cluster=~"pink"} * on (namespace, pod, cluster) group_left () max by (namespace, pod, cluster) ((kube_pod_status_phase{phase=~"Pending|Running", container!="", cluster=~"pink"} == 1))))`;
  const result = expandRecordingRules(query, mapping);
  expect(result).toBe(expected);
});

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions