Commit 9155f463 by Zoltán Bedi Committed by GitHub

Prometheus: fix missing labels from value (#28842)

parent 574553ec
......@@ -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',
......
......@@ -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<number>(data.map(val => (isMs ? val[0] : val[0] * 1000))),
};
}
function getValueField(
data: PromValue[],
valueName: string = TIME_SERIES_VALUE_FIELD_NAME,
parseValue = true
): MutableField {
type ValueFieldOptions = {
data: PromValue[];
valueName?: string;
parseValue?: boolean;
labels?: Labels;
displayName?: string;
};
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<number | null>(data.map(val => (parseValue ? parseSampleValue(val[1]) : val[1]))),
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment