diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index bb2c8e57af4..8fa8d3b1ef2 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -1,6 +1,22 @@ import { DataFrame } from '@grafana/data'; import { transform } from './result_transformer'; +const matrixResponse = { + status: 'success', + data: { + resultType: 'matrix', + result: [ + { + metric: { __name__: 'test', job: 'testjob' }, + values: [ + [1, '10'], + [2, '0'], + ], + }, + ], + }, +}; + describe('Prometheus Result Transformer', () => { const options: any = { target: {}, query: {} }; describe('When nothing is returned', () => { @@ -306,46 +322,14 @@ describe('Prometheus Result Transformer', () => { }); it('should fill null values', () => { - const response = { - status: 'success', - data: { - resultType: 'matrix', - result: [ - { - metric: { __name__: 'test', job: 'testjob' }, - values: [ - [1, '10'], - [2, '0'], - ], - }, - ], - }, - }; - - const result = transform({ data: response } as any, { ...options, query: { step: 1, start: 0, end: 2 } }); + const result = transform({ data: matrixResponse } as any, { ...options, query: { step: 1, start: 0, end: 2 } }); expect(result[0].fields[0].values.toArray()).toEqual([0, 1000, 2000]); expect(result[0].fields[1].values.toArray()).toEqual([null, 10, 0]); }); it('should use __name__ label as series name', () => { - const response = { - status: 'success', - data: { - resultType: 'matrix', - result: [ - { - metric: { __name__: 'test', job: 'testjob' }, - values: [ - [1, '10'], - [2, '0'], - ], - }, - ], - }, - }; - - const result = transform({ data: response } as any, { + const result = transform({ data: matrixResponse } as any, { ...options, query: { step: 1, @@ -384,6 +368,12 @@ describe('Prometheus Result Transformer', () => { expect(result[0].name).toBe('{job="testjob"}'); }); + it('should not set displayName for ValueFields', () => { + const result = transform({ data: matrixResponse } as any, options); + expect(result[0].fields[1].config.displayName).toBeUndefined(); + expect(result[0].fields[1].config.displayNameFromDS).toBe('test{job="testjob"}'); + }); + it('should align null values with step', () => { 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 580b4fcb639..a9a65cd89a1 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -139,10 +139,10 @@ function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOpti dps.push([t, null]); } fields.push(getTimeField(dps, true)); - fields.push(getValueField({ data: dps, parseValue: false, labels, displayName: name })); + fields.push(getValueField({ data: dps, parseValue: false, labels, displayNameFromDS: name })); } else { fields.push(getTimeField([data.value])); - fields.push(getValueField({ data: [data.value], labels, displayName: name })); + fields.push(getValueField({ data: [data.value], labels, displayNameFromDS: name })); } return { @@ -224,7 +224,7 @@ type ValueFieldOptions = { valueName?: string; parseValue?: boolean; labels?: Labels; - displayName?: string; + displayNameFromDS?: string; }; function getValueField({ @@ -232,13 +232,13 @@ function getValueField({ valueName = TIME_SERIES_VALUE_FIELD_NAME, parseValue = true, labels, - displayName, + displayNameFromDS, }: ValueFieldOptions): MutableField { return { name: valueName, type: FieldType.number, config: { - displayName, + displayNameFromDS, }, labels, values: new ArrayVector(data.map(val => (parseValue ? parseSampleValue(val[1]) : val[1]))),