Skip to content

Commit

Permalink
[APM] Fix debug param issue (elastic#19676)
Browse files Browse the repository at this point in the history
* [APM] Fix debug param issue

* [APM] Fix _debug issue and add test
  • Loading branch information
sorenlouv authored Jun 5, 2018
1 parent a9011b3 commit a428591
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 8 deletions.
25 changes: 25 additions & 0 deletions x-pack/plugins/apm/public/services/__test__/SessionStorageMock.js
Original file line number Diff line number Diff line change
@@ -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];
}
}
79 changes: 79 additions & 0 deletions x-pack/plugins/apm/public/services/__test__/rest.test.js
Original file line number Diff line number Diff line change
@@ -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' });
});
});
});
});
27 changes: 19 additions & 8 deletions x-pack/plugins/apm/public/services/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
const combinedKibanaOptions = {
camelcase: true,
...kibanaOptions
};
function fetchOptionsWithDebug(fetchOptions) {
const debugEnabled =
sessionStorage.getItem('apm_debug') === 'true' &&
startsWith(fetchOptions.pathname, '/api/apm');

if (!debugEnabled) {
return fetchOptions;
}

const combinedFetchOptions = {
return {
...fetchOptions,
query: {
...fetchOptions.query,
_debug: sessionStorage.getItem('apm_debug') || false
_debug: true
}
};
}

export async function callApi(fetchOptions, kibanaOptions) {
const combinedKibanaOptions = {
camelcase: true,
...kibanaOptions
};

const combinedFetchOptions = fetchOptionsWithDebug(fetchOptions);
const res = await kfetch(combinedFetchOptions, combinedKibanaOptions);
return combinedKibanaOptions.camelcase ? camelizeKeys(res) : res;
}
Expand Down

0 comments on commit a428591

Please sign in to comment.