diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index 01aaed0aa2f..bb2c8e57af4 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -248,6 +248,33 @@ describe('Prometheus Result Transformer', () => { }); describe('When the response is a matrix', () => { + it('should have labels with the value field', () => { + const response = { + status: 'success', + data: { + resultType: 'matrix', + result: [ + { + metric: { __name__: 'test', job: 'testjob', instance: 'testinstance' }, + values: [ + [0, '10'], + [1, '10'], + [2, '0'], + ], + }, + ], + }, + }; + + const result: DataFrame[] = transform({ data: response } as any, { + ...options, + }); + + expect(result[0].fields[1].labels).toBeDefined(); + expect(result[0].fields[1].labels?.instance).toBe('testinstance'); + expect(result[0].fields[1].labels?.job).toBe('testjob'); + }); + it('should transform into a data frame', () => { 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 dd93ff8587f..580b4fcb639 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -4,6 +4,7 @@ import { Field, FieldType, formatLabels, + Labels, MutableField, ScopedVars, TIME_SERIES_TIME_FIELD_NAME, @@ -71,7 +72,7 @@ export function transform( meta: options.meta, refId: options.refId, length: 1, - fields: [getTimeField([prometheusResult.result]), getValueField([prometheusResult.result])], + fields: [getTimeField([prometheusResult.result]), getValueField({ data: [prometheusResult.result] })], }, ]; } @@ -109,7 +110,7 @@ function getPreferredVisualisationType(isInstantQuery?: boolean, mixedQueries?: * Transforms matrix and vector result from Prometheus result to DataFrame */ function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOptions): DataFrame { - const { name } = createLabelInfo(data.metric, options); + const { name, labels } = createLabelInfo(data.metric, options); const fields: Field[] = []; @@ -138,10 +139,10 @@ function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOpti dps.push([t, null]); } fields.push(getTimeField(dps, true)); - fields.push(getValueField(dps, undefined, false)); + fields.push(getValueField({ data: dps, parseValue: false, labels, displayName: name })); } else { fields.push(getTimeField([data.value])); - fields.push(getValueField([data.value])); + fields.push(getValueField({ data: [data.value], labels, displayName: name })); } return { @@ -176,7 +177,7 @@ function transformMetricDataToTable(md: MatrixOrVectorResult[], options: Transfo values: new ArrayVector(), }; }); - const valueField = getValueField([], valueText); + const valueField = getValueField({ data: [], valueName: valueText }); md.forEach(d => { if (isMatrixData(d)) { @@ -218,16 +219,28 @@ function getTimeField(data: PromValue[], isMs = false): MutableField { values: new ArrayVector(data.map(val => (isMs ? val[0] : val[0] * 1000))), }; } +type ValueFieldOptions = { + data: PromValue[]; + valueName?: string; + parseValue?: boolean; + labels?: Labels; + displayName?: string; +}; -function getValueField( - data: PromValue[], - valueName: string = TIME_SERIES_VALUE_FIELD_NAME, - parseValue = true -): MutableField { +function getValueField({ + data, + valueName = TIME_SERIES_VALUE_FIELD_NAME, + parseValue = true, + labels, + displayName, +}: ValueFieldOptions): MutableField { return { name: valueName, type: FieldType.number, - config: {}, + config: { + displayName, + }, + labels, values: new ArrayVector(data.map(val => (parseValue ? parseSampleValue(val[1]) : val[1]))), }; }