From 38c1d45035c4b1715a82ceaeadda196cbcdcd6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Fri, 22 Jan 2021 18:03:37 +0100 Subject: [PATCH] Prometheus: Fix show query instead of Value if no __name__ and metric (#30511) Fixes #29466 --- .../prometheus/result_transformer.test.ts | 26 +++++++++++++++++++ .../prometheus/result_transformer.ts | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index 497170fd9b7..4d62bee4c3e 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -353,6 +353,32 @@ describe('Prometheus Result Transformer', () => { expect(result[0].name).toEqual('test{job="testjob"}'); }); + it('should use query as series name when __name__ is not available and metric is empty', () => { + const response = { + status: 'success', + data: { + resultType: 'matrix', + result: [ + { + metric: {}, + values: [[0, '10']], + }, + ], + }, + }; + const expr = 'histogram_quantile(0.95, sum(rate(tns_request_duration_seconds_bucket[5m])) by (le))'; + const result = transform({ data: response } as any, { + ...options, + query: { + step: 1, + start: 0, + end: 2, + expr, + }, + }); + expect(result[0].name).toEqual(expr); + }); + it('should set frame name to undefined if no __name__ label but there are other labels', () => { const response = { status: 'success', diff --git a/public/app/plugins/datasource/prometheus/result_transformer.ts b/public/app/plugins/datasource/prometheus/result_transformer.ts index 94f45a92dfd..f8ae608d453 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -383,7 +383,11 @@ function createLabelInfo(labels: { [key: string]: string }, options: TransformOp const { __name__, ...labelsWithoutName } = labels; const labelPart = formatLabels(labelsWithoutName); - const title = `${__name__ ?? ''}${labelPart}`; + let title = `${__name__ ?? ''}${labelPart}`; + + if (!title) { + title = options.query; + } return { name: title, labels: labelsWithoutName }; }