From 17c78db794be9ff8bbaed2b000cc2ee70ae46972 Mon Sep 17 00:00:00 2001 From: Miriam <31922082+MiriamAparicio@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:21:46 +0100 Subject: [PATCH] [ObsUX] Fix timestamp for anomaly alert test (#169255) Closes https://github.com/elastic/kibana/issues/160769 ### What was done The anomaly alert test was failing with timeout because was not generating alerts, the data and spikes were added to far away in time, so when the job is created doesn't take into account data that far in the past We fixed the timerange of the data generated and the spikes. BEFORE: image AFTER: image --- .../anomaly/register_anomaly_rule_type.ts | 1 - .../tests/alerts/anomaly_alert.spec.ts | 41 +++++++++---------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts index b74db63061306..2f4f3b75c8807 100644 --- a/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts +++ b/x-pack/plugins/apm/server/routes/alerts/rule_types/anomaly/register_anomaly_rule_type.ts @@ -174,7 +174,6 @@ export function registerAnomalyRuleType({ range: { timestamp: { gte: dateStart, - format: 'epoch_millis', }, }, }, diff --git a/x-pack/test/apm_api_integration/tests/alerts/anomaly_alert.spec.ts b/x-pack/test/apm_api_integration/tests/alerts/anomaly_alert.spec.ts index c08503baa37f9..3475888a5407e 100644 --- a/x-pack/test/apm_api_integration/tests/alerts/anomaly_alert.spec.ts +++ b/x-pack/test/apm_api_integration/tests/alerts/anomaly_alert.spec.ts @@ -5,6 +5,7 @@ * 2.0. */ +import moment from 'moment'; import { ApmRuleType } from '@kbn/apm-plugin/common/rules/apm_rule_types'; import { apm, timerange } from '@kbn/apm-synthtrace-client'; import expect from '@kbn/expect'; @@ -23,34 +24,32 @@ export default function ApiTest({ getService }: FtrProviderContext) { const logger = getService('log'); const synthtraceEsClient = getService('synthtraceEsClient'); - // FLAKY https://github.com/elastic/kibana/issues/160298 - registry.when.skip( + registry.when( 'fetching service anomalies with a trial license', { config: 'trial', archives: [] }, () => { - const start = '2021-01-01T00:00:00.000Z'; - const end = '2021-01-08T00:15:00.000Z'; + const start = moment().subtract(1, 'days').valueOf(); + const end = moment().valueOf(); - const spikeStart = new Date('2021-01-07T23:15:00.000Z').getTime(); - const spikeEnd = new Date('2021-01-08T00:15:00.000Z').getTime(); - - const NORMAL_DURATION = 100; - const NORMAL_RATE = 1; + const spikeStart = moment().subtract(15, 'minutes').valueOf(); + const spikeEnd = moment().valueOf(); let ruleId: string; before(async () => { + await cleanup(); + const serviceA = apm .service({ name: 'a', environment: 'production', agentName: 'java' }) .instance('a'); - const events = timerange(new Date(start).getTime(), new Date(end).getTime()) + const events = timerange(start, end) .interval('1m') .rate(1) .generator((timestamp) => { const isInSpike = timestamp >= spikeStart && timestamp < spikeEnd; - const count = isInSpike ? 4 : NORMAL_RATE; - const duration = isInSpike ? 1000 : NORMAL_DURATION; + const count = isInSpike ? 4 : 1; + const duration = isInSpike ? 1000 : 100; const outcome = isInSpike ? 'failure' : 'success'; return [ @@ -65,26 +64,25 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); await synthtraceEsClient.index(events); + + await createAndRunApmMlJobs({ es, ml, environments: ['production'] }); }); after(async () => { + await cleanup(); + }); + + async function cleanup() { try { await synthtraceEsClient.clean(); await deleteRuleById({ supertest, ruleId }); + await ml.cleanMlIndices(); } catch (e) { logger.info('Could not delete rule by id', e); } - }); + } describe('with ml jobs', () => { - before(async () => { - await createAndRunApmMlJobs({ es, ml, environments: ['production'] }); - }); - - after(async () => { - await ml.cleanMlIndices(); - }); - it('checks if alert is active', async () => { const createdRule = await createApmRule({ supertest, @@ -97,7 +95,6 @@ export default function ApiTest({ getService }: FtrProviderContext) { }, ruleTypeId: ApmRuleType.Anomaly, }); - ruleId = createdRule.id; if (!ruleId) { expect(ruleId).to.not.eql(undefined);