From 50faeb307808a62e210d5123f51c08bc8abd8151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 15 Feb 2021 11:27:16 +0100 Subject: [PATCH] DataSourceSrv: Filter out non queryable data sources by default (#31144) --- .../src/services/dataSourceSrv.ts | 19 +++++++++++++++++++ public/app/features/plugins/datasource_srv.ts | 9 +++++++++ .../plugins/specs/datasource_srv.test.ts | 13 +++++++++++++ 3 files changed, 41 insertions(+) diff --git a/packages/grafana-runtime/src/services/dataSourceSrv.ts b/packages/grafana-runtime/src/services/dataSourceSrv.ts index 1edc5b6f3b2..25094299e9e 100644 --- a/packages/grafana-runtime/src/services/dataSourceSrv.ts +++ b/packages/grafana-runtime/src/services/dataSourceSrv.ts @@ -28,12 +28,31 @@ export interface DataSourceSrv { /** @public */ export interface GetDataSourceListFilters { + /** Include mixed deta source by setting this to true */ mixed?: boolean; + + /** Only return data sources that support metrics response */ metrics?: boolean; + + /** Only return data sources that support tracing response */ tracing?: boolean; + + /** Only return data sources that support annotations */ annotations?: boolean; + + /** + * By default only data sources that can be queried will be returned. Meaning they have tracing, + * metrics, logs or annotations flag set in plugin.json file + * */ + all?: boolean; + + /** Set to true to return dashboard data source */ dashboard?: boolean; + + /** Set to true to return data source variables */ variables?: boolean; + + /** filter list by plugin */ pluginId?: string; } diff --git a/public/app/features/plugins/datasource_srv.ts b/public/app/features/plugins/datasource_srv.ts index 09f594b8ab0..2ecd3c8988e 100644 --- a/public/app/features/plugins/datasource_srv.ts +++ b/public/app/features/plugins/datasource_srv.ts @@ -157,6 +157,15 @@ export class DatasourceSrv implements DataSourceService { if (filters.pluginId && x.meta.id !== filters.pluginId) { return false; } + if ( + !filters.all && + x.meta.metrics !== true && + x.meta.annotations !== true && + x.meta.tracing !== true && + x.meta.logs !== true + ) { + return false; + } return true; }); diff --git a/public/app/features/plugins/specs/datasource_srv.test.ts b/public/app/features/plugins/specs/datasource_srv.test.ts index 54ea49f9a32..6185f16de73 100644 --- a/public/app/features/plugins/specs/datasource_srv.test.ts +++ b/public/app/features/plugins/specs/datasource_srv.test.ts @@ -75,6 +75,12 @@ describe('datasource_srv', () => { uid: 'uid-code-Jaeger', meta: { tracing: true, id: 'jaeger' }, }, + CannotBeQueried: { + type: 'no-query', + name: 'no-query', + uid: 'no-query', + meta: { id: 'no-query' }, + }, }; describe('Given a list of data sources', () => { @@ -120,6 +126,13 @@ describe('datasource_srv', () => { }); }); + it('Should by default filter out data sources that cannot be queried', () => { + const list = dataSourceSrv.getList({}); + expect(list.find((x) => x.name === 'no-query')).toBeUndefined(); + const all = dataSourceSrv.getList({ all: true }); + expect(all.find((x) => x.name === 'no-query')).toBeDefined(); + }); + it('Can get list of data sources with variables: true', () => { const list = dataSourceSrv.getList({ metrics: true, variables: true }); expect(list[0].name).toBe('${datasource}');