Skip to content

Commit

Permalink
[APM] migrate to io-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Aug 18, 2019
1 parent 6c28612 commit ef02690
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ import styled from 'styled-components';
import { idx } from '@kbn/elastic-idx';
import { NOT_AVAILABLE_LABEL } from '../../../../common/i18n';
import { useFetcher } from '../../../hooks/useFetcher';
import {
loadErrorDistribution,
loadErrorGroupDetails
} from '../../../services/rest/apm/error_groups';
import { fontFamilyCode, fontSizes, px, units } from '../../../style/variables';
import { ApmHeader } from '../../shared/ApmHeader';
import { DetailView } from './DetailView';
import { ErrorDistribution } from './Distribution';
import { useLocation } from '../../../hooks/useLocation';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { useTrackPageview } from '../../../../../infra/public';
import { callApmApi } from '../../../services/rest/callApi';

const Titles = styled.div`
margin-bottom: ${px(units.plus)};
Expand Down Expand Up @@ -68,24 +65,38 @@ export function ErrorGroupDetails() {

const { data: errorGroupData } = useFetcher(() => {
if (serviceName && start && end && errorGroupId) {
return loadErrorGroupDetails({
serviceName,
start,
end,
errorGroupId,
uiFilters
return callApmApi({
pathname: '/api/apm/services/{serviceName}/errors/{groupId}',
params: {
path: {
serviceName,
groupId: errorGroupId
},
query: {
start,
end,
uiFilters: JSON.stringify(uiFilters)
}
}
});
}
}, [serviceName, start, end, errorGroupId, uiFilters]);

const { data: errorDistributionData } = useFetcher(() => {
if (serviceName && start && end && errorGroupId) {
return loadErrorDistribution({
serviceName,
start,
end,
errorGroupId,
uiFilters
return callApmApi({
pathname: '/api/apm/services/{serviceName}/errors/distribution',
params: {
path: {
serviceName
},
query: {
start,
end,
groupId: errorGroupId,
uiFilters: JSON.stringify(uiFilters)
}
}
});
}
}, [serviceName, start, end, errorGroupId, uiFilters]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import {
import { i18n } from '@kbn/i18n';
import React, { useMemo } from 'react';
import { useFetcher } from '../../../hooks/useFetcher';
import {
loadErrorDistribution,
loadErrorGroupList
} from '../../../services/rest/apm/error_groups';
import { ErrorDistribution } from '../ErrorGroupDetails/Distribution';
import { ErrorGroupList } from './List';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { useTrackPageview } from '../../../../../infra/public';
import { PROJECTION } from '../../../../common/projections/typings';
import { LocalUIFilters } from '../../shared/LocalUIFilters';
import { callApmApi } from '../../../services/rest/callApi';

const ErrorGroupOverview: React.SFC = () => {
const { urlParams, uiFilters } = useUrlParams();
Expand All @@ -32,24 +29,38 @@ const ErrorGroupOverview: React.SFC = () => {

const { data: errorDistributionData } = useFetcher(() => {
if (serviceName && start && end) {
return loadErrorDistribution({
serviceName,
start,
end,
uiFilters
return callApmApi({
pathname: '/api/apm/services/{serviceName}/errors/distribution',
params: {
path: {
serviceName
},
query: {
start,
end,
uiFilters: JSON.stringify(uiFilters)
}
}
});
}
}, [serviceName, start, end, uiFilters]);

const { data: errorGroupListData } = useFetcher(() => {
if (serviceName && start && end) {
return loadErrorGroupList({
serviceName,
start,
end,
sortField,
sortDirection,
uiFilters
return callApmApi({
pathname: '/api/apm/services/{serviceName}/errors',
params: {
path: {
serviceName
},
query: {
start,
end,
sortField,
sortDirection,
uiFilters: JSON.stringify(uiFilters)
}
}
});
}
}, [serviceName, start, end, sortField, sortDirection, uiFilters]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ jest.mock('ui/kfetch');

jest
.spyOn(savedObjects, 'getAPMIndexPattern')
.mockReturnValue(
Promise.resolve({ id: 'apm-index-pattern-id' } as savedObjects.ISavedObject)
);
.mockReturnValue(Promise.resolve({ id: 'apm-index-pattern-id' } as any));

beforeAll(() => {
jest.spyOn(console, 'error').mockImplementation(() => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ jest.spyOn(hooks, 'useCore').mockReturnValue(coreMock);

jest
.spyOn(savedObjects, 'getAPMIndexPattern')
.mockReturnValue(
Promise.resolve({ id: 'apm-index-pattern-id' } as savedObjects.ISavedObject)
);
.mockReturnValue(Promise.resolve({ id: 'apm-index-pattern-id' } as any));

beforeAll(() => {
jest.spyOn(console, 'error').mockImplementation(() => null);
Expand Down
85 changes: 0 additions & 85 deletions x-pack/legacy/plugins/apm/public/services/rest/apm/error_groups.ts

This file was deleted.

26 changes: 26 additions & 0 deletions x-pack/legacy/plugins/apm/public/services/rest/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import LRU from 'lru-cache';
import hash from 'object-hash';
import { kfetch, KFetchOptions } from 'ui/kfetch';
import { KFetchKibanaOptions } from 'ui/kfetch/kfetch';
import { APMAPI } from '../../../server/routes/create_apm_api';
import { Client } from '../../../server/routes/typings';

function fetchOptionsWithDebug(fetchOptions: KFetchOptions) {
const debugEnabled =
Expand Down Expand Up @@ -55,6 +57,30 @@ export async function callApi<T = void>(
return res;
}

export const callApmApi: Client<APMAPI['_S']> = (options => {
const {
pathname,
params = { path: null, body: null, query: null },
...opts
} = options;

const path = (params.path || {}) as Record<string, any>;
const body = params.body ? JSON.stringify(params.body) : undefined;
const query = params.query || {};

return callApi({
...opts,
pathname: Object.keys(path || {}).reduce((acc, paramName) => {
if (!(paramName in path)) {
throw new Error(`Specified unexpected path parameter ${paramName}`);
}
return acc.replace(`{${paramName}}`, path[paramName]);
}, pathname),
body,
query
}) as any;
}) as Client<APMAPI['_S']>;

// only cache items that has a time range with `start` and `end` params,
// and where `end` is not a timestamp in the future
function isCachable(fetchOptions: KFetchOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { memoize } from 'lodash';
import { callApi } from './callApi';
import { callApmApi } from './callApi';

export interface ISavedObject {
attributes: {
Expand All @@ -18,9 +18,8 @@ export interface ISavedObject {

export const getAPMIndexPattern = memoize(async () => {
try {
return await callApi<ISavedObject>({
method: 'GET',
pathname: `/api/apm/index_pattern`
return await callApmApi({
pathname: '/api/apm/index_pattern'
});
} catch (error) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export async function getErrorGroups({
setup
}: {
serviceName: string;
sortField: string;
sortDirection: string;
sortField?: string;
sortDirection?: string;
setup: Setup;
}) {
const { client } = setup;
Expand Down
7 changes: 3 additions & 4 deletions x-pack/legacy/plugins/apm/server/new-platform/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import { InternalCoreSetup } from 'src/core/server';
import { makeApmUsageCollector } from '../lib/apm_telemetry';
import { CoreSetupWithUsageCollector } from '../lib/apm_telemetry/make_apm_usage_collector';
import { initErrorsApi } from '../routes/errors';
import { initMetricsApi } from '../routes/metrics';
import { initServicesApi } from '../routes/services';
import { initTracesApi } from '../routes/traces';
import { initTransactionGroupsApi } from '../routes/transaction_groups';
import { initUIFiltersApi } from '../routes/ui_filters';
import { initIndexPatternApi } from '../routes/index_pattern';
import { initSettingsApi } from '../routes/settings';
import { createApmApi } from '../routes/create_apm_api';

export class Plugin {
public setup(core: InternalCoreSetup) {
Expand All @@ -23,9 +22,9 @@ export class Plugin {
initTracesApi(core);
initServicesApi(core);
initSettingsApi(core);
initErrorsApi(core);
initMetricsApi(core);
initIndexPatternApi(core);
createApmApi().init(core);

makeApmUsageCollector(core as CoreSetupWithUsageCollector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { flatten } from 'lodash';
import { InternalCoreSetup } from 'src/core/server';
import { initErrorsApi } from '../errors';
import { initServicesApi } from '../services';
import { initTracesApi } from '../traces';

Expand Down Expand Up @@ -65,10 +64,6 @@ describe('route handlers should fail with a Boom error', () => {
consoleErrorSpy.mockRestore();
});

describe('error routes', () => {
testRouteFailures(initErrorsApi);
});

describe('service routes', () => {
testRouteFailures(initServicesApi);
});
Expand Down
Loading

0 comments on commit ef02690

Please sign in to comment.