Prometheus: Fix series to rows frame name issue for custom name from legend option (#69343)

* use custom name from legend

* revert prometheus change

* fix issue in the transformer and not prometheus

* getFrameDisplayName falls back to getFieldDisplayName already

* run prettier
This commit is contained in:
Brendan O'Handley 2023-06-08 11:40:14 -04:00 committed by GitHub
parent a2b3a70b62
commit 32f27d10ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -124,7 +124,7 @@ describe('Series to rows', () => {
});
});
it('combine two time series, where first serie fields has displayName, into one', async () => {
it('combine two time series, where first series fields has displayName, into one and displayNameFromDS overrides frame.name', async () => {
const cfg: DataTransformerConfig<SeriesToRowsTransformerOptions> = {
id: DataTransformerID.seriesToRows,
options: {},
@ -156,7 +156,7 @@ describe('Series to rows', () => {
const expected: Field[] = [
createField('Time', FieldType.time, [200, 150, 126, 125, 100, 100]),
createField('Metric', FieldType.string, ['A', 'A', 'B', 'B', 'A', 'B']),
createField('Metric', FieldType.string, ['dsName', 'dsName', 'B', 'B', 'dsName', 'B']),
createField('Value', FieldType.number, [5, 4, 3, 2, 1, -1]),
];

View File

@ -70,13 +70,25 @@ export const seriesToRowsTransformer: DataTransformerInfo<SeriesToRowsTransforme
for (let frameIndex = 0; frameIndex < data.length; frameIndex++) {
const frame = data[frameIndex];
const firstNonTimeField = frame.fields[1];
// To support consistent naming for dataplane and prometheus
// displayNameFromDS > frameDisplayName
// This supports new naming and custom names (from the prom legend option)
// and supports the older pattern of getting the frame name.
// if neither of those return a name we use getFieldDisplayName which is good.
const displayNameFromDS = firstNonTimeField.config.displayNameFromDS;
const frameDisplayName = getFrameDisplayName(frame);
const displayName = displayNameFromDS ?? frameDisplayName;
for (let valueIndex = 0; valueIndex < frame.length; valueIndex++) {
const timeFieldIndex = timeFieldByIndex[frameIndex];
const valueFieldIndex = timeFieldIndex === 0 ? 1 : 0;
dataFrame.add({
[TIME_SERIES_TIME_FIELD_NAME]: frame.fields[timeFieldIndex].values[valueIndex],
[TIME_SERIES_METRIC_FIELD_NAME]: getFrameDisplayName(frame),
[TIME_SERIES_METRIC_FIELD_NAME]: displayName,
[TIME_SERIES_VALUE_FIELD_NAME]: frame.fields[valueFieldIndex].values[valueIndex],
});
}