Prometheus: fix missing labels from value (#28842)

This commit is contained in:
Zoltán Bedi 2020-11-05 11:37:21 +01:00 committed by GitHub
parent 574553ec7b
commit 9155f46315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 11 deletions

View File

@ -248,6 +248,33 @@ describe('Prometheus Result Transformer', () => {
}); });
describe('When the response is a matrix', () => { 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', () => { it('should transform into a data frame', () => {
const response = { const response = {
status: 'success', status: 'success',

View File

@ -4,6 +4,7 @@ import {
Field, Field,
FieldType, FieldType,
formatLabels, formatLabels,
Labels,
MutableField, MutableField,
ScopedVars, ScopedVars,
TIME_SERIES_TIME_FIELD_NAME, TIME_SERIES_TIME_FIELD_NAME,
@ -71,7 +72,7 @@ export function transform(
meta: options.meta, meta: options.meta,
refId: options.refId, refId: options.refId,
length: 1, 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 * Transforms matrix and vector result from Prometheus result to DataFrame
*/ */
function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOptions): DataFrame { function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOptions): DataFrame {
const { name } = createLabelInfo(data.metric, options); const { name, labels } = createLabelInfo(data.metric, options);
const fields: Field[] = []; const fields: Field[] = [];
@ -138,10 +139,10 @@ function transformToDataFrame(data: MatrixOrVectorResult, options: TransformOpti
dps.push([t, null]); dps.push([t, null]);
} }
fields.push(getTimeField(dps, true)); fields.push(getTimeField(dps, true));
fields.push(getValueField(dps, undefined, false)); fields.push(getValueField({ data: dps, parseValue: false, labels, displayName: name }));
} else { } else {
fields.push(getTimeField([data.value])); fields.push(getTimeField([data.value]));
fields.push(getValueField([data.value])); fields.push(getValueField({ data: [data.value], labels, displayName: name }));
} }
return { return {
@ -176,7 +177,7 @@ function transformMetricDataToTable(md: MatrixOrVectorResult[], options: Transfo
values: new ArrayVector(), values: new ArrayVector(),
}; };
}); });
const valueField = getValueField([], valueText); const valueField = getValueField({ data: [], valueName: valueText });
md.forEach(d => { md.forEach(d => {
if (isMatrixData(d)) { if (isMatrixData(d)) {
@ -218,16 +219,28 @@ function getTimeField(data: PromValue[], isMs = false): MutableField {
values: new ArrayVector<number>(data.map(val => (isMs ? val[0] : val[0] * 1000))), values: new ArrayVector<number>(data.map(val => (isMs ? val[0] : val[0] * 1000))),
}; };
} }
type ValueFieldOptions = {
data: PromValue[];
valueName?: string;
parseValue?: boolean;
labels?: Labels;
displayName?: string;
};
function getValueField( function getValueField({
data: PromValue[], data,
valueName: string = TIME_SERIES_VALUE_FIELD_NAME, valueName = TIME_SERIES_VALUE_FIELD_NAME,
parseValue = true parseValue = true,
): MutableField { labels,
displayName,
}: ValueFieldOptions): MutableField {
return { return {
name: valueName, name: valueName,
type: FieldType.number, type: FieldType.number,
config: {}, config: {
displayName,
},
labels,
values: new ArrayVector<number | null>(data.map(val => (parseValue ? parseSampleValue(val[1]) : val[1]))), values: new ArrayVector<number | null>(data.map(val => (parseValue ? parseSampleValue(val[1]) : val[1]))),
}; };
} }