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] Migrate /anomalies to deployment agnostic tests #199269

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/*
* 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 { AnomalyDetectorType } from '@kbn/apm-plugin/common/anomaly_detection/apm_ml_detectors';
import { ServiceAnomalyTimeseries } from '@kbn/apm-plugin/common/anomaly_detection/service_anomaly_timeseries';
import { Environment } from '@kbn/apm-plugin/common/environment_rt';
import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';
import { apm, timerange } from '@kbn/apm-synthtrace-client';
import expect from '@kbn/expect';
import { last, omit, range } from 'lodash';
import moment from 'moment';
import { ApmApiError } from '../../../../../../apm_api_integration/common/apm_api_supertest';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import { createAndRunApmMlJobs } from '../../../../../../apm_api_integration/common/utils/create_and_run_apm_ml_jobs';

export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
const apmApiClient = getService('apmApi');
const ml = getService('mlApi');
const es = getService('es');
const logger = getService('log');
const synthtrace = getService('synthtrace');

const start = moment().subtract(2, 'days');
const end = moment();
const spikeStart = moment().subtract(8, 'hours');
const spikeEnd = moment().subtract(6, 'hours');

async function statusOf(p: Promise<{ status: number }>) {
try {
const { status } = await p;
return status;
} catch (err) {
if (err instanceof ApmApiError) {
return err.res.status;
}
throw err;
}
}

function getAnomalyCharts(
{
transactionType,
serviceName,
environment,
}: {
transactionType: string;
serviceName: string;
environment: Environment;
},
user = apmApiClient.readUser
) {
return user({
endpoint: 'GET /internal/apm/services/{serviceName}/anomaly_charts',
params: {
path: {
serviceName,
},
query: {
start: start.toISOString(),
end: end.toISOString(),
transactionType,
environment,
},
},
});
}

describe('Anomaly charts', () => {
const NORMAL_DURATION = 100;
const NORMAL_RATE = 1;
let apmSynthtraceEsClient: ApmSynthtraceEsClient;

before(async () => {
apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient();
});

beforeEach(async () => {
const serviceA = apm
.service({ name: 'a', environment: 'production', agentName: 'java' })
.instance('a');

const serviceB = apm
.service({ name: 'b', environment: 'development', agentName: 'go' })
.instance('b');

const events = timerange(start.valueOf(), end.valueOf())
.interval('1m')
.rate(1)
.generator((timestamp) => {
const isInSpike = timestamp >= spikeStart.valueOf() && timestamp < spikeEnd.valueOf();
const count = isInSpike ? 4 : NORMAL_RATE;
const duration = isInSpike ? 10000 : NORMAL_DURATION;
const outcome = isInSpike ? 'failure' : 'success';

return [
...range(0, count).flatMap((_) =>
serviceA
.transaction({ transactionName: 'tx', transactionType: 'request' })
.timestamp(timestamp)
.duration(duration)
.outcome(outcome)
),
serviceB
.transaction({ transactionName: 'tx', transactionType: 'Worker' })
.timestamp(timestamp)
.duration(duration)
.success(),
];
});

await apmSynthtraceEsClient.index(events);
});

afterEach(async () => {
await cleanup();
});

async function cleanup() {
await apmSynthtraceEsClient.clean();
await ml.cleanMlIndices();
}

describe('without ml jobs', () => {
it('returns a 200 for a user _with_ access to ML', async () => {
const status = await statusOf(
getAnomalyCharts({
serviceName: 'a',
transactionType: 'request',
environment: 'ENVIRONMENT_ALL',
})
);

expect(status).to.eql(200);
});
});

// FLAKY: https://github.com/elastic/kibana/issues/176966
describe('with ml jobs', () => {
beforeEach(async () => {
await createAndRunApmMlJobs({
es,
ml,
environments: ['production', 'development'],
logger,
});
});

it('returns a 200 for a user _with_ access to ML', async () => {
const status = await statusOf(
getAnomalyCharts({
serviceName: 'a',
transactionType: 'request',
environment: 'ENVIRONMENT_ALL',
})
);

expect(status).to.eql(200);
});

describe('inspecting the body', () => {
let allAnomalyTimeseries: ServiceAnomalyTimeseries[];

let latencySeries: ServiceAnomalyTimeseries | undefined;
let throughputSeries: ServiceAnomalyTimeseries | undefined;
let failureRateSeries: ServiceAnomalyTimeseries | undefined;
const endTimeMs = end.valueOf();

beforeEach(async () => {
allAnomalyTimeseries = (
await getAnomalyCharts({
serviceName: 'a',
transactionType: 'request',
environment: 'ENVIRONMENT_ALL',
})
).body.allAnomalyTimeseries;

latencySeries = allAnomalyTimeseries.find(
(spec) => spec.type === AnomalyDetectorType.txLatency
);
throughputSeries = allAnomalyTimeseries.find(
(spec) => spec.type === AnomalyDetectorType.txThroughput
);
failureRateSeries = allAnomalyTimeseries.find(
(spec) => spec.type === AnomalyDetectorType.txFailureRate
);
});

it('returns model plots for all detectors and job ids for the given transaction type', () => {
expect(allAnomalyTimeseries.length).to.eql(3);

expect(
allAnomalyTimeseries.every((spec) => spec.bounds.some((bound) => bound.y0 ?? 0 > 0))
);
});

it('returns model plots with bounds for x range within start and end', () => {
expect(allAnomalyTimeseries.length).to.eql(3);

expect(
allAnomalyTimeseries.every((spec) =>
spec.bounds.every((bound) => bound.x >= start.valueOf() && bound.x <= endTimeMs)
)
);
});

it('returns model plots with latest bucket matching the end time', () => {
expect(allAnomalyTimeseries.every((spec) => last(spec.bounds)?.x === endTimeMs));
});

it('returns the correct metadata', () => {
function omitTimeseriesData(series: ServiceAnomalyTimeseries | undefined) {
return series ? omit(series, 'anomalies', 'bounds') : undefined;
}

expect(omitTimeseriesData(latencySeries)).to.eql({
type: AnomalyDetectorType.txLatency,
jobId: 'apm-tx-metrics-production',
serviceName: 'a',
environment: 'production',
transactionType: 'request',
version: 3,
});

expect(omitTimeseriesData(throughputSeries)).to.eql({
type: AnomalyDetectorType.txThroughput,
jobId: 'apm-tx-metrics-production',
serviceName: 'a',
environment: 'production',
transactionType: 'request',
version: 3,
});

expect(omitTimeseriesData(failureRateSeries)).to.eql({
type: AnomalyDetectorType.txFailureRate,
jobId: 'apm-tx-metrics-production',
serviceName: 'a',
environment: 'production',
transactionType: 'request',
version: 3,
});
});

it('returns anomalies for during the spike', () => {
const latencyAnomalies = latencySeries?.anomalies.filter((anomaly) => anomaly.y ?? 0 > 0);

const throughputAnomalies = throughputSeries?.anomalies.filter(
(anomaly) => anomaly.y ?? 0 > 0
);

const failureRateAnomalies = failureRateSeries?.anomalies.filter(
(anomaly) => anomaly.y ?? 0 > 0
);

expect(latencyAnomalies?.length).to.be.greaterThan(0);

expect(throughputAnomalies?.length).to.be.greaterThan(0);

expect(failureRateAnomalies?.length).to.be.greaterThan(0);

expect(
latencyAnomalies?.every(
(anomaly) =>
anomaly.x >= spikeStart.valueOf() && (anomaly.actual ?? 0) > NORMAL_DURATION
)
);

expect(
throughputAnomalies?.every(
(anomaly) => anomaly.x >= spikeStart.valueOf() && (anomaly.actual ?? 0) > NORMAL_RATE
)
);

expect(
failureRateAnomalies?.every(
(anomaly) => anomaly.x >= spikeStart.valueOf() && (anomaly.actual ?? 0) > 0
)
);
});
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) {
describe('anomalies', () => {
loadTestFile(require.resolve('./anomaly_charts.spec.ts'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export default function apmApiIntegrationTests({
}: DeploymentAgnosticFtrProviderContext) {
describe('APM', function () {
loadTestFile(require.resolve('./agent_explorer'));
loadTestFile(require.resolve('./anomalies'));
});
}
Loading