From a4285916dd3773b97cd29619f4e6bd694efaf958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Tue, 5 Jun 2018 17:56:01 +0200 Subject: [PATCH] [APM] Fix debug param issue (#19676) * [APM] Fix debug param issue * [APM] Fix _debug issue and add test --- .../services/__test__/SessionStorageMock.js | 25 ++++++ .../apm/public/services/__test__/rest.test.js | 79 +++++++++++++++++++ x-pack/plugins/apm/public/services/rest.js | 31 +++++--- 3 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 x-pack/plugins/apm/public/services/__test__/SessionStorageMock.js create mode 100644 x-pack/plugins/apm/public/services/__test__/rest.test.js diff --git a/x-pack/plugins/apm/public/services/__test__/SessionStorageMock.js b/x-pack/plugins/apm/public/services/__test__/SessionStorageMock.js new file mode 100644 index 000000000000..c42d84b1da1d --- /dev/null +++ b/x-pack/plugins/apm/public/services/__test__/SessionStorageMock.js @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export class SessionStorageMock { + store = {}; + + clear() { + this.store = {}; + } + + getItem(key) { + return this.store[key] || null; + } + + setItem(key, value) { + this.store[key] = value.toString(); + } + + removeItem(key) { + delete this.store[key]; + } +} diff --git a/x-pack/plugins/apm/public/services/__test__/rest.test.js b/x-pack/plugins/apm/public/services/__test__/rest.test.js new file mode 100644 index 000000000000..e0d11c5a7dd0 --- /dev/null +++ b/x-pack/plugins/apm/public/services/__test__/rest.test.js @@ -0,0 +1,79 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as kfetchModule from 'ui/kfetch'; +import { SessionStorageMock } from './SessionStorageMock'; +import { callApi } from '../rest'; + +describe('rest', () => { + let kfetchSpy; + + beforeEach(() => { + kfetchSpy = jest.spyOn(kfetchModule, 'kfetch').mockResolvedValue({ + my_key: 'hello world' + }); + global.sessionStorage = new SessionStorageMock(); + }); + + afterEach(() => { + kfetchSpy.mockClear(); + }); + + describe('callApi', () => { + describe('debug param', () => { + describe('when apm_debug is true', () => { + beforeEach(() => { + sessionStorage.setItem('apm_debug', 'true'); + }); + + it('should add debug param for APM endpoints', async () => { + await callApi({ pathname: `/api/apm/status/server` }); + + expect(kfetchSpy).toHaveBeenCalledWith( + { pathname: '/api/apm/status/server', query: { _debug: true } }, + { camelcase: true } + ); + }); + + it('should not add debug param for non-APM endpoints', async () => { + await callApi({ pathname: `/api/kibana` }); + + expect(kfetchSpy).toHaveBeenCalledWith( + { pathname: '/api/kibana' }, + { camelcase: true } + ); + }); + }); + }); + + describe('camelcase', () => { + it('camelcase param should be true by default', async () => { + const res = await callApi({ pathname: `/api/kibana` }); + + expect(kfetchSpy).toHaveBeenCalledWith( + { pathname: '/api/kibana' }, + { camelcase: true } + ); + + expect(res).toEqual({ myKey: 'hello world' }); + }); + + it('should respect settings', async () => { + const res = await callApi( + { pathname: `/api/kibana` }, + { camelcase: false } + ); + + expect(kfetchSpy).toHaveBeenCalledWith( + { pathname: '/api/kibana' }, + { camelcase: false } + ); + + expect(res).toEqual({ my_key: 'hello world' }); + }); + }); + }); +}); diff --git a/x-pack/plugins/apm/public/services/rest.js b/x-pack/plugins/apm/public/services/rest.js index 6426d3d16145..d406fb9d80a4 100644 --- a/x-pack/plugins/apm/public/services/rest.js +++ b/x-pack/plugins/apm/public/services/rest.js @@ -7,25 +7,36 @@ import 'isomorphic-fetch'; import { camelizeKeys } from 'humps'; import { kfetch } from 'ui/kfetch'; -import { memoize, isEmpty, first } from 'lodash'; +import { memoize, isEmpty, first, startsWith } from 'lodash'; import chrome from 'ui/chrome'; import { convertKueryToEsQuery } from './kuery'; import { getFromSavedObject } from 'ui/index_patterns/static_utils'; -async function callApi(fetchOptions, kibanaOptions) { +function fetchOptionsWithDebug(fetchOptions) { + const debugEnabled = + sessionStorage.getItem('apm_debug') === 'true' && + startsWith(fetchOptions.pathname, '/api/apm'); + + if (!debugEnabled) { + return fetchOptions; + } + + return { + ...fetchOptions, + query: { + ...fetchOptions.query, + _debug: true + } + }; +} + +export async function callApi(fetchOptions, kibanaOptions) { const combinedKibanaOptions = { camelcase: true, ...kibanaOptions }; - const combinedFetchOptions = { - ...fetchOptions, - query: { - ...fetchOptions.query, - _debug: sessionStorage.getItem('apm_debug') || false - } - }; - + const combinedFetchOptions = fetchOptionsWithDebug(fetchOptions); const res = await kfetch(combinedFetchOptions, combinedKibanaOptions); return combinedKibanaOptions.camelcase ? camelizeKeys(res) : res; }