Commit a49daf56 by Torkel Ödegaard Committed by GitHub

BackendSrv: Improves logic for hiding requests from query inspector (#26877)

* BackendSrv: Improves logic for hiding requests from query inspector

* fixed tests

* Update explore query inspector
parent ad337344
...@@ -49,7 +49,7 @@ export type BackendSrvRequest = { ...@@ -49,7 +49,7 @@ export type BackendSrvRequest = {
/** /**
* Set to to true to not include call in query inspector * Set to to true to not include call in query inspector
*/ */
silent?: boolean; hideFromInspector?: boolean;
/** /**
* The data to send * The data to send
......
...@@ -118,6 +118,11 @@ export class BackendSrv implements BackendService { ...@@ -118,6 +118,11 @@ export class BackendSrv implements BackendService {
} }
} }
if (options.hideFromInspector === undefined) {
// Hide all local non data query calls
options.hideFromInspector = isLocalUrl(options.url) && !isDataQuery(options.url);
}
return options; return options;
} }
......
...@@ -83,21 +83,22 @@ describe('backendSrv', () => { ...@@ -83,21 +83,22 @@ describe('backendSrv', () => {
describe('parseRequestOptions', () => { describe('parseRequestOptions', () => {
it.each` it.each`
retry | url | headers | orgId | noBackendCache | expected retry | url | headers | orgId | noBackendCache | expected
${undefined} | ${'http://localhost:3000/api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ retry: 0, url: 'http://localhost:3000/api/dashboard' }} ${undefined} | ${'http://localhost:3000/api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: false, retry: 0, url: 'http://localhost:3000/api/dashboard' }}
${1} | ${'http://localhost:3000/api/dashboard'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ retry: 1, url: 'http://localhost:3000/api/dashboard', headers: { Authorization: 'Some Auth' } }} ${1} | ${'http://localhost:3000/api/dashboard'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ hideFromInspector: false, retry: 1, url: 'http://localhost:3000/api/dashboard', headers: { Authorization: 'Some Auth' } }}
${undefined} | ${'api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ retry: 0, url: 'api/dashboard' }} ${undefined} | ${'api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard' }}
${undefined} | ${'/api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ retry: 0, url: 'api/dashboard' }} ${undefined} | ${'/api/dashboard'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard' }}
${undefined} | ${'/api/dashboard/'} | ${undefined} | ${undefined} | ${undefined} | ${{ retry: 0, url: 'api/dashboard/' }} ${undefined} | ${'/api/dashboard/'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard/' }}
${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${undefined} | ${undefined} | ${{ retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth' } }} ${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth' } }}
${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${undefined} | ${{ retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1 } }} ${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${undefined} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1 } }}
${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1, 'X-Grafana-NoCache': 'true' } }} ${undefined} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ hideFromInspector: true, retry: 0, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1, 'X-Grafana-NoCache': 'true' } }}
${1} | ${'/api/dashboard/'} | ${undefined} | ${undefined} | ${undefined} | ${{ retry: 1, url: 'api/dashboard/' }} ${1} | ${'/api/dashboard/'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 1, url: 'api/dashboard/' }}
${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${undefined} | ${undefined} | ${{ retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth' } }} ${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${undefined} | ${undefined} | ${{ hideFromInspector: true, retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth' } }}
${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${undefined} | ${{ retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1 } }} ${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${undefined} | ${{ hideFromInspector: true, retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1 } }}
${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1, 'X-Grafana-NoCache': 'true' } }} ${1} | ${'/api/dashboard/'} | ${{ Authorization: 'Some Auth' }} | ${1} | ${true} | ${{ hideFromInspector: true, retry: 1, url: 'api/dashboard/', headers: { 'X-DS-Authorization': 'Some Auth', 'X-Grafana-Org-Id': 1, 'X-Grafana-NoCache': 'true' } }}
${undefined} | ${'api/datasources/proxy'} | ${undefined} | ${undefined} | ${undefined} | ${{ hideFromInspector: false, retry: 0, url: 'api/datasources/proxy' }}
`( `(
"when called with retry: '$retry', url: '$url' and orgId: '$orgId' then result should be '$expected'", "when called with retry: '$retry', url: '$url' and orgId: '$orgId' then result should be '$expected'",
({ retry, url, headers, orgId, noBackendCache, expected }) => { async ({ retry, url, headers, orgId, noBackendCache, expected }) => {
const srv = new BackendSrv({ const srv = new BackendSrv({
contextSrv: { contextSrv: {
user: { user: {
...@@ -107,7 +108,7 @@ describe('backendSrv', () => { ...@@ -107,7 +108,7 @@ describe('backendSrv', () => {
} as any); } as any);
if (noBackendCache) { if (noBackendCache) {
srv.withNoBackendCache(async () => { await srv.withNoBackendCache(async () => {
expect(srv['parseRequestOptions']({ retry, url, headers })).toEqual(expected); expect(srv['parseRequestOptions']({ retry, url, headers })).toEqual(expected);
}); });
} else { } else {
...@@ -298,58 +299,6 @@ describe('backendSrv', () => { ...@@ -298,58 +299,6 @@ describe('backendSrv', () => {
}); });
describe('datasourceRequest', () => { describe('datasourceRequest', () => {
describe('when making a successful call and silent is true', () => {
it('then it should not emit message', async () => {
const url = 'http://localhost:3000/api/some-mock';
const { backendSrv, appEventsMock, expectRequestCallChain } = getTestContext({ url });
const options = { url, method: 'GET', silent: true };
const result = await backendSrv.datasourceRequest(options);
expect(result).toEqual({
data: { test: 'hello world' },
ok: true,
redirected: false,
status: 200,
statusText: 'Ok',
type: 'basic',
url,
config: options,
});
expect(appEventsMock.emit).not.toHaveBeenCalled();
expectRequestCallChain({ url, method: 'GET', silent: true });
});
});
describe('when making a successful call and silent is not defined', () => {
it('then it should not emit message', async () => {
const url = 'http://localhost:3000/api/some-mock';
const { backendSrv, expectRequestCallChain } = getTestContext({ url });
const options = { url, method: 'GET' };
let inspectorPacket: any = null;
backendSrv.getInspectorStream().subscribe({
next: rsp => (inspectorPacket = rsp),
});
const result = await backendSrv.datasourceRequest(options);
const expectedResult = {
data: { test: 'hello world' },
ok: true,
redirected: false,
status: 200,
statusText: 'Ok',
type: 'basic',
url,
config: options,
};
expect(result).toEqual(expectedResult);
expect(inspectorPacket).toEqual(expectedResult);
expectRequestCallChain({ url, method: 'GET' });
});
});
describe('when called with the same requestId twice', () => { describe('when called with the same requestId twice', () => {
it('then it should cancel the first call and the first call should be unsubscribed', async () => { it('then it should cancel the first call and the first call should be unsubscribed', async () => {
const url = '/api/dashboard/'; const url = '/api/dashboard/';
......
...@@ -133,7 +133,7 @@ export class QueryInspector extends PureComponent<Props, State> { ...@@ -133,7 +133,7 @@ export class QueryInspector extends PureComponent<Props, State> {
onDataSourceResponse(response: any) { onDataSourceResponse(response: any) {
// ignore silent requests // ignore silent requests
if (response.config?.silent) { if (response.config?.hideFromInspector) {
return; return;
} }
......
...@@ -15,7 +15,7 @@ import { getPanelInspectorStyles } from '../dashboard/components/Inspector/style ...@@ -15,7 +15,7 @@ import { getPanelInspectorStyles } from '../dashboard/components/Inspector/style
function stripPropsFromResponse(response: any) { function stripPropsFromResponse(response: any) {
// ignore silent requests // ignore silent requests
if (response.config?.silent) { if (response.config?.hideFromInspector) {
return {}; return {};
} }
......
...@@ -26,7 +26,7 @@ export class JaegerDatasource extends DataSourceApi<JaegerQuery> { ...@@ -26,7 +26,7 @@ export class JaegerDatasource extends DataSourceApi<JaegerQuery> {
} }
async metadataRequest(url: string, params?: Record<string, any>): Promise<any> { async metadataRequest(url: string, params?: Record<string, any>): Promise<any> {
const res = await this._request(url, params, { silent: true }).toPromise(); const res = await this._request(url, params, { hideFromInspector: true }).toPromise();
return res.data.data; return res.data.data;
} }
......
...@@ -268,7 +268,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> { ...@@ -268,7 +268,7 @@ export class LokiDatasource extends DataSourceApi<LokiQuery, LokiOptions> {
} }
async metadataRequest(url: string, params?: Record<string, string>) { async metadataRequest(url: string, params?: Record<string, string>) {
const res = await this._request(url, params, { silent: true }).toPromise(); const res = await this._request(url, params, { hideFromInspector: true }).toPromise();
return res.data.data || res.data.values || []; return res.data.data || res.data.values || [];
} }
......
...@@ -145,7 +145,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions> ...@@ -145,7 +145,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
// Use this for tab completion features, wont publish response to other components // Use this for tab completion features, wont publish response to other components
metadataRequest(url: string) { metadataRequest(url: string) {
return this._request(url, null, { method: 'GET', silent: true }); return this._request(url, null, { method: 'GET', hideFromInspector: true });
} }
interpolateQueryExpr(value: string | string[] = [], variable: any) { interpolateQueryExpr(value: string | string[] = [], variable: any) {
......
...@@ -73,7 +73,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -73,7 +73,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(datasourceRequestMock).toHaveBeenCalledWith({ expect(datasourceRequestMock).toHaveBeenCalledWith({
method: 'GET', method: 'GET',
url: 'proxied/api/v1/labels', url: 'proxied/api/v1/labels',
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -92,7 +92,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -92,7 +92,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(datasourceRequestMock).toHaveBeenCalledWith({ expect(datasourceRequestMock).toHaveBeenCalledWith({
method: 'GET', method: 'GET',
url: 'proxied/api/v1/label/resource/values', url: 'proxied/api/v1/label/resource/values',
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -117,7 +117,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -117,7 +117,7 @@ describe('PrometheusMetricFindQuery', () => {
url: `proxied/api/v1/series?match${encodeURIComponent( url: `proxied/api/v1/series?match${encodeURIComponent(
'[]' '[]'
)}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`, )}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`,
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -141,7 +141,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -141,7 +141,7 @@ describe('PrometheusMetricFindQuery', () => {
method: 'GET', method: 'GET',
url: url:
'proxied/api/v1/series?match%5B%5D=metric%7Blabel1%3D%22foo%22%2C+label2%3D%22bar%22%2C+label3%3D%22baz%22%7D&start=1524650400&end=1524654000', 'proxied/api/v1/series?match%5B%5D=metric%7Blabel1%3D%22foo%22%2C+label2%3D%22bar%22%2C+label3%3D%22baz%22%7D&start=1524650400&end=1524654000',
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -168,7 +168,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -168,7 +168,7 @@ describe('PrometheusMetricFindQuery', () => {
url: `proxied/api/v1/series?match${encodeURIComponent( url: `proxied/api/v1/series?match${encodeURIComponent(
'[]' '[]'
)}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`, )}=metric&start=${raw.from.unix()}&end=${raw.to.unix()}`,
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -187,7 +187,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -187,7 +187,7 @@ describe('PrometheusMetricFindQuery', () => {
expect(datasourceRequestMock).toHaveBeenCalledWith({ expect(datasourceRequestMock).toHaveBeenCalledWith({
method: 'GET', method: 'GET',
url: 'proxied/api/v1/label/__name__/values', url: 'proxied/api/v1/label/__name__/values',
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
...@@ -243,7 +243,7 @@ describe('PrometheusMetricFindQuery', () => { ...@@ -243,7 +243,7 @@ describe('PrometheusMetricFindQuery', () => {
url: `proxied/api/v1/series?match${encodeURIComponent('[]')}=${encodeURIComponent( url: `proxied/api/v1/series?match${encodeURIComponent('[]')}=${encodeURIComponent(
'up{job="job1"}' 'up{job="job1"}'
)}&start=${raw.from.unix()}&end=${raw.to.unix()}`, )}&start=${raw.from.unix()}&end=${raw.to.unix()}`,
silent: true, hideFromInspector: true,
headers: {}, headers: {},
}); });
}); });
......
...@@ -36,7 +36,7 @@ export class ZipkinDatasource extends DataSourceApi<ZipkinQuery> { ...@@ -36,7 +36,7 @@ export class ZipkinDatasource extends DataSourceApi<ZipkinQuery> {
} }
async metadataRequest(url: string, params?: Record<string, any>): Promise<any> { async metadataRequest(url: string, params?: Record<string, any>): Promise<any> {
const res = await this.request(url, params, { silent: true }).toPromise(); const res = await this.request(url, params, { hideFromInspector: true }).toPromise();
return res.data; return res.data;
} }
......
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