Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM]Create API to return data to be used on the Overview page #69137

Merged
merged 11 commits into from
Jun 26, 2020
15 changes: 15 additions & 0 deletions x-pack/plugins/apm/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import { setHelpExtension } from './setHelpExtension';
import { toggleAppLinkInNav } from './toggleAppLinkInNav';
import { setReadonlyBadge } from './updateBadge';
import { createStaticIndexPattern } from './services/rest/index_pattern';
import {
fetchLandingPageData,
hasData,
} from './services/rest/observability_dashboard';

export type ApmPluginSetup = void;
export type ApmPluginStart = void;
Expand Down Expand Up @@ -73,6 +77,17 @@ export class ApmPlugin implements Plugin<ApmPluginSetup, ApmPluginStart> {
pluginSetupDeps.home.environment.update({ apmUi: true });
pluginSetupDeps.home.featureCatalogue.register(featureCatalogueEntry);

if (plugins.observability) {
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
const isDarkMode = core.uiSettings.get('theme:darkMode');
plugins.observability.dashboard.register({
appName: 'apm',
fetchData: async (params) => {
return fetchLandingPageData(params, { isDarkMode });
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
},
hasData,
});
}

core.application.register({
id: 'apm',
title: 'APM',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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 { fetchLandingPageData, hasData } from './observability_dashboard';
import * as createCallApmApi from './createCallApmApi';

describe('Observability dashboard data', () => {
const callApmApiMock = jest.spyOn(createCallApmApi, 'callApmApi');
afterEach(() => {
callApmApiMock.mockClear();
});
describe('hasData', () => {
it('returns false when no data is available', async () => {
callApmApiMock.mockImplementation(() => Promise.resolve(false));
const response = await hasData();
expect(response).toBeFalsy();
});
it('returns true when data is available', async () => {
callApmApiMock.mockImplementation(() => Promise.resolve(true));
const response = await hasData();
expect(response).toBeTruthy();
});
});

describe('fetchLandingPageData', () => {
it('returns APM data with series and stats', async () => {
callApmApiMock.mockImplementation(() =>
Promise.resolve({
serviceCount: 10,
transactionCoordinates: [
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 3 },
],
})
);
const response = await fetchLandingPageData(
{
startTime: '1',
endTime: '2',
bucketSize: '3',
},
{ isDarkMode: false }
);
expect(response).toEqual({
title: 'APM',
appLink: '/app/apm',
stats: {
services: {
label: 'Services',
value: 10,
},
transactions: {
label: 'Transactions',
value: 6,
color: '#6092c0',
},
},
series: {
transactions: {
label: 'Transactions',
coordinates: [
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 3 },
],
color: '#6092c0',
},
},
});
});
it('returns empty transaction coordinates', async () => {
callApmApiMock.mockImplementation(() =>
Promise.resolve({
serviceCount: 0,
transactionCoordinates: [],
})
);
const response = await fetchLandingPageData(
{
startTime: '1',
endTime: '2',
bucketSize: '3',
},
{ isDarkMode: false }
);
expect(response).toEqual({
title: 'APM',
appLink: '/app/apm',
stats: {
services: {
label: 'Services',
value: 0,
},
transactions: {
label: 'Transactions',
value: 0,
color: '#6092c0',
},
},
series: {
transactions: {
label: 'Transactions',
coordinates: [],
color: '#6092c0',
},
},
});
});
});
});
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
72 changes: 72 additions & 0 deletions x-pack/plugins/apm/public/services/rest/observability_dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { sum } from 'lodash';
import lightTheme from '@elastic/eui/dist/eui_theme_light.json';
import darkTheme from '@elastic/eui/dist/eui_theme_dark.json';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { FetchDataParams } from '../../../../observability/public/data_handler';
import { ApmFetchDataResponse } from '../../../../observability/public/typings/fetch_data_response';
import { callApmApi } from './createCallApmApi';

interface Options {
isDarkMode: boolean;
}

export const fetchLandingPageData = async (
{ startTime, endTime, bucketSize }: FetchDataParams,
{ isDarkMode }: Options
): Promise<ApmFetchDataResponse> => {
const theme = isDarkMode ? darkTheme : lightTheme;

const data = await callApmApi({
pathname: '/api/apm/observability_dashboard',
params: { query: { start: startTime, end: endTime, bucketSize } },
});

const { serviceCount, transactionCoordinates } = data;

return {
title: i18n.translate('xpack.apm.observabilityDashboard.title', {
defaultMessage: 'APM',
}),
appLink: '/app/apm',
stats: {
services: {
label: i18n.translate(
'xpack.apm.observabilityDashboard.stats.services',
{ defaultMessage: 'Services' }
),
value: serviceCount,
},
transactions: {
label: i18n.translate(
'xpack.apm.observabilityDashboard.stats.transactions',
{ defaultMessage: 'Transactions' }
),
value: sum(transactionCoordinates.map((coordinates) => coordinates.y)),
color: theme.euiColorVis1,
},
},
series: {
transactions: {
label: i18n.translate(
'xpack.apm.observabilityDashboard.chart.transactions',
{ defaultMessage: 'Transactions' }
),
color: theme.euiColorVis1,
coordinates: transactionCoordinates,
},
},
};
};

export async function hasData() {
return await callApmApi({
pathname: '/api/apm/observability_dashboard/has_data',
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { rangeFilter } from '../../../common/utils/range_filter';
import { SERVICE_NAME } from '../../../common/elasticsearch_fieldnames';
import { Setup, SetupTimeRange } from '../helpers/setup_request';

export async function getServiceCount({
setup,
}: {
setup: Setup & SetupTimeRange;
}) {
const { client, indices, start, end } = setup;

const params = {
index: [
indices['apm_oss.transactionIndices'],
indices['apm_oss.errorIndices'],
indices['apm_oss.metricsIndices'],
],
body: {
size: 0,
query: { bool: { filter: [{ range: rangeFilter(start, end) }] } },
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
aggs: { serviceCount: { cardinality: { field: SERVICE_NAME } } },
},
};

const { aggregations } = await client.search(params);
return aggregations?.serviceCount.value || 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.
*/
/*
* 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 { rangeFilter } from '../../../common/utils/range_filter';
import { Coordinates } from '../../../../observability/public/typings/fetch_data_response';
import { PROCESSOR_EVENT } from '../../../common/elasticsearch_fieldnames';
import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { ProcessorEvent } from '../../../common/processor_event';

export async function getTransactionCoordinates({
setup,
bucketSize,
}: {
setup: Setup & SetupTimeRange;
bucketSize: string;
}): Promise<Coordinates[]> {
const { client, indices, start, end } = setup;

const { aggregations } = await client.search({
index: indices['apm_oss.transactionIndices'],
body: {
size: 0,
query: {
bool: {
filter: [
{ term: { [PROCESSOR_EVENT]: ProcessorEvent.transaction } },
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
{ range: rangeFilter(start, end) },
],
},
},
aggs: {
distribution: {
date_histogram: {
field: '@timestamp',
fixed_interval: bucketSize,
min_doc_count: 0,
extended_bounds: { min: start, max: end },
},
},
},
},
});

return (
aggregations?.distribution.buckets.map((bucket) => ({
x: bucket.key,
y: bucket.doc_count,
})) || []
);
}
26 changes: 26 additions & 0 deletions x-pack/plugins/apm/server/lib/observability_dashboard/has_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 { Setup } from '../helpers/setup_request';

export async function hasData({ setup }: { setup: Setup }) {
const { client, indices } = setup;
try {
const params = {
index: [
indices['apm_oss.transactionIndices'],
indices['apm_oss.errorIndices'],
indices['apm_oss.metricsIndices'],
],
terminateAfter: 1,
size: 0,
};

const response = await client.search(params);
return response.hits.total.value > 0;
} catch (e) {
return false;
}
}
10 changes: 9 additions & 1 deletion x-pack/plugins/apm/server/routes/create_apm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ import {
rumPageViewsTrendRoute,
rumPageLoadDistributionRoute,
} from './rum_client';
import {
observabilityDashboardHasDataRoute,
observabilityDashboardDataRoute,
} from './observability_dashboard';

const createApmApi = () => {
const api = createApi()
Expand Down Expand Up @@ -160,7 +164,11 @@ const createApmApi = () => {
.add(rumOverviewLocalFiltersRoute)
.add(rumPageViewsTrendRoute)
.add(rumPageLoadDistributionRoute)
.add(rumClientMetricsRoute);
.add(rumClientMetricsRoute)

// Observability dashboard
.add(observabilityDashboardHasDataRoute)
.add(observabilityDashboardDataRoute);

return api;
};
Expand Down
Loading