From c804639b539211bbbb1fb60d7ef0d1e6d71542ec Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Fri, 24 Jun 2022 15:26:52 +0200 Subject: [PATCH] Hide datalink button when datasource for exemplar is undefined (#51236) --- .betterer.results | 63 ++++++------ .../prometheus/result_transformer.test.ts | 95 ++++++++++++++++++- .../prometheus/result_transformer.ts | 37 +++++--- 3 files changed, 146 insertions(+), 49 deletions(-) diff --git a/.betterer.results b/.betterer.results index 3921792bb79..f860c04f8b9 100644 --- a/.betterer.results +++ b/.betterer.results @@ -6744,36 +6744,39 @@ exports[`no explicit any`] = { [17, 50, 3, "Unexpected any. Specify a different type.", "193409811"], [32, 50, 3, "Unexpected any. Specify a different type.", "193409811"] ], - "public/app/plugins/datasource/prometheus/result_transformer.test.ts:1135772145": [ - [408, 19, 3, "Unexpected any. Specify a different type.", "193409811"], - [418, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [429, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [463, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [493, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [521, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [545, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [566, 68, 3, "Unexpected any. Specify a different type.", "193409811"], - [572, 35, 3, "Unexpected any. Specify a different type.", "193409811"], - [615, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [615, 99, 3, "Unexpected any. Specify a different type.", "193409811"], - [647, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [647, 99, 3, "Unexpected any. Specify a different type.", "193409811"], - [673, 68, 3, "Unexpected any. Specify a different type.", "193409811"], - [700, 68, 3, "Unexpected any. Specify a different type.", "193409811"], - [713, 61, 3, "Unexpected any. Specify a different type.", "193409811"], - [723, 61, 3, "Unexpected any. Specify a different type.", "193409811"], - [748, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [777, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [789, 61, 3, "Unexpected any. Specify a different type.", "193409811"], - [811, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [828, 70, 3, "Unexpected any. Specify a different type.", "193409811"], - [856, 72, 3, "Unexpected any. Specify a different type.", "193409811"], - [884, 64, 3, "Unexpected any. Specify a different type.", "193409811"], - [898, 15, 3, "Unexpected any. Specify a different type.", "193409811"], - [935, 55, 3, "Unexpected any. Specify a different type.", "193409811"], - [941, 66, 3, "Unexpected any. Specify a different type.", "193409811"], - [950, 66, 3, "Unexpected any. Specify a different type.", "193409811"], - [959, 66, 3, "Unexpected any. Specify a different type.", "193409811"] + "public/app/plugins/datasource/prometheus/result_transformer.test.ts:3921188682": [ + [408, 25, 3, "Unexpected any. Specify a different type.", "193409811"], + [477, 19, 3, "Unexpected any. Specify a different type.", "193409811"], + [487, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [498, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [532, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [562, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [590, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [614, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [635, 68, 3, "Unexpected any. Specify a different type.", "193409811"], + [641, 35, 3, "Unexpected any. Specify a different type.", "193409811"], + [684, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [684, 99, 3, "Unexpected any. Specify a different type.", "193409811"], + [716, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [716, 99, 3, "Unexpected any. Specify a different type.", "193409811"], + [742, 68, 3, "Unexpected any. Specify a different type.", "193409811"], + [769, 68, 3, "Unexpected any. Specify a different type.", "193409811"], + [782, 61, 3, "Unexpected any. Specify a different type.", "193409811"], + [792, 61, 3, "Unexpected any. Specify a different type.", "193409811"], + [817, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [846, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [858, 61, 3, "Unexpected any. Specify a different type.", "193409811"], + [880, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [897, 70, 3, "Unexpected any. Specify a different type.", "193409811"], + [925, 72, 3, "Unexpected any. Specify a different type.", "193409811"], + [953, 64, 3, "Unexpected any. Specify a different type.", "193409811"], + [967, 15, 3, "Unexpected any. Specify a different type.", "193409811"], + [1004, 55, 3, "Unexpected any. Specify a different type.", "193409811"], + [1010, 66, 3, "Unexpected any. Specify a different type.", "193409811"], + [1019, 66, 3, "Unexpected any. Specify a different type.", "193409811"], + [1028, 66, 3, "Unexpected any. Specify a different type.", "193409811"], + [1034, 29, 3, "Unexpected any. Specify a different type.", "193409811"], + [1045, 66, 3, "Unexpected any. Specify a different type.", "193409811"] ], "public/app/plugins/datasource/prometheus/types.ts:1752579129": [ [50, 12, 3, "Unexpected any. Specify a different type.", "193409811"], diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index 4639861c188..ca91f3d5d09 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -1,6 +1,6 @@ -import { DataFrame, FieldType, DataQueryRequest, DataQueryResponse, MutableDataFrame } from '@grafana/data'; +import { DataFrame, DataQueryRequest, DataQueryResponse, FieldType, MutableDataFrame } from '@grafana/data'; -import { transform, transformV2, transformDFToTable, parseSampleValue } from './result_transformer'; +import { parseSampleValue, transform, transformDFToTable, transformV2 } from './result_transformer'; import { PromQuery } from './types'; jest.mock('@grafana/runtime', () => ({ @@ -9,8 +9,9 @@ jest.mock('@grafana/runtime', () => ({ }), getDataSourceSrv: () => { return { - getInstanceSettings: () => { - return { name: 'Tempo' }; + getInstanceSettings: (uid: string) => { + const uids = ['Tempo', 'jaeger']; + return uids.find((u) => u === uid) ? { name: uid } : undefined; }, }; }, @@ -354,7 +355,75 @@ describe('Prometheus Result Transformer', () => { ]); expect(series.data[1].fields.length).toEqual(3); }); + + it('should not add a link with an error when exemplarTraceIdDestinations is not configured properly', () => { + const response = { + state: 'Done', + data: [ + new MutableDataFrame({ + refId: 'A', + fields: [ + { name: 'Time', type: FieldType.time, values: [6, 5, 4] }, + { + name: 'Value', + type: FieldType.number, + values: [10, 10, 0], + labels: { le: '1' }, + }, + ], + }), + new MutableDataFrame({ + refId: 'A', + name: 'exemplar', + meta: { + custom: { + resultType: 'exemplar', + }, + }, + fields: [ + { name: 'Time', type: FieldType.time, values: [6, 5, 4, 3, 2, 1] }, + { + name: 'Value', + type: FieldType.number, + values: [30, 10, 40, 90, 14, 21], + labels: { le: '6' }, + }, + { + name: 'traceID', + type: FieldType.string, + values: ['unknown'], + labels: { le: '6' }, + }, + ], + }), + ], + } as unknown as DataQueryResponse; + const request = { + targets: [ + { + format: 'heatmap', + refId: 'A', + }, + ], + } as unknown as DataQueryRequest; + const testOptions: any = { + exemplarTraceIdDestinations: [ + { + name: 'traceID', + datasourceUid: 'unknown', + }, + ], + }; + + const series = transformV2(response, request, testOptions); + expect(series.data[1].fields.length).toEqual(3); + expect(series.data[1].name).toEqual('exemplar'); + const traceField = series.data[1].fields.find((f) => f.name === 'traceID'); + expect(traceField).toBeDefined(); + expect(traceField!.config.links?.length).toBe(0); + }); }); + describe('transformDFToTable', () => { it('transforms dataFrame with response length 1 to table dataFrame', () => { const df = new MutableDataFrame({ @@ -961,6 +1030,24 @@ describe('Prometheus Result Transformer', () => { expect(result[0].fields.some((f) => f.config.links?.length)).toBe(false); }); + + it('should not add a datalink with an error when exemplarTraceIdDestinations is not configured', () => { + const testOptions: any = { + target: {}, + query: {}, + exemplarTraceIdDestinations: [ + { + name: 'traceID', + datasourceUid: 'unknown', + }, + ], + }; + + const result = transform({ data: exemplarsResponse } as any, testOptions); + const traceField = result[0].fields.find((f) => f.name === 'traceID'); + expect(traceField).toBeDefined(); + expect(traceField!.config.links?.length).toBe(0); + }); }); }); }); diff --git a/public/app/plugins/datasource/prometheus/result_transformer.ts b/public/app/plugins/datasource/prometheus/result_transformer.ts index 16bc2f792e6..90694e03fde 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -1,11 +1,15 @@ import { descending, deviation } from 'd3'; -import { partition, groupBy } from 'lodash'; +import { groupBy, partition } from 'lodash'; import { ArrayDataFrame, ArrayVector, + CoreApp, DataFrame, + DataFrameType, DataLink, + DataQueryRequest, + DataQueryResponse, DataTopic, Field, FieldType, @@ -13,14 +17,10 @@ import { getDisplayProcessor, Labels, MutableField, + PreferredVisualisationType, ScopedVars, TIME_SERIES_TIME_FIELD_NAME, TIME_SERIES_VALUE_FIELD_NAME, - DataQueryResponse, - DataQueryRequest, - PreferredVisualisationType, - DataFrameType, - CoreApp, } from '@grafana/data'; import { FetchResponse, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime'; @@ -293,15 +293,21 @@ function getDataLinks(options: ExemplarTraceIdDestination): DataLink[] { const dataSourceSrv = getDataSourceSrv(); const dsSettings = dataSourceSrv.getInstanceSettings(options.datasourceUid); - dataLinks.push({ - title: options.urlDisplayLabel || `Query with ${dsSettings?.name}`, - url: '', - internal: { - query: { query: '${__value.raw}', queryType: 'traceId' }, - datasourceUid: options.datasourceUid, - datasourceName: dsSettings?.name ?? 'Data source not found', - }, - }); + // dsSettings is undefined because of the reasons below: + // - permissions issues (probably most likely) + // - deleted datasource + // - misconfiguration + if (dsSettings) { + dataLinks.push({ + title: options.urlDisplayLabel || `Query with ${dsSettings?.name}`, + url: '', + internal: { + query: { query: '${__value.raw}', queryType: 'traceId' }, + datasourceUid: options.datasourceUid, + datasourceName: dsSettings?.name ?? 'Data source not found', + }, + }); + } } if (options.url) { @@ -485,6 +491,7 @@ 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;