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] switch get environment function to use terms_enum api #150175

Merged
merged 17 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c1bbce2
switch get environment function to use terms_enum api instead of aggr…
achyutjhunjhunwala Feb 2, 2023
ad1cd3e
pass service as empty string if unavailable
achyutjhunjhunwala Feb 2, 2023
274b7e6
Merge branch 'main' into switch_get_environments_to_terms_enum_api
achyutjhunjhunwala Feb 2, 2023
3f4323b
Merge branch 'main' into switch_get_environments_to_terms_enum_api
achyutjhunjhunwala Feb 6, 2023
9a66237
change environment fetcher logic to use suggestion api when no servic…
achyutjhunjhunwala Feb 6, 2023
9627da0
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Feb 6, 2023
9ac0a17
adjusting environment fetching logic based on useFetcher
achyutjhunjhunwala Feb 6, 2023
b87d22d
Merge remote-tracking branch 'origin/switch_get_environments_to_terms…
achyutjhunjhunwala Feb 6, 2023
f00787e
remove any type to more specific type
achyutjhunjhunwala Feb 7, 2023
3d0f71a
Merge branch 'main' into switch_get_environments_to_terms_enum_api
achyutjhunjhunwala Feb 7, 2023
8758f45
fix logic which broke the inspector component
achyutjhunjhunwala Feb 7, 2023
0a1e907
simplified type for getEnvironments on the server side
achyutjhunjhunwala Feb 7, 2023
122dcaf
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Feb 7, 2023
315ec23
fix final code reviews
achyutjhunjhunwala Feb 7, 2023
4e723d1
add api tests for getEnvironments
achyutjhunjhunwala Feb 7, 2023
f413e8d
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Feb 7, 2023
b05a3a2
Merge branch 'main' into switch_get_environments_to_terms_enum_api
achyutjhunjhunwala Feb 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions x-pack/plugins/apm/public/hooks/use_environments_fetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
* 2.0.
*/

import { SERVICE_ENVIRONMENT } from '../../common/es_fields/apm';
import { useFetcher } from './use_fetcher';
import { Environment } from '../../common/environment_rt';
import { APIReturnType } from '../services/rest/create_call_apm_api';

const INITIAL_DATA = { environments: [] };
achyutjhunjhunwala marked this conversation as resolved.
Show resolved Hide resolved
type EnvironmentsAPIResponse = APIReturnType<'GET /internal/apm/environments'>;

