Skip to content

Commit

Permalink
- Moves get_all_environments from agent_configuration dir to common dir
Browse files Browse the repository at this point in the history
- makes the 'all' environment name ALL_OPTION_VALUE agent configuration-specific
- replace field literals with constants
  • Loading branch information
ogupte committed Jul 4, 2020
1 parent 925b600 commit cb9a8ad
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useFetcher, FETCH_STATUS } from '../../../../hooks/useFetcher';
import { ALL_OPTION_VALUE } from '../../../../../common/agent_configuration/all_option';
import { useApmPluginContext } from '../../../../hooks/useApmPluginContext';
import { createJobs } from './create_jobs';

Expand All @@ -44,13 +43,11 @@ export const AddEnvironments = ({
{ preservePreviousData: false }
);

const availableEnvironmentOptions = data
.filter((env) => env !== ALL_OPTION_VALUE)
.map((env) => ({
label: env,
value: env,
disabled: currentEnvironments.includes(env),
}));
const availableEnvironmentOptions = data.map((env) => ({
label: env,
value: env,
disabled: currentEnvironments.includes(env),
}));

const [selectedOptions, setSelected] = useState<
Array<EuiComboBoxOptionOption<string>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import uuid from 'uuid/v4';
import { PromiseReturnType } from '../../../../observability/typings/common';
import { Setup } from '../helpers/setup_request';
import { JobResponse } from '../../../../ml/common/types/modules';
import {
SERVICE_ENVIRONMENT,
TRANSACTION_DURATION,
PROCESSOR_EVENT,
} from '../../../common/elasticsearch_fieldnames';

const ML_MODULE_ID_APM_TRANSACTION = 'apm_transaction';
export const ML_GROUP_NAME_APM = 'apm';

export type CreateAnomalyDetectionJobsAPIResponse = PromiseReturnType<
typeof createAnomalyDetectionJobs
Expand All @@ -18,7 +26,7 @@ export async function createAnomalyDetectionJobs(
environments: string[],
logger: Logger
) {
const { ml } = setup;
const { ml, config } = setup;
if (!ml) {
return [];
}
Expand All @@ -38,9 +46,10 @@ export async function createAnomalyDetectionJobs(
`Creating ML anomaly detection jobs for environments: [${environments}].`
);

const indexPatternName = config['apm_oss.transactionIndices']; // TODO [ML] - Do we want to use the config index name?
const dataRecognizerConfigResponses = await Promise.all(
environments.map((environment) =>
configureAnomalyDetectionJob({ ml, environment })
configureAnomalyDetectionJob({ ml, environment, indexPatternName })
)
);
const newJobResponses = dataRecognizerConfigResponses.reduce(
Expand Down Expand Up @@ -75,24 +84,26 @@ export async function createAnomalyDetectionJobs(
async function configureAnomalyDetectionJob({
ml,
environment,
indexPatternName = 'apm-*-transaction-*',
}: {
ml: Required<Setup>['ml'];
environment: string;
indexPatternName?: string | undefined;
}) {
const convertedEnvironmentName = convertToMLIdentifier(environment);
const randomToken = uuid().substr(-4);

return ml.modules.setup({
moduleId: 'apm_transaction',
prefix: `apm-${convertedEnvironmentName}-${randomToken}-`,
groups: ['apm', convertedEnvironmentName],
indexPatternName: 'apm-*-transaction-*',
moduleId: ML_MODULE_ID_APM_TRANSACTION,
prefix: `${ML_GROUP_NAME_APM}-${convertedEnvironmentName}-${randomToken}-`,
groups: [ML_GROUP_NAME_APM, convertedEnvironmentName],
indexPatternName,
query: {
bool: {
filter: [
{ term: { 'processor.event': 'transaction' } },
{ exists: { field: 'transaction.duration.us' } },
{ term: { 'service.environment': environment } },
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ exists: { field: TRANSACTION_DURATION } },
{ term: { [SERVICE_ENVIRONMENT]: environment } },
],
},
},
Expand All @@ -101,7 +112,7 @@ async function configureAnomalyDetectionJob({
{
custom_settings: {
job_tags: {
'service.environment': environment,
[SERVICE_ENVIRONMENT]: environment,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Logger } from 'kibana/server';
import { PromiseReturnType } from '../../../../observability/typings/common';
import { Setup } from '../helpers/setup_request';
import { AnomalyDetectionJobByEnv } from '../../../typings/anomaly_detection';
import { SERVICE_ENVIRONMENT } from '../../../common/elasticsearch_fieldnames';
import { ML_GROUP_NAME_APM } from './create_anomaly_detection_jobs';

export type AnomalyDetectionJobsAPIResponse = PromiseReturnType<
typeof getAnomalyDetectionJobs
Expand Down Expand Up @@ -39,19 +41,17 @@ export async function getAnomalyDetectionJobs(
return [];
}
try {
const { jobs } = await ml.anomalyDetectors.jobs('apm');
const { jobs } = await ml.anomalyDetectors.jobs(ML_GROUP_NAME_APM);
return jobs.reduce((acc, anomalyDetectionJob) => {
if (
anomalyDetectionJob.custom_settings?.job_tags?.['service.environment']
anomalyDetectionJob.custom_settings?.job_tags?.[SERVICE_ENVIRONMENT]
) {
return [
...acc,
{
job_id: anomalyDetectionJob.job_id,
'service.environment':
anomalyDetectionJob.custom_settings.job_tags[
'service.environment'
],
[SERVICE_ENVIRONMENT]:
anomalyDetectionJob.custom_settings.job_tags[SERVICE_ENVIRONMENT],
},
];
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { getAllEnvironments } from './get_all_environments';
import {
SearchParamsMock,
inspectSearchParams,
} from '../../../public/utils/testHelpers';

describe('getAllEnvironments', () => {
let mock: SearchParamsMock;

afterEach(() => {
mock.teardown();
});

it('fetches all environments', async () => {
mock = await inspectSearchParams((setup) =>
getAllEnvironments({
serviceName: 'test',
setup,
})
);

expect(mock.params).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../../helpers/setup_request';
import { Setup } from '../helpers/setup_request';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
SERVICE_ENVIRONMENT,
} from '../../../../../common/elasticsearch_fieldnames';
import { ALL_OPTION_VALUE } from '../../../../../common/agent_configuration/all_option';
} from '../../../common/elasticsearch_fieldnames';

export async function getAllEnvironments({
serviceName,
Expand Down Expand Up @@ -60,6 +59,5 @@ export async function getAllEnvironments({
resp.aggregations?.environments.buckets.map(
(bucket) => bucket.key as string
) || [];
return [ALL_OPTION_VALUE, ...environments]; // TODO move this file out of agent_configuration dir into common dir
// TODO and move ALL_OPTION_VALUE to the agent_configuration implementation
return environments;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getAllEnvironments } from './get_all_environments';
import { getAllEnvironments } from '../../../environments/get_all_environments';
import { Setup } from '../../../helpers/setup_request';
import { PromiseReturnType } from '../../../../../../observability/typings/common';
import { getExistingEnvironmentsForService } from './get_existing_environments_for_service';
import { ALL_OPTION_VALUE } from '../../../../../common/agent_configuration/all_option';

export type AgentConfigurationEnvironmentsAPIResponse = PromiseReturnType<
typeof getEnvironments
Expand All @@ -25,7 +26,7 @@ export async function getEnvironments({
getExistingEnvironmentsForService({ serviceName, setup }),
]);

return allEnvironments.map((environment) => {
return [ALL_OPTION_VALUE, ...allEnvironments].map((environment) => {
return {
name: environment,
alreadyConfigured: existingEnvironments.includes(environment),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getAllEnvironments } from './get_environments/get_all_environments';
import { getExistingEnvironmentsForService } from './get_environments/get_existing_environments_for_service';
import { getServiceNames } from './get_service_names';
import { listConfigurations } from './list_configurations';
Expand All @@ -22,19 +21,6 @@ describe('agent configuration queries', () => {
mock.teardown();
});

describe('getAllEnvironments', () => {
it('fetches all environments', async () => {
mock = await inspectSearchParams((setup) =>
getAllEnvironments({
serviceName: 'foo',
setup,
})
);

expect(mock.params).toMatchSnapshot();
});
});

describe('getExistingEnvironmentsForService', () => {
it('fetches unavailable environments', async () => {
mock = await inspectSearchParams((setup) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createRoute } from '../create_route';
import { getAnomalyDetectionJobs } from '../../lib/anomaly_detection/get_anomaly_detection_jobs';
import { createAnomalyDetectionJobs } from '../../lib/anomaly_detection/create_anomaly_detection_jobs';
import { setupRequest } from '../../lib/helpers/setup_request';
import { getAllEnvironments } from '../../lib/settings/agent_configuration/get_environments/get_all_environments';
import { getAllEnvironments } from '../../lib/environments/get_all_environments';

// get ML anomaly detection jobs for each environment
export const anomalyDetectionJobsRoute = createRoute(() => ({
Expand Down

0 comments on commit cb9a8ad

Please sign in to comment.