Commit 0427df8f by Ivana Huckova Committed by GitHub

Prometheus: Set type of labels to string (#30831)

parent 99acad44
import { DataFrame } from '@grafana/data';
import { DataFrame, FieldType } from '@grafana/data';
import { transform } from './result_transformer';
jest.mock('@grafana/runtime', () => ({
......@@ -101,15 +101,20 @@ describe('Prometheus Result Transformer', () => {
1443454531000,
]);
expect(result[0].fields[0].name).toBe('Time');
expect(result[0].fields[0].type).toBe(FieldType.time);
expect(result[0].fields[1].values.toArray()).toEqual(['test', 'test', 'test2', 'test2']);
expect(result[0].fields[1].name).toBe('__name__');
expect(result[0].fields[1].config.filterable).toBe(true);
expect(result[0].fields[1].type).toBe(FieldType.string);
expect(result[0].fields[2].values.toArray()).toEqual(['', '', 'localhost:8080', 'localhost:8080']);
expect(result[0].fields[2].name).toBe('instance');
expect(result[0].fields[2].type).toBe(FieldType.string);
expect(result[0].fields[3].values.toArray()).toEqual(['testjob', 'testjob', 'otherjob', 'otherjob']);
expect(result[0].fields[3].name).toBe('job');
expect(result[0].fields[3].type).toBe(FieldType.string);
expect(result[0].fields[4].values.toArray()).toEqual([3846, 3848, 3847, 3849]);
expect(result[0].fields[4].name).toEqual('Value');
expect(result[0].fields[4].type).toBe(FieldType.number);
expect(result[0].refId).toBe('A');
});
......@@ -168,6 +173,7 @@ describe('Prometheus Result Transformer', () => {
};
const result = transform({ data: response } as any, { ...options, target: { format: 'table' } });
expect(result[0].fields[1].values.toArray()).toEqual([102]);
expect(result[0].fields[1].type).toEqual(FieldType.number);
});
});
......
......@@ -299,10 +299,13 @@ function transformMetricDataToTable(md: MatrixOrVectorResult[], options: Transfo
const metricFields = Object.keys(md.reduce((acc, series) => ({ ...acc, ...series.metric }), {}))
.sort()
.map((label) => {
// Labels have string field type, otherwise table tries to figure out the type which can result in unexpected results
// Only "le" label has a number field type
const numberField = label === 'le';
return {
name: label,
config: { filterable: true },
type: FieldType.other,
type: numberField ? FieldType.number : FieldType.string,
values: new ArrayVector(),
};
});
......
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