const INITIAL_DATA: EnvironmentsAPIResponse = { environments: [] };
export function useEnvironmentsFetcher({
serviceName,
start,
Expand All @@ -20,7 +24,11 @@ export function useEnvironmentsFetcher({
}) {
const { data = INITIAL_DATA, status } = useFetcher(
(callApmApi) => {
if (start && end) {
if (!start || !end) {
return;
}

if (serviceName) {
return callApmApi('GET /internal/apm/environments', {
params: {
query: {
Expand All @@ -31,9 +39,24 @@ export function useEnvironmentsFetcher({
},
});
}
return callApmApi('GET /internal/apm/suggestions', {
params: {
query: {
start,
end,
fieldName: SERVICE_ENVIRONMENT,
fieldValue: '',
},
},
}).then((response) => {
return { environments: response.terms };
});
},
[start, end, serviceName]
);

return { environments: data.environments, status };
return {
environments: data.environments as Environment[],
status,
};
}
31 changes: 20 additions & 11 deletions x-pack/plugins/apm/public/hooks/use_fetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,28 @@ function getDetailsFromErrorResponse(
}

const createAutoAbortedAPMClient = (
signal: AbortSignal
signal: AbortSignal,
addInspectorRequest: <Data>(result: FetcherResult<Data>) => void
): AutoAbortedAPMClient => {
return ((endpoint, options) => {
return callApmApi(endpoint, {
...options,
signal,
} as any);
} as any)
.catch((err) => {
addInspectorRequest({
status: FETCH_STATUS.FAILURE,
data: err.body?.attributes,
});
throw err;
})
.then((response) => {
addInspectorRequest({
data: response,
status: FETCH_STATUS.SUCCESS,
});
return response;
});
}) as AutoAbortedAPMClient;
};

Expand Down Expand Up @@ -102,7 +117,9 @@ export function useFetcher<TReturn>(

const signal = controller.signal;

const promise = fn(createAutoAbortedAPMClient(signal));
const promise = fn(
createAutoAbortedAPMClient(signal, addInspectorRequest)
);
// if `fn` doesn't return a promise it is a signal that data fetching was not initiated.
// This can happen if the data fetching is conditional (based on certain inputs).
// In these cases it is not desirable to invoke the global loading spinner, or change the status to success
Expand Down Expand Up @@ -179,14 +196,6 @@ export function useFetcher<TReturn>(
/* eslint-enable react-hooks/exhaustive-deps */
]);

useEffect(() => {
if (result.error) {
addInspectorRequest({ ...result, data: result.error.body?.attributes });
} else {
addInspectorRequest(result);
}
}, [addInspectorRequest, result]);

achyutjhunjhunwala marked this conversation as resolved.
Show resolved Hide resolved
return useMemo(() => {
return {
...result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Environment } from '../../../common/environment_rt';
import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client';

/**
* This is used for getting the list of environments for the environments selector,
* This is used for getting the list of environments for the environment selector,
* filtered by range.
*/
export async function getEnvironments({
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/apm/server/routes/environments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as t from 'io-ts';
import { maxSuggestions } from '@kbn/observability-plugin/common';
import { Environment } from '../../../common/environment_rt';
import { getSearchTransactionsEvents } from '../../lib/helpers/transactions';
import { getEnvironments } from './get_environments';
import { rangeRt } from '../default_api_types';
Expand All @@ -27,11 +28,7 @@ const environmentsRoute = createApmServerRoute({
handler: async (
resources
): Promise<{
environments: Array<
| 'ENVIRONMENT_NOT_DEFINED'
| 'ENVIRONMENT_ALL'
| t.Branded<string, import('@kbn/io-ts-utils').NonEmptyStringBrand>
>;
environments: Environment[];
}> => {
const apmEventClient = await getApmEventClient(resources);
const { context, params, config } = resources;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { apm, timerange } from '@kbn/apm-synthtrace-client';
import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';

// Generate synthetic data for the environment test suite
export async function generateData({
synthtraceEsClient,
start,
end,
}: {
synthtraceEsClient: ApmSynthtraceEsClient;
start: number;
end: number;
}) {
const environmentNames = ['production', 'development', 'staging'];
const serviceNames = ['go', 'java', 'node'];

const services = environmentNames.flatMap((environment) => {
return serviceNames.flatMap((serviceName) => {
return apm
.service({
name: serviceName,
environment,
agentName: serviceName,
})
.instance('instance-a');
});
});

const goServiceWithAdditionalEnvironment = apm
.service({
name: 'go',
environment: 'custom-go-environment',
agentName: 'go',
})
.instance('instance-a');

// Generate a transaction for each service
const docs = timerange(start, end)
.ratePerMinute(1)
.generator((timestamp) => {
const loopGeneratedDocs = services.flatMap((service) => {
return service
.transaction({ transactionName: 'GET /api/product/:id' })
.timestamp(timestamp)
.duration(1000);
});

const customDoc = goServiceWithAdditionalEnvironment
.transaction({
transactionName: 'GET /api/go/memory',
transactionType: 'custom-go-type',
})
.timestamp(timestamp)
.duration(1000);

return [...loopGeneratedDocs, customDoc];
});

await synthtraceEsClient.index(docs);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { generateData } from './generate_data';

const startNumber = new Date('2021-01-01T00:00:00.000Z').getTime();
const endNumber = new Date('2021-01-01T00:05:00.000Z').getTime() - 1;

const start = new Date(startNumber).toISOString();
const end = new Date(endNumber).toISOString();

export default function environmentsAPITests({ getService }: FtrProviderContext) {
const registry = getService('registry');
const apmApiClient = getService('apmApiClient');
const synthtraceEsClient = getService('synthtraceEsClient');

registry.when('environments when data is loaded', { config: 'basic', archives: [] }, async () => {
before(async () => {
await generateData({
synthtraceEsClient,
start: startNumber,
end: endNumber,
});
});

after(() => synthtraceEsClient.clean());

describe('get environments', () => {
describe('when service name is not specified', () => {
it('returns all environments', async () => {
const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/environments',
params: {
query: { start, end },
},
});
expect(body.environments.length).to.be.equal(4);
expectSnapshot(body.environments).toMatchInline(`
Array [
"development",
"production",
"staging",
"custom-go-environment",
]
`);
});
});

describe('when service name is specified', () => {
it('returns service specific environments for go', async () => {
const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/environments',
params: {
query: { start, end, serviceName: 'go' },
},
});

expect(body.environments.length).to.be.equal(4);
expectSnapshot(body.environments).toMatchInline(`
Array [
"custom-go-environment",
"development",
"production",
"staging",
]
`);
});

it('returns service specific environments for java', async () => {
const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/environments',
params: {
query: { start, end, serviceName: 'java' },
},
});

expect(body.environments.length).to.be.equal(3);
expectSnapshot(body.environments).toMatchInline(`
Array [
"development",
"production",
"staging",
]
`);
});
});
});
});
}